blob: c844de224d3feea5886e88360d22f45cf3e21e02 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070069import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.LocalPowerManager;
71import android.os.Looper;
72import android.os.Message;
73import android.os.Parcel;
74import android.os.ParcelFileDescriptor;
75import android.os.Power;
76import android.os.PowerManager;
77import android.os.Process;
78import android.os.RemoteException;
79import android.os.ServiceManager;
80import android.os.SystemClock;
81import android.os.SystemProperties;
82import android.os.TokenWatcher;
83import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070084import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
87import android.util.SparseIntArray;
88import android.view.Display;
89import android.view.Gravity;
90import android.view.IApplicationToken;
91import android.view.IOnKeyguardExitResult;
92import android.view.IRotationWatcher;
93import android.view.IWindow;
94import android.view.IWindowManager;
95import android.view.IWindowSession;
96import android.view.KeyEvent;
97import android.view.MotionEvent;
98import android.view.RawInputEvent;
99import android.view.Surface;
100import android.view.SurfaceSession;
101import android.view.View;
102import android.view.ViewTreeObserver;
103import android.view.WindowManager;
104import android.view.WindowManagerImpl;
105import android.view.WindowManagerPolicy;
106import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700107import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.animation.Animation;
109import android.view.animation.AnimationUtils;
110import android.view.animation.Transformation;
111
112import java.io.BufferedWriter;
113import java.io.File;
114import java.io.FileDescriptor;
115import java.io.IOException;
116import java.io.OutputStream;
117import java.io.OutputStreamWriter;
118import java.io.PrintWriter;
119import java.io.StringWriter;
120import java.net.Socket;
121import java.util.ArrayList;
122import java.util.HashMap;
123import java.util.HashSet;
124import java.util.Iterator;
125import java.util.List;
126
127/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700128public class WindowManagerService extends IWindowManager.Stub
129 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final String TAG = "WindowManager";
131 static final boolean DEBUG = false;
132 static final boolean DEBUG_FOCUS = false;
133 static final boolean DEBUG_ANIM = false;
134 static final boolean DEBUG_LAYERS = false;
135 static final boolean DEBUG_INPUT = false;
136 static final boolean DEBUG_INPUT_METHOD = false;
137 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700138 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 static final boolean DEBUG_ORIENTATION = false;
140 static final boolean DEBUG_APP_TRANSITIONS = false;
141 static final boolean DEBUG_STARTING_WINDOW = false;
142 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700143 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700145 static final boolean MEASURE_LATENCY = false;
146 static private LatencyTimer lt;
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final boolean PROFILE_ORIENTATION = false;
149 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700150 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** How long to wait for first key repeat, in milliseconds */
155 static final int KEY_REPEAT_FIRST_DELAY = 750;
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.
227 */
228 private boolean mWaitingUntilKeyguardReenabled = false;
229
230
231 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardDisabled") {
233 public void acquired() {
234 mPolicy.enableKeyguard(false);
235 }
236 public void released() {
237 synchronized (mKeyguardDisabled) {
238 mPolicy.enableKeyguard(true);
239 mWaitingUntilKeyguardReenabled = false;
240 mKeyguardDisabled.notifyAll();
241 }
242 }
243 };
244
245 final Context mContext;
246
247 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
252
253 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * All currently active sessions with clients.
259 */
260 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Mapping from an IWindow IBinder to the server's Window object.
264 * This is also used as the lock for all of our state.
265 */
266 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
267
268 /**
269 * Mapping from a token IBinder to a WindowToken object.
270 */
271 final HashMap<IBinder, WindowToken> mTokenMap =
272 new HashMap<IBinder, WindowToken>();
273
274 /**
275 * The same tokens as mTokenMap, stored in a list for efficient iteration
276 * over them.
277 */
278 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Window tokens that are in the process of exiting, but still
282 * on screen for animations.
283 */
284 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
285
286 /**
287 * Z-ordered (bottom-most first) list of all application tokens, for
288 * controlling the ordering of windows in different applications. This
289 * contains WindowToken objects.
290 */
291 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * Application tokens that are in the process of exiting, but still
295 * on screen for animations.
296 */
297 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * List of window tokens that have finished starting their application,
301 * and now need to have the policy remove their windows.
302 */
303 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
304
305 /**
306 * Z-ordered (bottom-most first) list of all Window objects.
307 */
308 final ArrayList mWindows = new ArrayList();
309
310 /**
311 * Windows that are being resized. Used so we can tell the client about
312 * the resize after closing the transaction in which we resized the
313 * underlying surface.
314 */
315 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
316
317 /**
318 * Windows whose animations have ended and now must be removed.
319 */
320 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
321
322 /**
323 * Windows whose surface should be destroyed.
324 */
325 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
326
327 /**
328 * Windows that have lost input focus and are waiting for the new
329 * focus window to be displayed before they are told about this.
330 */
331 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
332
333 /**
334 * This is set when we have run out of memory, and will either be an empty
335 * list or contain windows that need to be force removed.
336 */
337 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700342 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Surface mBlurSurface;
344 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 final float[] mTmpFloats = new float[9];
349
350 boolean mSafeMode;
351 boolean mDisplayEnabled = false;
352 boolean mSystemBooted = false;
353 int mRotation = 0;
354 int mRequestedRotation = 0;
355 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700356 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 ArrayList<IRotationWatcher> mRotationWatchers
358 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 boolean mLayoutNeeded = true;
361 boolean mAnimationPending = false;
362 boolean mDisplayFrozen = false;
363 boolean mWindowsFreezingScreen = false;
364 long mFreezeGcPending = 0;
365 int mAppsFreezingScreen = 0;
366
367 // This is held as long as we have the screen frozen, to give us time to
368 // perform a rotation animation when turning off shows the lock screen which
369 // changes the orientation.
370 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // State management of app transitions. When we are preparing for a
373 // transition, mNextAppTransition will be the kind of transition to
374 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
375 // mOpeningApps and mClosingApps are the lists of tokens that will be
376 // made visible or hidden at the next transition.
377 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
378 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700379 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 boolean mAppTransitionTimeout = false;
381 boolean mStartingIconInTransition = false;
382 boolean mSkipAppTransitionAnimation = false;
383 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
384 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700385 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
386 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 //flag to detect fat touch events
389 boolean mFatTouch = false;
390 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 H mH = new H();
393
394 WindowState mCurrentFocus = null;
395 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 // This just indicates the window the input method is on top of, not
398 // necessarily the window its input is going to.
399 WindowState mInputMethodTarget = null;
400 WindowState mUpcomingInputMethodTarget = null;
401 boolean mInputMethodTargetWaitingAnim;
402 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 WindowState mInputMethodWindow = null;
405 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
406
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700407 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
408
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700409 // If non-null, this is the currently visible window that is associated
410 // with the wallpaper.
411 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700412 // If non-null, we are in the middle of animating from one wallpaper target
413 // to another, and this is the lower one in Z-order.
414 WindowState mLowerWallpaperTarget = null;
415 // If non-null, we are in the middle of animating from one wallpaper target
416 // to another, and this is the higher one in Z-order.
417 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700418 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700419 float mLastWallpaperX;
420 float mLastWallpaperY;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700421 // Lock for waiting for the wallpaper.
422 final Object mWaitingOnWallpaperLock = new Object();
423 // This is set when we are waiting for a wallpaper to tell us it is done
424 // changing its scroll position.
425 WindowState mWaitingOnWallpaper;
426 // The last time we had a timeout when waiting for a wallpaper.
427 long mLastWallpaperTimeoutTime;
428 // We give a wallpaper up to 150ms to finish scrolling.
429 static final long WALLPAPER_TIMEOUT = 150;
430 // Time we wait after a timeout before trying to wait again.
431 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 AppWindowToken mFocusedApp = null;
434
435 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 float mWindowAnimationScale = 1.0f;
438 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 final KeyWaiter mKeyWaiter = new KeyWaiter();
441 final KeyQ mQueue;
442 final InputDispatcherThread mInputThread;
443
444 // Who is holding the screen on.
445 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * Whether the UI is currently running in touch mode (not showing
449 * navigational focus because the user is directly pressing the screen).
450 */
451 boolean mInTouchMode = false;
452
453 private ViewServer mViewServer;
454
455 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700456
Dianne Hackbornc485a602009-03-24 22:39:49 -0700457 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700458 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700459
460 // The frame use to limit the size of the app running in compatibility mode.
461 Rect mCompatibleScreenFrame = new Rect();
462 // The surface used to fill the outer rim of the app running in compatibility mode.
463 Surface mBackgroundFillerSurface = null;
464 boolean mBackgroundFillerShown = false;
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 public static WindowManagerService main(Context context,
467 PowerManagerService pm, boolean haveInputMethods) {
468 WMThread thr = new WMThread(context, pm, haveInputMethods);
469 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 synchronized (thr) {
472 while (thr.mService == null) {
473 try {
474 thr.wait();
475 } catch (InterruptedException e) {
476 }
477 }
478 }
Romain Guy06882f82009-06-10 13:36:04 -0700479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return thr.mService;
481 }
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 static class WMThread extends Thread {
484 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 private final Context mContext;
487 private final PowerManagerService mPM;
488 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public WMThread(Context context, PowerManagerService pm,
491 boolean haveInputMethods) {
492 super("WindowManager");
493 mContext = context;
494 mPM = pm;
495 mHaveInputMethods = haveInputMethods;
496 }
Romain Guy06882f82009-06-10 13:36:04 -0700497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public void run() {
499 Looper.prepare();
500 WindowManagerService s = new WindowManagerService(mContext, mPM,
501 mHaveInputMethods);
502 android.os.Process.setThreadPriority(
503 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 synchronized (this) {
506 mService = s;
507 notifyAll();
508 }
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 Looper.loop();
511 }
512 }
513
514 static class PolicyThread extends Thread {
515 private final WindowManagerPolicy mPolicy;
516 private final WindowManagerService mService;
517 private final Context mContext;
518 private final PowerManagerService mPM;
519 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public PolicyThread(WindowManagerPolicy policy,
522 WindowManagerService service, Context context,
523 PowerManagerService pm) {
524 super("WindowManagerPolicy");
525 mPolicy = policy;
526 mService = service;
527 mContext = context;
528 mPM = pm;
529 }
Romain Guy06882f82009-06-10 13:36:04 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 public void run() {
532 Looper.prepare();
533 //Looper.myLooper().setMessageLogging(new LogPrinter(
534 // Log.VERBOSE, "WindowManagerPolicy"));
535 android.os.Process.setThreadPriority(
536 android.os.Process.THREAD_PRIORITY_FOREGROUND);
537 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 synchronized (this) {
540 mRunning = true;
541 notifyAll();
542 }
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 Looper.loop();
545 }
546 }
547
548 private WindowManagerService(Context context, PowerManagerService pm,
549 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700550 if (MEASURE_LATENCY) {
551 lt = new LatencyTimer(100, 1000);
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mContext = context;
555 mHaveInputMethods = haveInputMethods;
556 mLimitedAlphaCompositing = context.getResources().getBoolean(
557 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 mPowerManager = pm;
560 mPowerManager.setPolicy(mPolicy);
561 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
562 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
563 "SCREEN_FROZEN");
564 mScreenFrozenLock.setReferenceCounted(false);
565
566 mActivityManager = ActivityManagerNative.getDefault();
567 mBatteryStats = BatteryStatsService.getService();
568
569 // Get persisted window scale setting
570 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
571 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
572 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
573 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700574
Michael Chan9f028e62009-08-04 17:37:46 -0700575 int max_events_per_sec = 35;
576 try {
577 max_events_per_sec = Integer.parseInt(SystemProperties
578 .get("windowsmgr.max_events_per_sec"));
579 if (max_events_per_sec < 1) {
580 max_events_per_sec = 35;
581 }
582 } catch (NumberFormatException e) {
583 }
584 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mQueue = new KeyQ();
587
588 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
591 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 synchronized (thr) {
594 while (!thr.mRunning) {
595 try {
596 thr.wait();
597 } catch (InterruptedException e) {
598 }
599 }
600 }
Romain Guy06882f82009-06-10 13:36:04 -0700601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 // Add ourself to the Watchdog monitors.
605 Watchdog.getInstance().addMonitor(this);
606 }
607
608 @Override
609 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
610 throws RemoteException {
611 try {
612 return super.onTransact(code, data, reply, flags);
613 } catch (RuntimeException e) {
614 // The window manager only throws security exceptions, so let's
615 // log all others.
616 if (!(e instanceof SecurityException)) {
617 Log.e(TAG, "Window Manager Crash", e);
618 }
619 throw e;
620 }
621 }
622
623 private void placeWindowAfter(Object pos, WindowState window) {
624 final int i = mWindows.indexOf(pos);
625 if (localLOGV || DEBUG_FOCUS) Log.v(
626 TAG, "Adding window " + window + " at "
627 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
628 mWindows.add(i+1, window);
629 }
630
631 private void placeWindowBefore(Object pos, WindowState window) {
632 final int i = mWindows.indexOf(pos);
633 if (localLOGV || DEBUG_FOCUS) Log.v(
634 TAG, "Adding window " + window + " at "
635 + i + " of " + mWindows.size() + " (before " + pos + ")");
636 mWindows.add(i, window);
637 }
638
639 //This method finds out the index of a window that has the same app token as
640 //win. used for z ordering the windows in mWindows
641 private int findIdxBasedOnAppTokens(WindowState win) {
642 //use a local variable to cache mWindows
643 ArrayList localmWindows = mWindows;
644 int jmax = localmWindows.size();
645 if(jmax == 0) {
646 return -1;
647 }
648 for(int j = (jmax-1); j >= 0; j--) {
649 WindowState wentry = (WindowState)localmWindows.get(j);
650 if(wentry.mAppToken == win.mAppToken) {
651 return j;
652 }
653 }
654 return -1;
655 }
Romain Guy06882f82009-06-10 13:36:04 -0700656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
658 final IWindow client = win.mClient;
659 final WindowToken token = win.mToken;
660 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 final int N = localmWindows.size();
663 final WindowState attached = win.mAttachedWindow;
664 int i;
665 if (attached == null) {
666 int tokenWindowsPos = token.windows.size();
667 if (token.appWindowToken != null) {
668 int index = tokenWindowsPos-1;
669 if (index >= 0) {
670 // If this application has existing windows, we
671 // simply place the new window on top of them... but
672 // keep the starting window on top.
673 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
674 // Base windows go behind everything else.
675 placeWindowBefore(token.windows.get(0), win);
676 tokenWindowsPos = 0;
677 } else {
678 AppWindowToken atoken = win.mAppToken;
679 if (atoken != null &&
680 token.windows.get(index) == atoken.startingWindow) {
681 placeWindowBefore(token.windows.get(index), win);
682 tokenWindowsPos--;
683 } else {
684 int newIdx = findIdxBasedOnAppTokens(win);
685 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700686 //there is a window above this one associated with the same
687 //apptoken note that the window could be a floating window
688 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 //windows associated with this token.
690 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693 }
694 } else {
695 if (localLOGV) Log.v(
696 TAG, "Figuring out where to add app window "
697 + client.asBinder() + " (token=" + token + ")");
698 // Figure out where the window should go, based on the
699 // order of applications.
700 final int NA = mAppTokens.size();
701 Object pos = null;
702 for (i=NA-1; i>=0; i--) {
703 AppWindowToken t = mAppTokens.get(i);
704 if (t == token) {
705 i--;
706 break;
707 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700708
709 // We haven't reached the token yet; if this token
710 // is not going to the bottom and has windows, we can
711 // use it as an anchor for when we do reach the token.
712 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 pos = t.windows.get(0);
714 }
715 }
716 // We now know the index into the apps. If we found
717 // an app window above, that gives us the position; else
718 // we need to look some more.
719 if (pos != null) {
720 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700721 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 mTokenMap.get(((WindowState)pos).mClient.asBinder());
723 if (atoken != null) {
724 final int NC = atoken.windows.size();
725 if (NC > 0) {
726 WindowState bottom = atoken.windows.get(0);
727 if (bottom.mSubLayer < 0) {
728 pos = bottom;
729 }
730 }
731 }
732 placeWindowBefore(pos, win);
733 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700734 // Continue looking down until we find the first
735 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 while (i >= 0) {
737 AppWindowToken t = mAppTokens.get(i);
738 final int NW = t.windows.size();
739 if (NW > 0) {
740 pos = t.windows.get(NW-1);
741 break;
742 }
743 i--;
744 }
745 if (pos != null) {
746 // Move in front of any windows attached to this
747 // one.
748 WindowToken atoken =
749 mTokenMap.get(((WindowState)pos).mClient.asBinder());
750 if (atoken != null) {
751 final int NC = atoken.windows.size();
752 if (NC > 0) {
753 WindowState top = atoken.windows.get(NC-1);
754 if (top.mSubLayer >= 0) {
755 pos = top;
756 }
757 }
758 }
759 placeWindowAfter(pos, win);
760 } else {
761 // Just search for the start of this layer.
762 final int myLayer = win.mBaseLayer;
763 for (i=0; i<N; i++) {
764 WindowState w = (WindowState)localmWindows.get(i);
765 if (w.mBaseLayer > myLayer) {
766 break;
767 }
768 }
769 if (localLOGV || DEBUG_FOCUS) Log.v(
770 TAG, "Adding window " + win + " at "
771 + i + " of " + N);
772 localmWindows.add(i, win);
773 }
774 }
775 }
776 } else {
777 // Figure out where window should go, based on layer.
778 final int myLayer = win.mBaseLayer;
779 for (i=N-1; i>=0; i--) {
780 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
781 i++;
782 break;
783 }
784 }
785 if (i < 0) i = 0;
786 if (localLOGV || DEBUG_FOCUS) Log.v(
787 TAG, "Adding window " + win + " at "
788 + i + " of " + N);
789 localmWindows.add(i, win);
790 }
791 if (addToToken) {
792 token.windows.add(tokenWindowsPos, win);
793 }
794
795 } else {
796 // Figure out this window's ordering relative to the window
797 // it is attached to.
798 final int NA = token.windows.size();
799 final int sublayer = win.mSubLayer;
800 int largestSublayer = Integer.MIN_VALUE;
801 WindowState windowWithLargestSublayer = null;
802 for (i=0; i<NA; i++) {
803 WindowState w = token.windows.get(i);
804 final int wSublayer = w.mSubLayer;
805 if (wSublayer >= largestSublayer) {
806 largestSublayer = wSublayer;
807 windowWithLargestSublayer = w;
808 }
809 if (sublayer < 0) {
810 // For negative sublayers, we go below all windows
811 // in the same sublayer.
812 if (wSublayer >= sublayer) {
813 if (addToToken) {
814 token.windows.add(i, win);
815 }
816 placeWindowBefore(
817 wSublayer >= 0 ? attached : w, win);
818 break;
819 }
820 } else {
821 // For positive sublayers, we go above all windows
822 // in the same sublayer.
823 if (wSublayer > sublayer) {
824 if (addToToken) {
825 token.windows.add(i, win);
826 }
827 placeWindowBefore(w, win);
828 break;
829 }
830 }
831 }
832 if (i >= NA) {
833 if (addToToken) {
834 token.windows.add(win);
835 }
836 if (sublayer < 0) {
837 placeWindowBefore(attached, win);
838 } else {
839 placeWindowAfter(largestSublayer >= 0
840 ? windowWithLargestSublayer
841 : attached,
842 win);
843 }
844 }
845 }
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (win.mAppToken != null && addToToken) {
848 win.mAppToken.allAppWindows.add(win);
849 }
850 }
Romain Guy06882f82009-06-10 13:36:04 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 static boolean canBeImeTarget(WindowState w) {
853 final int fl = w.mAttrs.flags
854 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
855 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
856 return w.isVisibleOrAdding();
857 }
858 return false;
859 }
Romain Guy06882f82009-06-10 13:36:04 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
862 final ArrayList localmWindows = mWindows;
863 final int N = localmWindows.size();
864 WindowState w = null;
865 int i = N;
866 while (i > 0) {
867 i--;
868 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
871 // + Integer.toHexString(w.mAttrs.flags));
872 if (canBeImeTarget(w)) {
873 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // Yet more tricksyness! If this window is a "starting"
876 // window, we do actually want to be on top of it, but
877 // it is not -really- where input will go. So if the caller
878 // is not actually looking to move the IME, look down below
879 // for a real window to target...
880 if (!willMove
881 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
882 && i > 0) {
883 WindowState wb = (WindowState)localmWindows.get(i-1);
884 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
885 i--;
886 w = wb;
887 }
888 }
889 break;
890 }
891 }
Romain Guy06882f82009-06-10 13:36:04 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
896 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 if (willMove && w != null) {
899 final WindowState curTarget = mInputMethodTarget;
900 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Now some fun for dealing with window animations that
903 // modify the Z order. We need to look at all windows below
904 // the current target that are in this app, finding the highest
905 // visible one in layering.
906 AppWindowToken token = curTarget.mAppToken;
907 WindowState highestTarget = null;
908 int highestPos = 0;
909 if (token.animating || token.animation != null) {
910 int pos = 0;
911 pos = localmWindows.indexOf(curTarget);
912 while (pos >= 0) {
913 WindowState win = (WindowState)localmWindows.get(pos);
914 if (win.mAppToken != token) {
915 break;
916 }
917 if (!win.mRemoved) {
918 if (highestTarget == null || win.mAnimLayer >
919 highestTarget.mAnimLayer) {
920 highestTarget = win;
921 highestPos = pos;
922 }
923 }
924 pos--;
925 }
926 }
Romain Guy06882f82009-06-10 13:36:04 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700929 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 + mNextAppTransition + " " + highestTarget
931 + " animating=" + highestTarget.isAnimating()
932 + " layer=" + highestTarget.mAnimLayer
933 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
936 // If we are currently setting up for an animation,
937 // hold everything until we can find out what will happen.
938 mInputMethodTargetWaitingAnim = true;
939 mInputMethodTarget = highestTarget;
940 return highestPos + 1;
941 } else if (highestTarget.isAnimating() &&
942 highestTarget.mAnimLayer > w.mAnimLayer) {
943 // If the window we are currently targeting is involved
944 // with an animation, and it is on top of the next target
945 // we will be over, then hold off on moving until
946 // that is done.
947 mInputMethodTarget = highestTarget;
948 return highestPos + 1;
949 }
950 }
951 }
952 }
Romain Guy06882f82009-06-10 13:36:04 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 //Log.i(TAG, "Placing input method @" + (i+1));
955 if (w != null) {
956 if (willMove) {
957 RuntimeException e = new RuntimeException();
958 e.fillInStackTrace();
959 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
960 + mInputMethodTarget + " to " + w, e);
961 mInputMethodTarget = w;
962 if (w.mAppToken != null) {
963 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
964 } else {
965 setInputMethodAnimLayerAdjustment(0);
966 }
967 }
968 return i+1;
969 }
970 if (willMove) {
971 RuntimeException e = new RuntimeException();
972 e.fillInStackTrace();
973 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
974 + mInputMethodTarget + " to null", e);
975 mInputMethodTarget = null;
976 setInputMethodAnimLayerAdjustment(0);
977 }
978 return -1;
979 }
Romain Guy06882f82009-06-10 13:36:04 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 void addInputMethodWindowToListLocked(WindowState win) {
982 int pos = findDesiredInputMethodWindowIndexLocked(true);
983 if (pos >= 0) {
984 win.mTargetAppToken = mInputMethodTarget.mAppToken;
985 mWindows.add(pos, win);
986 moveInputMethodDialogsLocked(pos+1);
987 return;
988 }
989 win.mTargetAppToken = null;
990 addWindowToListInOrderLocked(win, true);
991 moveInputMethodDialogsLocked(pos);
992 }
Romain Guy06882f82009-06-10 13:36:04 -0700993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 void setInputMethodAnimLayerAdjustment(int adj) {
995 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
996 mInputMethodAnimLayerAdjustment = adj;
997 WindowState imw = mInputMethodWindow;
998 if (imw != null) {
999 imw.mAnimLayer = imw.mLayer + adj;
1000 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1001 + " anim layer: " + imw.mAnimLayer);
1002 int wi = imw.mChildWindows.size();
1003 while (wi > 0) {
1004 wi--;
1005 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1006 cw.mAnimLayer = cw.mLayer + adj;
1007 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
1008 + " anim layer: " + cw.mAnimLayer);
1009 }
1010 }
1011 int di = mInputMethodDialogs.size();
1012 while (di > 0) {
1013 di --;
1014 imw = mInputMethodDialogs.get(di);
1015 imw.mAnimLayer = imw.mLayer + adj;
1016 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1017 + " anim layer: " + imw.mAnimLayer);
1018 }
1019 }
Romain Guy06882f82009-06-10 13:36:04 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1022 int wpos = mWindows.indexOf(win);
1023 if (wpos >= 0) {
1024 if (wpos < interestingPos) interestingPos--;
1025 mWindows.remove(wpos);
1026 int NC = win.mChildWindows.size();
1027 while (NC > 0) {
1028 NC--;
1029 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1030 int cpos = mWindows.indexOf(cw);
1031 if (cpos >= 0) {
1032 if (cpos < interestingPos) interestingPos--;
1033 mWindows.remove(cpos);
1034 }
1035 }
1036 }
1037 return interestingPos;
1038 }
Romain Guy06882f82009-06-10 13:36:04 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 private void reAddWindowToListInOrderLocked(WindowState win) {
1041 addWindowToListInOrderLocked(win, false);
1042 // This is a hack to get all of the child windows added as well
1043 // at the right position. Child windows should be rare and
1044 // this case should be rare, so it shouldn't be that big a deal.
1045 int wpos = mWindows.indexOf(win);
1046 if (wpos >= 0) {
1047 mWindows.remove(wpos);
1048 reAddWindowLocked(wpos, win);
1049 }
1050 }
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 void logWindowList(String prefix) {
1053 int N = mWindows.size();
1054 while (N > 0) {
1055 N--;
1056 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1057 }
1058 }
Romain Guy06882f82009-06-10 13:36:04 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 void moveInputMethodDialogsLocked(int pos) {
1061 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 final int N = dialogs.size();
1064 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1065 for (int i=0; i<N; i++) {
1066 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1067 }
1068 if (DEBUG_INPUT_METHOD) {
1069 Log.v(TAG, "Window list w/pos=" + pos);
1070 logWindowList(" ");
1071 }
Romain Guy06882f82009-06-10 13:36:04 -07001072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (pos >= 0) {
1074 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1075 if (pos < mWindows.size()) {
1076 WindowState wp = (WindowState)mWindows.get(pos);
1077 if (wp == mInputMethodWindow) {
1078 pos++;
1079 }
1080 }
1081 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1082 for (int i=0; i<N; i++) {
1083 WindowState win = dialogs.get(i);
1084 win.mTargetAppToken = targetAppToken;
1085 pos = reAddWindowLocked(pos, win);
1086 }
1087 if (DEBUG_INPUT_METHOD) {
1088 Log.v(TAG, "Final window list:");
1089 logWindowList(" ");
1090 }
1091 return;
1092 }
1093 for (int i=0; i<N; i++) {
1094 WindowState win = dialogs.get(i);
1095 win.mTargetAppToken = null;
1096 reAddWindowToListInOrderLocked(win);
1097 if (DEBUG_INPUT_METHOD) {
1098 Log.v(TAG, "No IM target, final list:");
1099 logWindowList(" ");
1100 }
1101 }
1102 }
Romain Guy06882f82009-06-10 13:36:04 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1105 final WindowState imWin = mInputMethodWindow;
1106 final int DN = mInputMethodDialogs.size();
1107 if (imWin == null && DN == 0) {
1108 return false;
1109 }
Romain Guy06882f82009-06-10 13:36:04 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1112 if (imPos >= 0) {
1113 // In this case, the input method windows are to be placed
1114 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // First check to see if the input method windows are already
1117 // located here, and contiguous.
1118 final int N = mWindows.size();
1119 WindowState firstImWin = imPos < N
1120 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 // Figure out the actual input method window that should be
1123 // at the bottom of their stack.
1124 WindowState baseImWin = imWin != null
1125 ? imWin : mInputMethodDialogs.get(0);
1126 if (baseImWin.mChildWindows.size() > 0) {
1127 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1128 if (cw.mSubLayer < 0) baseImWin = cw;
1129 }
Romain Guy06882f82009-06-10 13:36:04 -07001130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (firstImWin == baseImWin) {
1132 // The windows haven't moved... but are they still contiguous?
1133 // First find the top IM window.
1134 int pos = imPos+1;
1135 while (pos < N) {
1136 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1137 break;
1138 }
1139 pos++;
1140 }
1141 pos++;
1142 // Now there should be no more input method windows above.
1143 while (pos < N) {
1144 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1145 break;
1146 }
1147 pos++;
1148 }
1149 if (pos >= N) {
1150 // All is good!
1151 return false;
1152 }
1153 }
Romain Guy06882f82009-06-10 13:36:04 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (imWin != null) {
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "Moving IM from " + imPos);
1158 logWindowList(" ");
1159 }
1160 imPos = tmpRemoveWindowLocked(imPos, imWin);
1161 if (DEBUG_INPUT_METHOD) {
1162 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1163 logWindowList(" ");
1164 }
1165 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1166 reAddWindowLocked(imPos, imWin);
1167 if (DEBUG_INPUT_METHOD) {
1168 Log.v(TAG, "List after moving IM to " + imPos + ":");
1169 logWindowList(" ");
1170 }
1171 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1172 } else {
1173 moveInputMethodDialogsLocked(imPos);
1174 }
Romain Guy06882f82009-06-10 13:36:04 -07001175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 } else {
1177 // In this case, the input method windows go in a fixed layer,
1178 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 if (imWin != null) {
1181 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1182 tmpRemoveWindowLocked(0, imWin);
1183 imWin.mTargetAppToken = null;
1184 reAddWindowToListInOrderLocked(imWin);
1185 if (DEBUG_INPUT_METHOD) {
1186 Log.v(TAG, "List with no IM target:");
1187 logWindowList(" ");
1188 }
1189 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1190 } else {
1191 moveInputMethodDialogsLocked(-1);;
1192 }
Romain Guy06882f82009-06-10 13:36:04 -07001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
Romain Guy06882f82009-06-10 13:36:04 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 if (needAssignLayers) {
1197 assignLayersLocked();
1198 }
Romain Guy06882f82009-06-10 13:36:04 -07001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 return true;
1201 }
Romain Guy06882f82009-06-10 13:36:04 -07001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 void adjustInputMethodDialogsLocked() {
1204 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1205 }
Romain Guy06882f82009-06-10 13:36:04 -07001206
Dianne Hackborn25994b42009-09-04 14:21:19 -07001207 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1208 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1209 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1210 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1211 ? wallpaperTarget.mAppToken.animation : null)
1212 + " upper=" + mUpperWallpaperTarget
1213 + " lower=" + mLowerWallpaperTarget);
1214 return (wallpaperTarget != null
1215 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1216 && wallpaperTarget.mAppToken.animation != null)))
1217 || mUpperWallpaperTarget != null
1218 || mLowerWallpaperTarget != null;
1219 }
1220
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001221 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1222 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1223
1224 int adjustWallpaperWindowsLocked() {
1225 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001226
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001227 final int dw = mDisplay.getWidth();
1228 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001229
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001230 // First find top-most window that has asked to be on top of the
1231 // wallpaper; all wallpapers go behind it.
1232 final ArrayList localmWindows = mWindows;
1233 int N = localmWindows.size();
1234 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001235 WindowState foundW = null;
1236 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001237 int i = N;
1238 while (i > 0) {
1239 i--;
1240 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001241 if (w.mAppToken != null) {
1242 // If this window's app token is hidden and not animating,
1243 // it is of no interest to us.
1244 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1245 if (DEBUG_WALLPAPER) Log.v(TAG,
1246 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001247 continue;
1248 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001249 // If this window's app token is ot fullscreen, also irrelevant.
1250 if (!w.mAppToken.appFullscreen) {
1251 if (DEBUG_WALLPAPER) Log.v(TAG,
1252 "Skipping non-fullscreen token: " + w);
1253 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001254 }
1255 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001256 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1257 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1258 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001259 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001260 && (mWallpaperTarget == w
1261 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001262 if (DEBUG_WALLPAPER) Log.v(TAG,
1263 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001264 foundW = w;
1265 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001266 if (w == mWallpaperTarget && w.mAppToken != null
1267 && w.mAppToken.animation != null) {
1268 // The current wallpaper target is animating, so we'll
1269 // look behind it for another possible target and figure
1270 // out what is going on below.
1271 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1272 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001273 continue;
1274 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001275 break;
1276 }
1277 }
1278
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001279 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1280 // If we are currently waiting for an app transition, and either
1281 // the current target or the next target are involved with it,
1282 // then hold off on doing anything with the wallpaper.
1283 // Note that we are checking here for just whether the target
1284 // is part of an app token... which is potentially overly aggressive
1285 // (the app token may not be involved in the transition), but good
1286 // enough (we'll just wait until whatever transition is pending
1287 // executes).
1288 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001289 if (DEBUG_WALLPAPER) Log.v(TAG,
1290 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001291 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001292 }
1293 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001294 if (DEBUG_WALLPAPER) Log.v(TAG,
1295 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001296 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001297 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001298 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001299
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001300 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001301 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001302 Log.v(TAG, "New wallpaper target: " + foundW
1303 + " oldTarget: " + mWallpaperTarget);
1304 }
1305
1306 mLowerWallpaperTarget = null;
1307 mUpperWallpaperTarget = null;
1308
1309 WindowState oldW = mWallpaperTarget;
1310 mWallpaperTarget = foundW;
1311
1312 // Now what is happening... if the current and new targets are
1313 // animating, then we are in our super special mode!
1314 if (foundW != null && foundW.mAppToken != null && oldW != null
1315 && oldW.mAppToken != null) {
1316 if (DEBUG_WALLPAPER) {
1317 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1318 + " old animation: " + oldW.mAppToken.animation);
1319 }
1320 if (foundW.mAppToken.animation != null
1321 && oldW.mAppToken.animation != null) {
1322 int oldI = localmWindows.indexOf(oldW);
1323 if (DEBUG_WALLPAPER) {
1324 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1325 }
1326 if (oldI >= 0) {
1327 if (DEBUG_WALLPAPER) {
1328 Log.v(TAG, "Animating wallpapers: old#" + oldI
1329 + "=" + oldW + "; new#" + foundI
1330 + "=" + foundW);
1331 }
1332
1333 // Set the new target correctly.
1334 if (foundW.mAppToken.hiddenRequested) {
1335 if (DEBUG_WALLPAPER) {
1336 Log.v(TAG, "Old wallpaper still the target.");
1337 }
1338 mWallpaperTarget = oldW;
1339 }
1340
1341 // Now set the upper and lower wallpaper targets
1342 // correctly, and make sure that we are positioning
1343 // the wallpaper below the lower.
1344 if (foundI > oldI) {
1345 // The new target is on top of the old one.
1346 if (DEBUG_WALLPAPER) {
1347 Log.v(TAG, "Found target above old target.");
1348 }
1349 mUpperWallpaperTarget = foundW;
1350 mLowerWallpaperTarget = oldW;
1351 foundW = oldW;
1352 foundI = oldI;
1353 } else {
1354 // The new target is below the old one.
1355 if (DEBUG_WALLPAPER) {
1356 Log.v(TAG, "Found target below old target.");
1357 }
1358 mUpperWallpaperTarget = oldW;
1359 mLowerWallpaperTarget = foundW;
1360 }
1361 }
1362 }
1363 }
1364
1365 } else {
1366 // Is it time to stop animating?
1367 if (mLowerWallpaperTarget == null
1368 || mLowerWallpaperTarget.mAppToken.animation == null
1369 || mUpperWallpaperTarget == null
1370 || mUpperWallpaperTarget.mAppToken.animation == null) {
1371 if (DEBUG_WALLPAPER) {
1372 Log.v(TAG, "No longer animating wallpaper targets!");
1373 }
1374 mLowerWallpaperTarget = null;
1375 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001376 }
1377 }
1378
1379 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001380 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001381 // The window is visible to the compositor... but is it visible
1382 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001383 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001384 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001385
1386 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001387 // its layer adjustment. Only do this if we are not transfering
1388 // between two wallpaper targets.
1389 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001390 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001391 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001392
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001393 // Now w is the window we are supposed to be behind... but we
1394 // need to be sure to also be behind any of its attached windows,
1395 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001396 while (foundI > 0) {
1397 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1398 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001399 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001400 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001401 // This window is not related to the previous one in any
1402 // interesting way, so stop here.
1403 break;
1404 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001405 foundW = wb;
1406 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001407 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001408 } else {
1409 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001410 }
1411
1412 // Okay i is the position immediately above the wallpaper. Look at
1413 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001414 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001415
Dianne Hackborn284ac932009-08-28 10:34:25 -07001416 if (visible) {
1417 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1418 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1419 }
1420
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001421 // Start stepping backwards from here, ensuring that our wallpaper windows
1422 // are correctly placed.
1423 int curTokenIndex = mWallpaperTokens.size();
1424 while (curTokenIndex > 0) {
1425 curTokenIndex--;
1426 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001427 if (token.hidden == visible) {
1428 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1429 token.hidden = !visible;
1430 // Need to do a layout to ensure the wallpaper now has the
1431 // correct size.
1432 mLayoutNeeded = true;
1433 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001434
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001435 int curWallpaperIndex = token.windows.size();
1436 while (curWallpaperIndex > 0) {
1437 curWallpaperIndex--;
1438 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001439
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001440 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001441 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001442 }
1443
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001444 // First, make sure the client has the current visibility
1445 // state.
1446 if (wallpaper.mWallpaperVisible != visible) {
1447 wallpaper.mWallpaperVisible = visible;
1448 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001449 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001450 "Setting visibility of wallpaper " + wallpaper
1451 + ": " + visible);
1452 wallpaper.mClient.dispatchAppVisibility(visible);
1453 } catch (RemoteException e) {
1454 }
1455 }
1456
1457 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001458 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1459 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001460
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 // First, if this window is at the current index, then all
1462 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001463 if (wallpaper == foundW) {
1464 foundI--;
1465 foundW = foundI > 0
1466 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001467 continue;
1468 }
1469
1470 // The window didn't match... the current wallpaper window,
1471 // wherever it is, is in the wrong place, so make sure it is
1472 // not in the list.
1473 int oldIndex = localmWindows.indexOf(wallpaper);
1474 if (oldIndex >= 0) {
1475 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001476 if (oldIndex < foundI) {
1477 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001478 }
1479 }
1480
1481 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001482 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001483 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001484
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001485 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001486 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001487 }
1488 }
1489
1490 return changed;
1491 }
1492
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001493 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001494 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1495 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001496 mWallpaperAnimLayerAdjustment = adj;
1497 int curTokenIndex = mWallpaperTokens.size();
1498 while (curTokenIndex > 0) {
1499 curTokenIndex--;
1500 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1501 int curWallpaperIndex = token.windows.size();
1502 while (curWallpaperIndex > 0) {
1503 curWallpaperIndex--;
1504 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1505 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001506 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1507 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 }
1509 }
1510 }
1511
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001512 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1513 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001514 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001515 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001516 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001517 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001518 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001519 changed = wallpaperWin.mXOffset != offset;
1520 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001521 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1522 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001523 wallpaperWin.mXOffset = offset;
1524 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001525 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1526 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001527 rawChanged = true;
1528 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001529 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001530
Dianne Hackborn284ac932009-08-28 10:34:25 -07001531 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001532 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001533 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001534 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001535 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1536 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001537 changed = true;
1538 wallpaperWin.mYOffset = offset;
1539 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001540 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1541 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001542 rawChanged = true;
1543 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001544 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001545
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001546 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001547 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001548 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1549 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1550 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001551 if (sync) {
1552 synchronized (mWaitingOnWallpaperLock) {
1553 mWaitingOnWallpaper = wallpaperWin;
1554 }
1555 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001556 wallpaperWin.mClient.dispatchWallpaperOffsets(
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001557 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY, sync);
1558 if (sync) {
1559 synchronized (mWaitingOnWallpaperLock) {
1560 if (mWaitingOnWallpaper != null) {
1561 long start = SystemClock.uptimeMillis();
1562 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1563 < start) {
1564 try {
1565 if (DEBUG_WALLPAPER) Log.v(TAG,
1566 "Waiting for offset complete...");
1567 mWaitingOnWallpaperLock.wait(WALLPAPER_TIMEOUT);
1568 } catch (InterruptedException e) {
1569 }
1570 if (DEBUG_WALLPAPER) Log.v(TAG, "Offset complete!");
1571 if ((start+WALLPAPER_TIMEOUT)
1572 < SystemClock.uptimeMillis()) {
1573 Log.i(TAG, "Timeout waiting for wallpaper to offset: "
1574 + wallpaperWin);
1575 mLastWallpaperTimeoutTime = start;
1576 }
1577 }
1578 mWaitingOnWallpaper = null;
1579 }
1580 }
1581 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001582 } catch (RemoteException e) {
1583 }
1584 }
1585
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001586 return changed;
1587 }
1588
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001589 void wallpaperOffsetsComplete(IBinder window) {
1590 synchronized (mWaitingOnWallpaperLock) {
1591 if (mWaitingOnWallpaper != null &&
1592 mWaitingOnWallpaper.mClient.asBinder() == window) {
1593 mWaitingOnWallpaper = null;
1594 mWaitingOnWallpaperLock.notifyAll();
1595 }
1596 }
1597 }
1598
1599 boolean updateWallpaperOffsetLocked(boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001600 final int dw = mDisplay.getWidth();
1601 final int dh = mDisplay.getHeight();
1602
1603 boolean changed = false;
1604
1605 WindowState target = mWallpaperTarget;
1606 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001607 mLastWallpaperX = target.mWallpaperX;
1608 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001609 int curTokenIndex = mWallpaperTokens.size();
1610 while (curTokenIndex > 0) {
1611 curTokenIndex--;
1612 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1613 int curWallpaperIndex = token.windows.size();
1614 while (curWallpaperIndex > 0) {
1615 curWallpaperIndex--;
1616 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001617 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001618 wallpaper.computeShownFrameLocked();
1619 changed = true;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 // We only want to be synchronous with one wallpaper.
1621 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001622 }
1623 }
1624 }
1625 }
1626
1627 return changed;
1628 }
1629
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001630 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001631 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001632 final int dw = mDisplay.getWidth();
1633 final int dh = mDisplay.getHeight();
1634
1635 int curTokenIndex = mWallpaperTokens.size();
1636 while (curTokenIndex > 0) {
1637 curTokenIndex--;
1638 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001639 if (token.hidden == visible) {
1640 token.hidden = !visible;
1641 // Need to do a layout to ensure the wallpaper now has the
1642 // correct size.
1643 mLayoutNeeded = true;
1644 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001645
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001646 int curWallpaperIndex = token.windows.size();
1647 while (curWallpaperIndex > 0) {
1648 curWallpaperIndex--;
1649 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1650 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001651 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001652 }
1653
1654 if (wallpaper.mWallpaperVisible != visible) {
1655 wallpaper.mWallpaperVisible = visible;
1656 try {
1657 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001658 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001659 + ": " + visible);
1660 wallpaper.mClient.dispatchAppVisibility(visible);
1661 } catch (RemoteException e) {
1662 }
1663 }
1664 }
1665 }
1666 }
1667
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001668 void sendPointerToWallpaperLocked(WindowState srcWin,
1669 MotionEvent pointer, long eventTime) {
1670 int curTokenIndex = mWallpaperTokens.size();
1671 while (curTokenIndex > 0) {
1672 curTokenIndex--;
1673 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1674 int curWallpaperIndex = token.windows.size();
1675 while (curWallpaperIndex > 0) {
1676 curWallpaperIndex--;
1677 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1678 if ((wallpaper.mAttrs.flags &
1679 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1680 continue;
1681 }
1682 try {
1683 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1684 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1685 srcWin.mFrame.top-wallpaper.mFrame.top);
1686 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1687 } catch (RemoteException e) {
1688 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1689 }
1690 }
1691 }
1692 }
1693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 public int addWindow(Session session, IWindow client,
1695 WindowManager.LayoutParams attrs, int viewVisibility,
1696 Rect outContentInsets) {
1697 int res = mPolicy.checkAddPermission(attrs);
1698 if (res != WindowManagerImpl.ADD_OKAY) {
1699 return res;
1700 }
Romain Guy06882f82009-06-10 13:36:04 -07001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 boolean reportNewConfig = false;
1703 WindowState attachedWindow = null;
1704 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 synchronized(mWindowMap) {
1707 // Instantiating a Display requires talking with the simulator,
1708 // so don't do it until we know the system is mostly up and
1709 // running.
1710 if (mDisplay == null) {
1711 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1712 mDisplay = wm.getDefaultDisplay();
1713 mQueue.setDisplay(mDisplay);
1714 reportNewConfig = true;
1715 }
Romain Guy06882f82009-06-10 13:36:04 -07001716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 if (mWindowMap.containsKey(client.asBinder())) {
1718 Log.w(TAG, "Window " + client + " is already added");
1719 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1720 }
1721
1722 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001723 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (attachedWindow == null) {
1725 Log.w(TAG, "Attempted to add window with token that is not a window: "
1726 + attrs.token + ". Aborting.");
1727 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1728 }
1729 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1730 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1731 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1732 + attrs.token + ". Aborting.");
1733 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1734 }
1735 }
1736
1737 boolean addToken = false;
1738 WindowToken token = mTokenMap.get(attrs.token);
1739 if (token == null) {
1740 if (attrs.type >= FIRST_APPLICATION_WINDOW
1741 && attrs.type <= LAST_APPLICATION_WINDOW) {
1742 Log.w(TAG, "Attempted to add application window with unknown token "
1743 + attrs.token + ". Aborting.");
1744 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1745 }
1746 if (attrs.type == TYPE_INPUT_METHOD) {
1747 Log.w(TAG, "Attempted to add input method window with unknown token "
1748 + attrs.token + ". Aborting.");
1749 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1750 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001751 if (attrs.type == TYPE_WALLPAPER) {
1752 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1753 + attrs.token + ". Aborting.");
1754 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 token = new WindowToken(attrs.token, -1, false);
1757 addToken = true;
1758 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1759 && attrs.type <= LAST_APPLICATION_WINDOW) {
1760 AppWindowToken atoken = token.appWindowToken;
1761 if (atoken == null) {
1762 Log.w(TAG, "Attempted to add window with non-application token "
1763 + token + ". Aborting.");
1764 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1765 } else if (atoken.removed) {
1766 Log.w(TAG, "Attempted to add window with exiting application token "
1767 + token + ". Aborting.");
1768 return WindowManagerImpl.ADD_APP_EXITING;
1769 }
1770 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1771 // No need for this guy!
1772 if (localLOGV) Log.v(
1773 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1774 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1775 }
1776 } else if (attrs.type == TYPE_INPUT_METHOD) {
1777 if (token.windowType != TYPE_INPUT_METHOD) {
1778 Log.w(TAG, "Attempted to add input method window with bad token "
1779 + attrs.token + ". Aborting.");
1780 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1781 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001782 } else if (attrs.type == TYPE_WALLPAPER) {
1783 if (token.windowType != TYPE_WALLPAPER) {
1784 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1785 + attrs.token + ". Aborting.");
1786 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789
1790 win = new WindowState(session, client, token,
1791 attachedWindow, attrs, viewVisibility);
1792 if (win.mDeathRecipient == null) {
1793 // Client has apparently died, so there is no reason to
1794 // continue.
1795 Log.w(TAG, "Adding window client " + client.asBinder()
1796 + " that is dead, aborting.");
1797 return WindowManagerImpl.ADD_APP_EXITING;
1798 }
1799
1800 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 res = mPolicy.prepareAddWindowLw(win, attrs);
1803 if (res != WindowManagerImpl.ADD_OKAY) {
1804 return res;
1805 }
1806
1807 // From now on, no exceptions or errors allowed!
1808
1809 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (addToken) {
1814 mTokenMap.put(attrs.token, token);
1815 mTokenList.add(token);
1816 }
1817 win.attach();
1818 mWindowMap.put(client.asBinder(), win);
1819
1820 if (attrs.type == TYPE_APPLICATION_STARTING &&
1821 token.appWindowToken != null) {
1822 token.appWindowToken.startingWindow = win;
1823 }
1824
1825 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (attrs.type == TYPE_INPUT_METHOD) {
1828 mInputMethodWindow = win;
1829 addInputMethodWindowToListLocked(win);
1830 imMayMove = false;
1831 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1832 mInputMethodDialogs.add(win);
1833 addWindowToListInOrderLocked(win, true);
1834 adjustInputMethodDialogsLocked();
1835 imMayMove = false;
1836 } else {
1837 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001838 if (attrs.type == TYPE_WALLPAPER) {
1839 mLastWallpaperTimeoutTime = 0;
1840 adjustWallpaperWindowsLocked();
1841 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001842 adjustWallpaperWindowsLocked();
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Romain Guy06882f82009-06-10 13:36:04 -07001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 if (mInTouchMode) {
1851 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1852 }
1853 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1854 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1855 }
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001857 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001859 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1860 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 imMayMove = false;
1862 }
1863 }
Romain Guy06882f82009-06-10 13:36:04 -07001864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001866 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Romain Guy06882f82009-06-10 13:36:04 -07001868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 assignLayersLocked();
1870 // Don't do layout here, the window must call
1871 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 //dump();
1874
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001875 if (focusChanged) {
1876 if (mCurrentFocus != null) {
1877 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1878 }
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (localLOGV) Log.v(
1881 TAG, "New client " + client.asBinder()
1882 + ": window=" + win);
1883 }
1884
1885 // sendNewConfiguration() checks caller permissions so we must call it with
1886 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1887 // identity anyway, so it's safe to just clear & restore around this whole
1888 // block.
1889 final long origId = Binder.clearCallingIdentity();
1890 if (reportNewConfig) {
1891 sendNewConfiguration();
1892 } else {
1893 // Update Orientation after adding a window, only if the window needs to be
1894 // displayed right away
1895 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001896 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 sendNewConfiguration();
1898 }
1899 }
1900 }
1901 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 return res;
1904 }
Romain Guy06882f82009-06-10 13:36:04 -07001905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 public void removeWindow(Session session, IWindow client) {
1907 synchronized(mWindowMap) {
1908 WindowState win = windowForClientLocked(session, client);
1909 if (win == null) {
1910 return;
1911 }
1912 removeWindowLocked(session, win);
1913 }
1914 }
Romain Guy06882f82009-06-10 13:36:04 -07001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 public void removeWindowLocked(Session session, WindowState win) {
1917
1918 if (localLOGV || DEBUG_FOCUS) Log.v(
1919 TAG, "Remove " + win + " client="
1920 + Integer.toHexString(System.identityHashCode(
1921 win.mClient.asBinder()))
1922 + ", surface=" + win.mSurface);
1923
1924 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (DEBUG_APP_TRANSITIONS) Log.v(
1927 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1928 + " mExiting=" + win.mExiting
1929 + " isAnimating=" + win.isAnimating()
1930 + " app-animation="
1931 + (win.mAppToken != null ? win.mAppToken.animation : null)
1932 + " inPendingTransaction="
1933 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1934 + " mDisplayFrozen=" + mDisplayFrozen);
1935 // Visibility of the removed window. Will be used later to update orientation later on.
1936 boolean wasVisible = false;
1937 // First, see if we need to run an animation. If we do, we have
1938 // to hold off on removing the window until the animation is done.
1939 // If the display is frozen, just remove immediately, since the
1940 // animation wouldn't be seen.
1941 if (win.mSurface != null && !mDisplayFrozen) {
1942 // If we are not currently running the exit animation, we
1943 // need to see about starting one.
1944 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1947 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1948 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1949 }
1950 // Try starting an animation.
1951 if (applyAnimationLocked(win, transit, false)) {
1952 win.mExiting = true;
1953 }
1954 }
1955 if (win.mExiting || win.isAnimating()) {
1956 // The exit animation is running... wait for it!
1957 //Log.i(TAG, "*** Running exit animation...");
1958 win.mExiting = true;
1959 win.mRemoveOnExit = true;
1960 mLayoutNeeded = true;
1961 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1962 performLayoutAndPlaceSurfacesLocked();
1963 if (win.mAppToken != null) {
1964 win.mAppToken.updateReportedVisibilityLocked();
1965 }
1966 //dump();
1967 Binder.restoreCallingIdentity(origId);
1968 return;
1969 }
1970 }
1971
1972 removeWindowInnerLocked(session, win);
1973 // Removing a visible window will effect the computed orientation
1974 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001975 if (wasVisible && computeForcedAppOrientationLocked()
1976 != mForcedAppOrientation) {
1977 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1980 Binder.restoreCallingIdentity(origId);
1981 }
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001984 mKeyWaiter.finishedKey(session, win.mClient, true,
1985 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1987 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (mInputMethodTarget == win) {
1992 moveInputMethodWindowsIfNeededLocked(false);
1993 }
Romain Guy06882f82009-06-10 13:36:04 -07001994
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001995 if (false) {
1996 RuntimeException e = new RuntimeException("here");
1997 e.fillInStackTrace();
1998 Log.w(TAG, "Removing window " + win, e);
1999 }
2000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 mPolicy.removeWindowLw(win);
2002 win.removeLocked();
2003
2004 mWindowMap.remove(win.mClient.asBinder());
2005 mWindows.remove(win);
2006
2007 if (mInputMethodWindow == win) {
2008 mInputMethodWindow = null;
2009 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2010 mInputMethodDialogs.remove(win);
2011 }
Romain Guy06882f82009-06-10 13:36:04 -07002012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 final WindowToken token = win.mToken;
2014 final AppWindowToken atoken = win.mAppToken;
2015 token.windows.remove(win);
2016 if (atoken != null) {
2017 atoken.allAppWindows.remove(win);
2018 }
2019 if (localLOGV) Log.v(
2020 TAG, "**** Removing window " + win + ": count="
2021 + token.windows.size());
2022 if (token.windows.size() == 0) {
2023 if (!token.explicit) {
2024 mTokenMap.remove(token.token);
2025 mTokenList.remove(token);
2026 } else if (atoken != null) {
2027 atoken.firstWindowDrawn = false;
2028 }
2029 }
2030
2031 if (atoken != null) {
2032 if (atoken.startingWindow == win) {
2033 atoken.startingWindow = null;
2034 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2035 // If this is the last window and we had requested a starting
2036 // transition window, well there is no point now.
2037 atoken.startingData = null;
2038 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2039 // If this is the last window except for a starting transition
2040 // window, we need to get rid of the starting transition.
2041 if (DEBUG_STARTING_WINDOW) {
2042 Log.v(TAG, "Schedule remove starting " + token
2043 + ": no more real windows");
2044 }
2045 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2046 mH.sendMessage(m);
2047 }
2048 }
Romain Guy06882f82009-06-10 13:36:04 -07002049
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002050 if (win.mAttrs.type == TYPE_WALLPAPER) {
2051 mLastWallpaperTimeoutTime = 0;
2052 adjustWallpaperWindowsLocked();
2053 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002054 adjustWallpaperWindowsLocked();
2055 }
2056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 if (!mInLayout) {
2058 assignLayersLocked();
2059 mLayoutNeeded = true;
2060 performLayoutAndPlaceSurfacesLocked();
2061 if (win.mAppToken != null) {
2062 win.mAppToken.updateReportedVisibilityLocked();
2063 }
2064 }
2065 }
2066
2067 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2068 long origId = Binder.clearCallingIdentity();
2069 try {
2070 synchronized (mWindowMap) {
2071 WindowState w = windowForClientLocked(session, client);
2072 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002073 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 Surface.openTransaction();
2075 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002076 if (SHOW_TRANSACTIONS) Log.i(
2077 TAG, " SURFACE " + w.mSurface
2078 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 w.mSurface.setTransparentRegionHint(region);
2080 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002081 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 Surface.closeTransaction();
2083 }
2084 }
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(origId);
2088 }
2089 }
2090
2091 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002092 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 Rect visibleInsets) {
2094 long origId = Binder.clearCallingIdentity();
2095 try {
2096 synchronized (mWindowMap) {
2097 WindowState w = windowForClientLocked(session, client);
2098 if (w != null) {
2099 w.mGivenInsetsPending = false;
2100 w.mGivenContentInsets.set(contentInsets);
2101 w.mGivenVisibleInsets.set(visibleInsets);
2102 w.mTouchableInsets = touchableInsets;
2103 mLayoutNeeded = true;
2104 performLayoutAndPlaceSurfacesLocked();
2105 }
2106 }
2107 } finally {
2108 Binder.restoreCallingIdentity(origId);
2109 }
2110 }
Romain Guy06882f82009-06-10 13:36:04 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 public void getWindowDisplayFrame(Session session, IWindow client,
2113 Rect outDisplayFrame) {
2114 synchronized(mWindowMap) {
2115 WindowState win = windowForClientLocked(session, client);
2116 if (win == null) {
2117 outDisplayFrame.setEmpty();
2118 return;
2119 }
2120 outDisplayFrame.set(win.mDisplayFrame);
2121 }
2122 }
2123
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002124 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2125 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2126 window.mWallpaperX = x;
2127 window.mWallpaperY = y;
2128
2129 if (mWallpaperTarget == window) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002130 if (updateWallpaperOffsetLocked(true)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002131 performLayoutAndPlaceSurfacesLocked();
2132 }
2133 }
2134 }
2135 }
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 public int relayoutWindow(Session session, IWindow client,
2138 WindowManager.LayoutParams attrs, int requestedWidth,
2139 int requestedHeight, int viewVisibility, boolean insetsPending,
2140 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2141 Surface outSurface) {
2142 boolean displayed = false;
2143 boolean inTouchMode;
2144 Configuration newConfig = null;
2145 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 synchronized(mWindowMap) {
2148 WindowState win = windowForClientLocked(session, client);
2149 if (win == null) {
2150 return 0;
2151 }
2152 win.mRequestedWidth = requestedWidth;
2153 win.mRequestedHeight = requestedHeight;
2154
2155 if (attrs != null) {
2156 mPolicy.adjustWindowParamsLw(attrs);
2157 }
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 int attrChanges = 0;
2160 int flagChanges = 0;
2161 if (attrs != null) {
2162 flagChanges = win.mAttrs.flags ^= attrs.flags;
2163 attrChanges = win.mAttrs.copyFrom(attrs);
2164 }
2165
2166 if (localLOGV) Log.v(
2167 TAG, "Relayout given client " + client.asBinder()
2168 + " (" + win.mAttrs.getTitle() + ")");
2169
2170
2171 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2172 win.mAlpha = attrs.alpha;
2173 }
2174
2175 final boolean scaledWindow =
2176 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2177
2178 if (scaledWindow) {
2179 // requested{Width|Height} Surface's physical size
2180 // attrs.{width|height} Size on screen
2181 win.mHScale = (attrs.width != requestedWidth) ?
2182 (attrs.width / (float)requestedWidth) : 1.0f;
2183 win.mVScale = (attrs.height != requestedHeight) ?
2184 (attrs.height / (float)requestedHeight) : 1.0f;
2185 }
2186
2187 boolean imMayMove = (flagChanges&(
2188 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2189 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 boolean focusMayChange = win.mViewVisibility != viewVisibility
2192 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2193 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002194
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002195 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2196 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 win.mRelayoutCalled = true;
2199 final int oldVisibility = win.mViewVisibility;
2200 win.mViewVisibility = viewVisibility;
2201 if (viewVisibility == View.VISIBLE &&
2202 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2203 displayed = !win.isVisibleLw();
2204 if (win.mExiting) {
2205 win.mExiting = false;
2206 win.mAnimation = null;
2207 }
2208 if (win.mDestroying) {
2209 win.mDestroying = false;
2210 mDestroySurface.remove(win);
2211 }
2212 if (oldVisibility == View.GONE) {
2213 win.mEnterAnimationPending = true;
2214 }
2215 if (displayed && win.mSurface != null && !win.mDrawPending
2216 && !win.mCommitDrawPending && !mDisplayFrozen) {
2217 applyEnterAnimationLocked(win);
2218 }
2219 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2220 // To change the format, we need to re-build the surface.
2221 win.destroySurfaceLocked();
2222 displayed = true;
2223 }
2224 try {
2225 Surface surface = win.createSurfaceLocked();
2226 if (surface != null) {
2227 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002228 if (SHOW_TRANSACTIONS) Log.i(TAG,
2229 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002231 // For some reason there isn't a surface. Clear the
2232 // caller's object so they see the same state.
2233 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
2235 } catch (Exception e) {
2236 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002237 + client + " (" + win.mAttrs.getTitle() + ")",
2238 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 Binder.restoreCallingIdentity(origId);
2240 return 0;
2241 }
2242 if (displayed) {
2243 focusMayChange = true;
2244 }
2245 if (win.mAttrs.type == TYPE_INPUT_METHOD
2246 && mInputMethodWindow == null) {
2247 mInputMethodWindow = win;
2248 imMayMove = true;
2249 }
2250 } else {
2251 win.mEnterAnimationPending = false;
2252 if (win.mSurface != null) {
2253 // If we are not currently running the exit animation, we
2254 // need to see about starting one.
2255 if (!win.mExiting) {
2256 // Try starting an animation; if there isn't one, we
2257 // can destroy the surface right away.
2258 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2259 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2260 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2261 }
2262 if (win.isWinVisibleLw() &&
2263 applyAnimationLocked(win, transit, false)) {
2264 win.mExiting = true;
2265 mKeyWaiter.finishedKey(session, client, true,
2266 KeyWaiter.RETURN_NOTHING);
2267 } else if (win.isAnimating()) {
2268 // Currently in a hide animation... turn this into
2269 // an exit.
2270 win.mExiting = true;
2271 } else {
2272 if (mInputMethodWindow == win) {
2273 mInputMethodWindow = null;
2274 }
2275 win.destroySurfaceLocked();
2276 }
2277 }
2278 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002279 // We are being called from a local process, which
2280 // means outSurface holds its current surface. Ensure the
2281 // surface object is cleared, but we don't want it actually
2282 // destroyed at this point.
2283 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
2285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 if (focusMayChange) {
2287 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2288 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 imMayMove = false;
2290 }
2291 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2292 }
Romain Guy06882f82009-06-10 13:36:04 -07002293
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002294 // updateFocusedWindowLocked() already assigned layers so we only need to
2295 // reassign them at this point if the IM window state gets shuffled
2296 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 if (imMayMove) {
2299 if (moveInputMethodWindowsIfNeededLocked(false)) {
2300 assignLayers = true;
2301 }
2302 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002303 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002304 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002305 assignLayers = true;
2306 }
2307 }
Romain Guy06882f82009-06-10 13:36:04 -07002308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 mLayoutNeeded = true;
2310 win.mGivenInsetsPending = insetsPending;
2311 if (assignLayers) {
2312 assignLayersLocked();
2313 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002314 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002316 if (displayed && win.mIsWallpaper) {
2317 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002318 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 if (win.mAppToken != null) {
2321 win.mAppToken.updateReportedVisibilityLocked();
2322 }
2323 outFrame.set(win.mFrame);
2324 outContentInsets.set(win.mContentInsets);
2325 outVisibleInsets.set(win.mVisibleInsets);
2326 if (localLOGV) Log.v(
2327 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002328 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 + ", requestedHeight=" + requestedHeight
2330 + ", viewVisibility=" + viewVisibility
2331 + "\nRelayout returning frame=" + outFrame
2332 + ", surface=" + outSurface);
2333
2334 if (localLOGV || DEBUG_FOCUS) Log.v(
2335 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2336
2337 inTouchMode = mInTouchMode;
2338 }
2339
2340 if (newConfig != null) {
2341 sendNewConfiguration();
2342 }
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2347 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2348 }
2349
2350 public void finishDrawingWindow(Session session, IWindow client) {
2351 final long origId = Binder.clearCallingIdentity();
2352 synchronized(mWindowMap) {
2353 WindowState win = windowForClientLocked(session, client);
2354 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002355 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2356 adjustWallpaperWindowsLocked();
2357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 mLayoutNeeded = true;
2359 performLayoutAndPlaceSurfacesLocked();
2360 }
2361 }
2362 Binder.restoreCallingIdentity(origId);
2363 }
2364
2365 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2366 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2367 + (lp != null ? lp.packageName : null)
2368 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2369 if (lp != null && lp.windowAnimations != 0) {
2370 // If this is a system resource, don't try to load it from the
2371 // application resources. It is nice to avoid loading application
2372 // resources if we can.
2373 String packageName = lp.packageName != null ? lp.packageName : "android";
2374 int resId = lp.windowAnimations;
2375 if ((resId&0xFF000000) == 0x01000000) {
2376 packageName = "android";
2377 }
2378 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2379 + packageName);
2380 return AttributeCache.instance().get(packageName, resId,
2381 com.android.internal.R.styleable.WindowAnimation);
2382 }
2383 return null;
2384 }
Romain Guy06882f82009-06-10 13:36:04 -07002385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 private void applyEnterAnimationLocked(WindowState win) {
2387 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2388 if (win.mEnterAnimationPending) {
2389 win.mEnterAnimationPending = false;
2390 transit = WindowManagerPolicy.TRANSIT_ENTER;
2391 }
2392
2393 applyAnimationLocked(win, transit, true);
2394 }
2395
2396 private boolean applyAnimationLocked(WindowState win,
2397 int transit, boolean isEntrance) {
2398 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2399 // If we are trying to apply an animation, but already running
2400 // an animation of the same type, then just leave that one alone.
2401 return true;
2402 }
Romain Guy06882f82009-06-10 13:36:04 -07002403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 // Only apply an animation if the display isn't frozen. If it is
2405 // frozen, there is no reason to animate and it can cause strange
2406 // artifacts when we unfreeze the display if some different animation
2407 // is running.
2408 if (!mDisplayFrozen) {
2409 int anim = mPolicy.selectAnimationLw(win, transit);
2410 int attr = -1;
2411 Animation a = null;
2412 if (anim != 0) {
2413 a = AnimationUtils.loadAnimation(mContext, anim);
2414 } else {
2415 switch (transit) {
2416 case WindowManagerPolicy.TRANSIT_ENTER:
2417 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2418 break;
2419 case WindowManagerPolicy.TRANSIT_EXIT:
2420 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2421 break;
2422 case WindowManagerPolicy.TRANSIT_SHOW:
2423 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2424 break;
2425 case WindowManagerPolicy.TRANSIT_HIDE:
2426 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2427 break;
2428 }
2429 if (attr >= 0) {
2430 a = loadAnimation(win.mAttrs, attr);
2431 }
2432 }
2433 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2434 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2435 + " mAnimation=" + win.mAnimation
2436 + " isEntrance=" + isEntrance);
2437 if (a != null) {
2438 if (DEBUG_ANIM) {
2439 RuntimeException e = new RuntimeException();
2440 e.fillInStackTrace();
2441 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2442 }
2443 win.setAnimation(a);
2444 win.mAnimationIsEntrance = isEntrance;
2445 }
2446 } else {
2447 win.clearAnimation();
2448 }
2449
2450 return win.mAnimation != null;
2451 }
2452
2453 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2454 int anim = 0;
2455 Context context = mContext;
2456 if (animAttr >= 0) {
2457 AttributeCache.Entry ent = getCachedAnimations(lp);
2458 if (ent != null) {
2459 context = ent.context;
2460 anim = ent.array.getResourceId(animAttr, 0);
2461 }
2462 }
2463 if (anim != 0) {
2464 return AnimationUtils.loadAnimation(context, anim);
2465 }
2466 return null;
2467 }
Romain Guy06882f82009-06-10 13:36:04 -07002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 private boolean applyAnimationLocked(AppWindowToken wtoken,
2470 WindowManager.LayoutParams lp, int transit, boolean enter) {
2471 // Only apply an animation if the display isn't frozen. If it is
2472 // frozen, there is no reason to animate and it can cause strange
2473 // artifacts when we unfreeze the display if some different animation
2474 // is running.
2475 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002476 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002477 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002478 a = new FadeInOutAnimation(enter);
2479 if (DEBUG_ANIM) Log.v(TAG,
2480 "applying FadeInOutAnimation for a window in compatibility mode");
2481 } else {
2482 int animAttr = 0;
2483 switch (transit) {
2484 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2485 animAttr = enter
2486 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2487 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2488 break;
2489 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2490 animAttr = enter
2491 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2492 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2493 break;
2494 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2495 animAttr = enter
2496 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2497 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2498 break;
2499 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2500 animAttr = enter
2501 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2502 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2503 break;
2504 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2505 animAttr = enter
2506 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2507 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2508 break;
2509 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2510 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002511 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002512 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2513 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002514 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002515 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002516 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2517 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002518 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002519 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002520 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002521 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2522 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2523 break;
2524 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2525 animAttr = enter
2526 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2527 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2528 break;
2529 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2530 animAttr = enter
2531 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2532 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002533 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002534 }
2535 a = loadAnimation(lp, animAttr);
2536 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2537 + " anim=" + a
2538 + " animAttr=0x" + Integer.toHexString(animAttr)
2539 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 if (a != null) {
2542 if (DEBUG_ANIM) {
2543 RuntimeException e = new RuntimeException();
2544 e.fillInStackTrace();
2545 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2546 }
2547 wtoken.setAnimation(a);
2548 }
2549 } else {
2550 wtoken.clearAnimation();
2551 }
2552
2553 return wtoken.animation != null;
2554 }
2555
2556 // -------------------------------------------------------------
2557 // Application Window Tokens
2558 // -------------------------------------------------------------
2559
2560 public void validateAppTokens(List tokens) {
2561 int v = tokens.size()-1;
2562 int m = mAppTokens.size()-1;
2563 while (v >= 0 && m >= 0) {
2564 AppWindowToken wtoken = mAppTokens.get(m);
2565 if (wtoken.removed) {
2566 m--;
2567 continue;
2568 }
2569 if (tokens.get(v) != wtoken.token) {
2570 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2571 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2572 }
2573 v--;
2574 m--;
2575 }
2576 while (v >= 0) {
2577 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2578 v--;
2579 }
2580 while (m >= 0) {
2581 AppWindowToken wtoken = mAppTokens.get(m);
2582 if (!wtoken.removed) {
2583 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2584 }
2585 m--;
2586 }
2587 }
2588
2589 boolean checkCallingPermission(String permission, String func) {
2590 // Quick check: if the calling permission is me, it's all okay.
2591 if (Binder.getCallingPid() == Process.myPid()) {
2592 return true;
2593 }
Romain Guy06882f82009-06-10 13:36:04 -07002594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 if (mContext.checkCallingPermission(permission)
2596 == PackageManager.PERMISSION_GRANTED) {
2597 return true;
2598 }
2599 String msg = "Permission Denial: " + func + " from pid="
2600 + Binder.getCallingPid()
2601 + ", uid=" + Binder.getCallingUid()
2602 + " requires " + permission;
2603 Log.w(TAG, msg);
2604 return false;
2605 }
Romain Guy06882f82009-06-10 13:36:04 -07002606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 AppWindowToken findAppWindowToken(IBinder token) {
2608 WindowToken wtoken = mTokenMap.get(token);
2609 if (wtoken == null) {
2610 return null;
2611 }
2612 return wtoken.appWindowToken;
2613 }
Romain Guy06882f82009-06-10 13:36:04 -07002614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 public void addWindowToken(IBinder token, int type) {
2616 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2617 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002618 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 }
Romain Guy06882f82009-06-10 13:36:04 -07002620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 synchronized(mWindowMap) {
2622 WindowToken wtoken = mTokenMap.get(token);
2623 if (wtoken != null) {
2624 Log.w(TAG, "Attempted to add existing input method token: " + token);
2625 return;
2626 }
2627 wtoken = new WindowToken(token, type, true);
2628 mTokenMap.put(token, wtoken);
2629 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002630 if (type == TYPE_WALLPAPER) {
2631 mWallpaperTokens.add(wtoken);
2632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
2634 }
Romain Guy06882f82009-06-10 13:36:04 -07002635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 public void removeWindowToken(IBinder token) {
2637 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2638 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002639 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 }
2641
2642 final long origId = Binder.clearCallingIdentity();
2643 synchronized(mWindowMap) {
2644 WindowToken wtoken = mTokenMap.remove(token);
2645 mTokenList.remove(wtoken);
2646 if (wtoken != null) {
2647 boolean delayed = false;
2648 if (!wtoken.hidden) {
2649 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 final int N = wtoken.windows.size();
2652 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 for (int i=0; i<N; i++) {
2655 WindowState win = wtoken.windows.get(i);
2656
2657 if (win.isAnimating()) {
2658 delayed = true;
2659 }
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 if (win.isVisibleNow()) {
2662 applyAnimationLocked(win,
2663 WindowManagerPolicy.TRANSIT_EXIT, false);
2664 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2665 KeyWaiter.RETURN_NOTHING);
2666 changed = true;
2667 }
2668 }
2669
2670 if (changed) {
2671 mLayoutNeeded = true;
2672 performLayoutAndPlaceSurfacesLocked();
2673 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2674 }
Romain Guy06882f82009-06-10 13:36:04 -07002675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 if (delayed) {
2677 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002678 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2679 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 }
2681 }
Romain Guy06882f82009-06-10 13:36:04 -07002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 } else {
2684 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2685 }
2686 }
2687 Binder.restoreCallingIdentity(origId);
2688 }
2689
2690 public void addAppToken(int addPos, IApplicationToken token,
2691 int groupId, int requestedOrientation, boolean fullscreen) {
2692 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2693 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002694 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
Romain Guy06882f82009-06-10 13:36:04 -07002696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 synchronized(mWindowMap) {
2698 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2699 if (wtoken != null) {
2700 Log.w(TAG, "Attempted to add existing app token: " + token);
2701 return;
2702 }
2703 wtoken = new AppWindowToken(token);
2704 wtoken.groupId = groupId;
2705 wtoken.appFullscreen = fullscreen;
2706 wtoken.requestedOrientation = requestedOrientation;
2707 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002708 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 mTokenMap.put(token.asBinder(), wtoken);
2710 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 // Application tokens start out hidden.
2713 wtoken.hidden = true;
2714 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 //dump();
2717 }
2718 }
Romain Guy06882f82009-06-10 13:36:04 -07002719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 public void setAppGroupId(IBinder token, int groupId) {
2721 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2722 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002723 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
2725
2726 synchronized(mWindowMap) {
2727 AppWindowToken wtoken = findAppWindowToken(token);
2728 if (wtoken == null) {
2729 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2730 return;
2731 }
2732 wtoken.groupId = groupId;
2733 }
2734 }
Romain Guy06882f82009-06-10 13:36:04 -07002735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 public int getOrientationFromWindowsLocked() {
2737 int pos = mWindows.size() - 1;
2738 while (pos >= 0) {
2739 WindowState wtoken = (WindowState) mWindows.get(pos);
2740 pos--;
2741 if (wtoken.mAppToken != null) {
2742 // We hit an application window. so the orientation will be determined by the
2743 // app window. No point in continuing further.
2744 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2745 }
2746 if (!wtoken.isVisibleLw()) {
2747 continue;
2748 }
2749 int req = wtoken.mAttrs.screenOrientation;
2750 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2751 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2752 continue;
2753 } else {
2754 return req;
2755 }
2756 }
2757 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2758 }
Romain Guy06882f82009-06-10 13:36:04 -07002759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 public int getOrientationFromAppTokensLocked() {
2761 int pos = mAppTokens.size() - 1;
2762 int curGroup = 0;
2763 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002764 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002766 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 while (pos >= 0) {
2768 AppWindowToken wtoken = mAppTokens.get(pos);
2769 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002770 // if we're about to tear down this window and not seek for
2771 // the behind activity, don't use it for orientation
2772 if (!findingBehind
2773 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002774 continue;
2775 }
2776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 if (!haveGroup) {
2778 // We ignore any hidden applications on the top.
2779 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2780 continue;
2781 }
2782 haveGroup = true;
2783 curGroup = wtoken.groupId;
2784 lastOrientation = wtoken.requestedOrientation;
2785 } else if (curGroup != wtoken.groupId) {
2786 // If we have hit a new application group, and the bottom
2787 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002788 // the orientation behind it, and the last app was
2789 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002791 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2792 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 return lastOrientation;
2794 }
2795 }
2796 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002797 // If this application is fullscreen, and didn't explicitly say
2798 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002800 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002801 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002802 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 return or;
2804 }
2805 // If this application has requested an explicit orientation,
2806 // then use it.
2807 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2808 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2809 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2810 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2811 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2812 return or;
2813 }
Owen Lin3413b892009-05-01 17:12:32 -07002814 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
2816 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2817 }
Romain Guy06882f82009-06-10 13:36:04 -07002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002820 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002821 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2822 "updateOrientationFromAppTokens()")) {
2823 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2824 }
2825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 Configuration config;
2827 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002828 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2829 freezeThisOneIfNeeded);
2830 Binder.restoreCallingIdentity(ident);
2831 return config;
2832 }
2833
2834 Configuration updateOrientationFromAppTokensUnchecked(
2835 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2836 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002838 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002839 if (config != null) {
2840 mLayoutNeeded = true;
2841 performLayoutAndPlaceSurfacesLocked();
2842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return config;
2845 }
Romain Guy06882f82009-06-10 13:36:04 -07002846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 /*
2848 * The orientation is computed from non-application windows first. If none of
2849 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002850 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2852 * android.os.IBinder)
2853 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002854 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002855 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 long ident = Binder.clearCallingIdentity();
2858 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002859 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (req != mForcedAppOrientation) {
2862 changed = true;
2863 mForcedAppOrientation = req;
2864 //send a message to Policy indicating orientation change to take
2865 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002866 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 }
Romain Guy06882f82009-06-10 13:36:04 -07002868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 if (changed) {
2870 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002871 WindowManagerPolicy.USE_LAST_ROTATION,
2872 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 if (changed) {
2874 if (freezeThisOneIfNeeded != null) {
2875 AppWindowToken wtoken = findAppWindowToken(
2876 freezeThisOneIfNeeded);
2877 if (wtoken != null) {
2878 startAppFreezingScreenLocked(wtoken,
2879 ActivityInfo.CONFIG_ORIENTATION);
2880 }
2881 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002882 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 }
2884 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002885
2886 // No obvious action we need to take, but if our current
2887 // state mismatches the activity maanager's, update it
2888 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002889 mTempConfiguration.setToDefaults();
2890 if (computeNewConfigurationLocked(mTempConfiguration)) {
2891 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002892 return new Configuration(mTempConfiguration);
2893 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002894 }
2895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } finally {
2897 Binder.restoreCallingIdentity(ident);
2898 }
Romain Guy06882f82009-06-10 13:36:04 -07002899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 return null;
2901 }
Romain Guy06882f82009-06-10 13:36:04 -07002902
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002903 int computeForcedAppOrientationLocked() {
2904 int req = getOrientationFromWindowsLocked();
2905 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2906 req = getOrientationFromAppTokensLocked();
2907 }
2908 return req;
2909 }
Romain Guy06882f82009-06-10 13:36:04 -07002910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2912 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2913 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002914 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
Romain Guy06882f82009-06-10 13:36:04 -07002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 synchronized(mWindowMap) {
2918 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2919 if (wtoken == null) {
2920 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2921 return;
2922 }
Romain Guy06882f82009-06-10 13:36:04 -07002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 wtoken.requestedOrientation = requestedOrientation;
2925 }
2926 }
Romain Guy06882f82009-06-10 13:36:04 -07002927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 public int getAppOrientation(IApplicationToken token) {
2929 synchronized(mWindowMap) {
2930 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2931 if (wtoken == null) {
2932 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2933 }
Romain Guy06882f82009-06-10 13:36:04 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 return wtoken.requestedOrientation;
2936 }
2937 }
Romain Guy06882f82009-06-10 13:36:04 -07002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2940 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2941 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002942 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 }
2944
2945 synchronized(mWindowMap) {
2946 boolean changed = false;
2947 if (token == null) {
2948 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2949 changed = mFocusedApp != null;
2950 mFocusedApp = null;
2951 mKeyWaiter.tickle();
2952 } else {
2953 AppWindowToken newFocus = findAppWindowToken(token);
2954 if (newFocus == null) {
2955 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2956 return;
2957 }
2958 changed = mFocusedApp != newFocus;
2959 mFocusedApp = newFocus;
2960 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2961 mKeyWaiter.tickle();
2962 }
2963
2964 if (moveFocusNow && changed) {
2965 final long origId = Binder.clearCallingIdentity();
2966 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2967 Binder.restoreCallingIdentity(origId);
2968 }
2969 }
2970 }
2971
2972 public void prepareAppTransition(int transit) {
2973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2974 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 }
Romain Guy06882f82009-06-10 13:36:04 -07002977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 synchronized(mWindowMap) {
2979 if (DEBUG_APP_TRANSITIONS) Log.v(
2980 TAG, "Prepare app transition: transit=" + transit
2981 + " mNextAppTransition=" + mNextAppTransition);
2982 if (!mDisplayFrozen) {
2983 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2984 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002985 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2986 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2987 // Opening a new task always supersedes a close for the anim.
2988 mNextAppTransition = transit;
2989 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2990 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2991 // Opening a new activity always supersedes a close for the anim.
2992 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 }
2994 mAppTransitionReady = false;
2995 mAppTransitionTimeout = false;
2996 mStartingIconInTransition = false;
2997 mSkipAppTransitionAnimation = false;
2998 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2999 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3000 5000);
3001 }
3002 }
3003 }
3004
3005 public int getPendingAppTransition() {
3006 return mNextAppTransition;
3007 }
Romain Guy06882f82009-06-10 13:36:04 -07003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 public void executeAppTransition() {
3010 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3011 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003012 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
Romain Guy06882f82009-06-10 13:36:04 -07003014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 synchronized(mWindowMap) {
3016 if (DEBUG_APP_TRANSITIONS) Log.v(
3017 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
3018 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3019 mAppTransitionReady = true;
3020 final long origId = Binder.clearCallingIdentity();
3021 performLayoutAndPlaceSurfacesLocked();
3022 Binder.restoreCallingIdentity(origId);
3023 }
3024 }
3025 }
3026
3027 public void setAppStartingWindow(IBinder token, String pkg,
3028 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3029 IBinder transferFrom, boolean createIfNeeded) {
3030 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3031 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003032 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 }
3034
3035 synchronized(mWindowMap) {
3036 if (DEBUG_STARTING_WINDOW) Log.v(
3037 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3038 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 AppWindowToken wtoken = findAppWindowToken(token);
3041 if (wtoken == null) {
3042 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
3043 return;
3044 }
3045
3046 // If the display is frozen, we won't do anything until the
3047 // actual window is displayed so there is no reason to put in
3048 // the starting window.
3049 if (mDisplayFrozen) {
3050 return;
3051 }
Romain Guy06882f82009-06-10 13:36:04 -07003052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 if (wtoken.startingData != null) {
3054 return;
3055 }
Romain Guy06882f82009-06-10 13:36:04 -07003056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (transferFrom != null) {
3058 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3059 if (ttoken != null) {
3060 WindowState startingWindow = ttoken.startingWindow;
3061 if (startingWindow != null) {
3062 if (mStartingIconInTransition) {
3063 // In this case, the starting icon has already
3064 // been displayed, so start letting windows get
3065 // shown immediately without any more transitions.
3066 mSkipAppTransitionAnimation = true;
3067 }
3068 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3069 "Moving existing starting from " + ttoken
3070 + " to " + wtoken);
3071 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 // Transfer the starting window over to the new
3074 // token.
3075 wtoken.startingData = ttoken.startingData;
3076 wtoken.startingView = ttoken.startingView;
3077 wtoken.startingWindow = startingWindow;
3078 ttoken.startingData = null;
3079 ttoken.startingView = null;
3080 ttoken.startingWindow = null;
3081 ttoken.startingMoved = true;
3082 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003083 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 startingWindow.mAppToken = wtoken;
3085 mWindows.remove(startingWindow);
3086 ttoken.windows.remove(startingWindow);
3087 ttoken.allAppWindows.remove(startingWindow);
3088 addWindowToListInOrderLocked(startingWindow, true);
3089 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 // Propagate other interesting state between the
3092 // tokens. If the old token is displayed, we should
3093 // immediately force the new one to be displayed. If
3094 // it is animating, we need to move that animation to
3095 // the new one.
3096 if (ttoken.allDrawn) {
3097 wtoken.allDrawn = true;
3098 }
3099 if (ttoken.firstWindowDrawn) {
3100 wtoken.firstWindowDrawn = true;
3101 }
3102 if (!ttoken.hidden) {
3103 wtoken.hidden = false;
3104 wtoken.hiddenRequested = false;
3105 wtoken.willBeHidden = false;
3106 }
3107 if (wtoken.clientHidden != ttoken.clientHidden) {
3108 wtoken.clientHidden = ttoken.clientHidden;
3109 wtoken.sendAppVisibilityToClients();
3110 }
3111 if (ttoken.animation != null) {
3112 wtoken.animation = ttoken.animation;
3113 wtoken.animating = ttoken.animating;
3114 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3115 ttoken.animation = null;
3116 ttoken.animLayerAdjustment = 0;
3117 wtoken.updateLayers();
3118 ttoken.updateLayers();
3119 }
Romain Guy06882f82009-06-10 13:36:04 -07003120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 mLayoutNeeded = true;
3123 performLayoutAndPlaceSurfacesLocked();
3124 Binder.restoreCallingIdentity(origId);
3125 return;
3126 } else if (ttoken.startingData != null) {
3127 // The previous app was getting ready to show a
3128 // starting window, but hasn't yet done so. Steal it!
3129 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3130 "Moving pending starting from " + ttoken
3131 + " to " + wtoken);
3132 wtoken.startingData = ttoken.startingData;
3133 ttoken.startingData = null;
3134 ttoken.startingMoved = true;
3135 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3136 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3137 // want to process the message ASAP, before any other queued
3138 // messages.
3139 mH.sendMessageAtFrontOfQueue(m);
3140 return;
3141 }
3142 }
3143 }
3144
3145 // There is no existing starting window, and the caller doesn't
3146 // want us to create one, so that's it!
3147 if (!createIfNeeded) {
3148 return;
3149 }
Romain Guy06882f82009-06-10 13:36:04 -07003150
Dianne Hackborn284ac932009-08-28 10:34:25 -07003151 // If this is a translucent or wallpaper window, then don't
3152 // show a starting window -- the current effect (a full-screen
3153 // opaque starting window that fades away to the real contents
3154 // when it is ready) does not work for this.
3155 if (theme != 0) {
3156 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3157 com.android.internal.R.styleable.Window);
3158 if (ent.array.getBoolean(
3159 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3160 return;
3161 }
3162 if (ent.array.getBoolean(
3163 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3164 return;
3165 }
3166 }
3167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 mStartingIconInTransition = true;
3169 wtoken.startingData = new StartingData(
3170 pkg, theme, nonLocalizedLabel,
3171 labelRes, icon);
3172 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3173 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3174 // want to process the message ASAP, before any other queued
3175 // messages.
3176 mH.sendMessageAtFrontOfQueue(m);
3177 }
3178 }
3179
3180 public void setAppWillBeHidden(IBinder token) {
3181 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3182 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003183 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
3185
3186 AppWindowToken wtoken;
3187
3188 synchronized(mWindowMap) {
3189 wtoken = findAppWindowToken(token);
3190 if (wtoken == null) {
3191 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3192 return;
3193 }
3194 wtoken.willBeHidden = true;
3195 }
3196 }
Romain Guy06882f82009-06-10 13:36:04 -07003197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3199 boolean visible, int transit, boolean performLayout) {
3200 boolean delayed = false;
3201
3202 if (wtoken.clientHidden == visible) {
3203 wtoken.clientHidden = !visible;
3204 wtoken.sendAppVisibilityToClients();
3205 }
Romain Guy06882f82009-06-10 13:36:04 -07003206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 wtoken.willBeHidden = false;
3208 if (wtoken.hidden == visible) {
3209 final int N = wtoken.allAppWindows.size();
3210 boolean changed = false;
3211 if (DEBUG_APP_TRANSITIONS) Log.v(
3212 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3213 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3218 if (wtoken.animation == sDummyAnimation) {
3219 wtoken.animation = null;
3220 }
3221 applyAnimationLocked(wtoken, lp, transit, visible);
3222 changed = true;
3223 if (wtoken.animation != null) {
3224 delayed = runningAppAnimation = true;
3225 }
3226 }
Romain Guy06882f82009-06-10 13:36:04 -07003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 for (int i=0; i<N; i++) {
3229 WindowState win = wtoken.allAppWindows.get(i);
3230 if (win == wtoken.startingWindow) {
3231 continue;
3232 }
3233
3234 if (win.isAnimating()) {
3235 delayed = true;
3236 }
Romain Guy06882f82009-06-10 13:36:04 -07003237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3239 //win.dump(" ");
3240 if (visible) {
3241 if (!win.isVisibleNow()) {
3242 if (!runningAppAnimation) {
3243 applyAnimationLocked(win,
3244 WindowManagerPolicy.TRANSIT_ENTER, true);
3245 }
3246 changed = true;
3247 }
3248 } else if (win.isVisibleNow()) {
3249 if (!runningAppAnimation) {
3250 applyAnimationLocked(win,
3251 WindowManagerPolicy.TRANSIT_EXIT, false);
3252 }
3253 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3254 KeyWaiter.RETURN_NOTHING);
3255 changed = true;
3256 }
3257 }
3258
3259 wtoken.hidden = wtoken.hiddenRequested = !visible;
3260 if (!visible) {
3261 unsetAppFreezingScreenLocked(wtoken, true, true);
3262 } else {
3263 // If we are being set visible, and the starting window is
3264 // not yet displayed, then make sure it doesn't get displayed.
3265 WindowState swin = wtoken.startingWindow;
3266 if (swin != null && (swin.mDrawPending
3267 || swin.mCommitDrawPending)) {
3268 swin.mPolicyVisibility = false;
3269 swin.mPolicyVisibilityAfterAnim = false;
3270 }
3271 }
Romain Guy06882f82009-06-10 13:36:04 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3274 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3275 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 if (changed && performLayout) {
3278 mLayoutNeeded = true;
3279 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 performLayoutAndPlaceSurfacesLocked();
3281 }
3282 }
3283
3284 if (wtoken.animation != null) {
3285 delayed = true;
3286 }
Romain Guy06882f82009-06-10 13:36:04 -07003287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 return delayed;
3289 }
3290
3291 public void setAppVisibility(IBinder token, boolean visible) {
3292 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3293 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003294 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
3296
3297 AppWindowToken wtoken;
3298
3299 synchronized(mWindowMap) {
3300 wtoken = findAppWindowToken(token);
3301 if (wtoken == null) {
3302 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3303 return;
3304 }
3305
3306 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3307 RuntimeException e = new RuntimeException();
3308 e.fillInStackTrace();
3309 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3310 + "): mNextAppTransition=" + mNextAppTransition
3311 + " hidden=" + wtoken.hidden
3312 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3313 }
Romain Guy06882f82009-06-10 13:36:04 -07003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 // If we are preparing an app transition, then delay changing
3316 // the visibility of this token until we execute that transition.
3317 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3318 // Already in requested state, don't do anything more.
3319 if (wtoken.hiddenRequested != visible) {
3320 return;
3321 }
3322 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 if (DEBUG_APP_TRANSITIONS) Log.v(
3325 TAG, "Setting dummy animation on: " + wtoken);
3326 wtoken.setDummyAnimation();
3327 mOpeningApps.remove(wtoken);
3328 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003329 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 wtoken.inPendingTransaction = true;
3331 if (visible) {
3332 mOpeningApps.add(wtoken);
3333 wtoken.allDrawn = false;
3334 wtoken.startingDisplayed = false;
3335 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003336 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 if (wtoken.clientHidden) {
3339 // In the case where we are making an app visible
3340 // but holding off for a transition, we still need
3341 // to tell the client to make its windows visible so
3342 // they get drawn. Otherwise, we will wait on
3343 // performing the transition until all windows have
3344 // been drawn, they never will be, and we are sad.
3345 wtoken.clientHidden = false;
3346 wtoken.sendAppVisibilityToClients();
3347 }
3348 } else {
3349 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003350 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 }
3352 return;
3353 }
Romain Guy06882f82009-06-10 13:36:04 -07003354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 final long origId = Binder.clearCallingIdentity();
3356 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3357 wtoken.updateReportedVisibilityLocked();
3358 Binder.restoreCallingIdentity(origId);
3359 }
3360 }
3361
3362 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3363 boolean unfreezeSurfaceNow, boolean force) {
3364 if (wtoken.freezingScreen) {
3365 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3366 + " force=" + force);
3367 final int N = wtoken.allAppWindows.size();
3368 boolean unfrozeWindows = false;
3369 for (int i=0; i<N; i++) {
3370 WindowState w = wtoken.allAppWindows.get(i);
3371 if (w.mAppFreezing) {
3372 w.mAppFreezing = false;
3373 if (w.mSurface != null && !w.mOrientationChanging) {
3374 w.mOrientationChanging = true;
3375 }
3376 unfrozeWindows = true;
3377 }
3378 }
3379 if (force || unfrozeWindows) {
3380 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3381 wtoken.freezingScreen = false;
3382 mAppsFreezingScreen--;
3383 }
3384 if (unfreezeSurfaceNow) {
3385 if (unfrozeWindows) {
3386 mLayoutNeeded = true;
3387 performLayoutAndPlaceSurfacesLocked();
3388 }
3389 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3390 stopFreezingDisplayLocked();
3391 }
3392 }
3393 }
3394 }
Romain Guy06882f82009-06-10 13:36:04 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3397 int configChanges) {
3398 if (DEBUG_ORIENTATION) {
3399 RuntimeException e = new RuntimeException();
3400 e.fillInStackTrace();
3401 Log.i(TAG, "Set freezing of " + wtoken.appToken
3402 + ": hidden=" + wtoken.hidden + " freezing="
3403 + wtoken.freezingScreen, e);
3404 }
3405 if (!wtoken.hiddenRequested) {
3406 if (!wtoken.freezingScreen) {
3407 wtoken.freezingScreen = true;
3408 mAppsFreezingScreen++;
3409 if (mAppsFreezingScreen == 1) {
3410 startFreezingDisplayLocked();
3411 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3412 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3413 5000);
3414 }
3415 }
3416 final int N = wtoken.allAppWindows.size();
3417 for (int i=0; i<N; i++) {
3418 WindowState w = wtoken.allAppWindows.get(i);
3419 w.mAppFreezing = true;
3420 }
3421 }
3422 }
Romain Guy06882f82009-06-10 13:36:04 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 public void startAppFreezingScreen(IBinder token, int configChanges) {
3425 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3426 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003427 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 }
3429
3430 synchronized(mWindowMap) {
3431 if (configChanges == 0 && !mDisplayFrozen) {
3432 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3433 return;
3434 }
Romain Guy06882f82009-06-10 13:36:04 -07003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 AppWindowToken wtoken = findAppWindowToken(token);
3437 if (wtoken == null || wtoken.appToken == null) {
3438 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3439 return;
3440 }
3441 final long origId = Binder.clearCallingIdentity();
3442 startAppFreezingScreenLocked(wtoken, configChanges);
3443 Binder.restoreCallingIdentity(origId);
3444 }
3445 }
Romain Guy06882f82009-06-10 13:36:04 -07003446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 public void stopAppFreezingScreen(IBinder token, boolean force) {
3448 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3449 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003450 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 }
3452
3453 synchronized(mWindowMap) {
3454 AppWindowToken wtoken = findAppWindowToken(token);
3455 if (wtoken == null || wtoken.appToken == null) {
3456 return;
3457 }
3458 final long origId = Binder.clearCallingIdentity();
3459 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3460 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3461 unsetAppFreezingScreenLocked(wtoken, true, force);
3462 Binder.restoreCallingIdentity(origId);
3463 }
3464 }
Romain Guy06882f82009-06-10 13:36:04 -07003465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 public void removeAppToken(IBinder token) {
3467 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3468 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003469 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
3471
3472 AppWindowToken wtoken = null;
3473 AppWindowToken startingToken = null;
3474 boolean delayed = false;
3475
3476 final long origId = Binder.clearCallingIdentity();
3477 synchronized(mWindowMap) {
3478 WindowToken basewtoken = mTokenMap.remove(token);
3479 mTokenList.remove(basewtoken);
3480 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3481 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3482 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3483 wtoken.inPendingTransaction = false;
3484 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003485 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if (mClosingApps.contains(wtoken)) {
3487 delayed = true;
3488 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3489 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003490 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 delayed = true;
3492 }
3493 if (DEBUG_APP_TRANSITIONS) Log.v(
3494 TAG, "Removing app " + wtoken + " delayed=" + delayed
3495 + " animation=" + wtoken.animation
3496 + " animating=" + wtoken.animating);
3497 if (delayed) {
3498 // set the token aside because it has an active animation to be finished
3499 mExitingAppTokens.add(wtoken);
3500 }
3501 mAppTokens.remove(wtoken);
3502 wtoken.removed = true;
3503 if (wtoken.startingData != null) {
3504 startingToken = wtoken;
3505 }
3506 unsetAppFreezingScreenLocked(wtoken, true, true);
3507 if (mFocusedApp == wtoken) {
3508 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3509 mFocusedApp = null;
3510 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3511 mKeyWaiter.tickle();
3512 }
3513 } else {
3514 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3515 }
Romain Guy06882f82009-06-10 13:36:04 -07003516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (!delayed && wtoken != null) {
3518 wtoken.updateReportedVisibilityLocked();
3519 }
3520 }
3521 Binder.restoreCallingIdentity(origId);
3522
3523 if (startingToken != null) {
3524 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3525 + startingToken + ": app token removed");
3526 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3527 mH.sendMessage(m);
3528 }
3529 }
3530
3531 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3532 final int NW = token.windows.size();
3533 for (int i=0; i<NW; i++) {
3534 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003535 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 mWindows.remove(win);
3537 int j = win.mChildWindows.size();
3538 while (j > 0) {
3539 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003540 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3541 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3542 "Tmp removing child window " + cwin);
3543 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545 }
3546 return NW > 0;
3547 }
3548
3549 void dumpAppTokensLocked() {
3550 for (int i=mAppTokens.size()-1; i>=0; i--) {
3551 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3552 }
3553 }
Romain Guy06882f82009-06-10 13:36:04 -07003554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 void dumpWindowsLocked() {
3556 for (int i=mWindows.size()-1; i>=0; i--) {
3557 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3558 }
3559 }
Romain Guy06882f82009-06-10 13:36:04 -07003560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 private int findWindowOffsetLocked(int tokenPos) {
3562 final int NW = mWindows.size();
3563
3564 if (tokenPos >= mAppTokens.size()) {
3565 int i = NW;
3566 while (i > 0) {
3567 i--;
3568 WindowState win = (WindowState)mWindows.get(i);
3569 if (win.getAppToken() != null) {
3570 return i+1;
3571 }
3572 }
3573 }
3574
3575 while (tokenPos > 0) {
3576 // Find the first app token below the new position that has
3577 // a window displayed.
3578 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3579 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3580 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003581 if (wtoken.sendingToBottom) {
3582 if (DEBUG_REORDER) Log.v(TAG,
3583 "Skipping token -- currently sending to bottom");
3584 tokenPos--;
3585 continue;
3586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 int i = wtoken.windows.size();
3588 while (i > 0) {
3589 i--;
3590 WindowState win = wtoken.windows.get(i);
3591 int j = win.mChildWindows.size();
3592 while (j > 0) {
3593 j--;
3594 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003595 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 for (int pos=NW-1; pos>=0; pos--) {
3597 if (mWindows.get(pos) == cwin) {
3598 if (DEBUG_REORDER) Log.v(TAG,
3599 "Found child win @" + (pos+1));
3600 return pos+1;
3601 }
3602 }
3603 }
3604 }
3605 for (int pos=NW-1; pos>=0; pos--) {
3606 if (mWindows.get(pos) == win) {
3607 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3608 return pos+1;
3609 }
3610 }
3611 }
3612 tokenPos--;
3613 }
3614
3615 return 0;
3616 }
3617
3618 private final int reAddWindowLocked(int index, WindowState win) {
3619 final int NCW = win.mChildWindows.size();
3620 boolean added = false;
3621 for (int j=0; j<NCW; j++) {
3622 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3623 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003624 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3625 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 mWindows.add(index, win);
3627 index++;
3628 added = true;
3629 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003630 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3631 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 mWindows.add(index, cwin);
3633 index++;
3634 }
3635 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003636 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3637 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 mWindows.add(index, win);
3639 index++;
3640 }
3641 return index;
3642 }
Romain Guy06882f82009-06-10 13:36:04 -07003643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3645 final int NW = token.windows.size();
3646 for (int i=0; i<NW; i++) {
3647 index = reAddWindowLocked(index, token.windows.get(i));
3648 }
3649 return index;
3650 }
3651
3652 public void moveAppToken(int index, IBinder token) {
3653 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3654 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003655 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 }
3657
3658 synchronized(mWindowMap) {
3659 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3660 if (DEBUG_REORDER) dumpAppTokensLocked();
3661 final AppWindowToken wtoken = findAppWindowToken(token);
3662 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3663 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3664 + token + " (" + wtoken + ")");
3665 return;
3666 }
3667 mAppTokens.add(index, wtoken);
3668 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3669 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 final long origId = Binder.clearCallingIdentity();
3672 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3673 if (DEBUG_REORDER) dumpWindowsLocked();
3674 if (tmpRemoveAppWindowsLocked(wtoken)) {
3675 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3676 if (DEBUG_REORDER) dumpWindowsLocked();
3677 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3678 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3679 if (DEBUG_REORDER) dumpWindowsLocked();
3680 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 mLayoutNeeded = true;
3682 performLayoutAndPlaceSurfacesLocked();
3683 }
3684 Binder.restoreCallingIdentity(origId);
3685 }
3686 }
3687
3688 private void removeAppTokensLocked(List<IBinder> tokens) {
3689 // XXX This should be done more efficiently!
3690 // (take advantage of the fact that both lists should be
3691 // ordered in the same way.)
3692 int N = tokens.size();
3693 for (int i=0; i<N; i++) {
3694 IBinder token = tokens.get(i);
3695 final AppWindowToken wtoken = findAppWindowToken(token);
3696 if (!mAppTokens.remove(wtoken)) {
3697 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3698 + token + " (" + wtoken + ")");
3699 i--;
3700 N--;
3701 }
3702 }
3703 }
3704
Dianne Hackborna8f60182009-09-01 19:01:50 -07003705 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3706 boolean updateFocusAndLayout) {
3707 // First remove all of the windows from the list.
3708 tmpRemoveAppWindowsLocked(wtoken);
3709
3710 // Where to start adding?
3711 int pos = findWindowOffsetLocked(tokenPos);
3712
3713 // And now add them back at the correct place.
3714 pos = reAddAppWindowsLocked(pos, wtoken);
3715
3716 if (updateFocusAndLayout) {
3717 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3718 assignLayersLocked();
3719 }
3720 mLayoutNeeded = true;
3721 performLayoutAndPlaceSurfacesLocked();
3722 }
3723 }
3724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3726 // First remove all of the windows from the list.
3727 final int N = tokens.size();
3728 int i;
3729 for (i=0; i<N; i++) {
3730 WindowToken token = mTokenMap.get(tokens.get(i));
3731 if (token != null) {
3732 tmpRemoveAppWindowsLocked(token);
3733 }
3734 }
3735
3736 // Where to start adding?
3737 int pos = findWindowOffsetLocked(tokenPos);
3738
3739 // And now add them back at the correct place.
3740 for (i=0; i<N; i++) {
3741 WindowToken token = mTokenMap.get(tokens.get(i));
3742 if (token != null) {
3743 pos = reAddAppWindowsLocked(pos, token);
3744 }
3745 }
3746
Dianne Hackborna8f60182009-09-01 19:01:50 -07003747 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3748 assignLayersLocked();
3749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 mLayoutNeeded = true;
3751 performLayoutAndPlaceSurfacesLocked();
3752
3753 //dump();
3754 }
3755
3756 public void moveAppTokensToTop(List<IBinder> tokens) {
3757 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3758 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003759 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761
3762 final long origId = Binder.clearCallingIdentity();
3763 synchronized(mWindowMap) {
3764 removeAppTokensLocked(tokens);
3765 final int N = tokens.size();
3766 for (int i=0; i<N; i++) {
3767 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3768 if (wt != null) {
3769 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003770 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3771 mToTopApps.remove(wt);
3772 mToBottomApps.remove(wt);
3773 mToTopApps.add(wt);
3774 wt.sendingToBottom = false;
3775 wt.sendingToTop = true;
3776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
3778 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003779
3780 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3781 moveAppWindowsLocked(tokens, mAppTokens.size());
3782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 }
3784 Binder.restoreCallingIdentity(origId);
3785 }
3786
3787 public void moveAppTokensToBottom(List<IBinder> tokens) {
3788 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3789 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003790 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792
3793 final long origId = Binder.clearCallingIdentity();
3794 synchronized(mWindowMap) {
3795 removeAppTokensLocked(tokens);
3796 final int N = tokens.size();
3797 int pos = 0;
3798 for (int i=0; i<N; i++) {
3799 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3800 if (wt != null) {
3801 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003802 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3803 mToTopApps.remove(wt);
3804 mToBottomApps.remove(wt);
3805 mToBottomApps.add(i, wt);
3806 wt.sendingToTop = false;
3807 wt.sendingToBottom = true;
3808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 pos++;
3810 }
3811 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003812
3813 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3814 moveAppWindowsLocked(tokens, 0);
3815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
3817 Binder.restoreCallingIdentity(origId);
3818 }
3819
3820 // -------------------------------------------------------------
3821 // Misc IWindowSession methods
3822 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 public void disableKeyguard(IBinder token, String tag) {
3825 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3826 != PackageManager.PERMISSION_GRANTED) {
3827 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3828 }
3829 mKeyguardDisabled.acquire(token, tag);
3830 }
3831
3832 public void reenableKeyguard(IBinder token) {
3833 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3834 != PackageManager.PERMISSION_GRANTED) {
3835 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3836 }
3837 synchronized (mKeyguardDisabled) {
3838 mKeyguardDisabled.release(token);
3839
3840 if (!mKeyguardDisabled.isAcquired()) {
3841 // if we are the last one to reenable the keyguard wait until
3842 // we have actaully finished reenabling until returning
3843 mWaitingUntilKeyguardReenabled = true;
3844 while (mWaitingUntilKeyguardReenabled) {
3845 try {
3846 mKeyguardDisabled.wait();
3847 } catch (InterruptedException e) {
3848 Thread.currentThread().interrupt();
3849 }
3850 }
3851 }
3852 }
3853 }
3854
3855 /**
3856 * @see android.app.KeyguardManager#exitKeyguardSecurely
3857 */
3858 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3859 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3860 != PackageManager.PERMISSION_GRANTED) {
3861 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3862 }
3863 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3864 public void onKeyguardExitResult(boolean success) {
3865 try {
3866 callback.onKeyguardExitResult(success);
3867 } catch (RemoteException e) {
3868 // Client has died, we don't care.
3869 }
3870 }
3871 });
3872 }
3873
3874 public boolean inKeyguardRestrictedInputMode() {
3875 return mPolicy.inKeyguardRestrictedKeyInputMode();
3876 }
Romain Guy06882f82009-06-10 13:36:04 -07003877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 static float fixScale(float scale) {
3879 if (scale < 0) scale = 0;
3880 else if (scale > 20) scale = 20;
3881 return Math.abs(scale);
3882 }
Romain Guy06882f82009-06-10 13:36:04 -07003883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 public void setAnimationScale(int which, float scale) {
3885 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3886 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003887 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889
3890 if (scale < 0) scale = 0;
3891 else if (scale > 20) scale = 20;
3892 scale = Math.abs(scale);
3893 switch (which) {
3894 case 0: mWindowAnimationScale = fixScale(scale); break;
3895 case 1: mTransitionAnimationScale = fixScale(scale); break;
3896 }
Romain Guy06882f82009-06-10 13:36:04 -07003897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 // Persist setting
3899 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3900 }
Romain Guy06882f82009-06-10 13:36:04 -07003901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 public void setAnimationScales(float[] scales) {
3903 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3904 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003905 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
3907
3908 if (scales != null) {
3909 if (scales.length >= 1) {
3910 mWindowAnimationScale = fixScale(scales[0]);
3911 }
3912 if (scales.length >= 2) {
3913 mTransitionAnimationScale = fixScale(scales[1]);
3914 }
3915 }
Romain Guy06882f82009-06-10 13:36:04 -07003916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 // Persist setting
3918 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3919 }
Romain Guy06882f82009-06-10 13:36:04 -07003920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 public float getAnimationScale(int which) {
3922 switch (which) {
3923 case 0: return mWindowAnimationScale;
3924 case 1: return mTransitionAnimationScale;
3925 }
3926 return 0;
3927 }
Romain Guy06882f82009-06-10 13:36:04 -07003928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 public float[] getAnimationScales() {
3930 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3931 }
Romain Guy06882f82009-06-10 13:36:04 -07003932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 public int getSwitchState(int sw) {
3934 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3935 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003936 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 }
3938 return KeyInputQueue.getSwitchState(sw);
3939 }
Romain Guy06882f82009-06-10 13:36:04 -07003940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 public int getSwitchStateForDevice(int devid, int sw) {
3942 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3943 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003944 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 }
3946 return KeyInputQueue.getSwitchState(devid, sw);
3947 }
Romain Guy06882f82009-06-10 13:36:04 -07003948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 public int getScancodeState(int sw) {
3950 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3951 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003952 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
3954 return KeyInputQueue.getScancodeState(sw);
3955 }
Romain Guy06882f82009-06-10 13:36:04 -07003956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 public int getScancodeStateForDevice(int devid, int sw) {
3958 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3959 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003960 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 }
3962 return KeyInputQueue.getScancodeState(devid, sw);
3963 }
Romain Guy06882f82009-06-10 13:36:04 -07003964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 public int getKeycodeState(int sw) {
3966 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3967 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003968 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 }
3970 return KeyInputQueue.getKeycodeState(sw);
3971 }
Romain Guy06882f82009-06-10 13:36:04 -07003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 public int getKeycodeStateForDevice(int devid, int sw) {
3974 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3975 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003976 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 }
3978 return KeyInputQueue.getKeycodeState(devid, sw);
3979 }
Romain Guy06882f82009-06-10 13:36:04 -07003980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3982 return KeyInputQueue.hasKeys(keycodes, keyExists);
3983 }
Romain Guy06882f82009-06-10 13:36:04 -07003984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 public void enableScreenAfterBoot() {
3986 synchronized(mWindowMap) {
3987 if (mSystemBooted) {
3988 return;
3989 }
3990 mSystemBooted = true;
3991 }
Romain Guy06882f82009-06-10 13:36:04 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 performEnableScreen();
3994 }
Romain Guy06882f82009-06-10 13:36:04 -07003995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 public void enableScreenIfNeededLocked() {
3997 if (mDisplayEnabled) {
3998 return;
3999 }
4000 if (!mSystemBooted) {
4001 return;
4002 }
4003 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4004 }
Romain Guy06882f82009-06-10 13:36:04 -07004005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 public void performEnableScreen() {
4007 synchronized(mWindowMap) {
4008 if (mDisplayEnabled) {
4009 return;
4010 }
4011 if (!mSystemBooted) {
4012 return;
4013 }
Romain Guy06882f82009-06-10 13:36:04 -07004014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 // Don't enable the screen until all existing windows
4016 // have been drawn.
4017 final int N = mWindows.size();
4018 for (int i=0; i<N; i++) {
4019 WindowState w = (WindowState)mWindows.get(i);
4020 if (w.isVisibleLw() && !w.isDisplayedLw()) {
4021 return;
4022 }
4023 }
Romain Guy06882f82009-06-10 13:36:04 -07004024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 mDisplayEnabled = true;
4026 if (false) {
4027 Log.i(TAG, "ENABLING SCREEN!");
4028 StringWriter sw = new StringWriter();
4029 PrintWriter pw = new PrintWriter(sw);
4030 this.dump(null, pw, null);
4031 Log.i(TAG, sw.toString());
4032 }
4033 try {
4034 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4035 if (surfaceFlinger != null) {
4036 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
4037 Parcel data = Parcel.obtain();
4038 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4039 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4040 data, null, 0);
4041 data.recycle();
4042 }
4043 } catch (RemoteException ex) {
4044 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
4045 }
4046 }
Romain Guy06882f82009-06-10 13:36:04 -07004047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004051 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4052 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
Romain Guy06882f82009-06-10 13:36:04 -07004054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 public void setInTouchMode(boolean mode) {
4056 synchronized(mWindowMap) {
4057 mInTouchMode = mode;
4058 }
4059 }
4060
Romain Guy06882f82009-06-10 13:36:04 -07004061 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004062 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004064 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004065 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004068 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
Romain Guy06882f82009-06-10 13:36:04 -07004070
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004071 public void setRotationUnchecked(int rotation,
4072 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 if(DEBUG_ORIENTATION) Log.v(TAG,
4074 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 long origId = Binder.clearCallingIdentity();
4077 boolean changed;
4078 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004079 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 }
Romain Guy06882f82009-06-10 13:36:04 -07004081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 if (changed) {
4083 sendNewConfiguration();
4084 synchronized(mWindowMap) {
4085 mLayoutNeeded = true;
4086 performLayoutAndPlaceSurfacesLocked();
4087 }
4088 } else if (alwaysSendConfiguration) {
4089 //update configuration ignoring orientation change
4090 sendNewConfiguration();
4091 }
Romain Guy06882f82009-06-10 13:36:04 -07004092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 Binder.restoreCallingIdentity(origId);
4094 }
Romain Guy06882f82009-06-10 13:36:04 -07004095
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004096 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 boolean changed;
4098 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4099 rotation = mRequestedRotation;
4100 } else {
4101 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004102 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 }
4104 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004105 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 mRotation, mDisplayEnabled);
4107 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4108 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004111 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 "Rotation changed to " + rotation
4113 + " from " + mRotation
4114 + " (forceApp=" + mForcedAppOrientation
4115 + ", req=" + mRequestedRotation + ")");
4116 mRotation = rotation;
4117 mWindowsFreezingScreen = true;
4118 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4119 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4120 2000);
4121 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004122 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 mQueue.setOrientation(rotation);
4124 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004125 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 for (int i=mWindows.size()-1; i>=0; i--) {
4128 WindowState w = (WindowState)mWindows.get(i);
4129 if (w.mSurface != null) {
4130 w.mOrientationChanging = true;
4131 }
4132 }
4133 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4134 try {
4135 mRotationWatchers.get(i).onRotationChanged(rotation);
4136 } catch (RemoteException e) {
4137 }
4138 }
4139 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 return changed;
4142 }
Romain Guy06882f82009-06-10 13:36:04 -07004143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 public int getRotation() {
4145 return mRotation;
4146 }
4147
4148 public int watchRotation(IRotationWatcher watcher) {
4149 final IBinder watcherBinder = watcher.asBinder();
4150 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4151 public void binderDied() {
4152 synchronized (mWindowMap) {
4153 for (int i=0; i<mRotationWatchers.size(); i++) {
4154 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004155 IRotationWatcher removed = mRotationWatchers.remove(i);
4156 if (removed != null) {
4157 removed.asBinder().unlinkToDeath(this, 0);
4158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 i--;
4160 }
4161 }
4162 }
4163 }
4164 };
Romain Guy06882f82009-06-10 13:36:04 -07004165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 synchronized (mWindowMap) {
4167 try {
4168 watcher.asBinder().linkToDeath(dr, 0);
4169 mRotationWatchers.add(watcher);
4170 } catch (RemoteException e) {
4171 // Client died, no cleanup needed.
4172 }
Romain Guy06882f82009-06-10 13:36:04 -07004173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 return mRotation;
4175 }
4176 }
4177
4178 /**
4179 * Starts the view server on the specified port.
4180 *
4181 * @param port The port to listener to.
4182 *
4183 * @return True if the server was successfully started, false otherwise.
4184 *
4185 * @see com.android.server.ViewServer
4186 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4187 */
4188 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004189 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 return false;
4191 }
4192
4193 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4194 return false;
4195 }
4196
4197 if (port < 1024) {
4198 return false;
4199 }
4200
4201 if (mViewServer != null) {
4202 if (!mViewServer.isRunning()) {
4203 try {
4204 return mViewServer.start();
4205 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004206 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
4208 }
4209 return false;
4210 }
4211
4212 try {
4213 mViewServer = new ViewServer(this, port);
4214 return mViewServer.start();
4215 } catch (IOException e) {
4216 Log.w(TAG, "View server did not start");
4217 }
4218 return false;
4219 }
4220
Romain Guy06882f82009-06-10 13:36:04 -07004221 private boolean isSystemSecure() {
4222 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4223 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4224 }
4225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 /**
4227 * Stops the view server if it exists.
4228 *
4229 * @return True if the server stopped, false if it wasn't started or
4230 * couldn't be stopped.
4231 *
4232 * @see com.android.server.ViewServer
4233 */
4234 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004235 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 return false;
4237 }
4238
4239 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4240 return false;
4241 }
4242
4243 if (mViewServer != null) {
4244 return mViewServer.stop();
4245 }
4246 return false;
4247 }
4248
4249 /**
4250 * Indicates whether the view server is running.
4251 *
4252 * @return True if the server is running, false otherwise.
4253 *
4254 * @see com.android.server.ViewServer
4255 */
4256 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004257 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 return false;
4259 }
4260
4261 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4262 return false;
4263 }
4264
4265 return mViewServer != null && mViewServer.isRunning();
4266 }
4267
4268 /**
4269 * Lists all availble windows in the system. The listing is written in the
4270 * specified Socket's output stream with the following syntax:
4271 * windowHashCodeInHexadecimal windowName
4272 * Each line of the ouput represents a different window.
4273 *
4274 * @param client The remote client to send the listing to.
4275 * @return False if an error occured, true otherwise.
4276 */
4277 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004278 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 return false;
4280 }
4281
4282 boolean result = true;
4283
4284 Object[] windows;
4285 synchronized (mWindowMap) {
4286 windows = new Object[mWindows.size()];
4287 //noinspection unchecked
4288 windows = mWindows.toArray(windows);
4289 }
4290
4291 BufferedWriter out = null;
4292
4293 // Any uncaught exception will crash the system process
4294 try {
4295 OutputStream clientStream = client.getOutputStream();
4296 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4297
4298 final int count = windows.length;
4299 for (int i = 0; i < count; i++) {
4300 final WindowState w = (WindowState) windows[i];
4301 out.write(Integer.toHexString(System.identityHashCode(w)));
4302 out.write(' ');
4303 out.append(w.mAttrs.getTitle());
4304 out.write('\n');
4305 }
4306
4307 out.write("DONE.\n");
4308 out.flush();
4309 } catch (Exception e) {
4310 result = false;
4311 } finally {
4312 if (out != null) {
4313 try {
4314 out.close();
4315 } catch (IOException e) {
4316 result = false;
4317 }
4318 }
4319 }
4320
4321 return result;
4322 }
4323
4324 /**
4325 * Sends a command to a target window. The result of the command, if any, will be
4326 * written in the output stream of the specified socket.
4327 *
4328 * The parameters must follow this syntax:
4329 * windowHashcode extra
4330 *
4331 * Where XX is the length in characeters of the windowTitle.
4332 *
4333 * The first parameter is the target window. The window with the specified hashcode
4334 * will be the target. If no target can be found, nothing happens. The extra parameters
4335 * will be delivered to the target window and as parameters to the command itself.
4336 *
4337 * @param client The remote client to sent the result, if any, to.
4338 * @param command The command to execute.
4339 * @param parameters The command parameters.
4340 *
4341 * @return True if the command was successfully delivered, false otherwise. This does
4342 * not indicate whether the command itself was successful.
4343 */
4344 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004345 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 return false;
4347 }
4348
4349 boolean success = true;
4350 Parcel data = null;
4351 Parcel reply = null;
4352
4353 // Any uncaught exception will crash the system process
4354 try {
4355 // Find the hashcode of the window
4356 int index = parameters.indexOf(' ');
4357 if (index == -1) {
4358 index = parameters.length();
4359 }
4360 final String code = parameters.substring(0, index);
4361 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4362
4363 // Extract the command's parameter after the window description
4364 if (index < parameters.length()) {
4365 parameters = parameters.substring(index + 1);
4366 } else {
4367 parameters = "";
4368 }
4369
4370 final WindowManagerService.WindowState window = findWindow(hashCode);
4371 if (window == null) {
4372 return false;
4373 }
4374
4375 data = Parcel.obtain();
4376 data.writeInterfaceToken("android.view.IWindow");
4377 data.writeString(command);
4378 data.writeString(parameters);
4379 data.writeInt(1);
4380 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4381
4382 reply = Parcel.obtain();
4383
4384 final IBinder binder = window.mClient.asBinder();
4385 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4386 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4387
4388 reply.readException();
4389
4390 } catch (Exception e) {
4391 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4392 success = false;
4393 } finally {
4394 if (data != null) {
4395 data.recycle();
4396 }
4397 if (reply != null) {
4398 reply.recycle();
4399 }
4400 }
4401
4402 return success;
4403 }
4404
4405 private WindowState findWindow(int hashCode) {
4406 if (hashCode == -1) {
4407 return getFocusedWindow();
4408 }
4409
4410 synchronized (mWindowMap) {
4411 final ArrayList windows = mWindows;
4412 final int count = windows.size();
4413
4414 for (int i = 0; i < count; i++) {
4415 WindowState w = (WindowState) windows.get(i);
4416 if (System.identityHashCode(w) == hashCode) {
4417 return w;
4418 }
4419 }
4420 }
4421
4422 return null;
4423 }
4424
4425 /*
4426 * Instruct the Activity Manager to fetch the current configuration and broadcast
4427 * that to config-changed listeners if appropriate.
4428 */
4429 void sendNewConfiguration() {
4430 try {
4431 mActivityManager.updateConfiguration(null);
4432 } catch (RemoteException e) {
4433 }
4434 }
Romain Guy06882f82009-06-10 13:36:04 -07004435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 public Configuration computeNewConfiguration() {
4437 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004438 return computeNewConfigurationLocked();
4439 }
4440 }
Romain Guy06882f82009-06-10 13:36:04 -07004441
Dianne Hackbornc485a602009-03-24 22:39:49 -07004442 Configuration computeNewConfigurationLocked() {
4443 Configuration config = new Configuration();
4444 if (!computeNewConfigurationLocked(config)) {
4445 return null;
4446 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004447 return config;
4448 }
Romain Guy06882f82009-06-10 13:36:04 -07004449
Dianne Hackbornc485a602009-03-24 22:39:49 -07004450 boolean computeNewConfigurationLocked(Configuration config) {
4451 if (mDisplay == null) {
4452 return false;
4453 }
4454 mQueue.getInputConfiguration(config);
4455 final int dw = mDisplay.getWidth();
4456 final int dh = mDisplay.getHeight();
4457 int orientation = Configuration.ORIENTATION_SQUARE;
4458 if (dw < dh) {
4459 orientation = Configuration.ORIENTATION_PORTRAIT;
4460 } else if (dw > dh) {
4461 orientation = Configuration.ORIENTATION_LANDSCAPE;
4462 }
4463 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004464
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004465 DisplayMetrics dm = new DisplayMetrics();
4466 mDisplay.getMetrics(dm);
4467 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4468
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004469 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004470 // Note we only do this once because at this point we don't
4471 // expect the screen to change in this way at runtime, and want
4472 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004473 int longSize = dw;
4474 int shortSize = dh;
4475 if (longSize < shortSize) {
4476 int tmp = longSize;
4477 longSize = shortSize;
4478 shortSize = tmp;
4479 }
4480 longSize = (int)(longSize/dm.density);
4481 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004482
Dianne Hackborn723738c2009-06-25 19:48:04 -07004483 // These semi-magic numbers define our compatibility modes for
4484 // applications with different screens. Don't change unless you
4485 // make sure to test lots and lots of apps!
4486 if (longSize < 470) {
4487 // This is shorter than an HVGA normal density screen (which
4488 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004489 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4490 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004491 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004492 // Is this a large screen?
4493 if (longSize > 640 && shortSize >= 480) {
4494 // VGA or larger screens at medium density are the point
4495 // at which we consider it to be a large screen.
4496 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4497 } else {
4498 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4499
4500 // If this screen is wider than normal HVGA, or taller
4501 // than FWVGA, then for old apps we want to run in size
4502 // compatibility mode.
4503 if (shortSize > 321 || longSize > 570) {
4504 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4505 }
4506 }
4507
4508 // Is this a long screen?
4509 if (((longSize*3)/5) >= (shortSize-1)) {
4510 // Anything wider than WVGA (5:3) is considering to be long.
4511 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4512 } else {
4513 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4514 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004515 }
4516 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004517 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004518
Dianne Hackbornc485a602009-03-24 22:39:49 -07004519 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4520 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4521 mPolicy.adjustConfigurationLw(config);
4522 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
Romain Guy06882f82009-06-10 13:36:04 -07004524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 // -------------------------------------------------------------
4526 // Input Events and Focus Management
4527 // -------------------------------------------------------------
4528
4529 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004530 long curTime = SystemClock.uptimeMillis();
4531
Michael Chane10de972009-05-18 11:24:50 -07004532 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004533 if (mLastTouchEventType == eventType &&
4534 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4535 return;
4536 }
4537 mLastUserActivityCallTime = curTime;
4538 mLastTouchEventType = eventType;
4539 }
4540
4541 if (targetWin == null
4542 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4543 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 }
4545 }
4546
4547 // tells if it's a cheek event or not -- this function is stateful
4548 private static final int EVENT_NONE = 0;
4549 private static final int EVENT_UNKNOWN = 0;
4550 private static final int EVENT_CHEEK = 0;
4551 private static final int EVENT_IGNORE_DURATION = 300; // ms
4552 private static final float CHEEK_THRESHOLD = 0.6f;
4553 private int mEventState = EVENT_NONE;
4554 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 private int eventType(MotionEvent ev) {
4557 float size = ev.getSize();
4558 switch (ev.getAction()) {
4559 case MotionEvent.ACTION_DOWN:
4560 mEventSize = size;
4561 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4562 case MotionEvent.ACTION_UP:
4563 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004564 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 case MotionEvent.ACTION_MOVE:
4566 final int N = ev.getHistorySize();
4567 if (size > mEventSize) mEventSize = size;
4568 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4569 for (int i=0; i<N; i++) {
4570 size = ev.getHistoricalSize(i);
4571 if (size > mEventSize) mEventSize = size;
4572 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4573 }
4574 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4575 return TOUCH_EVENT;
4576 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004577 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 }
4579 default:
4580 // not good
4581 return OTHER_EVENT;
4582 }
4583 }
4584
4585 /**
4586 * @return Returns true if event was dispatched, false if it was dropped for any reason
4587 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004588 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4590 "dispatchPointer " + ev);
4591
Michael Chan53071d62009-05-13 17:29:48 -07004592 if (MEASURE_LATENCY) {
4593 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4594 }
4595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004597 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598
Michael Chan53071d62009-05-13 17:29:48 -07004599 if (MEASURE_LATENCY) {
4600 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4601 }
4602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 if (action == MotionEvent.ACTION_UP) {
4606 // let go of our target
4607 mKeyWaiter.mMotionTarget = null;
4608 mPowerManager.logPointerUpEvent();
4609 } else if (action == MotionEvent.ACTION_DOWN) {
4610 mPowerManager.logPointerDownEvent();
4611 }
4612
4613 if (targetObj == null) {
4614 // In this case we are either dropping the event, or have received
4615 // a move or up without a down. It is common to receive move
4616 // events in such a way, since this means the user is moving the
4617 // pointer without actually pressing down. All other cases should
4618 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004619 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4621 }
4622 if (qev != null) {
4623 mQueue.recycleEvent(qev);
4624 }
4625 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004626 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 }
4628 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4629 if (qev != null) {
4630 mQueue.recycleEvent(qev);
4631 }
4632 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004633 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 }
Romain Guy06882f82009-06-10 13:36:04 -07004635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004639 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640
4641 //Log.i(TAG, "Sending " + ev + " to " + target);
4642
4643 if (uid != 0 && uid != target.mSession.mUid) {
4644 if (mContext.checkPermission(
4645 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4646 != PackageManager.PERMISSION_GRANTED) {
4647 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4648 + pid + " uid " + uid + " to window " + target
4649 + " owned by uid " + target.mSession.mUid);
4650 if (qev != null) {
4651 mQueue.recycleEvent(qev);
4652 }
4653 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004654 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655 }
4656 }
4657
Michael Chan53071d62009-05-13 17:29:48 -07004658 if (MEASURE_LATENCY) {
4659 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4660 }
4661
Romain Guy06882f82009-06-10 13:36:04 -07004662 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4664 //target wants to ignore fat touch events
4665 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4666 //explicit flag to return without processing event further
4667 boolean returnFlag = false;
4668 if((action == MotionEvent.ACTION_DOWN)) {
4669 mFatTouch = false;
4670 if(cheekPress) {
4671 mFatTouch = true;
4672 returnFlag = true;
4673 }
4674 } else {
4675 if(action == MotionEvent.ACTION_UP) {
4676 if(mFatTouch) {
4677 //earlier even was invalid doesnt matter if current up is cheekpress or not
4678 mFatTouch = false;
4679 returnFlag = true;
4680 } else if(cheekPress) {
4681 //cancel the earlier event
4682 ev.setAction(MotionEvent.ACTION_CANCEL);
4683 action = MotionEvent.ACTION_CANCEL;
4684 }
4685 } else if(action == MotionEvent.ACTION_MOVE) {
4686 if(mFatTouch) {
4687 //two cases here
4688 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004689 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 returnFlag = true;
4691 } else if(cheekPress) {
4692 //valid down followed by invalid moves
4693 //an invalid move have to cancel earlier action
4694 ev.setAction(MotionEvent.ACTION_CANCEL);
4695 action = MotionEvent.ACTION_CANCEL;
4696 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4697 //note that the subsequent invalid moves will not get here
4698 mFatTouch = true;
4699 }
4700 }
4701 } //else if action
4702 if(returnFlag) {
4703 //recycle que, ev
4704 if (qev != null) {
4705 mQueue.recycleEvent(qev);
4706 }
4707 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004708 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709 }
4710 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004711
Michael Chan9f028e62009-08-04 17:37:46 -07004712 // Enable this for testing the "right" value
4713 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004714 int max_events_per_sec = 35;
4715 try {
4716 max_events_per_sec = Integer.parseInt(SystemProperties
4717 .get("windowsmgr.max_events_per_sec"));
4718 if (max_events_per_sec < 1) {
4719 max_events_per_sec = 35;
4720 }
4721 } catch (NumberFormatException e) {
4722 }
4723 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4724 }
4725
4726 /*
4727 * Throttle events to minimize CPU usage when there's a flood of events
4728 * e.g. constant contact with the screen
4729 */
4730 if (action == MotionEvent.ACTION_MOVE) {
4731 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4732 long now = SystemClock.uptimeMillis();
4733 if (now < nextEventTime) {
4734 try {
4735 Thread.sleep(nextEventTime - now);
4736 } catch (InterruptedException e) {
4737 }
4738 mLastTouchEventTime = nextEventTime;
4739 } else {
4740 mLastTouchEventTime = now;
4741 }
4742 }
4743
Michael Chan53071d62009-05-13 17:29:48 -07004744 if (MEASURE_LATENCY) {
4745 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4746 }
4747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 synchronized(mWindowMap) {
4749 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4750 mKeyWaiter.bindTargetWindowLocked(target,
4751 KeyWaiter.RETURN_PENDING_POINTER, qev);
4752 ev = null;
4753 } else {
4754 if (action == MotionEvent.ACTION_DOWN) {
4755 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4756 if (out != null) {
4757 MotionEvent oev = MotionEvent.obtain(ev);
4758 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4759 do {
4760 final Rect frame = out.mFrame;
4761 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4762 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004763 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 } catch (android.os.RemoteException e) {
4765 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4766 }
4767 oev.offsetLocation((float)frame.left, (float)frame.top);
4768 out = out.mNextOutsideTouch;
4769 } while (out != null);
4770 mKeyWaiter.mOutsideTouchTargets = null;
4771 }
4772 }
4773 final Rect frame = target.mFrame;
4774 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4775 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004776
4777 // If we are on top of the wallpaper, then the wallpaper also
4778 // gets to see this movement.
4779 if (mWallpaperTarget == target) {
4780 sendPointerToWallpaperLocked(target, ev, eventTime);
4781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 }
4783 }
Romain Guy06882f82009-06-10 13:36:04 -07004784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 // finally offset the event to the target's coordinate system and
4786 // dispatch the event.
4787 try {
4788 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4789 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4790 }
Michael Chan53071d62009-05-13 17:29:48 -07004791
4792 if (MEASURE_LATENCY) {
4793 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4794 }
4795
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004796 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004797
4798 if (MEASURE_LATENCY) {
4799 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4800 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004801 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 } catch (android.os.RemoteException e) {
4803 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4804 mKeyWaiter.mMotionTarget = null;
4805 try {
4806 removeWindow(target.mSession, target.mClient);
4807 } catch (java.util.NoSuchElementException ex) {
4808 // This will happen if the window has already been
4809 // removed.
4810 }
4811 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004812 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 }
Romain Guy06882f82009-06-10 13:36:04 -07004814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 /**
4816 * @return Returns true if event was dispatched, false if it was dropped for any reason
4817 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004818 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 if (DEBUG_INPUT) Log.v(
4820 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004823 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 if (focusObj == null) {
4825 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4826 if (qev != null) {
4827 mQueue.recycleEvent(qev);
4828 }
4829 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004830 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 }
4832 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4833 if (qev != null) {
4834 mQueue.recycleEvent(qev);
4835 }
4836 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004837 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 }
Romain Guy06882f82009-06-10 13:36:04 -07004839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 if (uid != 0 && uid != focus.mSession.mUid) {
4843 if (mContext.checkPermission(
4844 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4845 != PackageManager.PERMISSION_GRANTED) {
4846 Log.w(TAG, "Permission denied: injecting key event from pid "
4847 + pid + " uid " + uid + " to window " + focus
4848 + " owned by uid " + focus.mSession.mUid);
4849 if (qev != null) {
4850 mQueue.recycleEvent(qev);
4851 }
4852 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004853 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 }
4855 }
Romain Guy06882f82009-06-10 13:36:04 -07004856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 synchronized(mWindowMap) {
4860 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4861 mKeyWaiter.bindTargetWindowLocked(focus,
4862 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4863 // We don't deliver movement events to the client, we hold
4864 // them and wait for them to call back.
4865 ev = null;
4866 } else {
4867 mKeyWaiter.bindTargetWindowLocked(focus);
4868 }
4869 }
Romain Guy06882f82009-06-10 13:36:04 -07004870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004872 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004873 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 } catch (android.os.RemoteException e) {
4875 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4876 try {
4877 removeWindow(focus.mSession, focus.mClient);
4878 } catch (java.util.NoSuchElementException ex) {
4879 // This will happen if the window has already been
4880 // removed.
4881 }
4882 }
Romain Guy06882f82009-06-10 13:36:04 -07004883
Dianne Hackborncfaef692009-06-15 14:24:44 -07004884 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 }
Romain Guy06882f82009-06-10 13:36:04 -07004886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 /**
4888 * @return Returns true if event was dispatched, false if it was dropped for any reason
4889 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004890 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004891 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4892
4893 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004894 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 if (focusObj == null) {
4896 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004897 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 }
4899 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004900 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 }
Romain Guy06882f82009-06-10 13:36:04 -07004902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 if (DEBUG_INPUT) Log.v(
4906 TAG, "Dispatching to " + focus + ": " + event);
4907
4908 if (uid != 0 && uid != focus.mSession.mUid) {
4909 if (mContext.checkPermission(
4910 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4911 != PackageManager.PERMISSION_GRANTED) {
4912 Log.w(TAG, "Permission denied: injecting key event from pid "
4913 + pid + " uid " + uid + " to window " + focus
4914 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004915 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 }
4917 }
Romain Guy06882f82009-06-10 13:36:04 -07004918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 synchronized(mWindowMap) {
4920 mKeyWaiter.bindTargetWindowLocked(focus);
4921 }
4922
4923 // NOSHIP extra state logging
4924 mKeyWaiter.recordDispatchState(event, focus);
4925 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 try {
4928 if (DEBUG_INPUT || DEBUG_FOCUS) {
4929 Log.v(TAG, "Delivering key " + event.getKeyCode()
4930 + " to " + focus);
4931 }
4932 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004933 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 } catch (android.os.RemoteException e) {
4935 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4936 try {
4937 removeWindow(focus.mSession, focus.mClient);
4938 } catch (java.util.NoSuchElementException ex) {
4939 // This will happen if the window has already been
4940 // removed.
4941 }
4942 }
Romain Guy06882f82009-06-10 13:36:04 -07004943
Dianne Hackborncfaef692009-06-15 14:24:44 -07004944 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 }
Romain Guy06882f82009-06-10 13:36:04 -07004946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 public void pauseKeyDispatching(IBinder _token) {
4948 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4949 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004950 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 }
4952
4953 synchronized (mWindowMap) {
4954 WindowToken token = mTokenMap.get(_token);
4955 if (token != null) {
4956 mKeyWaiter.pauseDispatchingLocked(token);
4957 }
4958 }
4959 }
4960
4961 public void resumeKeyDispatching(IBinder _token) {
4962 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4963 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004964 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 }
4966
4967 synchronized (mWindowMap) {
4968 WindowToken token = mTokenMap.get(_token);
4969 if (token != null) {
4970 mKeyWaiter.resumeDispatchingLocked(token);
4971 }
4972 }
4973 }
4974
4975 public void setEventDispatching(boolean enabled) {
4976 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4977 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004978 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 }
4980
4981 synchronized (mWindowMap) {
4982 mKeyWaiter.setEventDispatchingLocked(enabled);
4983 }
4984 }
Romain Guy06882f82009-06-10 13:36:04 -07004985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 /**
4987 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004988 *
4989 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 * {@link SystemClock#uptimeMillis()} as the timebase.)
4991 * @param sync If true, wait for the event to be completed before returning to the caller.
4992 * @return Returns true if event was dispatched, false if it was dropped for any reason
4993 */
4994 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4995 long downTime = ev.getDownTime();
4996 long eventTime = ev.getEventTime();
4997
4998 int action = ev.getAction();
4999 int code = ev.getKeyCode();
5000 int repeatCount = ev.getRepeatCount();
5001 int metaState = ev.getMetaState();
5002 int deviceId = ev.getDeviceId();
5003 int scancode = ev.getScanCode();
5004
5005 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5006 if (downTime == 0) downTime = eventTime;
5007
5008 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005009 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005011 final int pid = Binder.getCallingPid();
5012 final int uid = Binder.getCallingUid();
5013 final long ident = Binder.clearCallingIdentity();
5014 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005016 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005018 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005019 switch (result) {
5020 case INJECT_NO_PERMISSION:
5021 throw new SecurityException(
5022 "Injecting to another application requires INJECT_EVENT permission");
5023 case INJECT_SUCCEEDED:
5024 return true;
5025 }
5026 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 }
5028
5029 /**
5030 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005031 *
5032 * @param ev A motion event describing the pointer (touch) action. (As noted in
5033 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 * {@link SystemClock#uptimeMillis()} as the timebase.)
5035 * @param sync If true, wait for the event to be completed before returning to the caller.
5036 * @return Returns true if event was dispatched, false if it was dropped for any reason
5037 */
5038 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005039 final int pid = Binder.getCallingPid();
5040 final int uid = Binder.getCallingUid();
5041 final long ident = Binder.clearCallingIdentity();
5042 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005044 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005046 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005047 switch (result) {
5048 case INJECT_NO_PERMISSION:
5049 throw new SecurityException(
5050 "Injecting to another application requires INJECT_EVENT permission");
5051 case INJECT_SUCCEEDED:
5052 return true;
5053 }
5054 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 }
Romain Guy06882f82009-06-10 13:36:04 -07005056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 /**
5058 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005059 *
5060 * @param ev A motion event describing the trackball action. (As noted in
5061 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 * {@link SystemClock#uptimeMillis()} as the timebase.)
5063 * @param sync If true, wait for the event to be completed before returning to the caller.
5064 * @return Returns true if event was dispatched, false if it was dropped for any reason
5065 */
5066 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005067 final int pid = Binder.getCallingPid();
5068 final int uid = Binder.getCallingUid();
5069 final long ident = Binder.clearCallingIdentity();
5070 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005072 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005074 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005075 switch (result) {
5076 case INJECT_NO_PERMISSION:
5077 throw new SecurityException(
5078 "Injecting to another application requires INJECT_EVENT permission");
5079 case INJECT_SUCCEEDED:
5080 return true;
5081 }
5082 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 }
Romain Guy06882f82009-06-10 13:36:04 -07005084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 private WindowState getFocusedWindow() {
5086 synchronized (mWindowMap) {
5087 return getFocusedWindowLocked();
5088 }
5089 }
5090
5091 private WindowState getFocusedWindowLocked() {
5092 return mCurrentFocus;
5093 }
Romain Guy06882f82009-06-10 13:36:04 -07005094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 /**
5096 * This class holds the state for dispatching key events. This state
5097 * is protected by the KeyWaiter instance, NOT by the window lock. You
5098 * can be holding the main window lock while acquire the KeyWaiter lock,
5099 * but not the other way around.
5100 */
5101 final class KeyWaiter {
5102 // NOSHIP debugging
5103 public class DispatchState {
5104 private KeyEvent event;
5105 private WindowState focus;
5106 private long time;
5107 private WindowState lastWin;
5108 private IBinder lastBinder;
5109 private boolean finished;
5110 private boolean gotFirstWindow;
5111 private boolean eventDispatching;
5112 private long timeToSwitch;
5113 private boolean wasFrozen;
5114 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005115 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5118 focus = theFocus;
5119 event = theEvent;
5120 time = System.currentTimeMillis();
5121 // snapshot KeyWaiter state
5122 lastWin = mLastWin;
5123 lastBinder = mLastBinder;
5124 finished = mFinished;
5125 gotFirstWindow = mGotFirstWindow;
5126 eventDispatching = mEventDispatching;
5127 timeToSwitch = mTimeToSwitch;
5128 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005129 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 // cache the paused state at ctor time as well
5131 if (theFocus == null || theFocus.mToken == null) {
5132 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5133 focusPaused = false;
5134 } else {
5135 focusPaused = theFocus.mToken.paused;
5136 }
5137 }
Romain Guy06882f82009-06-10 13:36:04 -07005138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 public String toString() {
5140 return "{{" + event + " to " + focus + " @ " + time
5141 + " lw=" + lastWin + " lb=" + lastBinder
5142 + " fin=" + finished + " gfw=" + gotFirstWindow
5143 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005144 + " wf=" + wasFrozen + " fp=" + focusPaused
5145 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 }
5147 };
5148 private DispatchState mDispatchState = null;
5149 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5150 mDispatchState = new DispatchState(theEvent, theFocus);
5151 }
5152 // END NOSHIP
5153
5154 public static final int RETURN_NOTHING = 0;
5155 public static final int RETURN_PENDING_POINTER = 1;
5156 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 final Object SKIP_TARGET_TOKEN = new Object();
5159 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 private WindowState mLastWin = null;
5162 private IBinder mLastBinder = null;
5163 private boolean mFinished = true;
5164 private boolean mGotFirstWindow = false;
5165 private boolean mEventDispatching = true;
5166 private long mTimeToSwitch = 0;
5167 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 // Target of Motion events
5170 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 // Windows above the target who would like to receive an "outside"
5173 // touch event for any down events outside of them.
5174 WindowState mOutsideTouchTargets;
5175
5176 /**
5177 * Wait for the last event dispatch to complete, then find the next
5178 * target that should receive the given event and wait for that one
5179 * to be ready to receive it.
5180 */
5181 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5182 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005183 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 long startTime = SystemClock.uptimeMillis();
5185 long keyDispatchingTimeout = 5 * 1000;
5186 long waitedFor = 0;
5187
5188 while (true) {
5189 // Figure out which window we care about. It is either the
5190 // last window we are waiting to have process the event or,
5191 // if none, then the next window we think the event should go
5192 // to. Note: we retrieve mLastWin outside of the lock, so
5193 // it may change before we lock. Thus we must check it again.
5194 WindowState targetWin = mLastWin;
5195 boolean targetIsNew = targetWin == null;
5196 if (DEBUG_INPUT) Log.v(
5197 TAG, "waitForLastKey: mFinished=" + mFinished +
5198 ", mLastWin=" + mLastWin);
5199 if (targetIsNew) {
5200 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005201 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 if (target == SKIP_TARGET_TOKEN) {
5203 // The user has pressed a special key, and we are
5204 // dropping all pending events before it.
5205 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5206 + " " + nextMotion);
5207 return null;
5208 }
5209 if (target == CONSUMED_EVENT_TOKEN) {
5210 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5211 + " " + nextMotion);
5212 return target;
5213 }
5214 targetWin = (WindowState)target;
5215 }
Romain Guy06882f82009-06-10 13:36:04 -07005216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 // Now: is it okay to send the next event to this window?
5220 synchronized (this) {
5221 // First: did we come here based on the last window not
5222 // being null, but it changed by the time we got here?
5223 // If so, try again.
5224 if (!targetIsNew && mLastWin == null) {
5225 continue;
5226 }
Romain Guy06882f82009-06-10 13:36:04 -07005227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 // We never dispatch events if not finished with the
5229 // last one, or the display is frozen.
5230 if (mFinished && !mDisplayFrozen) {
5231 // If event dispatching is disabled, then we
5232 // just consume the events.
5233 if (!mEventDispatching) {
5234 if (DEBUG_INPUT) Log.v(TAG,
5235 "Skipping event; dispatching disabled: "
5236 + nextKey + " " + nextMotion);
5237 return null;
5238 }
5239 if (targetWin != null) {
5240 // If this is a new target, and that target is not
5241 // paused or unresponsive, then all looks good to
5242 // handle the event.
5243 if (targetIsNew && !targetWin.mToken.paused) {
5244 return targetWin;
5245 }
Romain Guy06882f82009-06-10 13:36:04 -07005246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 // If we didn't find a target window, and there is no
5248 // focused app window, then just eat the events.
5249 } else if (mFocusedApp == null) {
5250 if (DEBUG_INPUT) Log.v(TAG,
5251 "Skipping event; no focused app: "
5252 + nextKey + " " + nextMotion);
5253 return null;
5254 }
5255 }
Romain Guy06882f82009-06-10 13:36:04 -07005256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 if (DEBUG_INPUT) Log.v(
5258 TAG, "Waiting for last key in " + mLastBinder
5259 + " target=" + targetWin
5260 + " mFinished=" + mFinished
5261 + " mDisplayFrozen=" + mDisplayFrozen
5262 + " targetIsNew=" + targetIsNew
5263 + " paused="
5264 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005265 + " mFocusedApp=" + mFocusedApp
5266 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 targetApp = targetWin != null
5269 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 long curTimeout = keyDispatchingTimeout;
5272 if (mTimeToSwitch != 0) {
5273 long now = SystemClock.uptimeMillis();
5274 if (mTimeToSwitch <= now) {
5275 // If an app switch key has been pressed, and we have
5276 // waited too long for the current app to finish
5277 // processing keys, then wait no more!
5278 doFinishedKeyLocked(true);
5279 continue;
5280 }
5281 long switchTimeout = mTimeToSwitch - now;
5282 if (curTimeout > switchTimeout) {
5283 curTimeout = switchTimeout;
5284 }
5285 }
Romain Guy06882f82009-06-10 13:36:04 -07005286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 try {
5288 // after that continue
5289 // processing keys, so we don't get stuck.
5290 if (DEBUG_INPUT) Log.v(
5291 TAG, "Waiting for key dispatch: " + curTimeout);
5292 wait(curTimeout);
5293 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5294 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005295 + startTime + " switchTime=" + mTimeToSwitch
5296 + " target=" + targetWin + " mLW=" + mLastWin
5297 + " mLB=" + mLastBinder + " fin=" + mFinished
5298 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 } catch (InterruptedException e) {
5300 }
5301 }
5302
5303 // If we were frozen during configuration change, restart the
5304 // timeout checks from now; otherwise look at whether we timed
5305 // out before awakening.
5306 if (mWasFrozen) {
5307 waitedFor = 0;
5308 mWasFrozen = false;
5309 } else {
5310 waitedFor = SystemClock.uptimeMillis() - startTime;
5311 }
5312
5313 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5314 IApplicationToken at = null;
5315 synchronized (this) {
5316 Log.w(TAG, "Key dispatching timed out sending to " +
5317 (targetWin != null ? targetWin.mAttrs.getTitle()
5318 : "<null>"));
5319 // NOSHIP debugging
5320 Log.w(TAG, "Dispatch state: " + mDispatchState);
5321 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5322 // END NOSHIP
5323 //dump();
5324 if (targetWin != null) {
5325 at = targetWin.getAppToken();
5326 } else if (targetApp != null) {
5327 at = targetApp.appToken;
5328 }
5329 }
5330
5331 boolean abort = true;
5332 if (at != null) {
5333 try {
5334 long timeout = at.getKeyDispatchingTimeout();
5335 if (timeout > waitedFor) {
5336 // we did not wait the proper amount of time for this application.
5337 // set the timeout to be the real timeout and wait again.
5338 keyDispatchingTimeout = timeout - waitedFor;
5339 continue;
5340 } else {
5341 abort = at.keyDispatchingTimedOut();
5342 }
5343 } catch (RemoteException ex) {
5344 }
5345 }
5346
5347 synchronized (this) {
5348 if (abort && (mLastWin == targetWin || targetWin == null)) {
5349 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005350 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 if (DEBUG_INPUT) Log.v(TAG,
5352 "Window " + mLastWin +
5353 " timed out on key input");
5354 if (mLastWin.mToken.paused) {
5355 Log.w(TAG, "Un-pausing dispatching to this window");
5356 mLastWin.mToken.paused = false;
5357 }
5358 }
5359 if (mMotionTarget == targetWin) {
5360 mMotionTarget = null;
5361 }
5362 mLastWin = null;
5363 mLastBinder = null;
5364 if (failIfTimeout || targetWin == null) {
5365 return null;
5366 }
5367 } else {
5368 Log.w(TAG, "Continuing to wait for key to be dispatched");
5369 startTime = SystemClock.uptimeMillis();
5370 }
5371 }
5372 }
5373 }
5374 }
Romain Guy06882f82009-06-10 13:36:04 -07005375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005377 MotionEvent nextMotion, boolean isPointerEvent,
5378 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 if (nextKey != null) {
5382 // Find the target window for a normal key event.
5383 final int keycode = nextKey.getKeyCode();
5384 final int repeatCount = nextKey.getRepeatCount();
5385 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5386 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005389 if (callingUid == 0 ||
5390 mContext.checkPermission(
5391 android.Manifest.permission.INJECT_EVENTS,
5392 callingPid, callingUid)
5393 == PackageManager.PERMISSION_GRANTED) {
5394 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005395 nextKey.getMetaState(), down, repeatCount,
5396 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 Log.w(TAG, "Event timeout during app switch: dropping "
5399 + nextKey);
5400 return SKIP_TARGET_TOKEN;
5401 }
Romain Guy06882f82009-06-10 13:36:04 -07005402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 WindowState focus = null;
5406 synchronized(mWindowMap) {
5407 focus = getFocusedWindowLocked();
5408 }
Romain Guy06882f82009-06-10 13:36:04 -07005409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005411
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005412 if (callingUid == 0 ||
5413 (focus != null && callingUid == focus.mSession.mUid) ||
5414 mContext.checkPermission(
5415 android.Manifest.permission.INJECT_EVENTS,
5416 callingPid, callingUid)
5417 == PackageManager.PERMISSION_GRANTED) {
5418 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005419 keycode, nextKey.getMetaState(), down, repeatCount,
5420 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005421 return CONSUMED_EVENT_TOKEN;
5422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 }
Romain Guy06882f82009-06-10 13:36:04 -07005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 } else if (!isPointerEvent) {
5428 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5429 if (!dispatch) {
5430 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5431 + nextMotion);
5432 return SKIP_TARGET_TOKEN;
5433 }
Romain Guy06882f82009-06-10 13:36:04 -07005434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 WindowState focus = null;
5436 synchronized(mWindowMap) {
5437 focus = getFocusedWindowLocked();
5438 }
Romain Guy06882f82009-06-10 13:36:04 -07005439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5441 return focus;
5442 }
Romain Guy06882f82009-06-10 13:36:04 -07005443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 if (nextMotion == null) {
5445 return SKIP_TARGET_TOKEN;
5446 }
Romain Guy06882f82009-06-10 13:36:04 -07005447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5449 KeyEvent.KEYCODE_UNKNOWN);
5450 if (!dispatch) {
5451 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5452 + nextMotion);
5453 return SKIP_TARGET_TOKEN;
5454 }
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 // Find the target window for a pointer event.
5457 int action = nextMotion.getAction();
5458 final float xf = nextMotion.getX();
5459 final float yf = nextMotion.getY();
5460 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 final boolean screenWasOff = qev != null
5463 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 synchronized(mWindowMap) {
5468 synchronized (this) {
5469 if (action == MotionEvent.ACTION_DOWN) {
5470 if (mMotionTarget != null) {
5471 // this is weird, we got a pen down, but we thought it was
5472 // already down!
5473 // XXX: We should probably send an ACTION_UP to the current
5474 // target.
5475 Log.w(TAG, "Pointer down received while already down in: "
5476 + mMotionTarget);
5477 mMotionTarget = null;
5478 }
Romain Guy06882f82009-06-10 13:36:04 -07005479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 // ACTION_DOWN is special, because we need to lock next events to
5481 // the window we'll land onto.
5482 final int x = (int)xf;
5483 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 final ArrayList windows = mWindows;
5486 final int N = windows.size();
5487 WindowState topErrWindow = null;
5488 final Rect tmpRect = mTempRect;
5489 for (int i=N-1; i>=0; i--) {
5490 WindowState child = (WindowState)windows.get(i);
5491 //Log.i(TAG, "Checking dispatch to: " + child);
5492 final int flags = child.mAttrs.flags;
5493 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5494 if (topErrWindow == null) {
5495 topErrWindow = child;
5496 }
5497 }
5498 if (!child.isVisibleLw()) {
5499 //Log.i(TAG, "Not visible!");
5500 continue;
5501 }
5502 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5503 //Log.i(TAG, "Not touchable!");
5504 if ((flags & WindowManager.LayoutParams
5505 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5506 child.mNextOutsideTouch = mOutsideTouchTargets;
5507 mOutsideTouchTargets = child;
5508 }
5509 continue;
5510 }
5511 tmpRect.set(child.mFrame);
5512 if (child.mTouchableInsets == ViewTreeObserver
5513 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5514 // The touch is inside of the window if it is
5515 // inside the frame, AND the content part of that
5516 // frame that was given by the application.
5517 tmpRect.left += child.mGivenContentInsets.left;
5518 tmpRect.top += child.mGivenContentInsets.top;
5519 tmpRect.right -= child.mGivenContentInsets.right;
5520 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5521 } else if (child.mTouchableInsets == ViewTreeObserver
5522 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5523 // The touch is inside of the window if it is
5524 // inside the frame, AND the visible part of that
5525 // frame that was given by the application.
5526 tmpRect.left += child.mGivenVisibleInsets.left;
5527 tmpRect.top += child.mGivenVisibleInsets.top;
5528 tmpRect.right -= child.mGivenVisibleInsets.right;
5529 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5530 }
5531 final int touchFlags = flags &
5532 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5533 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5534 if (tmpRect.contains(x, y) || touchFlags == 0) {
5535 //Log.i(TAG, "Using this target!");
5536 if (!screenWasOff || (flags &
5537 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5538 mMotionTarget = child;
5539 } else {
5540 //Log.i(TAG, "Waking, skip!");
5541 mMotionTarget = null;
5542 }
5543 break;
5544 }
Romain Guy06882f82009-06-10 13:36:04 -07005545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 if ((flags & WindowManager.LayoutParams
5547 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5548 child.mNextOutsideTouch = mOutsideTouchTargets;
5549 mOutsideTouchTargets = child;
5550 //Log.i(TAG, "Adding to outside target list: " + child);
5551 }
5552 }
5553
5554 // if there's an error window but it's not accepting
5555 // focus (typically because it is not yet visible) just
5556 // wait for it -- any other focused window may in fact
5557 // be in ANR state.
5558 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5559 mMotionTarget = null;
5560 }
5561 }
Romain Guy06882f82009-06-10 13:36:04 -07005562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 target = mMotionTarget;
5564 }
5565 }
Romain Guy06882f82009-06-10 13:36:04 -07005566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 // Pointer events are a little different -- if there isn't a
5570 // target found for any event, then just drop it.
5571 return target != null ? target : SKIP_TARGET_TOKEN;
5572 }
Romain Guy06882f82009-06-10 13:36:04 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 boolean checkShouldDispatchKey(int keycode) {
5575 synchronized (this) {
5576 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5577 mTimeToSwitch = 0;
5578 return true;
5579 }
5580 if (mTimeToSwitch != 0
5581 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5582 return false;
5583 }
5584 return true;
5585 }
5586 }
Romain Guy06882f82009-06-10 13:36:04 -07005587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 void bindTargetWindowLocked(WindowState win,
5589 int pendingWhat, QueuedEvent pendingMotion) {
5590 synchronized (this) {
5591 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5592 }
5593 }
Romain Guy06882f82009-06-10 13:36:04 -07005594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 void bindTargetWindowLocked(WindowState win) {
5596 synchronized (this) {
5597 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5598 }
5599 }
5600
5601 void bindTargetWindowLockedLocked(WindowState win,
5602 int pendingWhat, QueuedEvent pendingMotion) {
5603 mLastWin = win;
5604 mLastBinder = win.mClient.asBinder();
5605 mFinished = false;
5606 if (pendingMotion != null) {
5607 final Session s = win.mSession;
5608 if (pendingWhat == RETURN_PENDING_POINTER) {
5609 releasePendingPointerLocked(s);
5610 s.mPendingPointerMove = pendingMotion;
5611 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005612 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 "bindTargetToWindow " + s.mPendingPointerMove);
5614 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5615 releasePendingTrackballLocked(s);
5616 s.mPendingTrackballMove = pendingMotion;
5617 s.mPendingTrackballWindow = win;
5618 }
5619 }
5620 }
Romain Guy06882f82009-06-10 13:36:04 -07005621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 void releasePendingPointerLocked(Session s) {
5623 if (DEBUG_INPUT) Log.v(TAG,
5624 "releasePendingPointer " + s.mPendingPointerMove);
5625 if (s.mPendingPointerMove != null) {
5626 mQueue.recycleEvent(s.mPendingPointerMove);
5627 s.mPendingPointerMove = null;
5628 }
5629 }
Romain Guy06882f82009-06-10 13:36:04 -07005630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 void releasePendingTrackballLocked(Session s) {
5632 if (s.mPendingTrackballMove != null) {
5633 mQueue.recycleEvent(s.mPendingTrackballMove);
5634 s.mPendingTrackballMove = null;
5635 }
5636 }
Romain Guy06882f82009-06-10 13:36:04 -07005637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5639 int returnWhat) {
5640 if (DEBUG_INPUT) Log.v(
5641 TAG, "finishedKey: client=" + client + ", force=" + force);
5642
5643 if (client == null) {
5644 return null;
5645 }
5646
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005647 MotionEvent res = null;
5648 QueuedEvent qev = null;
5649 WindowState win = null;
5650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 synchronized (this) {
5652 if (DEBUG_INPUT) Log.v(
5653 TAG, "finishedKey: client=" + client.asBinder()
5654 + ", force=" + force + ", last=" + mLastBinder
5655 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 if (returnWhat == RETURN_PENDING_POINTER) {
5658 qev = session.mPendingPointerMove;
5659 win = session.mPendingPointerWindow;
5660 session.mPendingPointerMove = null;
5661 session.mPendingPointerWindow = null;
5662 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5663 qev = session.mPendingTrackballMove;
5664 win = session.mPendingTrackballWindow;
5665 session.mPendingTrackballMove = null;
5666 session.mPendingTrackballWindow = null;
5667 }
Romain Guy06882f82009-06-10 13:36:04 -07005668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 if (mLastBinder == client.asBinder()) {
5670 if (DEBUG_INPUT) Log.v(
5671 TAG, "finishedKey: last paused="
5672 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5673 if (mLastWin != null && (!mLastWin.mToken.paused || force
5674 || !mEventDispatching)) {
5675 doFinishedKeyLocked(false);
5676 } else {
5677 // Make sure to wake up anyone currently waiting to
5678 // dispatch a key, so they can re-evaluate their
5679 // current situation.
5680 mFinished = true;
5681 notifyAll();
5682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 }
Romain Guy06882f82009-06-10 13:36:04 -07005684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005686 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 if (DEBUG_INPUT) Log.v(TAG,
5688 "Returning pending motion: " + res);
5689 mQueue.recycleEvent(qev);
5690 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5691 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005695
5696 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5697 synchronized (mWindowMap) {
5698 if (mWallpaperTarget == win) {
5699 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5700 }
5701 }
5702 }
5703
5704 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 }
5706
5707 void tickle() {
5708 synchronized (this) {
5709 notifyAll();
5710 }
5711 }
Romain Guy06882f82009-06-10 13:36:04 -07005712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 void handleNewWindowLocked(WindowState newWindow) {
5714 if (!newWindow.canReceiveKeys()) {
5715 return;
5716 }
5717 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005718 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 TAG, "New key dispatch window: win="
5720 + newWindow.mClient.asBinder()
5721 + ", last=" + mLastBinder
5722 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5723 + "), finished=" + mFinished + ", paused="
5724 + newWindow.mToken.paused);
5725
5726 // Displaying a window implicitly causes dispatching to
5727 // be unpaused. (This is to protect against bugs if someone
5728 // pauses dispatching but forgets to resume.)
5729 newWindow.mToken.paused = false;
5730
5731 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732
5733 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5734 if (DEBUG_INPUT) Log.v(TAG,
5735 "New SYSTEM_ERROR window; resetting state");
5736 mLastWin = null;
5737 mLastBinder = null;
5738 mMotionTarget = null;
5739 mFinished = true;
5740 } else if (mLastWin != null) {
5741 // If the new window is above the window we are
5742 // waiting on, then stop waiting and let key dispatching
5743 // start on the new guy.
5744 if (DEBUG_INPUT) Log.v(
5745 TAG, "Last win layer=" + mLastWin.mLayer
5746 + ", new win layer=" + newWindow.mLayer);
5747 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005748 // The new window is above the old; finish pending input to the last
5749 // window and start directing it to the new one.
5750 mLastWin.mToken.paused = false;
5751 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005753 // Either the new window is lower, so there is no need to wake key waiters,
5754 // or we just finished key input to the previous window, which implicitly
5755 // notified the key waiters. In both cases, we don't need to issue the
5756 // notification here.
5757 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 }
5759
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005760 // Now that we've put a new window state in place, make the event waiter
5761 // take notice and retarget its attentions.
5762 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 }
5764 }
5765
5766 void pauseDispatchingLocked(WindowToken token) {
5767 synchronized (this)
5768 {
5769 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5770 token.paused = true;
5771
5772 /*
5773 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5774 mPaused = true;
5775 } else {
5776 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005777 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005778 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005779 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005781 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 }
5783 }
5784 */
5785 }
5786 }
5787
5788 void resumeDispatchingLocked(WindowToken token) {
5789 synchronized (this) {
5790 if (token.paused) {
5791 if (DEBUG_INPUT) Log.v(
5792 TAG, "Resuming WindowToken " + token
5793 + ", last=" + mLastBinder
5794 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5795 + "), finished=" + mFinished + ", paused="
5796 + token.paused);
5797 token.paused = false;
5798 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5799 doFinishedKeyLocked(true);
5800 } else {
5801 notifyAll();
5802 }
5803 }
5804 }
5805 }
5806
5807 void setEventDispatchingLocked(boolean enabled) {
5808 synchronized (this) {
5809 mEventDispatching = enabled;
5810 notifyAll();
5811 }
5812 }
Romain Guy06882f82009-06-10 13:36:04 -07005813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 void appSwitchComing() {
5815 synchronized (this) {
5816 // Don't wait for more than .5 seconds for app to finish
5817 // processing the pending events.
5818 long now = SystemClock.uptimeMillis() + 500;
5819 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5820 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5821 mTimeToSwitch = now;
5822 }
5823 notifyAll();
5824 }
5825 }
Romain Guy06882f82009-06-10 13:36:04 -07005826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 private final void doFinishedKeyLocked(boolean doRecycle) {
5828 if (mLastWin != null) {
5829 releasePendingPointerLocked(mLastWin.mSession);
5830 releasePendingTrackballLocked(mLastWin.mSession);
5831 }
Romain Guy06882f82009-06-10 13:36:04 -07005832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 if (mLastWin == null || !mLastWin.mToken.paused
5834 || !mLastWin.isVisibleLw()) {
5835 // If the current window has been paused, we aren't -really-
5836 // finished... so let the waiters still wait.
5837 mLastWin = null;
5838 mLastBinder = null;
5839 }
5840 mFinished = true;
5841 notifyAll();
5842 }
5843 }
5844
5845 private class KeyQ extends KeyInputQueue
5846 implements KeyInputQueue.FilterCallback {
5847 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005850 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5852 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5853 "KEEP_SCREEN_ON_FLAG");
5854 mHoldingScreen.setReferenceCounted(false);
5855 }
5856
5857 @Override
5858 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5859 if (mPolicy.preprocessInputEventTq(event)) {
5860 return true;
5861 }
Romain Guy06882f82009-06-10 13:36:04 -07005862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 switch (event.type) {
5864 case RawInputEvent.EV_KEY: {
5865 // XXX begin hack
5866 if (DEBUG) {
5867 if (event.keycode == KeyEvent.KEYCODE_G) {
5868 if (event.value != 0) {
5869 // G down
5870 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5871 }
5872 return false;
5873 }
5874 if (event.keycode == KeyEvent.KEYCODE_D) {
5875 if (event.value != 0) {
5876 //dump();
5877 }
5878 return false;
5879 }
5880 }
5881 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 boolean screenIsOff = !mPowerManager.screenIsOn();
5884 boolean screenIsDim = !mPowerManager.screenIsBright();
5885 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5888 mPowerManager.goToSleep(event.when);
5889 }
5890
5891 if (screenIsOff) {
5892 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5893 }
5894 if (screenIsDim) {
5895 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5896 }
5897 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5898 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005899 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 }
Romain Guy06882f82009-06-10 13:36:04 -07005901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5903 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5904 filterQueue(this);
5905 mKeyWaiter.appSwitchComing();
5906 }
5907 return true;
5908 } else {
5909 return false;
5910 }
5911 }
Romain Guy06882f82009-06-10 13:36:04 -07005912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 case RawInputEvent.EV_REL: {
5914 boolean screenIsOff = !mPowerManager.screenIsOn();
5915 boolean screenIsDim = !mPowerManager.screenIsBright();
5916 if (screenIsOff) {
5917 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5918 device.classes, event)) {
5919 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5920 return false;
5921 }
5922 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5923 }
5924 if (screenIsDim) {
5925 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5926 }
5927 return true;
5928 }
Romain Guy06882f82009-06-10 13:36:04 -07005929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 case RawInputEvent.EV_ABS: {
5931 boolean screenIsOff = !mPowerManager.screenIsOn();
5932 boolean screenIsDim = !mPowerManager.screenIsBright();
5933 if (screenIsOff) {
5934 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5935 device.classes, event)) {
5936 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5937 return false;
5938 }
5939 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5940 }
5941 if (screenIsDim) {
5942 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5943 }
5944 return true;
5945 }
Romain Guy06882f82009-06-10 13:36:04 -07005946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 default:
5948 return true;
5949 }
5950 }
5951
5952 public int filterEvent(QueuedEvent ev) {
5953 switch (ev.classType) {
5954 case RawInputEvent.CLASS_KEYBOARD:
5955 KeyEvent ke = (KeyEvent)ev.event;
5956 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5957 Log.w(TAG, "Dropping movement key during app switch: "
5958 + ke.getKeyCode() + ", action=" + ke.getAction());
5959 return FILTER_REMOVE;
5960 }
5961 return FILTER_ABORT;
5962 default:
5963 return FILTER_KEEP;
5964 }
5965 }
Romain Guy06882f82009-06-10 13:36:04 -07005966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 /**
5968 * Must be called with the main window manager lock held.
5969 */
5970 void setHoldScreenLocked(boolean holding) {
5971 boolean state = mHoldingScreen.isHeld();
5972 if (holding != state) {
5973 if (holding) {
5974 mHoldingScreen.acquire();
5975 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005976 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 mHoldingScreen.release();
5978 }
5979 }
5980 }
Michael Chan53071d62009-05-13 17:29:48 -07005981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982
5983 public boolean detectSafeMode() {
5984 mSafeMode = mPolicy.detectSafeMode();
5985 return mSafeMode;
5986 }
Romain Guy06882f82009-06-10 13:36:04 -07005987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 public void systemReady() {
5989 mPolicy.systemReady();
5990 }
Romain Guy06882f82009-06-10 13:36:04 -07005991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 private final class InputDispatcherThread extends Thread {
5993 // Time to wait when there is nothing to do: 9999 seconds.
5994 static final int LONG_WAIT=9999*1000;
5995
5996 public InputDispatcherThread() {
5997 super("InputDispatcher");
5998 }
Romain Guy06882f82009-06-10 13:36:04 -07005999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 @Override
6001 public void run() {
6002 while (true) {
6003 try {
6004 process();
6005 } catch (Exception e) {
6006 Log.e(TAG, "Exception in input dispatcher", e);
6007 }
6008 }
6009 }
Romain Guy06882f82009-06-10 13:36:04 -07006010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 private void process() {
6012 android.os.Process.setThreadPriority(
6013 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 // The last key event we saw
6016 KeyEvent lastKey = null;
6017
6018 // Last keydown time for auto-repeating keys
6019 long lastKeyTime = SystemClock.uptimeMillis();
6020 long nextKeyTime = lastKeyTime+LONG_WAIT;
6021
Romain Guy06882f82009-06-10 13:36:04 -07006022 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 int keyRepeatCount = 0;
6024
6025 // Need to report that configuration has changed?
6026 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 while (true) {
6029 long curTime = SystemClock.uptimeMillis();
6030
6031 if (DEBUG_INPUT) Log.v(
6032 TAG, "Waiting for next key: now=" + curTime
6033 + ", repeat @ " + nextKeyTime);
6034
6035 // Retrieve next event, waiting only as long as the next
6036 // repeat timeout. If the configuration has changed, then
6037 // don't wait at all -- we'll report the change as soon as
6038 // we have processed all events.
6039 QueuedEvent ev = mQueue.getEvent(
6040 (int)((!configChanged && curTime < nextKeyTime)
6041 ? (nextKeyTime-curTime) : 0));
6042
6043 if (DEBUG_INPUT && ev != null) Log.v(
6044 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6045
Michael Chan53071d62009-05-13 17:29:48 -07006046 if (MEASURE_LATENCY) {
6047 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6048 }
6049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 try {
6051 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006052 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 int eventType;
6054 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6055 eventType = eventType((MotionEvent)ev.event);
6056 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6057 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6058 eventType = LocalPowerManager.BUTTON_EVENT;
6059 } else {
6060 eventType = LocalPowerManager.OTHER_EVENT;
6061 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006062 try {
Michael Chan53071d62009-05-13 17:29:48 -07006063 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006064 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006065 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006066 mBatteryStats.noteInputEvent();
6067 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006068 } catch (RemoteException e) {
6069 // Ignore
6070 }
Michael Chane10de972009-05-18 11:24:50 -07006071
6072 if (eventType != TOUCH_EVENT
6073 && eventType != LONG_TOUCH_EVENT
6074 && eventType != CHEEK_EVENT) {
6075 mPowerManager.userActivity(curTime, false,
6076 eventType, false);
6077 } else if (mLastTouchEventType != eventType
6078 || (curTime - mLastUserActivityCallTime)
6079 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6080 mLastUserActivityCallTime = curTime;
6081 mLastTouchEventType = eventType;
6082 mPowerManager.userActivity(curTime, false,
6083 eventType, false);
6084 }
6085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 switch (ev.classType) {
6087 case RawInputEvent.CLASS_KEYBOARD:
6088 KeyEvent ke = (KeyEvent)ev.event;
6089 if (ke.isDown()) {
6090 lastKey = ke;
6091 keyRepeatCount = 0;
6092 lastKeyTime = curTime;
6093 nextKeyTime = lastKeyTime
6094 + KEY_REPEAT_FIRST_DELAY;
6095 if (DEBUG_INPUT) Log.v(
6096 TAG, "Received key down: first repeat @ "
6097 + nextKeyTime);
6098 } else {
6099 lastKey = null;
6100 // Arbitrary long timeout.
6101 lastKeyTime = curTime;
6102 nextKeyTime = curTime + LONG_WAIT;
6103 if (DEBUG_INPUT) Log.v(
6104 TAG, "Received key up: ignore repeat @ "
6105 + nextKeyTime);
6106 }
6107 dispatchKey((KeyEvent)ev.event, 0, 0);
6108 mQueue.recycleEvent(ev);
6109 break;
6110 case RawInputEvent.CLASS_TOUCHSCREEN:
6111 //Log.i(TAG, "Read next event " + ev);
6112 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6113 break;
6114 case RawInputEvent.CLASS_TRACKBALL:
6115 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6116 break;
6117 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6118 configChanged = true;
6119 break;
6120 default:
6121 mQueue.recycleEvent(ev);
6122 break;
6123 }
Romain Guy06882f82009-06-10 13:36:04 -07006124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 } else if (configChanged) {
6126 configChanged = false;
6127 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 } else if (lastKey != null) {
6130 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 // Timeout occurred while key was down. If it is at or
6133 // past the key repeat time, dispatch the repeat.
6134 if (DEBUG_INPUT) Log.v(
6135 TAG, "Key timeout: repeat=" + nextKeyTime
6136 + ", now=" + curTime);
6137 if (curTime < nextKeyTime) {
6138 continue;
6139 }
Romain Guy06882f82009-06-10 13:36:04 -07006140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 lastKeyTime = nextKeyTime;
6142 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6143 keyRepeatCount++;
6144 if (DEBUG_INPUT) Log.v(
6145 TAG, "Key repeat: count=" + keyRepeatCount
6146 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006147 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 } else {
6150 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 lastKeyTime = curTime;
6153 nextKeyTime = curTime + LONG_WAIT;
6154 }
Romain Guy06882f82009-06-10 13:36:04 -07006155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 } catch (Exception e) {
6157 Log.e(TAG,
6158 "Input thread received uncaught exception: " + e, e);
6159 }
6160 }
6161 }
6162 }
6163
6164 // -------------------------------------------------------------
6165 // Client Session State
6166 // -------------------------------------------------------------
6167
6168 private final class Session extends IWindowSession.Stub
6169 implements IBinder.DeathRecipient {
6170 final IInputMethodClient mClient;
6171 final IInputContext mInputContext;
6172 final int mUid;
6173 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006174 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 SurfaceSession mSurfaceSession;
6176 int mNumWindow = 0;
6177 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 /**
6180 * Current pointer move event being dispatched to client window... must
6181 * hold key lock to access.
6182 */
6183 QueuedEvent mPendingPointerMove;
6184 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 /**
6187 * Current trackball move event being dispatched to client window... must
6188 * hold key lock to access.
6189 */
6190 QueuedEvent mPendingTrackballMove;
6191 WindowState mPendingTrackballWindow;
6192
6193 public Session(IInputMethodClient client, IInputContext inputContext) {
6194 mClient = client;
6195 mInputContext = inputContext;
6196 mUid = Binder.getCallingUid();
6197 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006198 StringBuilder sb = new StringBuilder();
6199 sb.append("Session{");
6200 sb.append(Integer.toHexString(System.identityHashCode(this)));
6201 sb.append(" uid ");
6202 sb.append(mUid);
6203 sb.append("}");
6204 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 synchronized (mWindowMap) {
6207 if (mInputMethodManager == null && mHaveInputMethods) {
6208 IBinder b = ServiceManager.getService(
6209 Context.INPUT_METHOD_SERVICE);
6210 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6211 }
6212 }
6213 long ident = Binder.clearCallingIdentity();
6214 try {
6215 // Note: it is safe to call in to the input method manager
6216 // here because we are not holding our lock.
6217 if (mInputMethodManager != null) {
6218 mInputMethodManager.addClient(client, inputContext,
6219 mUid, mPid);
6220 } else {
6221 client.setUsingInputMethod(false);
6222 }
6223 client.asBinder().linkToDeath(this, 0);
6224 } catch (RemoteException e) {
6225 // The caller has died, so we can just forget about this.
6226 try {
6227 if (mInputMethodManager != null) {
6228 mInputMethodManager.removeClient(client);
6229 }
6230 } catch (RemoteException ee) {
6231 }
6232 } finally {
6233 Binder.restoreCallingIdentity(ident);
6234 }
6235 }
Romain Guy06882f82009-06-10 13:36:04 -07006236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 @Override
6238 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6239 throws RemoteException {
6240 try {
6241 return super.onTransact(code, data, reply, flags);
6242 } catch (RuntimeException e) {
6243 // Log all 'real' exceptions thrown to the caller
6244 if (!(e instanceof SecurityException)) {
6245 Log.e(TAG, "Window Session Crash", e);
6246 }
6247 throw e;
6248 }
6249 }
6250
6251 public void binderDied() {
6252 // Note: it is safe to call in to the input method manager
6253 // here because we are not holding our lock.
6254 try {
6255 if (mInputMethodManager != null) {
6256 mInputMethodManager.removeClient(mClient);
6257 }
6258 } catch (RemoteException e) {
6259 }
6260 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006261 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 mClientDead = true;
6263 killSessionLocked();
6264 }
6265 }
6266
6267 public int add(IWindow window, WindowManager.LayoutParams attrs,
6268 int viewVisibility, Rect outContentInsets) {
6269 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6270 }
Romain Guy06882f82009-06-10 13:36:04 -07006271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 public void remove(IWindow window) {
6273 removeWindow(this, window);
6274 }
Romain Guy06882f82009-06-10 13:36:04 -07006275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6277 int requestedWidth, int requestedHeight, int viewFlags,
6278 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6279 Rect outVisibleInsets, Surface outSurface) {
6280 return relayoutWindow(this, window, attrs,
6281 requestedWidth, requestedHeight, viewFlags, insetsPending,
6282 outFrame, outContentInsets, outVisibleInsets, outSurface);
6283 }
Romain Guy06882f82009-06-10 13:36:04 -07006284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 public void setTransparentRegion(IWindow window, Region region) {
6286 setTransparentRegionWindow(this, window, region);
6287 }
Romain Guy06882f82009-06-10 13:36:04 -07006288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 public void setInsets(IWindow window, int touchableInsets,
6290 Rect contentInsets, Rect visibleInsets) {
6291 setInsetsWindow(this, window, touchableInsets, contentInsets,
6292 visibleInsets);
6293 }
Romain Guy06882f82009-06-10 13:36:04 -07006294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6296 getWindowDisplayFrame(this, window, outDisplayFrame);
6297 }
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 public void finishDrawing(IWindow window) {
6300 if (localLOGV) Log.v(
6301 TAG, "IWindow finishDrawing called for " + window);
6302 finishDrawingWindow(this, window);
6303 }
6304
6305 public void finishKey(IWindow window) {
6306 if (localLOGV) Log.v(
6307 TAG, "IWindow finishKey called for " + window);
6308 mKeyWaiter.finishedKey(this, window, false,
6309 KeyWaiter.RETURN_NOTHING);
6310 }
6311
6312 public MotionEvent getPendingPointerMove(IWindow window) {
6313 if (localLOGV) Log.v(
6314 TAG, "IWindow getPendingMotionEvent called for " + window);
6315 return mKeyWaiter.finishedKey(this, window, false,
6316 KeyWaiter.RETURN_PENDING_POINTER);
6317 }
Romain Guy06882f82009-06-10 13:36:04 -07006318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 public MotionEvent getPendingTrackballMove(IWindow window) {
6320 if (localLOGV) Log.v(
6321 TAG, "IWindow getPendingMotionEvent called for " + window);
6322 return mKeyWaiter.finishedKey(this, window, false,
6323 KeyWaiter.RETURN_PENDING_TRACKBALL);
6324 }
6325
6326 public void setInTouchMode(boolean mode) {
6327 synchronized(mWindowMap) {
6328 mInTouchMode = mode;
6329 }
6330 }
6331
6332 public boolean getInTouchMode() {
6333 synchronized(mWindowMap) {
6334 return mInTouchMode;
6335 }
6336 }
6337
6338 public boolean performHapticFeedback(IWindow window, int effectId,
6339 boolean always) {
6340 synchronized(mWindowMap) {
6341 long ident = Binder.clearCallingIdentity();
6342 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006343 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 windowForClientLocked(this, window), effectId, always);
6345 } finally {
6346 Binder.restoreCallingIdentity(ident);
6347 }
6348 }
6349 }
Romain Guy06882f82009-06-10 13:36:04 -07006350
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006351 public void setWallpaperPosition(IBinder window, float x, float y) {
6352 synchronized(mWindowMap) {
6353 long ident = Binder.clearCallingIdentity();
6354 try {
6355 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6356 x, y);
6357 } finally {
6358 Binder.restoreCallingIdentity(ident);
6359 }
6360 }
6361 }
6362
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006363 public void wallpaperOffsetsComplete(IBinder window) {
6364 WindowManagerService.this.wallpaperOffsetsComplete(window);
6365 }
6366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 void windowAddedLocked() {
6368 if (mSurfaceSession == null) {
6369 if (localLOGV) Log.v(
6370 TAG, "First window added to " + this + ", creating SurfaceSession");
6371 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006372 if (SHOW_TRANSACTIONS) Log.i(
6373 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 mSessions.add(this);
6375 }
6376 mNumWindow++;
6377 }
6378
6379 void windowRemovedLocked() {
6380 mNumWindow--;
6381 killSessionLocked();
6382 }
Romain Guy06882f82009-06-10 13:36:04 -07006383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 void killSessionLocked() {
6385 if (mNumWindow <= 0 && mClientDead) {
6386 mSessions.remove(this);
6387 if (mSurfaceSession != null) {
6388 if (localLOGV) Log.v(
6389 TAG, "Last window removed from " + this
6390 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006391 if (SHOW_TRANSACTIONS) Log.i(
6392 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006393 try {
6394 mSurfaceSession.kill();
6395 } catch (Exception e) {
6396 Log.w(TAG, "Exception thrown when killing surface session "
6397 + mSurfaceSession + " in session " + this
6398 + ": " + e.toString());
6399 }
6400 mSurfaceSession = null;
6401 }
6402 }
6403 }
Romain Guy06882f82009-06-10 13:36:04 -07006404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006406 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6407 pw.print(" mClientDead="); pw.print(mClientDead);
6408 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6409 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6410 pw.print(prefix);
6411 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6412 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6413 }
6414 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6415 pw.print(prefix);
6416 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6417 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 }
6420
6421 @Override
6422 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006423 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 }
6425 }
6426
6427 // -------------------------------------------------------------
6428 // Client Window State
6429 // -------------------------------------------------------------
6430
6431 private final class WindowState implements WindowManagerPolicy.WindowState {
6432 final Session mSession;
6433 final IWindow mClient;
6434 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006435 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 AppWindowToken mAppToken;
6437 AppWindowToken mTargetAppToken;
6438 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6439 final DeathRecipient mDeathRecipient;
6440 final WindowState mAttachedWindow;
6441 final ArrayList mChildWindows = new ArrayList();
6442 final int mBaseLayer;
6443 final int mSubLayer;
6444 final boolean mLayoutAttached;
6445 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006446 final boolean mIsWallpaper;
6447 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 int mViewVisibility;
6449 boolean mPolicyVisibility = true;
6450 boolean mPolicyVisibilityAfterAnim = true;
6451 boolean mAppFreezing;
6452 Surface mSurface;
6453 boolean mAttachedHidden; // is our parent window hidden?
6454 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006455 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 int mRequestedWidth;
6457 int mRequestedHeight;
6458 int mLastRequestedWidth;
6459 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460 int mLayer;
6461 int mAnimLayer;
6462 int mLastLayer;
6463 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006464 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465
6466 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 // Actual frame shown on-screen (may be modified by animation)
6469 final Rect mShownFrame = new Rect();
6470 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 /**
6473 * Insets that determine the actually visible area
6474 */
6475 final Rect mVisibleInsets = new Rect();
6476 final Rect mLastVisibleInsets = new Rect();
6477 boolean mVisibleInsetsChanged;
6478
6479 /**
6480 * Insets that are covered by system windows
6481 */
6482 final Rect mContentInsets = new Rect();
6483 final Rect mLastContentInsets = new Rect();
6484 boolean mContentInsetsChanged;
6485
6486 /**
6487 * Set to true if we are waiting for this window to receive its
6488 * given internal insets before laying out other windows based on it.
6489 */
6490 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 /**
6493 * These are the content insets that were given during layout for
6494 * this window, to be applied to windows behind it.
6495 */
6496 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 /**
6499 * These are the visible insets that were given during layout for
6500 * this window, to be applied to windows behind it.
6501 */
6502 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 /**
6505 * Flag indicating whether the touchable region should be adjusted by
6506 * the visible insets; if false the area outside the visible insets is
6507 * NOT touchable, so we must use those to adjust the frame during hit
6508 * tests.
6509 */
6510 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 // Current transformation being applied.
6513 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6514 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6515 float mHScale=1, mVScale=1;
6516 float mLastHScale=1, mLastVScale=1;
6517 final Matrix mTmpMatrix = new Matrix();
6518
6519 // "Real" frame that the application sees.
6520 final Rect mFrame = new Rect();
6521 final Rect mLastFrame = new Rect();
6522
6523 final Rect mContainingFrame = new Rect();
6524 final Rect mDisplayFrame = new Rect();
6525 final Rect mContentFrame = new Rect();
6526 final Rect mVisibleFrame = new Rect();
6527
6528 float mShownAlpha = 1;
6529 float mAlpha = 1;
6530 float mLastAlpha = 1;
6531
6532 // Set to true if, when the window gets displayed, it should perform
6533 // an enter animation.
6534 boolean mEnterAnimationPending;
6535
6536 // Currently running animation.
6537 boolean mAnimating;
6538 boolean mLocalAnimating;
6539 Animation mAnimation;
6540 boolean mAnimationIsEntrance;
6541 boolean mHasTransformation;
6542 boolean mHasLocalTransformation;
6543 final Transformation mTransformation = new Transformation();
6544
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006545 // If a window showing a wallpaper: the requested offset for the
6546 // wallpaper; if a wallpaper window: the currently applied offset.
6547 float mWallpaperX = -1;
6548 float mWallpaperY = -1;
6549
6550 // Wallpaper windows: pixels offset based on above variables.
6551 int mXOffset;
6552 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 // This is set after IWindowSession.relayout() has been called at
6555 // least once for the window. It allows us to detect the situation
6556 // where we don't yet have a surface, but should have one soon, so
6557 // we can give the window focus before waiting for the relayout.
6558 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 // This is set after the Surface has been created but before the
6561 // window has been drawn. During this time the surface is hidden.
6562 boolean mDrawPending;
6563
6564 // This is set after the window has finished drawing for the first
6565 // time but before its surface is shown. The surface will be
6566 // displayed when the next layout is run.
6567 boolean mCommitDrawPending;
6568
6569 // This is set during the time after the window's drawing has been
6570 // committed, and before its surface is actually shown. It is used
6571 // to delay showing the surface until all windows in a token are ready
6572 // to be shown.
6573 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 // Set when the window has been shown in the screen the first time.
6576 boolean mHasDrawn;
6577
6578 // Currently running an exit animation?
6579 boolean mExiting;
6580
6581 // Currently on the mDestroySurface list?
6582 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 // Completely remove from window manager after exit animation?
6585 boolean mRemoveOnExit;
6586
6587 // Set when the orientation is changing and this window has not yet
6588 // been updated for the new orientation.
6589 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 // Is this window now (or just being) removed?
6592 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 WindowState(Session s, IWindow c, WindowToken token,
6595 WindowState attachedWindow, WindowManager.LayoutParams a,
6596 int viewVisibility) {
6597 mSession = s;
6598 mClient = c;
6599 mToken = token;
6600 mAttrs.copyFrom(a);
6601 mViewVisibility = viewVisibility;
6602 DeathRecipient deathRecipient = new DeathRecipient();
6603 mAlpha = a.alpha;
6604 if (localLOGV) Log.v(
6605 TAG, "Window " + this + " client=" + c.asBinder()
6606 + " token=" + token + " (" + mAttrs.token + ")");
6607 try {
6608 c.asBinder().linkToDeath(deathRecipient, 0);
6609 } catch (RemoteException e) {
6610 mDeathRecipient = null;
6611 mAttachedWindow = null;
6612 mLayoutAttached = false;
6613 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006614 mIsWallpaper = false;
6615 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 mBaseLayer = 0;
6617 mSubLayer = 0;
6618 return;
6619 }
6620 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6623 mAttrs.type <= LAST_SUB_WINDOW)) {
6624 // The multiplier here is to reserve space for multiple
6625 // windows in the same type layer.
6626 mBaseLayer = mPolicy.windowTypeToLayerLw(
6627 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6628 + TYPE_LAYER_OFFSET;
6629 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6630 mAttachedWindow = attachedWindow;
6631 mAttachedWindow.mChildWindows.add(this);
6632 mLayoutAttached = mAttrs.type !=
6633 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6634 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6635 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006636 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6637 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 } else {
6639 // The multiplier here is to reserve space for multiple
6640 // windows in the same type layer.
6641 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6642 * TYPE_LAYER_MULTIPLIER
6643 + TYPE_LAYER_OFFSET;
6644 mSubLayer = 0;
6645 mAttachedWindow = null;
6646 mLayoutAttached = false;
6647 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6648 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006649 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6650 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 }
6652
6653 WindowState appWin = this;
6654 while (appWin.mAttachedWindow != null) {
6655 appWin = mAttachedWindow;
6656 }
6657 WindowToken appToken = appWin.mToken;
6658 while (appToken.appWindowToken == null) {
6659 WindowToken parent = mTokenMap.get(appToken.token);
6660 if (parent == null || appToken == parent) {
6661 break;
6662 }
6663 appToken = parent;
6664 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006665 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666 mAppToken = appToken.appWindowToken;
6667
6668 mSurface = null;
6669 mRequestedWidth = 0;
6670 mRequestedHeight = 0;
6671 mLastRequestedWidth = 0;
6672 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006673 mXOffset = 0;
6674 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 mLayer = 0;
6676 mAnimLayer = 0;
6677 mLastLayer = 0;
6678 }
6679
6680 void attach() {
6681 if (localLOGV) Log.v(
6682 TAG, "Attaching " + this + " token=" + mToken
6683 + ", list=" + mToken.windows);
6684 mSession.windowAddedLocked();
6685 }
6686
6687 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6688 mHaveFrame = true;
6689
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006690 final Rect container = mContainingFrame;
6691 container.set(pf);
6692
6693 final Rect display = mDisplayFrame;
6694 display.set(df);
6695
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006696 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006697 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006698 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6699 display.intersect(mCompatibleScreenFrame);
6700 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006701 }
6702
6703 final int pw = container.right - container.left;
6704 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705
6706 int w,h;
6707 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6708 w = mAttrs.width < 0 ? pw : mAttrs.width;
6709 h = mAttrs.height< 0 ? ph : mAttrs.height;
6710 } else {
6711 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6712 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6713 }
Romain Guy06882f82009-06-10 13:36:04 -07006714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 final Rect content = mContentFrame;
6716 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 final Rect visible = mVisibleFrame;
6719 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006722 final int fw = frame.width();
6723 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6726 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6727
6728 Gravity.apply(mAttrs.gravity, w, h, container,
6729 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6730 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6731
6732 //System.out.println("Out: " + mFrame);
6733
6734 // Now make sure the window fits in the overall display.
6735 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 // Make sure the content and visible frames are inside of the
6738 // final window frame.
6739 if (content.left < frame.left) content.left = frame.left;
6740 if (content.top < frame.top) content.top = frame.top;
6741 if (content.right > frame.right) content.right = frame.right;
6742 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6743 if (visible.left < frame.left) visible.left = frame.left;
6744 if (visible.top < frame.top) visible.top = frame.top;
6745 if (visible.right > frame.right) visible.right = frame.right;
6746 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006748 final Rect contentInsets = mContentInsets;
6749 contentInsets.left = content.left-frame.left;
6750 contentInsets.top = content.top-frame.top;
6751 contentInsets.right = frame.right-content.right;
6752 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 final Rect visibleInsets = mVisibleInsets;
6755 visibleInsets.left = visible.left-frame.left;
6756 visibleInsets.top = visible.top-frame.top;
6757 visibleInsets.right = frame.right-visible.right;
6758 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006759
Dianne Hackborn284ac932009-08-28 10:34:25 -07006760 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6761 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006762 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006763 }
6764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 if (localLOGV) {
6766 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6767 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6768 Log.v(TAG, "Resolving (mRequestedWidth="
6769 + mRequestedWidth + ", mRequestedheight="
6770 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6771 + "): frame=" + mFrame.toShortString()
6772 + " ci=" + contentInsets.toShortString()
6773 + " vi=" + visibleInsets.toShortString());
6774 //}
6775 }
6776 }
Romain Guy06882f82009-06-10 13:36:04 -07006777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 public Rect getFrameLw() {
6779 return mFrame;
6780 }
6781
6782 public Rect getShownFrameLw() {
6783 return mShownFrame;
6784 }
6785
6786 public Rect getDisplayFrameLw() {
6787 return mDisplayFrame;
6788 }
6789
6790 public Rect getContentFrameLw() {
6791 return mContentFrame;
6792 }
6793
6794 public Rect getVisibleFrameLw() {
6795 return mVisibleFrame;
6796 }
6797
6798 public boolean getGivenInsetsPendingLw() {
6799 return mGivenInsetsPending;
6800 }
6801
6802 public Rect getGivenContentInsetsLw() {
6803 return mGivenContentInsets;
6804 }
Romain Guy06882f82009-06-10 13:36:04 -07006805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 public Rect getGivenVisibleInsetsLw() {
6807 return mGivenVisibleInsets;
6808 }
Romain Guy06882f82009-06-10 13:36:04 -07006809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 public WindowManager.LayoutParams getAttrs() {
6811 return mAttrs;
6812 }
6813
6814 public int getSurfaceLayer() {
6815 return mLayer;
6816 }
Romain Guy06882f82009-06-10 13:36:04 -07006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 public IApplicationToken getAppToken() {
6819 return mAppToken != null ? mAppToken.appToken : null;
6820 }
6821
6822 public boolean hasAppShownWindows() {
6823 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6824 }
6825
6826 public boolean hasAppStartingIcon() {
6827 return mAppToken != null ? (mAppToken.startingData != null) : false;
6828 }
6829
6830 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6831 return mAppToken != null ? mAppToken.startingWindow : null;
6832 }
6833
6834 public void setAnimation(Animation anim) {
6835 if (localLOGV) Log.v(
6836 TAG, "Setting animation in " + this + ": " + anim);
6837 mAnimating = false;
6838 mLocalAnimating = false;
6839 mAnimation = anim;
6840 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6841 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6842 }
6843
6844 public void clearAnimation() {
6845 if (mAnimation != null) {
6846 mAnimating = true;
6847 mLocalAnimating = false;
6848 mAnimation = null;
6849 }
6850 }
Romain Guy06882f82009-06-10 13:36:04 -07006851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006852 Surface createSurfaceLocked() {
6853 if (mSurface == null) {
6854 mDrawPending = true;
6855 mCommitDrawPending = false;
6856 mReadyToShow = false;
6857 if (mAppToken != null) {
6858 mAppToken.allDrawn = false;
6859 }
6860
6861 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006862 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 flags |= Surface.PUSH_BUFFERS;
6864 }
6865
6866 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6867 flags |= Surface.SECURE;
6868 }
6869 if (DEBUG_VISIBILITY) Log.v(
6870 TAG, "Creating surface in session "
6871 + mSession.mSurfaceSession + " window " + this
6872 + " w=" + mFrame.width()
6873 + " h=" + mFrame.height() + " format="
6874 + mAttrs.format + " flags=" + flags);
6875
6876 int w = mFrame.width();
6877 int h = mFrame.height();
6878 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6879 // for a scaled surface, we always want the requested
6880 // size.
6881 w = mRequestedWidth;
6882 h = mRequestedHeight;
6883 }
6884
6885 try {
6886 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006887 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006888 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006889 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6890 + mSurface + " IN SESSION "
6891 + mSession.mSurfaceSession
6892 + ": pid=" + mSession.mPid + " format="
6893 + mAttrs.format + " flags=0x"
6894 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 } catch (Surface.OutOfResourcesException e) {
6896 Log.w(TAG, "OutOfResourcesException creating surface");
6897 reclaimSomeSurfaceMemoryLocked(this, "create");
6898 return null;
6899 } catch (Exception e) {
6900 Log.e(TAG, "Exception creating surface", e);
6901 return null;
6902 }
Romain Guy06882f82009-06-10 13:36:04 -07006903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 if (localLOGV) Log.v(
6905 TAG, "Got surface: " + mSurface
6906 + ", set left=" + mFrame.left + " top=" + mFrame.top
6907 + ", animLayer=" + mAnimLayer);
6908 if (SHOW_TRANSACTIONS) {
6909 Log.i(TAG, ">>> OPEN TRANSACTION");
6910 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6911 + mAttrs.getTitle() + ") pos=(" +
6912 mFrame.left + "," + mFrame.top + ") (" +
6913 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6914 mAnimLayer + " HIDE");
6915 }
6916 Surface.openTransaction();
6917 try {
6918 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006919 mSurface.setPosition(mFrame.left + mXOffset,
6920 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 mSurface.setLayer(mAnimLayer);
6922 mSurface.hide();
6923 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006924 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6925 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 mSurface.setFlags(Surface.SURFACE_DITHER,
6927 Surface.SURFACE_DITHER);
6928 }
6929 } catch (RuntimeException e) {
6930 Log.w(TAG, "Error creating surface in " + w, e);
6931 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6932 }
6933 mLastHidden = true;
6934 } finally {
6935 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6936 Surface.closeTransaction();
6937 }
6938 if (localLOGV) Log.v(
6939 TAG, "Created surface " + this);
6940 }
6941 return mSurface;
6942 }
Romain Guy06882f82009-06-10 13:36:04 -07006943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 void destroySurfaceLocked() {
6945 // Window is no longer on-screen, so can no longer receive
6946 // key events... if we were waiting for it to finish
6947 // handling a key event, the wait is over!
6948 mKeyWaiter.finishedKey(mSession, mClient, true,
6949 KeyWaiter.RETURN_NOTHING);
6950 mKeyWaiter.releasePendingPointerLocked(mSession);
6951 mKeyWaiter.releasePendingTrackballLocked(mSession);
6952
6953 if (mAppToken != null && this == mAppToken.startingWindow) {
6954 mAppToken.startingDisplayed = false;
6955 }
Romain Guy06882f82009-06-10 13:36:04 -07006956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957 if (mSurface != null) {
6958 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006959 if (DEBUG_VISIBILITY) {
6960 RuntimeException e = new RuntimeException();
6961 e.fillInStackTrace();
6962 Log.w(TAG, "Window " + this + " destroying surface "
6963 + mSurface + ", session " + mSession, e);
6964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 if (SHOW_TRANSACTIONS) {
6966 RuntimeException ex = new RuntimeException();
6967 ex.fillInStackTrace();
6968 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6969 + mAttrs.getTitle() + ")", ex);
6970 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006971 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 } catch (RuntimeException e) {
6973 Log.w(TAG, "Exception thrown when destroying Window " + this
6974 + " surface " + mSurface + " session " + mSession
6975 + ": " + e.toString());
6976 }
6977 mSurface = null;
6978 mDrawPending = false;
6979 mCommitDrawPending = false;
6980 mReadyToShow = false;
6981
6982 int i = mChildWindows.size();
6983 while (i > 0) {
6984 i--;
6985 WindowState c = (WindowState)mChildWindows.get(i);
6986 c.mAttachedHidden = true;
6987 }
6988 }
6989 }
6990
6991 boolean finishDrawingLocked() {
6992 if (mDrawPending) {
6993 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6994 TAG, "finishDrawingLocked: " + mSurface);
6995 mCommitDrawPending = true;
6996 mDrawPending = false;
6997 return true;
6998 }
6999 return false;
7000 }
7001
7002 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007003 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007004 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
7005 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007006 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 }
7008 mCommitDrawPending = false;
7009 mReadyToShow = true;
7010 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7011 final AppWindowToken atoken = mAppToken;
7012 if (atoken == null || atoken.allDrawn || starting) {
7013 performShowLocked();
7014 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007015 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016 }
7017
7018 // This must be called while inside a transaction.
7019 boolean performShowLocked() {
7020 if (DEBUG_VISIBILITY) {
7021 RuntimeException e = new RuntimeException();
7022 e.fillInStackTrace();
7023 Log.v(TAG, "performShow on " + this
7024 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7025 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7026 }
7027 if (mReadyToShow && isReadyForDisplay()) {
7028 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
7029 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
7030 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
7031 + " during animation: policyVis=" + mPolicyVisibility
7032 + " attHidden=" + mAttachedHidden
7033 + " tok.hiddenRequested="
7034 + (mAppToken != null ? mAppToken.hiddenRequested : false)
7035 + " tok.idden="
7036 + (mAppToken != null ? mAppToken.hidden : false)
7037 + " animating=" + mAnimating
7038 + " tok animating="
7039 + (mAppToken != null ? mAppToken.animating : false));
7040 if (!showSurfaceRobustlyLocked(this)) {
7041 return false;
7042 }
7043 mLastAlpha = -1;
7044 mHasDrawn = true;
7045 mLastHidden = false;
7046 mReadyToShow = false;
7047 enableScreenIfNeededLocked();
7048
7049 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 int i = mChildWindows.size();
7052 while (i > 0) {
7053 i--;
7054 WindowState c = (WindowState)mChildWindows.get(i);
7055 if (c.mSurface != null && c.mAttachedHidden) {
7056 c.mAttachedHidden = false;
7057 c.performShowLocked();
7058 }
7059 }
Romain Guy06882f82009-06-10 13:36:04 -07007060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061 if (mAttrs.type != TYPE_APPLICATION_STARTING
7062 && mAppToken != null) {
7063 mAppToken.firstWindowDrawn = true;
7064 if (mAnimation == null && mAppToken.startingData != null) {
7065 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7066 + mToken
7067 + ": first real window is shown, no animation");
7068 mFinishedStarting.add(mAppToken);
7069 mH.sendEmptyMessage(H.FINISHED_STARTING);
7070 }
7071 mAppToken.updateReportedVisibilityLocked();
7072 }
7073 }
7074 return true;
7075 }
Romain Guy06882f82009-06-10 13:36:04 -07007076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 // This must be called while inside a transaction. Returns true if
7078 // there is more animation to run.
7079 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7080 if (!mDisplayFrozen) {
7081 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7084 mHasTransformation = true;
7085 mHasLocalTransformation = true;
7086 if (!mLocalAnimating) {
7087 if (DEBUG_ANIM) Log.v(
7088 TAG, "Starting animation in " + this +
7089 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7090 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7091 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7092 mAnimation.setStartTime(currentTime);
7093 mLocalAnimating = true;
7094 mAnimating = true;
7095 }
7096 mTransformation.clear();
7097 final boolean more = mAnimation.getTransformation(
7098 currentTime, mTransformation);
7099 if (DEBUG_ANIM) Log.v(
7100 TAG, "Stepped animation in " + this +
7101 ": more=" + more + ", xform=" + mTransformation);
7102 if (more) {
7103 // we're not done!
7104 return true;
7105 }
7106 if (DEBUG_ANIM) Log.v(
7107 TAG, "Finished animation in " + this +
7108 " @ " + currentTime);
7109 mAnimation = null;
7110 //WindowManagerService.this.dump();
7111 }
7112 mHasLocalTransformation = false;
7113 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007114 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 // When our app token is animating, we kind-of pretend like
7116 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7117 // part of this check means that we will only do this if
7118 // our window is not currently exiting, or it is not
7119 // locally animating itself. The idea being that one that
7120 // is exiting and doing a local animation should be removed
7121 // once that animation is done.
7122 mAnimating = true;
7123 mHasTransformation = true;
7124 mTransformation.clear();
7125 return false;
7126 } else if (mHasTransformation) {
7127 // Little trick to get through the path below to act like
7128 // we have finished an animation.
7129 mAnimating = true;
7130 } else if (isAnimating()) {
7131 mAnimating = true;
7132 }
7133 } else if (mAnimation != null) {
7134 // If the display is frozen, and there is a pending animation,
7135 // clear it and make sure we run the cleanup code.
7136 mAnimating = true;
7137 mLocalAnimating = true;
7138 mAnimation = null;
7139 }
Romain Guy06882f82009-06-10 13:36:04 -07007140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 if (!mAnimating && !mLocalAnimating) {
7142 return false;
7143 }
7144
7145 if (DEBUG_ANIM) Log.v(
7146 TAG, "Animation done in " + this + ": exiting=" + mExiting
7147 + ", reportedVisible="
7148 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 mAnimating = false;
7151 mLocalAnimating = false;
7152 mAnimation = null;
7153 mAnimLayer = mLayer;
7154 if (mIsImWindow) {
7155 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007156 } else if (mIsWallpaper) {
7157 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 }
7159 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7160 + " anim layer: " + mAnimLayer);
7161 mHasTransformation = false;
7162 mHasLocalTransformation = false;
7163 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7164 mTransformation.clear();
7165 if (mHasDrawn
7166 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7167 && mAppToken != null
7168 && mAppToken.firstWindowDrawn
7169 && mAppToken.startingData != null) {
7170 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7171 + mToken + ": first real window done animating");
7172 mFinishedStarting.add(mAppToken);
7173 mH.sendEmptyMessage(H.FINISHED_STARTING);
7174 }
Romain Guy06882f82009-06-10 13:36:04 -07007175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 finishExit();
7177
7178 if (mAppToken != null) {
7179 mAppToken.updateReportedVisibilityLocked();
7180 }
7181
7182 return false;
7183 }
7184
7185 void finishExit() {
7186 if (DEBUG_ANIM) Log.v(
7187 TAG, "finishExit in " + this
7188 + ": exiting=" + mExiting
7189 + " remove=" + mRemoveOnExit
7190 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 final int N = mChildWindows.size();
7193 for (int i=0; i<N; i++) {
7194 ((WindowState)mChildWindows.get(i)).finishExit();
7195 }
Romain Guy06882f82009-06-10 13:36:04 -07007196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 if (!mExiting) {
7198 return;
7199 }
Romain Guy06882f82009-06-10 13:36:04 -07007200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 if (isWindowAnimating()) {
7202 return;
7203 }
7204
7205 if (localLOGV) Log.v(
7206 TAG, "Exit animation finished in " + this
7207 + ": remove=" + mRemoveOnExit);
7208 if (mSurface != null) {
7209 mDestroySurface.add(this);
7210 mDestroying = true;
7211 if (SHOW_TRANSACTIONS) Log.i(
7212 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7213 try {
7214 mSurface.hide();
7215 } catch (RuntimeException e) {
7216 Log.w(TAG, "Error hiding surface in " + this, e);
7217 }
7218 mLastHidden = true;
7219 mKeyWaiter.releasePendingPointerLocked(mSession);
7220 }
7221 mExiting = false;
7222 if (mRemoveOnExit) {
7223 mPendingRemove.add(this);
7224 mRemoveOnExit = false;
7225 }
7226 }
Romain Guy06882f82009-06-10 13:36:04 -07007227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7229 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7230 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7231 if (dtdx < -.000001f || dtdx > .000001f) return false;
7232 if (dsdy < -.000001f || dsdy > .000001f) return false;
7233 return true;
7234 }
Romain Guy06882f82009-06-10 13:36:04 -07007235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 void computeShownFrameLocked() {
7237 final boolean selfTransformation = mHasLocalTransformation;
7238 Transformation attachedTransformation =
7239 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7240 ? mAttachedWindow.mTransformation : null;
7241 Transformation appTransformation =
7242 (mAppToken != null && mAppToken.hasTransformation)
7243 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007244
7245 // Wallpapers are animated based on the "real" window they
7246 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007247 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007248 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007249 if (mWallpaperTarget.mHasLocalTransformation) {
7250 attachedTransformation = mWallpaperTarget.mTransformation;
7251 }
7252 if (mWallpaperTarget.mAppToken != null &&
7253 mWallpaperTarget.mAppToken.hasTransformation) {
7254 appTransformation = mWallpaperTarget.mAppToken.transformation;
7255 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007256 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7257 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7258 }
7259 if (DEBUG_WALLPAPER && appTransformation != null) {
7260 Log.v(TAG, "WP target app xform: " + appTransformation);
7261 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007262 }
7263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 if (selfTransformation || attachedTransformation != null
7265 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007266 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007267 final Rect frame = mFrame;
7268 final float tmpFloats[] = mTmpFloats;
7269 final Matrix tmpMatrix = mTmpMatrix;
7270
7271 // Compute the desired transformation.
7272 tmpMatrix.setTranslate(frame.left, frame.top);
7273 if (selfTransformation) {
7274 tmpMatrix.preConcat(mTransformation.getMatrix());
7275 }
7276 if (attachedTransformation != null) {
7277 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7278 }
7279 if (appTransformation != null) {
7280 tmpMatrix.preConcat(appTransformation.getMatrix());
7281 }
7282
7283 // "convert" it into SurfaceFlinger's format
7284 // (a 2x2 matrix + an offset)
7285 // Here we must not transform the position of the surface
7286 // since it is already included in the transformation.
7287 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 tmpMatrix.getValues(tmpFloats);
7290 mDsDx = tmpFloats[Matrix.MSCALE_X];
7291 mDtDx = tmpFloats[Matrix.MSKEW_X];
7292 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7293 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007294 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7295 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 int w = frame.width();
7297 int h = frame.height();
7298 mShownFrame.set(x, y, x+w, y+h);
7299
7300 // Now set the alpha... but because our current hardware
7301 // can't do alpha transformation on a non-opaque surface,
7302 // turn it off if we are running an animation that is also
7303 // transforming since it is more important to have that
7304 // animation be smooth.
7305 mShownAlpha = mAlpha;
7306 if (!mLimitedAlphaCompositing
7307 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7308 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7309 && x == frame.left && y == frame.top))) {
7310 //Log.i(TAG, "Applying alpha transform");
7311 if (selfTransformation) {
7312 mShownAlpha *= mTransformation.getAlpha();
7313 }
7314 if (attachedTransformation != null) {
7315 mShownAlpha *= attachedTransformation.getAlpha();
7316 }
7317 if (appTransformation != null) {
7318 mShownAlpha *= appTransformation.getAlpha();
7319 }
7320 } else {
7321 //Log.i(TAG, "Not applying alpha transform");
7322 }
Romain Guy06882f82009-06-10 13:36:04 -07007323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 if (localLOGV) Log.v(
7325 TAG, "Continuing animation in " + this +
7326 ": " + mShownFrame +
7327 ", alpha=" + mTransformation.getAlpha());
7328 return;
7329 }
Romain Guy06882f82009-06-10 13:36:04 -07007330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007332 if (mXOffset != 0 || mYOffset != 0) {
7333 mShownFrame.offset(mXOffset, mYOffset);
7334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 mShownAlpha = mAlpha;
7336 mDsDx = 1;
7337 mDtDx = 0;
7338 mDsDy = 0;
7339 mDtDy = 1;
7340 }
Romain Guy06882f82009-06-10 13:36:04 -07007341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007342 /**
7343 * Is this window visible? It is not visible if there is no
7344 * surface, or we are in the process of running an exit animation
7345 * that will remove the surface, or its app token has been hidden.
7346 */
7347 public boolean isVisibleLw() {
7348 final AppWindowToken atoken = mAppToken;
7349 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7350 && (atoken == null || !atoken.hiddenRequested)
7351 && !mExiting && !mDestroying;
7352 }
7353
7354 /**
7355 * Is this window visible, ignoring its app token? It is not visible
7356 * if there is no surface, or we are in the process of running an exit animation
7357 * that will remove the surface.
7358 */
7359 public boolean isWinVisibleLw() {
7360 final AppWindowToken atoken = mAppToken;
7361 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7362 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7363 && !mExiting && !mDestroying;
7364 }
7365
7366 /**
7367 * The same as isVisible(), but follows the current hidden state of
7368 * the associated app token, not the pending requested hidden state.
7369 */
7370 boolean isVisibleNow() {
7371 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007372 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 }
7374
7375 /**
7376 * Same as isVisible(), but we also count it as visible between the
7377 * call to IWindowSession.add() and the first relayout().
7378 */
7379 boolean isVisibleOrAdding() {
7380 final AppWindowToken atoken = mAppToken;
7381 return (mSurface != null
7382 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7383 && mPolicyVisibility && !mAttachedHidden
7384 && (atoken == null || !atoken.hiddenRequested)
7385 && !mExiting && !mDestroying;
7386 }
7387
7388 /**
7389 * Is this window currently on-screen? It is on-screen either if it
7390 * is visible or it is currently running an animation before no longer
7391 * being visible.
7392 */
7393 boolean isOnScreen() {
7394 final AppWindowToken atoken = mAppToken;
7395 if (atoken != null) {
7396 return mSurface != null && mPolicyVisibility && !mDestroying
7397 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007398 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 } else {
7400 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007401 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 }
7403 }
Romain Guy06882f82009-06-10 13:36:04 -07007404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 /**
7406 * Like isOnScreen(), but we don't return true if the window is part
7407 * of a transition that has not yet been started.
7408 */
7409 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007410 if (mRootToken.waitingToShow &&
7411 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7412 return false;
7413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007415 final boolean animating = atoken != null
7416 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007418 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7419 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007420 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 }
7422
7423 /** Is the window or its container currently animating? */
7424 boolean isAnimating() {
7425 final WindowState attached = mAttachedWindow;
7426 final AppWindowToken atoken = mAppToken;
7427 return mAnimation != null
7428 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007429 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 (atoken.animation != null
7431 || atoken.inPendingTransaction));
7432 }
7433
7434 /** Is this window currently animating? */
7435 boolean isWindowAnimating() {
7436 return mAnimation != null;
7437 }
7438
7439 /**
7440 * Like isOnScreen, but returns false if the surface hasn't yet
7441 * been drawn.
7442 */
7443 public boolean isDisplayedLw() {
7444 final AppWindowToken atoken = mAppToken;
7445 return mSurface != null && mPolicyVisibility && !mDestroying
7446 && !mDrawPending && !mCommitDrawPending
7447 && ((!mAttachedHidden &&
7448 (atoken == null || !atoken.hiddenRequested))
7449 || mAnimating);
7450 }
7451
7452 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7453 boolean shownFrame, boolean onlyOpaque) {
7454 if (mSurface == null) {
7455 return false;
7456 }
7457 if (mAppToken != null && !mAppToken.appFullscreen) {
7458 return false;
7459 }
7460 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7461 return false;
7462 }
7463 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007464
7465 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7466 return frame.left <= mCompatibleScreenFrame.left &&
7467 frame.top <= mCompatibleScreenFrame.top &&
7468 frame.right >= mCompatibleScreenFrame.right &&
7469 frame.bottom >= mCompatibleScreenFrame.bottom;
7470 } else {
7471 return frame.left <= 0 && frame.top <= 0
7472 && frame.right >= screenWidth
7473 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 }
Romain Guy06882f82009-06-10 13:36:04 -07007476
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007477 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007478 * Return true if the window is opaque and fully drawn. This indicates
7479 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007480 */
7481 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007482 return (mAttrs.format == PixelFormat.OPAQUE
7483 || mAttrs.type == TYPE_WALLPAPER)
7484 && mSurface != null && mAnimation == null
7485 && (mAppToken == null || mAppToken.animation == null)
7486 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007487 }
7488
7489 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7490 return
7491 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007492 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7493 // only if it's visible
7494 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007495 // and only if the application fills the compatible screen
7496 mFrame.left <= mCompatibleScreenFrame.left &&
7497 mFrame.top <= mCompatibleScreenFrame.top &&
7498 mFrame.right >= mCompatibleScreenFrame.right &&
7499 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007500 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007501 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007502 }
7503
7504 boolean isFullscreen(int screenWidth, int screenHeight) {
7505 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007506 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 }
7508
7509 void removeLocked() {
7510 if (mAttachedWindow != null) {
7511 mAttachedWindow.mChildWindows.remove(this);
7512 }
7513 destroySurfaceLocked();
7514 mSession.windowRemovedLocked();
7515 try {
7516 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7517 } catch (RuntimeException e) {
7518 // Ignore if it has already been removed (usually because
7519 // we are doing this as part of processing a death note.)
7520 }
7521 }
7522
7523 private class DeathRecipient implements IBinder.DeathRecipient {
7524 public void binderDied() {
7525 try {
7526 synchronized(mWindowMap) {
7527 WindowState win = windowForClientLocked(mSession, mClient);
7528 Log.i(TAG, "WIN DEATH: " + win);
7529 if (win != null) {
7530 removeWindowLocked(mSession, win);
7531 }
7532 }
7533 } catch (IllegalArgumentException ex) {
7534 // This will happen if the window has already been
7535 // removed.
7536 }
7537 }
7538 }
7539
7540 /** Returns true if this window desires key events. */
7541 public final boolean canReceiveKeys() {
7542 return isVisibleOrAdding()
7543 && (mViewVisibility == View.VISIBLE)
7544 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7545 }
7546
7547 public boolean hasDrawnLw() {
7548 return mHasDrawn;
7549 }
7550
7551 public boolean showLw(boolean doAnimation) {
7552 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7553 mPolicyVisibility = true;
7554 mPolicyVisibilityAfterAnim = true;
7555 if (doAnimation) {
7556 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7557 }
7558 requestAnimationLocked(0);
7559 return true;
7560 }
7561 return false;
7562 }
7563
7564 public boolean hideLw(boolean doAnimation) {
7565 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7566 : mPolicyVisibility;
7567 if (current) {
7568 if (doAnimation) {
7569 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7570 if (mAnimation == null) {
7571 doAnimation = false;
7572 }
7573 }
7574 if (doAnimation) {
7575 mPolicyVisibilityAfterAnim = false;
7576 } else {
7577 mPolicyVisibilityAfterAnim = false;
7578 mPolicyVisibility = false;
7579 }
7580 requestAnimationLocked(0);
7581 return true;
7582 }
7583 return false;
7584 }
7585
7586 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007587 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007588
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007589 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7590 pw.print(" mClient="); pw.println(mClient.asBinder());
7591 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7592 if (mAttachedWindow != null || mLayoutAttached) {
7593 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7594 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7595 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007596 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7597 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7598 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007599 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7600 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007601 }
7602 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7603 pw.print(" mSubLayer="); pw.print(mSubLayer);
7604 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7605 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7606 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7607 pw.print("="); pw.print(mAnimLayer);
7608 pw.print(" mLastLayer="); pw.println(mLastLayer);
7609 if (mSurface != null) {
7610 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7611 }
7612 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7613 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7614 if (mAppToken != null) {
7615 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7616 }
7617 if (mTargetAppToken != null) {
7618 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7619 }
7620 pw.print(prefix); pw.print("mViewVisibility=0x");
7621 pw.print(Integer.toHexString(mViewVisibility));
7622 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007623 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7624 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007625 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7626 pw.print(prefix); pw.print("mPolicyVisibility=");
7627 pw.print(mPolicyVisibility);
7628 pw.print(" mPolicyVisibilityAfterAnim=");
7629 pw.print(mPolicyVisibilityAfterAnim);
7630 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7631 }
7632 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007633 pw.print(" h="); pw.println(mRequestedHeight);
7634 if (mXOffset != 0 || mYOffset != 0) {
7635 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7636 pw.print(" y="); pw.println(mYOffset);
7637 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007638 pw.print(prefix); pw.print("mGivenContentInsets=");
7639 mGivenContentInsets.printShortString(pw);
7640 pw.print(" mGivenVisibleInsets=");
7641 mGivenVisibleInsets.printShortString(pw);
7642 pw.println();
7643 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7644 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7645 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7646 }
7647 pw.print(prefix); pw.print("mShownFrame=");
7648 mShownFrame.printShortString(pw);
7649 pw.print(" last="); mLastShownFrame.printShortString(pw);
7650 pw.println();
7651 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7652 pw.print(" last="); mLastFrame.printShortString(pw);
7653 pw.println();
7654 pw.print(prefix); pw.print("mContainingFrame=");
7655 mContainingFrame.printShortString(pw);
7656 pw.print(" mDisplayFrame=");
7657 mDisplayFrame.printShortString(pw);
7658 pw.println();
7659 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7660 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7661 pw.println();
7662 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7663 pw.print(" last="); mLastContentInsets.printShortString(pw);
7664 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7665 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7666 pw.println();
7667 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7668 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7669 pw.print(" mAlpha="); pw.print(mAlpha);
7670 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7671 }
7672 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7673 || mAnimation != null) {
7674 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7675 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7676 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7677 pw.print(" mAnimation="); pw.println(mAnimation);
7678 }
7679 if (mHasTransformation || mHasLocalTransformation) {
7680 pw.print(prefix); pw.print("XForm: has=");
7681 pw.print(mHasTransformation);
7682 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7683 pw.print(" "); mTransformation.printShortString(pw);
7684 pw.println();
7685 }
7686 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7687 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7688 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7689 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7690 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7691 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7692 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7693 pw.print(" mDestroying="); pw.print(mDestroying);
7694 pw.print(" mRemoved="); pw.println(mRemoved);
7695 }
7696 if (mOrientationChanging || mAppFreezing) {
7697 pw.print(prefix); pw.print("mOrientationChanging=");
7698 pw.print(mOrientationChanging);
7699 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7700 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007701 if (mHScale != 1 || mVScale != 1) {
7702 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7703 pw.print(" mVScale="); pw.println(mVScale);
7704 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007705 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007706 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7707 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 }
7710
7711 @Override
7712 public String toString() {
7713 return "Window{"
7714 + Integer.toHexString(System.identityHashCode(this))
7715 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7716 }
7717 }
Romain Guy06882f82009-06-10 13:36:04 -07007718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 // -------------------------------------------------------------
7720 // Window Token State
7721 // -------------------------------------------------------------
7722
7723 class WindowToken {
7724 // The actual token.
7725 final IBinder token;
7726
7727 // The type of window this token is for, as per WindowManager.LayoutParams.
7728 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 // Set if this token was explicitly added by a client, so should
7731 // not be removed when all windows are removed.
7732 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007733
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007734 // For printing.
7735 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 // If this is an AppWindowToken, this is non-null.
7738 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 // All of the windows associated with this token.
7741 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7742
7743 // Is key dispatching paused for this token?
7744 boolean paused = false;
7745
7746 // Should this token's windows be hidden?
7747 boolean hidden;
7748
7749 // Temporary for finding which tokens no longer have visible windows.
7750 boolean hasVisible;
7751
Dianne Hackborna8f60182009-09-01 19:01:50 -07007752 // Set to true when this token is in a pending transaction where it
7753 // will be shown.
7754 boolean waitingToShow;
7755
7756 // Set to true when this token is in a pending transaction where it
7757 // will be hidden.
7758 boolean waitingToHide;
7759
7760 // Set to true when this token is in a pending transaction where its
7761 // windows will be put to the bottom of the list.
7762 boolean sendingToBottom;
7763
7764 // Set to true when this token is in a pending transaction where its
7765 // windows will be put to the top of the list.
7766 boolean sendingToTop;
7767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 WindowToken(IBinder _token, int type, boolean _explicit) {
7769 token = _token;
7770 windowType = type;
7771 explicit = _explicit;
7772 }
7773
7774 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007775 pw.print(prefix); pw.print("token="); pw.println(token);
7776 pw.print(prefix); pw.print("windows="); pw.println(windows);
7777 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7778 pw.print(" hidden="); pw.print(hidden);
7779 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007780 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7781 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7782 pw.print(" waitingToHide="); pw.print(waitingToHide);
7783 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7784 pw.print(" sendingToTop="); pw.println(sendingToTop);
7785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 }
7787
7788 @Override
7789 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007790 if (stringName == null) {
7791 StringBuilder sb = new StringBuilder();
7792 sb.append("WindowToken{");
7793 sb.append(Integer.toHexString(System.identityHashCode(this)));
7794 sb.append(" token="); sb.append(token); sb.append('}');
7795 stringName = sb.toString();
7796 }
7797 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 }
7799 };
7800
7801 class AppWindowToken extends WindowToken {
7802 // Non-null only for application tokens.
7803 final IApplicationToken appToken;
7804
7805 // All of the windows and child windows that are included in this
7806 // application token. Note this list is NOT sorted!
7807 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7808
7809 int groupId = -1;
7810 boolean appFullscreen;
7811 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 // These are used for determining when all windows associated with
7814 // an activity have been drawn, so they can be made visible together
7815 // at the same time.
7816 int lastTransactionSequence = mTransactionSequence-1;
7817 int numInterestingWindows;
7818 int numDrawnWindows;
7819 boolean inPendingTransaction;
7820 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 // Is this token going to be hidden in a little while? If so, it
7823 // won't be taken into account for setting the screen orientation.
7824 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 // Is this window's surface needed? This is almost like hidden, except
7827 // it will sometimes be true a little earlier: when the token has
7828 // been shown, but is still waiting for its app transition to execute
7829 // before making its windows shown.
7830 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 // Have we told the window clients to hide themselves?
7833 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 // Last visibility state we reported to the app token.
7836 boolean reportedVisible;
7837
7838 // Set to true when the token has been removed from the window mgr.
7839 boolean removed;
7840
7841 // Have we been asked to have this token keep the screen frozen?
7842 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 boolean animating;
7845 Animation animation;
7846 boolean hasTransformation;
7847 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 // Offset to the window of all layers in the token, for use by
7850 // AppWindowToken animations.
7851 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 // Information about an application starting window if displayed.
7854 StartingData startingData;
7855 WindowState startingWindow;
7856 View startingView;
7857 boolean startingDisplayed;
7858 boolean startingMoved;
7859 boolean firstWindowDrawn;
7860
7861 AppWindowToken(IApplicationToken _token) {
7862 super(_token.asBinder(),
7863 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7864 appWindowToken = this;
7865 appToken = _token;
7866 }
Romain Guy06882f82009-06-10 13:36:04 -07007867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 public void setAnimation(Animation anim) {
7869 if (localLOGV) Log.v(
7870 TAG, "Setting animation in " + this + ": " + anim);
7871 animation = anim;
7872 animating = false;
7873 anim.restrictDuration(MAX_ANIMATION_DURATION);
7874 anim.scaleCurrentDuration(mTransitionAnimationScale);
7875 int zorder = anim.getZAdjustment();
7876 int adj = 0;
7877 if (zorder == Animation.ZORDER_TOP) {
7878 adj = TYPE_LAYER_OFFSET;
7879 } else if (zorder == Animation.ZORDER_BOTTOM) {
7880 adj = -TYPE_LAYER_OFFSET;
7881 }
Romain Guy06882f82009-06-10 13:36:04 -07007882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 if (animLayerAdjustment != adj) {
7884 animLayerAdjustment = adj;
7885 updateLayers();
7886 }
7887 }
Romain Guy06882f82009-06-10 13:36:04 -07007888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 public void setDummyAnimation() {
7890 if (animation == null) {
7891 if (localLOGV) Log.v(
7892 TAG, "Setting dummy animation in " + this);
7893 animation = sDummyAnimation;
7894 }
7895 }
7896
7897 public void clearAnimation() {
7898 if (animation != null) {
7899 animation = null;
7900 animating = true;
7901 }
7902 }
Romain Guy06882f82009-06-10 13:36:04 -07007903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 void updateLayers() {
7905 final int N = allAppWindows.size();
7906 final int adj = animLayerAdjustment;
7907 for (int i=0; i<N; i++) {
7908 WindowState w = allAppWindows.get(i);
7909 w.mAnimLayer = w.mLayer + adj;
7910 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7911 + w.mAnimLayer);
7912 if (w == mInputMethodTarget) {
7913 setInputMethodAnimLayerAdjustment(adj);
7914 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007915 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007916 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 }
7919 }
Romain Guy06882f82009-06-10 13:36:04 -07007920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 void sendAppVisibilityToClients() {
7922 final int N = allAppWindows.size();
7923 for (int i=0; i<N; i++) {
7924 WindowState win = allAppWindows.get(i);
7925 if (win == startingWindow && clientHidden) {
7926 // Don't hide the starting window.
7927 continue;
7928 }
7929 try {
7930 if (DEBUG_VISIBILITY) Log.v(TAG,
7931 "Setting visibility of " + win + ": " + (!clientHidden));
7932 win.mClient.dispatchAppVisibility(!clientHidden);
7933 } catch (RemoteException e) {
7934 }
7935 }
7936 }
Romain Guy06882f82009-06-10 13:36:04 -07007937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 void showAllWindowsLocked() {
7939 final int NW = allAppWindows.size();
7940 for (int i=0; i<NW; i++) {
7941 WindowState w = allAppWindows.get(i);
7942 if (DEBUG_VISIBILITY) Log.v(TAG,
7943 "performing show on: " + w);
7944 w.performShowLocked();
7945 }
7946 }
Romain Guy06882f82009-06-10 13:36:04 -07007947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 // This must be called while inside a transaction.
7949 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7950 if (!mDisplayFrozen) {
7951 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 if (animation == sDummyAnimation) {
7954 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007955 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 // when it is really time to animate, this will be set to
7957 // a real animation and the next call will execute normally.
7958 return false;
7959 }
Romain Guy06882f82009-06-10 13:36:04 -07007960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7962 if (!animating) {
7963 if (DEBUG_ANIM) Log.v(
7964 TAG, "Starting animation in " + this +
7965 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7966 + " scale=" + mTransitionAnimationScale
7967 + " allDrawn=" + allDrawn + " animating=" + animating);
7968 animation.initialize(dw, dh, dw, dh);
7969 animation.setStartTime(currentTime);
7970 animating = true;
7971 }
7972 transformation.clear();
7973 final boolean more = animation.getTransformation(
7974 currentTime, transformation);
7975 if (DEBUG_ANIM) Log.v(
7976 TAG, "Stepped animation in " + this +
7977 ": more=" + more + ", xform=" + transformation);
7978 if (more) {
7979 // we're done!
7980 hasTransformation = true;
7981 return true;
7982 }
7983 if (DEBUG_ANIM) Log.v(
7984 TAG, "Finished animation in " + this +
7985 " @ " + currentTime);
7986 animation = null;
7987 }
7988 } else if (animation != null) {
7989 // If the display is frozen, and there is a pending animation,
7990 // clear it and make sure we run the cleanup code.
7991 animating = true;
7992 animation = null;
7993 }
7994
7995 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 if (!animating) {
7998 return false;
7999 }
8000
8001 clearAnimation();
8002 animating = false;
8003 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8004 moveInputMethodWindowsIfNeededLocked(true);
8005 }
Romain Guy06882f82009-06-10 13:36:04 -07008006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 if (DEBUG_ANIM) Log.v(
8008 TAG, "Animation done in " + this
8009 + ": reportedVisible=" + reportedVisible);
8010
8011 transformation.clear();
8012 if (animLayerAdjustment != 0) {
8013 animLayerAdjustment = 0;
8014 updateLayers();
8015 }
Romain Guy06882f82009-06-10 13:36:04 -07008016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 final int N = windows.size();
8018 for (int i=0; i<N; i++) {
8019 ((WindowState)windows.get(i)).finishExit();
8020 }
8021 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 return false;
8024 }
8025
8026 void updateReportedVisibilityLocked() {
8027 if (appToken == null) {
8028 return;
8029 }
Romain Guy06882f82009-06-10 13:36:04 -07008030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 int numInteresting = 0;
8032 int numVisible = 0;
8033 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008035 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
8036 final int N = allAppWindows.size();
8037 for (int i=0; i<N; i++) {
8038 WindowState win = allAppWindows.get(i);
8039 if (win == startingWindow || win.mAppFreezing) {
8040 continue;
8041 }
8042 if (DEBUG_VISIBILITY) {
8043 Log.v(TAG, "Win " + win + ": isDisplayed="
8044 + win.isDisplayedLw()
8045 + ", isAnimating=" + win.isAnimating());
8046 if (!win.isDisplayedLw()) {
8047 Log.v(TAG, "Not displayed: s=" + win.mSurface
8048 + " pv=" + win.mPolicyVisibility
8049 + " dp=" + win.mDrawPending
8050 + " cdp=" + win.mCommitDrawPending
8051 + " ah=" + win.mAttachedHidden
8052 + " th="
8053 + (win.mAppToken != null
8054 ? win.mAppToken.hiddenRequested : false)
8055 + " a=" + win.mAnimating);
8056 }
8057 }
8058 numInteresting++;
8059 if (win.isDisplayedLw()) {
8060 if (!win.isAnimating()) {
8061 numVisible++;
8062 }
8063 nowGone = false;
8064 } else if (win.isAnimating()) {
8065 nowGone = false;
8066 }
8067 }
Romain Guy06882f82009-06-10 13:36:04 -07008068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8070 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8071 + numInteresting + " visible=" + numVisible);
8072 if (nowVisible != reportedVisible) {
8073 if (DEBUG_VISIBILITY) Log.v(
8074 TAG, "Visibility changed in " + this
8075 + ": vis=" + nowVisible);
8076 reportedVisible = nowVisible;
8077 Message m = mH.obtainMessage(
8078 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8079 nowVisible ? 1 : 0,
8080 nowGone ? 1 : 0,
8081 this);
8082 mH.sendMessage(m);
8083 }
8084 }
Romain Guy06882f82009-06-10 13:36:04 -07008085
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008086 WindowState findMainWindow() {
8087 int j = windows.size();
8088 while (j > 0) {
8089 j--;
8090 WindowState win = windows.get(j);
8091 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8092 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8093 return win;
8094 }
8095 }
8096 return null;
8097 }
8098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 void dump(PrintWriter pw, String prefix) {
8100 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008101 if (appToken != null) {
8102 pw.print(prefix); pw.println("app=true");
8103 }
8104 if (allAppWindows.size() > 0) {
8105 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8106 }
8107 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008108 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008109 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8110 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8111 pw.print(" clientHidden="); pw.print(clientHidden);
8112 pw.print(" willBeHidden="); pw.print(willBeHidden);
8113 pw.print(" reportedVisible="); pw.println(reportedVisible);
8114 if (paused || freezingScreen) {
8115 pw.print(prefix); pw.print("paused="); pw.print(paused);
8116 pw.print(" freezingScreen="); pw.println(freezingScreen);
8117 }
8118 if (numInterestingWindows != 0 || numDrawnWindows != 0
8119 || inPendingTransaction || allDrawn) {
8120 pw.print(prefix); pw.print("numInterestingWindows=");
8121 pw.print(numInterestingWindows);
8122 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8123 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8124 pw.print(" allDrawn="); pw.println(allDrawn);
8125 }
8126 if (animating || animation != null) {
8127 pw.print(prefix); pw.print("animating="); pw.print(animating);
8128 pw.print(" animation="); pw.println(animation);
8129 }
8130 if (animLayerAdjustment != 0) {
8131 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8132 }
8133 if (hasTransformation) {
8134 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8135 pw.print(" transformation="); transformation.printShortString(pw);
8136 pw.println();
8137 }
8138 if (startingData != null || removed || firstWindowDrawn) {
8139 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8140 pw.print(" removed="); pw.print(removed);
8141 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8142 }
8143 if (startingWindow != null || startingView != null
8144 || startingDisplayed || startingMoved) {
8145 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8146 pw.print(" startingView="); pw.print(startingView);
8147 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8148 pw.print(" startingMoved"); pw.println(startingMoved);
8149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 }
8151
8152 @Override
8153 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008154 if (stringName == null) {
8155 StringBuilder sb = new StringBuilder();
8156 sb.append("AppWindowToken{");
8157 sb.append(Integer.toHexString(System.identityHashCode(this)));
8158 sb.append(" token="); sb.append(token); sb.append('}');
8159 stringName = sb.toString();
8160 }
8161 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 }
8163 }
Romain Guy06882f82009-06-10 13:36:04 -07008164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 // -------------------------------------------------------------
8166 // DummyAnimation
8167 // -------------------------------------------------------------
8168
8169 // This is an animation that does nothing: it just immediately finishes
8170 // itself every time it is called. It is used as a stub animation in cases
8171 // where we want to synchronize multiple things that may be animating.
8172 static final class DummyAnimation extends Animation {
8173 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8174 return false;
8175 }
8176 }
8177 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 // -------------------------------------------------------------
8180 // Async Handler
8181 // -------------------------------------------------------------
8182
8183 static final class StartingData {
8184 final String pkg;
8185 final int theme;
8186 final CharSequence nonLocalizedLabel;
8187 final int labelRes;
8188 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8191 int _labelRes, int _icon) {
8192 pkg = _pkg;
8193 theme = _theme;
8194 nonLocalizedLabel = _nonLocalizedLabel;
8195 labelRes = _labelRes;
8196 icon = _icon;
8197 }
8198 }
8199
8200 private final class H extends Handler {
8201 public static final int REPORT_FOCUS_CHANGE = 2;
8202 public static final int REPORT_LOSING_FOCUS = 3;
8203 public static final int ANIMATE = 4;
8204 public static final int ADD_STARTING = 5;
8205 public static final int REMOVE_STARTING = 6;
8206 public static final int FINISHED_STARTING = 7;
8207 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008208 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8209 public static final int HOLD_SCREEN_CHANGED = 12;
8210 public static final int APP_TRANSITION_TIMEOUT = 13;
8211 public static final int PERSIST_ANIMATION_SCALE = 14;
8212 public static final int FORCE_GC = 15;
8213 public static final int ENABLE_SCREEN = 16;
8214 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008215 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 public H() {
8220 }
Romain Guy06882f82009-06-10 13:36:04 -07008221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 @Override
8223 public void handleMessage(Message msg) {
8224 switch (msg.what) {
8225 case REPORT_FOCUS_CHANGE: {
8226 WindowState lastFocus;
8227 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008229 synchronized(mWindowMap) {
8230 lastFocus = mLastFocus;
8231 newFocus = mCurrentFocus;
8232 if (lastFocus == newFocus) {
8233 // Focus is not changing, so nothing to do.
8234 return;
8235 }
8236 mLastFocus = newFocus;
8237 //Log.i(TAG, "Focus moving from " + lastFocus
8238 // + " to " + newFocus);
8239 if (newFocus != null && lastFocus != null
8240 && !newFocus.isDisplayedLw()) {
8241 //Log.i(TAG, "Delaying loss of focus...");
8242 mLosingFocus.add(lastFocus);
8243 lastFocus = null;
8244 }
8245 }
8246
8247 if (lastFocus != newFocus) {
8248 //System.out.println("Changing focus from " + lastFocus
8249 // + " to " + newFocus);
8250 if (newFocus != null) {
8251 try {
8252 //Log.i(TAG, "Gaining focus: " + newFocus);
8253 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8254 } catch (RemoteException e) {
8255 // Ignore if process has died.
8256 }
8257 }
8258
8259 if (lastFocus != null) {
8260 try {
8261 //Log.i(TAG, "Losing focus: " + lastFocus);
8262 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8263 } catch (RemoteException e) {
8264 // Ignore if process has died.
8265 }
8266 }
8267 }
8268 } break;
8269
8270 case REPORT_LOSING_FOCUS: {
8271 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 synchronized(mWindowMap) {
8274 losers = mLosingFocus;
8275 mLosingFocus = new ArrayList<WindowState>();
8276 }
8277
8278 final int N = losers.size();
8279 for (int i=0; i<N; i++) {
8280 try {
8281 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8282 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8283 } catch (RemoteException e) {
8284 // Ignore if process has died.
8285 }
8286 }
8287 } break;
8288
8289 case ANIMATE: {
8290 synchronized(mWindowMap) {
8291 mAnimationPending = false;
8292 performLayoutAndPlaceSurfacesLocked();
8293 }
8294 } break;
8295
8296 case ADD_STARTING: {
8297 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8298 final StartingData sd = wtoken.startingData;
8299
8300 if (sd == null) {
8301 // Animation has been canceled... do nothing.
8302 return;
8303 }
Romain Guy06882f82009-06-10 13:36:04 -07008304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8306 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 View view = null;
8309 try {
8310 view = mPolicy.addStartingWindow(
8311 wtoken.token, sd.pkg,
8312 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8313 sd.icon);
8314 } catch (Exception e) {
8315 Log.w(TAG, "Exception when adding starting window", e);
8316 }
8317
8318 if (view != null) {
8319 boolean abort = false;
8320
8321 synchronized(mWindowMap) {
8322 if (wtoken.removed || wtoken.startingData == null) {
8323 // If the window was successfully added, then
8324 // we need to remove it.
8325 if (wtoken.startingWindow != null) {
8326 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8327 "Aborted starting " + wtoken
8328 + ": removed=" + wtoken.removed
8329 + " startingData=" + wtoken.startingData);
8330 wtoken.startingWindow = null;
8331 wtoken.startingData = null;
8332 abort = true;
8333 }
8334 } else {
8335 wtoken.startingView = view;
8336 }
8337 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8338 "Added starting " + wtoken
8339 + ": startingWindow="
8340 + wtoken.startingWindow + " startingView="
8341 + wtoken.startingView);
8342 }
8343
8344 if (abort) {
8345 try {
8346 mPolicy.removeStartingWindow(wtoken.token, view);
8347 } catch (Exception e) {
8348 Log.w(TAG, "Exception when removing starting window", e);
8349 }
8350 }
8351 }
8352 } break;
8353
8354 case REMOVE_STARTING: {
8355 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8356 IBinder token = null;
8357 View view = null;
8358 synchronized (mWindowMap) {
8359 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8360 + wtoken + ": startingWindow="
8361 + wtoken.startingWindow + " startingView="
8362 + wtoken.startingView);
8363 if (wtoken.startingWindow != null) {
8364 view = wtoken.startingView;
8365 token = wtoken.token;
8366 wtoken.startingData = null;
8367 wtoken.startingView = null;
8368 wtoken.startingWindow = null;
8369 }
8370 }
8371 if (view != null) {
8372 try {
8373 mPolicy.removeStartingWindow(token, view);
8374 } catch (Exception e) {
8375 Log.w(TAG, "Exception when removing starting window", e);
8376 }
8377 }
8378 } break;
8379
8380 case FINISHED_STARTING: {
8381 IBinder token = null;
8382 View view = null;
8383 while (true) {
8384 synchronized (mWindowMap) {
8385 final int N = mFinishedStarting.size();
8386 if (N <= 0) {
8387 break;
8388 }
8389 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8390
8391 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8392 "Finished starting " + wtoken
8393 + ": startingWindow=" + wtoken.startingWindow
8394 + " startingView=" + wtoken.startingView);
8395
8396 if (wtoken.startingWindow == null) {
8397 continue;
8398 }
8399
8400 view = wtoken.startingView;
8401 token = wtoken.token;
8402 wtoken.startingData = null;
8403 wtoken.startingView = null;
8404 wtoken.startingWindow = null;
8405 }
8406
8407 try {
8408 mPolicy.removeStartingWindow(token, view);
8409 } catch (Exception e) {
8410 Log.w(TAG, "Exception when removing starting window", e);
8411 }
8412 }
8413 } break;
8414
8415 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8416 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8417
8418 boolean nowVisible = msg.arg1 != 0;
8419 boolean nowGone = msg.arg2 != 0;
8420
8421 try {
8422 if (DEBUG_VISIBILITY) Log.v(
8423 TAG, "Reporting visible in " + wtoken
8424 + " visible=" + nowVisible
8425 + " gone=" + nowGone);
8426 if (nowVisible) {
8427 wtoken.appToken.windowsVisible();
8428 } else {
8429 wtoken.appToken.windowsGone();
8430 }
8431 } catch (RemoteException ex) {
8432 }
8433 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 case WINDOW_FREEZE_TIMEOUT: {
8436 synchronized (mWindowMap) {
8437 Log.w(TAG, "Window freeze timeout expired.");
8438 int i = mWindows.size();
8439 while (i > 0) {
8440 i--;
8441 WindowState w = (WindowState)mWindows.get(i);
8442 if (w.mOrientationChanging) {
8443 w.mOrientationChanging = false;
8444 Log.w(TAG, "Force clearing orientation change: " + w);
8445 }
8446 }
8447 performLayoutAndPlaceSurfacesLocked();
8448 }
8449 break;
8450 }
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 case HOLD_SCREEN_CHANGED: {
8453 Session oldHold;
8454 Session newHold;
8455 synchronized (mWindowMap) {
8456 oldHold = mLastReportedHold;
8457 newHold = (Session)msg.obj;
8458 mLastReportedHold = newHold;
8459 }
Romain Guy06882f82009-06-10 13:36:04 -07008460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 if (oldHold != newHold) {
8462 try {
8463 if (oldHold != null) {
8464 mBatteryStats.noteStopWakelock(oldHold.mUid,
8465 "window",
8466 BatteryStats.WAKE_TYPE_WINDOW);
8467 }
8468 if (newHold != null) {
8469 mBatteryStats.noteStartWakelock(newHold.mUid,
8470 "window",
8471 BatteryStats.WAKE_TYPE_WINDOW);
8472 }
8473 } catch (RemoteException e) {
8474 }
8475 }
8476 break;
8477 }
Romain Guy06882f82009-06-10 13:36:04 -07008478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 case APP_TRANSITION_TIMEOUT: {
8480 synchronized (mWindowMap) {
8481 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8482 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8483 "*** APP TRANSITION TIMEOUT");
8484 mAppTransitionReady = true;
8485 mAppTransitionTimeout = true;
8486 performLayoutAndPlaceSurfacesLocked();
8487 }
8488 }
8489 break;
8490 }
Romain Guy06882f82009-06-10 13:36:04 -07008491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 case PERSIST_ANIMATION_SCALE: {
8493 Settings.System.putFloat(mContext.getContentResolver(),
8494 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8495 Settings.System.putFloat(mContext.getContentResolver(),
8496 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8497 break;
8498 }
Romain Guy06882f82009-06-10 13:36:04 -07008499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 case FORCE_GC: {
8501 synchronized(mWindowMap) {
8502 if (mAnimationPending) {
8503 // If we are animating, don't do the gc now but
8504 // delay a bit so we don't interrupt the animation.
8505 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8506 2000);
8507 return;
8508 }
8509 // If we are currently rotating the display, it will
8510 // schedule a new message when done.
8511 if (mDisplayFrozen) {
8512 return;
8513 }
8514 mFreezeGcPending = 0;
8515 }
8516 Runtime.getRuntime().gc();
8517 break;
8518 }
Romain Guy06882f82009-06-10 13:36:04 -07008519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 case ENABLE_SCREEN: {
8521 performEnableScreen();
8522 break;
8523 }
Romain Guy06882f82009-06-10 13:36:04 -07008524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 case APP_FREEZE_TIMEOUT: {
8526 synchronized (mWindowMap) {
8527 Log.w(TAG, "App freeze timeout expired.");
8528 int i = mAppTokens.size();
8529 while (i > 0) {
8530 i--;
8531 AppWindowToken tok = mAppTokens.get(i);
8532 if (tok.freezingScreen) {
8533 Log.w(TAG, "Force clearing freeze: " + tok);
8534 unsetAppFreezingScreenLocked(tok, true, true);
8535 }
8536 }
8537 }
8538 break;
8539 }
Romain Guy06882f82009-06-10 13:36:04 -07008540
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008541 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008542 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008543 sendNewConfiguration();
8544 }
8545 break;
8546 }
Romain Guy06882f82009-06-10 13:36:04 -07008547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 }
8549 }
8550 }
8551
8552 // -------------------------------------------------------------
8553 // IWindowManager API
8554 // -------------------------------------------------------------
8555
8556 public IWindowSession openSession(IInputMethodClient client,
8557 IInputContext inputContext) {
8558 if (client == null) throw new IllegalArgumentException("null client");
8559 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8560 return new Session(client, inputContext);
8561 }
8562
8563 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8564 synchronized (mWindowMap) {
8565 // The focus for the client is the window immediately below
8566 // where we would place the input method window.
8567 int idx = findDesiredInputMethodWindowIndexLocked(false);
8568 WindowState imFocus;
8569 if (idx > 0) {
8570 imFocus = (WindowState)mWindows.get(idx-1);
8571 if (imFocus != null) {
8572 if (imFocus.mSession.mClient != null &&
8573 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8574 return true;
8575 }
8576 }
8577 }
8578 }
8579 return false;
8580 }
Romain Guy06882f82009-06-10 13:36:04 -07008581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 // -------------------------------------------------------------
8583 // Internals
8584 // -------------------------------------------------------------
8585
8586 final WindowState windowForClientLocked(Session session, IWindow client) {
8587 return windowForClientLocked(session, client.asBinder());
8588 }
Romain Guy06882f82009-06-10 13:36:04 -07008589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 final WindowState windowForClientLocked(Session session, IBinder client) {
8591 WindowState win = mWindowMap.get(client);
8592 if (localLOGV) Log.v(
8593 TAG, "Looking up client " + client + ": " + win);
8594 if (win == null) {
8595 RuntimeException ex = new RuntimeException();
8596 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8597 return null;
8598 }
8599 if (session != null && win.mSession != session) {
8600 RuntimeException ex = new RuntimeException();
8601 Log.w(TAG, "Requested window " + client + " is in session " +
8602 win.mSession + ", not " + session, ex);
8603 return null;
8604 }
8605
8606 return win;
8607 }
8608
Dianne Hackborna8f60182009-09-01 19:01:50 -07008609 final void rebuildAppWindowListLocked() {
8610 int NW = mWindows.size();
8611 int i;
8612
8613 // First remove all existing app windows.
8614 i=0;
8615 while (i < NW) {
8616 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008617 WindowState win = (WindowState)mWindows.remove(i);
8618 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8619 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008620 NW--;
8621 continue;
8622 }
8623 i++;
8624 }
8625
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008626 // First add all of the exiting app tokens... these are no longer
8627 // in the main app list, but still have windows shown. We put them
8628 // in the back because now that the animation is over we no longer
8629 // will care about them.
8630 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008631 i = 0;
8632 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008633 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8634 }
8635
8636 // And add in the still active app tokens in Z order.
8637 NT = mAppTokens.size();
8638 for (int j=0; j<NT; j++) {
8639 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008640 }
8641 }
8642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 private final void assignLayersLocked() {
8644 int N = mWindows.size();
8645 int curBaseLayer = 0;
8646 int curLayer = 0;
8647 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649 for (i=0; i<N; i++) {
8650 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008651 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8652 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 curLayer += WINDOW_LAYER_MULTIPLIER;
8654 w.mLayer = curLayer;
8655 } else {
8656 curBaseLayer = curLayer = w.mBaseLayer;
8657 w.mLayer = curLayer;
8658 }
8659 if (w.mTargetAppToken != null) {
8660 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8661 } else if (w.mAppToken != null) {
8662 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8663 } else {
8664 w.mAnimLayer = w.mLayer;
8665 }
8666 if (w.mIsImWindow) {
8667 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008668 } else if (w.mIsWallpaper) {
8669 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 }
8671 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8672 + w.mAnimLayer);
8673 //System.out.println(
8674 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8675 }
8676 }
8677
8678 private boolean mInLayout = false;
8679 private final void performLayoutAndPlaceSurfacesLocked() {
8680 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008681 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 throw new RuntimeException("Recursive call!");
8683 }
8684 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8685 return;
8686 }
8687
8688 boolean recoveringMemory = false;
8689 if (mForceRemoves != null) {
8690 recoveringMemory = true;
8691 // Wait a little it for things to settle down, and off we go.
8692 for (int i=0; i<mForceRemoves.size(); i++) {
8693 WindowState ws = mForceRemoves.get(i);
8694 Log.i(TAG, "Force removing: " + ws);
8695 removeWindowInnerLocked(ws.mSession, ws);
8696 }
8697 mForceRemoves = null;
8698 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8699 Object tmp = new Object();
8700 synchronized (tmp) {
8701 try {
8702 tmp.wait(250);
8703 } catch (InterruptedException e) {
8704 }
8705 }
8706 }
Romain Guy06882f82009-06-10 13:36:04 -07008707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 mInLayout = true;
8709 try {
8710 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712 int i = mPendingRemove.size()-1;
8713 if (i >= 0) {
8714 while (i >= 0) {
8715 WindowState w = mPendingRemove.get(i);
8716 removeWindowInnerLocked(w.mSession, w);
8717 i--;
8718 }
8719 mPendingRemove.clear();
8720
8721 mInLayout = false;
8722 assignLayersLocked();
8723 mLayoutNeeded = true;
8724 performLayoutAndPlaceSurfacesLocked();
8725
8726 } else {
8727 mInLayout = false;
8728 if (mLayoutNeeded) {
8729 requestAnimationLocked(0);
8730 }
8731 }
8732 } catch (RuntimeException e) {
8733 mInLayout = false;
8734 Log.e(TAG, "Unhandled exception while layout out windows", e);
8735 }
8736 }
8737
8738 private final void performLayoutLockedInner() {
8739 final int dw = mDisplay.getWidth();
8740 final int dh = mDisplay.getHeight();
8741
8742 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008743 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 int i;
8745
8746 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008747
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008748 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 mPolicy.beginLayoutLw(dw, dh);
8750
8751 // First perform layout of any root windows (not attached
8752 // to another window).
8753 int topAttached = -1;
8754 for (i = N-1; i >= 0; i--) {
8755 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008756
8757 // Don't do layout of a window if it is not visible, or
8758 // soon won't be visible, to avoid wasting time and funky
8759 // changes while a window is animating away.
8760 final AppWindowToken atoken = win.mAppToken;
8761 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008763 || win.mRootToken.hidden
8764 || (atoken != null && atoken.hiddenRequested)
8765 || !win.mPolicyVisibility
8766 || win.mAttachedHidden
8767 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768
8769 // If this view is GONE, then skip it -- keep the current
8770 // frame, and let the caller know so they can ignore it
8771 // if they want. (We do the normal layout for INVISIBLE
8772 // windows, since that means "perform layout as normal,
8773 // just don't display").
8774 if (!gone || !win.mHaveFrame) {
8775 if (!win.mLayoutAttached) {
8776 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8777 } else {
8778 if (topAttached < 0) topAttached = i;
8779 }
8780 }
8781 }
Romain Guy06882f82009-06-10 13:36:04 -07008782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008783 // Now perform layout of attached windows, which usually
8784 // depend on the position of the window they are attached to.
8785 // XXX does not deal with windows that are attached to windows
8786 // that are themselves attached.
8787 for (i = topAttached; i >= 0; i--) {
8788 WindowState win = (WindowState) mWindows.get(i);
8789
8790 // If this view is GONE, then skip it -- keep the current
8791 // frame, and let the caller know so they can ignore it
8792 // if they want. (We do the normal layout for INVISIBLE
8793 // windows, since that means "perform layout as normal,
8794 // just don't display").
8795 if (win.mLayoutAttached) {
8796 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8797 || !win.mHaveFrame) {
8798 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8799 }
8800 }
8801 }
8802
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008803 if (!mPolicy.finishLayoutLw()) {
8804 mLayoutNeeded = false;
8805 } else if (repeats > 2) {
8806 Log.w(TAG, "Layout repeat aborted after too many iterations");
8807 mLayoutNeeded = false;
8808 } else {
8809 repeats++;
8810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 }
8812 }
Romain Guy06882f82009-06-10 13:36:04 -07008813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 private final void performLayoutAndPlaceSurfacesLockedInner(
8815 boolean recoveringMemory) {
8816 final long currentTime = SystemClock.uptimeMillis();
8817 final int dw = mDisplay.getWidth();
8818 final int dh = mDisplay.getHeight();
8819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 int i;
8821
8822 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 if (mFxSession == null) {
8826 mFxSession = new SurfaceSession();
8827 }
Romain Guy06882f82009-06-10 13:36:04 -07008828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008829 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8830
8831 // Initialize state of exiting tokens.
8832 for (i=mExitingTokens.size()-1; i>=0; i--) {
8833 mExitingTokens.get(i).hasVisible = false;
8834 }
8835
8836 // Initialize state of exiting applications.
8837 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8838 mExitingAppTokens.get(i).hasVisible = false;
8839 }
8840
8841 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 boolean orientationChangeComplete = true;
8843 Session holdScreen = null;
8844 float screenBrightness = -1;
8845 boolean focusDisplayed = false;
8846 boolean animating = false;
8847
8848 Surface.openTransaction();
8849 try {
8850 boolean restart;
8851
8852 do {
8853 final int transactionSequence = ++mTransactionSequence;
8854
8855 // Update animations of all applications, including those
8856 // associated with exiting/removed apps
8857 boolean tokensAnimating = false;
8858 final int NAT = mAppTokens.size();
8859 for (i=0; i<NAT; i++) {
8860 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8861 tokensAnimating = true;
8862 }
8863 }
8864 final int NEAT = mExitingAppTokens.size();
8865 for (i=0; i<NEAT; i++) {
8866 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8867 tokensAnimating = true;
8868 }
8869 }
8870
8871 animating = tokensAnimating;
8872 restart = false;
8873
8874 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008875 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876
8877 mPolicy.beginAnimationLw(dw, dh);
8878
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008879 final int N = mWindows.size();
8880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 for (i=N-1; i>=0; i--) {
8882 WindowState w = (WindowState)mWindows.get(i);
8883
8884 final WindowManager.LayoutParams attrs = w.mAttrs;
8885
8886 if (w.mSurface != null) {
8887 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008888 if (w.commitFinishDrawingLocked(currentTime)) {
8889 if ((w.mAttrs.flags
8890 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008891 if (DEBUG_WALLPAPER) Log.v(TAG,
8892 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008893 wallpaperMayChange = true;
8894 }
8895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8897 animating = true;
8898 //w.dump(" ");
8899 }
8900
8901 mPolicy.animatingWindowLw(w, attrs);
8902 }
8903
8904 final AppWindowToken atoken = w.mAppToken;
8905 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8906 if (atoken.lastTransactionSequence != transactionSequence) {
8907 atoken.lastTransactionSequence = transactionSequence;
8908 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8909 atoken.startingDisplayed = false;
8910 }
8911 if ((w.isOnScreen() || w.mAttrs.type
8912 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8913 && !w.mExiting && !w.mDestroying) {
8914 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8915 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8916 + w.isDisplayedLw()
8917 + ", isAnimating=" + w.isAnimating());
8918 if (!w.isDisplayedLw()) {
8919 Log.v(TAG, "Not displayed: s=" + w.mSurface
8920 + " pv=" + w.mPolicyVisibility
8921 + " dp=" + w.mDrawPending
8922 + " cdp=" + w.mCommitDrawPending
8923 + " ah=" + w.mAttachedHidden
8924 + " th=" + atoken.hiddenRequested
8925 + " a=" + w.mAnimating);
8926 }
8927 }
8928 if (w != atoken.startingWindow) {
8929 if (!atoken.freezingScreen || !w.mAppFreezing) {
8930 atoken.numInterestingWindows++;
8931 if (w.isDisplayedLw()) {
8932 atoken.numDrawnWindows++;
8933 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8934 "tokenMayBeDrawn: " + atoken
8935 + " freezingScreen=" + atoken.freezingScreen
8936 + " mAppFreezing=" + w.mAppFreezing);
8937 tokenMayBeDrawn = true;
8938 }
8939 }
8940 } else if (w.isDisplayedLw()) {
8941 atoken.startingDisplayed = true;
8942 }
8943 }
8944 } else if (w.mReadyToShow) {
8945 w.performShowLocked();
8946 }
8947 }
8948
8949 if (mPolicy.finishAnimationLw()) {
8950 restart = true;
8951 }
8952
8953 if (tokenMayBeDrawn) {
8954 // See if any windows have been drawn, so they (and others
8955 // associated with them) can now be shown.
8956 final int NT = mTokenList.size();
8957 for (i=0; i<NT; i++) {
8958 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8959 if (wtoken == null) {
8960 continue;
8961 }
8962 if (wtoken.freezingScreen) {
8963 int numInteresting = wtoken.numInterestingWindows;
8964 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8965 if (DEBUG_VISIBILITY) Log.v(TAG,
8966 "allDrawn: " + wtoken
8967 + " interesting=" + numInteresting
8968 + " drawn=" + wtoken.numDrawnWindows);
8969 wtoken.showAllWindowsLocked();
8970 unsetAppFreezingScreenLocked(wtoken, false, true);
8971 orientationChangeComplete = true;
8972 }
8973 } else if (!wtoken.allDrawn) {
8974 int numInteresting = wtoken.numInterestingWindows;
8975 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8976 if (DEBUG_VISIBILITY) Log.v(TAG,
8977 "allDrawn: " + wtoken
8978 + " interesting=" + numInteresting
8979 + " drawn=" + wtoken.numDrawnWindows);
8980 wtoken.allDrawn = true;
8981 restart = true;
8982
8983 // We can now show all of the drawn windows!
8984 if (!mOpeningApps.contains(wtoken)) {
8985 wtoken.showAllWindowsLocked();
8986 }
8987 }
8988 }
8989 }
8990 }
8991
8992 // If we are ready to perform an app transition, check through
8993 // all of the app tokens to be shown and see if they are ready
8994 // to go.
8995 if (mAppTransitionReady) {
8996 int NN = mOpeningApps.size();
8997 boolean goodToGo = true;
8998 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8999 "Checking " + NN + " opening apps (frozen="
9000 + mDisplayFrozen + " timeout="
9001 + mAppTransitionTimeout + ")...");
9002 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9003 // If the display isn't frozen, wait to do anything until
9004 // all of the apps are ready. Otherwise just go because
9005 // we'll unfreeze the display when everyone is ready.
9006 for (i=0; i<NN && goodToGo; i++) {
9007 AppWindowToken wtoken = mOpeningApps.get(i);
9008 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9009 "Check opening app" + wtoken + ": allDrawn="
9010 + wtoken.allDrawn + " startingDisplayed="
9011 + wtoken.startingDisplayed);
9012 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9013 && !wtoken.startingMoved) {
9014 goodToGo = false;
9015 }
9016 }
9017 }
9018 if (goodToGo) {
9019 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
9020 int transit = mNextAppTransition;
9021 if (mSkipAppTransitionAnimation) {
9022 transit = WindowManagerPolicy.TRANSIT_NONE;
9023 }
9024 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9025 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009026 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009027 mAppTransitionTimeout = false;
9028 mStartingIconInTransition = false;
9029 mSkipAppTransitionAnimation = false;
9030
9031 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9032
Dianne Hackborna8f60182009-09-01 19:01:50 -07009033 // If there are applications waiting to come to the
9034 // top of the stack, now is the time to move their windows.
9035 // (Note that we don't do apps going to the bottom
9036 // here -- we want to keep their windows in the old
9037 // Z-order until the animation completes.)
9038 if (mToTopApps.size() > 0) {
9039 NN = mAppTokens.size();
9040 for (i=0; i<NN; i++) {
9041 AppWindowToken wtoken = mAppTokens.get(i);
9042 if (wtoken.sendingToTop) {
9043 wtoken.sendingToTop = false;
9044 moveAppWindowsLocked(wtoken, NN, false);
9045 }
9046 }
9047 mToTopApps.clear();
9048 }
9049
Dianne Hackborn25994b42009-09-04 14:21:19 -07009050 WindowState oldWallpaper = mWallpaperTarget;
9051
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009052 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009053 wallpaperMayChange = false;
9054
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009055 // The top-most window will supply the layout params,
9056 // and we will determine it below.
9057 LayoutParams animLp = null;
9058 int bestAnimLayer = -1;
9059
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009060 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009061 "New wallpaper target=" + mWallpaperTarget
9062 + ", lower target=" + mLowerWallpaperTarget
9063 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009064 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009065 // Do a first pass through the tokens for two
9066 // things:
9067 // (1) Determine if both the closing and opening
9068 // app token sets are wallpaper targets, in which
9069 // case special animations are needed
9070 // (since the wallpaper needs to stay static
9071 // behind them).
9072 // (2) Find the layout params of the top-most
9073 // application window in the tokens, which is
9074 // what will control the animation theme.
9075 final int NC = mClosingApps.size();
9076 NN = NC + mOpeningApps.size();
9077 for (i=0; i<NN; i++) {
9078 AppWindowToken wtoken;
9079 int mode;
9080 if (i < NC) {
9081 wtoken = mClosingApps.get(i);
9082 mode = 1;
9083 } else {
9084 wtoken = mOpeningApps.get(i-NC);
9085 mode = 2;
9086 }
9087 if (mLowerWallpaperTarget != null) {
9088 if (mLowerWallpaperTarget.mAppToken == wtoken
9089 || mUpperWallpaperTarget.mAppToken == wtoken) {
9090 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009091 }
9092 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009093 if (wtoken.appFullscreen) {
9094 WindowState ws = wtoken.findMainWindow();
9095 if (ws != null) {
9096 // If this is a compatibility mode
9097 // window, we will always use its anim.
9098 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9099 animLp = ws.mAttrs;
9100 bestAnimLayer = Integer.MAX_VALUE;
9101 } else if (ws.mLayer > bestAnimLayer) {
9102 animLp = ws.mAttrs;
9103 bestAnimLayer = ws.mLayer;
9104 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009105 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009106 }
9107 }
9108
Dianne Hackborn25994b42009-09-04 14:21:19 -07009109 if (foundWallpapers == 3) {
9110 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9111 "Wallpaper animation!");
9112 switch (transit) {
9113 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9114 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9115 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9116 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9117 break;
9118 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9119 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9120 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9121 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9122 break;
9123 }
9124 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9125 "New transit: " + transit);
9126 } else if (oldWallpaper != null) {
9127 // We are transitioning from an activity with
9128 // a wallpaper to one without.
9129 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9130 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9131 "New transit away from wallpaper: " + transit);
9132 } else if (mWallpaperTarget != null) {
9133 // We are transitioning from an activity without
9134 // a wallpaper to now showing the wallpaper
9135 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9136 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9137 "New transit into wallpaper: " + transit);
9138 }
9139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009140 NN = mOpeningApps.size();
9141 for (i=0; i<NN; i++) {
9142 AppWindowToken wtoken = mOpeningApps.get(i);
9143 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9144 "Now opening app" + wtoken);
9145 wtoken.reportedVisible = false;
9146 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009147 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009148 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009150 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 wtoken.showAllWindowsLocked();
9152 }
9153 NN = mClosingApps.size();
9154 for (i=0; i<NN; i++) {
9155 AppWindowToken wtoken = mClosingApps.get(i);
9156 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9157 "Now closing app" + wtoken);
9158 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009159 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009160 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009162 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 // Force the allDrawn flag, because we want to start
9164 // this guy's animations regardless of whether it's
9165 // gotten drawn.
9166 wtoken.allDrawn = true;
9167 }
9168
9169 mOpeningApps.clear();
9170 mClosingApps.clear();
9171
9172 // This has changed the visibility of windows, so perform
9173 // a new layout to get them all up-to-date.
9174 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009175 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9176 assignLayersLocked();
9177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 performLayoutLockedInner();
9179 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9180
9181 restart = true;
9182 }
9183 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009184
Dianne Hackborna8f60182009-09-01 19:01:50 -07009185 if (!animating && mAppTransitionRunning) {
9186 // We have finished the animation of an app transition. To do
9187 // this, we have delayed a lot of operations like showing and
9188 // hiding apps, moving apps in Z-order, etc. The app token list
9189 // reflects the correct Z-order, but the window list may now
9190 // be out of sync with it. So here we will just rebuild the
9191 // entire app window list. Fun!
9192 mAppTransitionRunning = false;
9193 // Clear information about apps that were moving.
9194 mToBottomApps.clear();
9195
9196 rebuildAppWindowListLocked();
9197 restart = true;
9198 moveInputMethodWindowsIfNeededLocked(false);
9199 wallpaperMayChange = true;
9200 mLayoutNeeded = true;
9201 }
9202
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009203 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009204 if (DEBUG_WALLPAPER) Log.v(TAG,
9205 "Wallpaper may change! Adjusting");
9206 int adjResult = adjustWallpaperWindowsLocked();
9207 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9208 if (DEBUG_WALLPAPER) Log.v(TAG,
9209 "Wallpaper layer changed: assigning layers + relayout");
9210 restart = true;
9211 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009212 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009213 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9214 if (DEBUG_WALLPAPER) Log.v(TAG,
9215 "Wallpaper visibility changed: relayout");
9216 restart = true;
9217 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009218 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009219 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009220 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009221 performLayoutLockedInner();
9222 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009223 }
9224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009225 } while (restart);
9226
9227 // THIRD LOOP: Update the surfaces of all windows.
9228
9229 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9230
9231 boolean obscured = false;
9232 boolean blurring = false;
9233 boolean dimming = false;
9234 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009235 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009236 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009238 final int N = mWindows.size();
9239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 for (i=N-1; i>=0; i--) {
9241 WindowState w = (WindowState)mWindows.get(i);
9242
9243 boolean displayed = false;
9244 final WindowManager.LayoutParams attrs = w.mAttrs;
9245 final int attrFlags = attrs.flags;
9246
9247 if (w.mSurface != null) {
9248 w.computeShownFrameLocked();
9249 if (localLOGV) Log.v(
9250 TAG, "Placing surface #" + i + " " + w.mSurface
9251 + ": new=" + w.mShownFrame + ", old="
9252 + w.mLastShownFrame);
9253
9254 boolean resize;
9255 int width, height;
9256 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9257 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9258 w.mLastRequestedHeight != w.mRequestedHeight;
9259 // for a scaled surface, we just want to use
9260 // the requested size.
9261 width = w.mRequestedWidth;
9262 height = w.mRequestedHeight;
9263 w.mLastRequestedWidth = width;
9264 w.mLastRequestedHeight = height;
9265 w.mLastShownFrame.set(w.mShownFrame);
9266 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009267 if (SHOW_TRANSACTIONS) Log.i(
9268 TAG, " SURFACE " + w.mSurface
9269 + ": POS " + w.mShownFrame.left
9270 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9272 } catch (RuntimeException e) {
9273 Log.w(TAG, "Error positioning surface in " + w, e);
9274 if (!recoveringMemory) {
9275 reclaimSomeSurfaceMemoryLocked(w, "position");
9276 }
9277 }
9278 } else {
9279 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9280 width = w.mShownFrame.width();
9281 height = w.mShownFrame.height();
9282 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 }
9284
9285 if (resize) {
9286 if (width < 1) width = 1;
9287 if (height < 1) height = 1;
9288 if (w.mSurface != null) {
9289 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009290 if (SHOW_TRANSACTIONS) Log.i(
9291 TAG, " SURFACE " + w.mSurface + ": POS "
9292 + w.mShownFrame.left + ","
9293 + w.mShownFrame.top + " SIZE "
9294 + w.mShownFrame.width() + "x"
9295 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 w.mSurface.setSize(width, height);
9297 w.mSurface.setPosition(w.mShownFrame.left,
9298 w.mShownFrame.top);
9299 } catch (RuntimeException e) {
9300 // If something goes wrong with the surface (such
9301 // as running out of memory), don't take down the
9302 // entire system.
9303 Log.e(TAG, "Failure updating surface of " + w
9304 + "size=(" + width + "x" + height
9305 + "), pos=(" + w.mShownFrame.left
9306 + "," + w.mShownFrame.top + ")", e);
9307 if (!recoveringMemory) {
9308 reclaimSomeSurfaceMemoryLocked(w, "size");
9309 }
9310 }
9311 }
9312 }
9313 if (!w.mAppFreezing) {
9314 w.mContentInsetsChanged =
9315 !w.mLastContentInsets.equals(w.mContentInsets);
9316 w.mVisibleInsetsChanged =
9317 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009318 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 || w.mContentInsetsChanged
9320 || w.mVisibleInsetsChanged) {
9321 w.mLastFrame.set(w.mFrame);
9322 w.mLastContentInsets.set(w.mContentInsets);
9323 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009324 // If the screen is currently frozen, then keep
9325 // it frozen until this window draws at its new
9326 // orientation.
9327 if (mDisplayFrozen) {
9328 if (DEBUG_ORIENTATION) Log.v(TAG,
9329 "Resizing while display frozen: " + w);
9330 w.mOrientationChanging = true;
9331 if (mWindowsFreezingScreen) {
9332 mWindowsFreezingScreen = true;
9333 // XXX should probably keep timeout from
9334 // when we first froze the display.
9335 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9336 mH.sendMessageDelayed(mH.obtainMessage(
9337 H.WINDOW_FREEZE_TIMEOUT), 2000);
9338 }
9339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 // If the orientation is changing, then we need to
9341 // hold off on unfreezing the display until this
9342 // window has been redrawn; to do that, we need
9343 // to go through the process of getting informed
9344 // by the application when it has finished drawing.
9345 if (w.mOrientationChanging) {
9346 if (DEBUG_ORIENTATION) Log.v(TAG,
9347 "Orientation start waiting for draw in "
9348 + w + ", surface " + w.mSurface);
9349 w.mDrawPending = true;
9350 w.mCommitDrawPending = false;
9351 w.mReadyToShow = false;
9352 if (w.mAppToken != null) {
9353 w.mAppToken.allDrawn = false;
9354 }
9355 }
Romain Guy06882f82009-06-10 13:36:04 -07009356 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 "Resizing window " + w + " to " + w.mFrame);
9358 mResizingWindows.add(w);
9359 } else if (w.mOrientationChanging) {
9360 if (!w.mDrawPending && !w.mCommitDrawPending) {
9361 if (DEBUG_ORIENTATION) Log.v(TAG,
9362 "Orientation not waiting for draw in "
9363 + w + ", surface " + w.mSurface);
9364 w.mOrientationChanging = false;
9365 }
9366 }
9367 }
9368
9369 if (w.mAttachedHidden) {
9370 if (!w.mLastHidden) {
9371 //dump();
9372 w.mLastHidden = true;
9373 if (SHOW_TRANSACTIONS) Log.i(
9374 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9375 if (w.mSurface != null) {
9376 try {
9377 w.mSurface.hide();
9378 } catch (RuntimeException e) {
9379 Log.w(TAG, "Exception hiding surface in " + w);
9380 }
9381 }
9382 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9383 }
9384 // If we are waiting for this window to handle an
9385 // orientation change, well, it is hidden, so
9386 // doesn't really matter. Note that this does
9387 // introduce a potential glitch if the window
9388 // becomes unhidden before it has drawn for the
9389 // new orientation.
9390 if (w.mOrientationChanging) {
9391 w.mOrientationChanging = false;
9392 if (DEBUG_ORIENTATION) Log.v(TAG,
9393 "Orientation change skips hidden " + w);
9394 }
9395 } else if (!w.isReadyForDisplay()) {
9396 if (!w.mLastHidden) {
9397 //dump();
9398 w.mLastHidden = true;
9399 if (SHOW_TRANSACTIONS) Log.i(
9400 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9401 if (w.mSurface != null) {
9402 try {
9403 w.mSurface.hide();
9404 } catch (RuntimeException e) {
9405 Log.w(TAG, "Exception exception hiding surface in " + w);
9406 }
9407 }
9408 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9409 }
9410 // If we are waiting for this window to handle an
9411 // orientation change, well, it is hidden, so
9412 // doesn't really matter. Note that this does
9413 // introduce a potential glitch if the window
9414 // becomes unhidden before it has drawn for the
9415 // new orientation.
9416 if (w.mOrientationChanging) {
9417 w.mOrientationChanging = false;
9418 if (DEBUG_ORIENTATION) Log.v(TAG,
9419 "Orientation change skips hidden " + w);
9420 }
9421 } else if (w.mLastLayer != w.mAnimLayer
9422 || w.mLastAlpha != w.mShownAlpha
9423 || w.mLastDsDx != w.mDsDx
9424 || w.mLastDtDx != w.mDtDx
9425 || w.mLastDsDy != w.mDsDy
9426 || w.mLastDtDy != w.mDtDy
9427 || w.mLastHScale != w.mHScale
9428 || w.mLastVScale != w.mVScale
9429 || w.mLastHidden) {
9430 displayed = true;
9431 w.mLastAlpha = w.mShownAlpha;
9432 w.mLastLayer = w.mAnimLayer;
9433 w.mLastDsDx = w.mDsDx;
9434 w.mLastDtDx = w.mDtDx;
9435 w.mLastDsDy = w.mDsDy;
9436 w.mLastDtDy = w.mDtDy;
9437 w.mLastHScale = w.mHScale;
9438 w.mLastVScale = w.mVScale;
9439 if (SHOW_TRANSACTIONS) Log.i(
9440 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009441 + w.mShownAlpha + " layer=" + w.mAnimLayer
9442 + " matrix=[" + (w.mDsDx*w.mHScale)
9443 + "," + (w.mDtDx*w.mVScale)
9444 + "][" + (w.mDsDy*w.mHScale)
9445 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 if (w.mSurface != null) {
9447 try {
9448 w.mSurface.setAlpha(w.mShownAlpha);
9449 w.mSurface.setLayer(w.mAnimLayer);
9450 w.mSurface.setMatrix(
9451 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9452 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9453 } catch (RuntimeException e) {
9454 Log.w(TAG, "Error updating surface in " + w, e);
9455 if (!recoveringMemory) {
9456 reclaimSomeSurfaceMemoryLocked(w, "update");
9457 }
9458 }
9459 }
9460
9461 if (w.mLastHidden && !w.mDrawPending
9462 && !w.mCommitDrawPending
9463 && !w.mReadyToShow) {
9464 if (SHOW_TRANSACTIONS) Log.i(
9465 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9466 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9467 + " during relayout");
9468 if (showSurfaceRobustlyLocked(w)) {
9469 w.mHasDrawn = true;
9470 w.mLastHidden = false;
9471 } else {
9472 w.mOrientationChanging = false;
9473 }
9474 }
9475 if (w.mSurface != null) {
9476 w.mToken.hasVisible = true;
9477 }
9478 } else {
9479 displayed = true;
9480 }
9481
9482 if (displayed) {
9483 if (!covered) {
9484 if (attrs.width == LayoutParams.FILL_PARENT
9485 && attrs.height == LayoutParams.FILL_PARENT) {
9486 covered = true;
9487 }
9488 }
9489 if (w.mOrientationChanging) {
9490 if (w.mDrawPending || w.mCommitDrawPending) {
9491 orientationChangeComplete = false;
9492 if (DEBUG_ORIENTATION) Log.v(TAG,
9493 "Orientation continue waiting for draw in " + w);
9494 } else {
9495 w.mOrientationChanging = false;
9496 if (DEBUG_ORIENTATION) Log.v(TAG,
9497 "Orientation change complete in " + w);
9498 }
9499 }
9500 w.mToken.hasVisible = true;
9501 }
9502 } else if (w.mOrientationChanging) {
9503 if (DEBUG_ORIENTATION) Log.v(TAG,
9504 "Orientation change skips hidden " + w);
9505 w.mOrientationChanging = false;
9506 }
9507
9508 final boolean canBeSeen = w.isDisplayedLw();
9509
9510 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9511 focusDisplayed = true;
9512 }
9513
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009514 final boolean obscuredChanged = w.mObscured != obscured;
9515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009517 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009518 if (w.mSurface != null) {
9519 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9520 holdScreen = w.mSession;
9521 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009522 if (!syswin && w.mAttrs.screenBrightness >= 0
9523 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 screenBrightness = w.mAttrs.screenBrightness;
9525 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009526 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9527 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9528 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9529 syswin = true;
9530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009532
Dianne Hackborn25994b42009-09-04 14:21:19 -07009533 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9534 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 // This window completely covers everything behind it,
9536 // so we want to leave all of them as unblurred (for
9537 // performance reasons).
9538 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009539 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9540 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009541 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009542 obscured = true;
9543 if (mBackgroundFillerSurface == null) {
9544 try {
9545 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9546 0, dw, dh,
9547 PixelFormat.OPAQUE,
9548 Surface.FX_SURFACE_NORMAL);
9549 } catch (Exception e) {
9550 Log.e(TAG, "Exception creating filler surface", e);
9551 }
9552 }
9553 try {
9554 mBackgroundFillerSurface.setPosition(0, 0);
9555 mBackgroundFillerSurface.setSize(dw, dh);
9556 // Using the same layer as Dim because they will never be shown at the
9557 // same time.
9558 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9559 mBackgroundFillerSurface.show();
9560 } catch (RuntimeException e) {
9561 Log.e(TAG, "Exception showing filler surface");
9562 }
9563 backgroundFillerShown = true;
9564 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 } else if (canBeSeen && !obscured &&
9566 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9567 if (localLOGV) Log.v(TAG, "Win " + w
9568 + ": blurring=" + blurring
9569 + " obscured=" + obscured
9570 + " displayed=" + displayed);
9571 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9572 if (!dimming) {
9573 //Log.i(TAG, "DIM BEHIND: " + w);
9574 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009575 if (mDimAnimator == null) {
9576 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009577 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009578 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009580 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581 }
9582 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9583 if (!blurring) {
9584 //Log.i(TAG, "BLUR BEHIND: " + w);
9585 blurring = true;
9586 mBlurShown = true;
9587 if (mBlurSurface == null) {
9588 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9589 + mBlurSurface + ": CREATE");
9590 try {
Romain Guy06882f82009-06-10 13:36:04 -07009591 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 -1, 16, 16,
9593 PixelFormat.OPAQUE,
9594 Surface.FX_SURFACE_BLUR);
9595 } catch (Exception e) {
9596 Log.e(TAG, "Exception creating Blur surface", e);
9597 }
9598 }
9599 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9600 + mBlurSurface + ": SHOW pos=(0,0) (" +
9601 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9602 if (mBlurSurface != null) {
9603 mBlurSurface.setPosition(0, 0);
9604 mBlurSurface.setSize(dw, dh);
9605 try {
9606 mBlurSurface.show();
9607 } catch (RuntimeException e) {
9608 Log.w(TAG, "Failure showing blur surface", e);
9609 }
9610 }
9611 }
9612 mBlurSurface.setLayer(w.mAnimLayer-2);
9613 }
9614 }
9615 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009616
9617 if (obscuredChanged && mWallpaperTarget == w) {
9618 // This is the wallpaper target and its obscured state
9619 // changed... make sure the current wallaper's visibility
9620 // has been updated accordingly.
9621 updateWallpaperVisibilityLocked();
9622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009624
9625 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9626 mBackgroundFillerShown = false;
9627 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9628 try {
9629 mBackgroundFillerSurface.hide();
9630 } catch (RuntimeException e) {
9631 Log.e(TAG, "Exception hiding filler surface", e);
9632 }
9633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009634
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009635 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9636 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 }
Romain Guy06882f82009-06-10 13:36:04 -07009638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 if (!blurring && mBlurShown) {
9640 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9641 + ": HIDE");
9642 try {
9643 mBlurSurface.hide();
9644 } catch (IllegalArgumentException e) {
9645 Log.w(TAG, "Illegal argument exception hiding blur surface");
9646 }
9647 mBlurShown = false;
9648 }
9649
9650 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9651 } catch (RuntimeException e) {
9652 Log.e(TAG, "Unhandled exception in Window Manager", e);
9653 }
9654
9655 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009657 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9658 "With display frozen, orientationChangeComplete="
9659 + orientationChangeComplete);
9660 if (orientationChangeComplete) {
9661 if (mWindowsFreezingScreen) {
9662 mWindowsFreezingScreen = false;
9663 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9664 }
9665 if (mAppsFreezingScreen == 0) {
9666 stopFreezingDisplayLocked();
9667 }
9668 }
Romain Guy06882f82009-06-10 13:36:04 -07009669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009670 i = mResizingWindows.size();
9671 if (i > 0) {
9672 do {
9673 i--;
9674 WindowState win = mResizingWindows.get(i);
9675 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009676 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9677 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 win.mClient.resized(win.mFrame.width(),
9679 win.mFrame.height(), win.mLastContentInsets,
9680 win.mLastVisibleInsets, win.mDrawPending);
9681 win.mContentInsetsChanged = false;
9682 win.mVisibleInsetsChanged = false;
9683 } catch (RemoteException e) {
9684 win.mOrientationChanging = false;
9685 }
9686 } while (i > 0);
9687 mResizingWindows.clear();
9688 }
Romain Guy06882f82009-06-10 13:36:04 -07009689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009690 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009691 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 i = mDestroySurface.size();
9693 if (i > 0) {
9694 do {
9695 i--;
9696 WindowState win = mDestroySurface.get(i);
9697 win.mDestroying = false;
9698 if (mInputMethodWindow == win) {
9699 mInputMethodWindow = null;
9700 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009701 if (win == mWallpaperTarget) {
9702 wallpaperDestroyed = true;
9703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704 win.destroySurfaceLocked();
9705 } while (i > 0);
9706 mDestroySurface.clear();
9707 }
9708
9709 // Time to remove any exiting tokens?
9710 for (i=mExitingTokens.size()-1; i>=0; i--) {
9711 WindowToken token = mExitingTokens.get(i);
9712 if (!token.hasVisible) {
9713 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009714 if (token.windowType == TYPE_WALLPAPER) {
9715 mWallpaperTokens.remove(token);
9716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 }
9718 }
9719
9720 // Time to remove any exiting applications?
9721 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9722 AppWindowToken token = mExitingAppTokens.get(i);
9723 if (!token.hasVisible && !mClosingApps.contains(token)) {
9724 mAppTokens.remove(token);
9725 mExitingAppTokens.remove(i);
9726 }
9727 }
9728
Dianne Hackborna8f60182009-09-01 19:01:50 -07009729 boolean needRelayout = false;
9730
9731 if (!animating && mAppTransitionRunning) {
9732 // We have finished the animation of an app transition. To do
9733 // this, we have delayed a lot of operations like showing and
9734 // hiding apps, moving apps in Z-order, etc. The app token list
9735 // reflects the correct Z-order, but the window list may now
9736 // be out of sync with it. So here we will just rebuild the
9737 // entire app window list. Fun!
9738 mAppTransitionRunning = false;
9739 needRelayout = true;
9740 rebuildAppWindowListLocked();
9741 // Clear information about apps that were moving.
9742 mToBottomApps.clear();
9743 }
9744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 if (focusDisplayed) {
9746 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9747 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009748 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009749 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009750 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009751 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009752 requestAnimationLocked(0);
9753 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009754 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9755 }
9756 mQueue.setHoldScreenLocked(holdScreen != null);
9757 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9758 mPowerManager.setScreenBrightnessOverride(-1);
9759 } else {
9760 mPowerManager.setScreenBrightnessOverride((int)
9761 (screenBrightness * Power.BRIGHTNESS_ON));
9762 }
9763 if (holdScreen != mHoldingScreenOn) {
9764 mHoldingScreenOn = holdScreen;
9765 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9766 mH.sendMessage(m);
9767 }
9768 }
9769
9770 void requestAnimationLocked(long delay) {
9771 if (!mAnimationPending) {
9772 mAnimationPending = true;
9773 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9774 }
9775 }
Romain Guy06882f82009-06-10 13:36:04 -07009776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 /**
9778 * Have the surface flinger show a surface, robustly dealing with
9779 * error conditions. In particular, if there is not enough memory
9780 * to show the surface, then we will try to get rid of other surfaces
9781 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009782 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 * @return Returns true if the surface was successfully shown.
9784 */
9785 boolean showSurfaceRobustlyLocked(WindowState win) {
9786 try {
9787 if (win.mSurface != null) {
9788 win.mSurface.show();
9789 }
9790 return true;
9791 } catch (RuntimeException e) {
9792 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9793 }
Romain Guy06882f82009-06-10 13:36:04 -07009794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009795 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 return false;
9798 }
Romain Guy06882f82009-06-10 13:36:04 -07009799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9801 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9804 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 if (mForceRemoves == null) {
9807 mForceRemoves = new ArrayList<WindowState>();
9808 }
Romain Guy06882f82009-06-10 13:36:04 -07009809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 long callingIdentity = Binder.clearCallingIdentity();
9811 try {
9812 // There was some problem... first, do a sanity check of the
9813 // window list to make sure we haven't left any dangling surfaces
9814 // around.
9815 int N = mWindows.size();
9816 boolean leakedSurface = false;
9817 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9818 for (int i=0; i<N; i++) {
9819 WindowState ws = (WindowState)mWindows.get(i);
9820 if (ws.mSurface != null) {
9821 if (!mSessions.contains(ws.mSession)) {
9822 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9823 + ws + " surface=" + ws.mSurface
9824 + " token=" + win.mToken
9825 + " pid=" + ws.mSession.mPid
9826 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009827 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 ws.mSurface = null;
9829 mForceRemoves.add(ws);
9830 i--;
9831 N--;
9832 leakedSurface = true;
9833 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9834 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9835 + ws + " surface=" + ws.mSurface
9836 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009837 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 ws.mSurface = null;
9839 leakedSurface = true;
9840 }
9841 }
9842 }
Romain Guy06882f82009-06-10 13:36:04 -07009843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844 boolean killedApps = false;
9845 if (!leakedSurface) {
9846 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9847 SparseIntArray pidCandidates = new SparseIntArray();
9848 for (int i=0; i<N; i++) {
9849 WindowState ws = (WindowState)mWindows.get(i);
9850 if (ws.mSurface != null) {
9851 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9852 }
9853 }
9854 if (pidCandidates.size() > 0) {
9855 int[] pids = new int[pidCandidates.size()];
9856 for (int i=0; i<pids.length; i++) {
9857 pids[i] = pidCandidates.keyAt(i);
9858 }
9859 try {
9860 if (mActivityManager.killPidsForMemory(pids)) {
9861 killedApps = true;
9862 }
9863 } catch (RemoteException e) {
9864 }
9865 }
9866 }
Romain Guy06882f82009-06-10 13:36:04 -07009867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 if (leakedSurface || killedApps) {
9869 // We managed to reclaim some memory, so get rid of the trouble
9870 // surface and ask the app to request another one.
9871 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9872 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009873 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009874 win.mSurface = null;
9875 }
Romain Guy06882f82009-06-10 13:36:04 -07009876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009877 try {
9878 win.mClient.dispatchGetNewSurface();
9879 } catch (RemoteException e) {
9880 }
9881 }
9882 } finally {
9883 Binder.restoreCallingIdentity(callingIdentity);
9884 }
9885 }
Romain Guy06882f82009-06-10 13:36:04 -07009886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 private boolean updateFocusedWindowLocked(int mode) {
9888 WindowState newFocus = computeFocusedWindowLocked();
9889 if (mCurrentFocus != newFocus) {
9890 // This check makes sure that we don't already have the focus
9891 // change message pending.
9892 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9893 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9894 if (localLOGV) Log.v(
9895 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9896 final WindowState oldFocus = mCurrentFocus;
9897 mCurrentFocus = newFocus;
9898 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 final WindowState imWindow = mInputMethodWindow;
9901 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009902 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009904 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9905 mLayoutNeeded = true;
9906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9908 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009909 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9910 // Client will do the layout, but we need to assign layers
9911 // for handleNewWindowLocked() below.
9912 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 }
9914 }
Romain Guy06882f82009-06-10 13:36:04 -07009915
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009916 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9917 mKeyWaiter.handleNewWindowLocked(newFocus);
9918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 return true;
9920 }
9921 return false;
9922 }
9923
9924 private WindowState computeFocusedWindowLocked() {
9925 WindowState result = null;
9926 WindowState win;
9927
9928 int i = mWindows.size() - 1;
9929 int nextAppIndex = mAppTokens.size()-1;
9930 WindowToken nextApp = nextAppIndex >= 0
9931 ? mAppTokens.get(nextAppIndex) : null;
9932
9933 while (i >= 0) {
9934 win = (WindowState)mWindows.get(i);
9935
9936 if (localLOGV || DEBUG_FOCUS) Log.v(
9937 TAG, "Looking for focus: " + i
9938 + " = " + win
9939 + ", flags=" + win.mAttrs.flags
9940 + ", canReceive=" + win.canReceiveKeys());
9941
9942 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 // If this window's application has been removed, just skip it.
9945 if (thisApp != null && thisApp.removed) {
9946 i--;
9947 continue;
9948 }
Romain Guy06882f82009-06-10 13:36:04 -07009949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950 // If there is a focused app, don't allow focus to go to any
9951 // windows below it. If this is an application window, step
9952 // through the app tokens until we find its app.
9953 if (thisApp != null && nextApp != null && thisApp != nextApp
9954 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9955 int origAppIndex = nextAppIndex;
9956 while (nextAppIndex > 0) {
9957 if (nextApp == mFocusedApp) {
9958 // Whoops, we are below the focused app... no focus
9959 // for you!
9960 if (localLOGV || DEBUG_FOCUS) Log.v(
9961 TAG, "Reached focused app: " + mFocusedApp);
9962 return null;
9963 }
9964 nextAppIndex--;
9965 nextApp = mAppTokens.get(nextAppIndex);
9966 if (nextApp == thisApp) {
9967 break;
9968 }
9969 }
9970 if (thisApp != nextApp) {
9971 // Uh oh, the app token doesn't exist! This shouldn't
9972 // happen, but if it does we can get totally hosed...
9973 // so restart at the original app.
9974 nextAppIndex = origAppIndex;
9975 nextApp = mAppTokens.get(nextAppIndex);
9976 }
9977 }
9978
9979 // Dispatch to this window if it is wants key events.
9980 if (win.canReceiveKeys()) {
9981 if (DEBUG_FOCUS) Log.v(
9982 TAG, "Found focus @ " + i + " = " + win);
9983 result = win;
9984 break;
9985 }
9986
9987 i--;
9988 }
9989
9990 return result;
9991 }
9992
9993 private void startFreezingDisplayLocked() {
9994 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009995 // Freezing the display also suspends key event delivery, to
9996 // keep events from going astray while the display is reconfigured.
9997 // If someone has changed orientation again while the screen is
9998 // still frozen, the events will continue to be blocked while the
9999 // successive orientation change is processed. To prevent spurious
10000 // ANRs, we reset the event dispatch timeout in this case.
10001 synchronized (mKeyWaiter) {
10002 mKeyWaiter.mWasFrozen = true;
10003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 return;
10005 }
Romain Guy06882f82009-06-10 13:36:04 -070010006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010007 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010009 long now = SystemClock.uptimeMillis();
10010 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
10011 if (mFreezeGcPending != 0) {
10012 if (now > (mFreezeGcPending+1000)) {
10013 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
10014 mH.removeMessages(H.FORCE_GC);
10015 Runtime.getRuntime().gc();
10016 mFreezeGcPending = now;
10017 }
10018 } else {
10019 mFreezeGcPending = now;
10020 }
Romain Guy06882f82009-06-10 13:36:04 -070010021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 mDisplayFrozen = true;
10023 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
10024 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
10025 mAppTransitionReady = true;
10026 }
Romain Guy06882f82009-06-10 13:36:04 -070010027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 if (PROFILE_ORIENTATION) {
10029 File file = new File("/data/system/frozen");
10030 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10031 }
10032 Surface.freezeDisplay(0);
10033 }
Romain Guy06882f82009-06-10 13:36:04 -070010034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 private void stopFreezingDisplayLocked() {
10036 if (!mDisplayFrozen) {
10037 return;
10038 }
Romain Guy06882f82009-06-10 13:36:04 -070010039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 mDisplayFrozen = false;
10041 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10042 if (PROFILE_ORIENTATION) {
10043 Debug.stopMethodTracing();
10044 }
10045 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010046
Chris Tate2ad63a92009-03-25 17:36:48 -070010047 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10048 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049 synchronized (mKeyWaiter) {
10050 mKeyWaiter.mWasFrozen = true;
10051 mKeyWaiter.notifyAll();
10052 }
10053
10054 // A little kludge: a lot could have happened while the
10055 // display was frozen, so now that we are coming back we
10056 // do a gc so that any remote references the system
10057 // processes holds on others can be released if they are
10058 // no longer needed.
10059 mH.removeMessages(H.FORCE_GC);
10060 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10061 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 mScreenFrozenLock.release();
10064 }
Romain Guy06882f82009-06-10 13:36:04 -070010065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 @Override
10067 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10068 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10069 != PackageManager.PERMISSION_GRANTED) {
10070 pw.println("Permission Denial: can't dump WindowManager from from pid="
10071 + Binder.getCallingPid()
10072 + ", uid=" + Binder.getCallingUid());
10073 return;
10074 }
Romain Guy06882f82009-06-10 13:36:04 -070010075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 synchronized(mWindowMap) {
10077 pw.println("Current Window Manager state:");
10078 for (int i=mWindows.size()-1; i>=0; i--) {
10079 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010080 pw.print(" Window #"); pw.print(i); pw.print(' ');
10081 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 w.dump(pw, " ");
10083 }
10084 if (mInputMethodDialogs.size() > 0) {
10085 pw.println(" ");
10086 pw.println(" Input method dialogs:");
10087 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10088 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010089 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 }
10091 }
10092 if (mPendingRemove.size() > 0) {
10093 pw.println(" ");
10094 pw.println(" Remove pending for:");
10095 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10096 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010097 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10098 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 w.dump(pw, " ");
10100 }
10101 }
10102 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10103 pw.println(" ");
10104 pw.println(" Windows force removing:");
10105 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10106 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010107 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10108 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010109 w.dump(pw, " ");
10110 }
10111 }
10112 if (mDestroySurface.size() > 0) {
10113 pw.println(" ");
10114 pw.println(" Windows waiting to destroy their surface:");
10115 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10116 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010117 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10118 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 w.dump(pw, " ");
10120 }
10121 }
10122 if (mLosingFocus.size() > 0) {
10123 pw.println(" ");
10124 pw.println(" Windows losing focus:");
10125 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10126 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010127 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10128 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 w.dump(pw, " ");
10130 }
10131 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010132 if (mResizingWindows.size() > 0) {
10133 pw.println(" ");
10134 pw.println(" Windows waiting to resize:");
10135 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10136 WindowState w = mResizingWindows.get(i);
10137 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10138 pw.print(w); pw.println(":");
10139 w.dump(pw, " ");
10140 }
10141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 if (mSessions.size() > 0) {
10143 pw.println(" ");
10144 pw.println(" All active sessions:");
10145 Iterator<Session> it = mSessions.iterator();
10146 while (it.hasNext()) {
10147 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010148 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 s.dump(pw, " ");
10150 }
10151 }
10152 if (mTokenMap.size() > 0) {
10153 pw.println(" ");
10154 pw.println(" All tokens:");
10155 Iterator<WindowToken> it = mTokenMap.values().iterator();
10156 while (it.hasNext()) {
10157 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010158 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 token.dump(pw, " ");
10160 }
10161 }
10162 if (mTokenList.size() > 0) {
10163 pw.println(" ");
10164 pw.println(" Window token list:");
10165 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010166 pw.print(" #"); pw.print(i); pw.print(": ");
10167 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 }
10169 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010170 if (mWallpaperTokens.size() > 0) {
10171 pw.println(" ");
10172 pw.println(" Wallpaper tokens:");
10173 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10174 WindowToken token = mWallpaperTokens.get(i);
10175 pw.print(" Wallpaper #"); pw.print(i);
10176 pw.print(' '); pw.print(token); pw.println(':');
10177 token.dump(pw, " ");
10178 }
10179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 if (mAppTokens.size() > 0) {
10181 pw.println(" ");
10182 pw.println(" Application tokens in Z order:");
10183 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010184 pw.print(" App #"); pw.print(i); pw.print(": ");
10185 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 }
10187 }
10188 if (mFinishedStarting.size() > 0) {
10189 pw.println(" ");
10190 pw.println(" Finishing start of application tokens:");
10191 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10192 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010193 pw.print(" Finished Starting #"); pw.print(i);
10194 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 token.dump(pw, " ");
10196 }
10197 }
10198 if (mExitingTokens.size() > 0) {
10199 pw.println(" ");
10200 pw.println(" Exiting tokens:");
10201 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10202 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010203 pw.print(" Exiting #"); pw.print(i);
10204 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 token.dump(pw, " ");
10206 }
10207 }
10208 if (mExitingAppTokens.size() > 0) {
10209 pw.println(" ");
10210 pw.println(" Exiting application tokens:");
10211 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10212 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010213 pw.print(" Exiting App #"); pw.print(i);
10214 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 token.dump(pw, " ");
10216 }
10217 }
10218 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010219 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10220 pw.print(" mLastFocus="); pw.println(mLastFocus);
10221 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10222 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10223 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010224 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010225 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10226 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10227 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10228 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010229 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10230 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10231 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10232 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10233 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010234 if (mDimAnimator != null) {
10235 mDimAnimator.printTo(pw);
10236 } else {
10237 pw.print( " no DimAnimator ");
10238 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010239 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010240 pw.print(mInputMethodAnimLayerAdjustment);
10241 pw.print(" mWallpaperAnimLayerAdjustment=");
10242 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010243 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10244 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010245 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10246 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10247 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10248 pw.print(" mRotation="); pw.print(mRotation);
10249 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10250 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10251 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10252 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10253 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10254 pw.print(" mNextAppTransition=0x");
10255 pw.print(Integer.toHexString(mNextAppTransition));
10256 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010257 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010258 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10259 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10260 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10261 if (mOpeningApps.size() > 0) {
10262 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10263 }
10264 if (mClosingApps.size() > 0) {
10265 pw.print(" mClosingApps="); pw.println(mClosingApps);
10266 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010267 if (mToTopApps.size() > 0) {
10268 pw.print(" mToTopApps="); pw.println(mToTopApps);
10269 }
10270 if (mToBottomApps.size() > 0) {
10271 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10272 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010273 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10274 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010275 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010276 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10277 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10278 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10279 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10280 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10281 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 }
10283 }
10284
10285 public void monitor() {
10286 synchronized (mWindowMap) { }
10287 synchronized (mKeyguardDisabled) { }
10288 synchronized (mKeyWaiter) { }
10289 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010290
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010291 public void virtualKeyFeedback(KeyEvent event) {
10292 mPolicy.keyFeedbackFromInput(event);
10293 }
10294
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010295 /**
10296 * DimAnimator class that controls the dim animation. This holds the surface and
10297 * all state used for dim animation.
10298 */
10299 private static class DimAnimator {
10300 Surface mDimSurface;
10301 boolean mDimShown = false;
10302 float mDimCurrentAlpha;
10303 float mDimTargetAlpha;
10304 float mDimDeltaPerMs;
10305 long mLastDimAnimTime;
10306
10307 DimAnimator (SurfaceSession session) {
10308 if (mDimSurface == null) {
10309 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10310 + mDimSurface + ": CREATE");
10311 try {
10312 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10313 Surface.FX_SURFACE_DIM);
10314 } catch (Exception e) {
10315 Log.e(TAG, "Exception creating Dim surface", e);
10316 }
10317 }
10318 }
10319
10320 /**
10321 * Show the dim surface.
10322 */
10323 void show(int dw, int dh) {
10324 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10325 dw + "x" + dh + ")");
10326 mDimShown = true;
10327 try {
10328 mDimSurface.setPosition(0, 0);
10329 mDimSurface.setSize(dw, dh);
10330 mDimSurface.show();
10331 } catch (RuntimeException e) {
10332 Log.w(TAG, "Failure showing dim surface", e);
10333 }
10334 }
10335
10336 /**
10337 * Set's the dim surface's layer and update dim parameters that will be used in
10338 * {@link updateSurface} after all windows are examined.
10339 */
10340 void updateParameters(WindowState w, long currentTime) {
10341 mDimSurface.setLayer(w.mAnimLayer-1);
10342
10343 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010344 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10345 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010346 if (mDimTargetAlpha != target) {
10347 // If the desired dim level has changed, then
10348 // start an animation to it.
10349 mLastDimAnimTime = currentTime;
10350 long duration = (w.mAnimating && w.mAnimation != null)
10351 ? w.mAnimation.computeDurationHint()
10352 : DEFAULT_DIM_DURATION;
10353 if (target > mDimTargetAlpha) {
10354 // This is happening behind the activity UI,
10355 // so we can make it run a little longer to
10356 // give a stronger impression without disrupting
10357 // the user.
10358 duration *= DIM_DURATION_MULTIPLIER;
10359 }
10360 if (duration < 1) {
10361 // Don't divide by zero
10362 duration = 1;
10363 }
10364 mDimTargetAlpha = target;
10365 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10366 }
10367 }
10368
10369 /**
10370 * Updating the surface's alpha. Returns true if the animation continues, or returns
10371 * false when the animation is finished and the dim surface is hidden.
10372 */
10373 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10374 if (!dimming) {
10375 if (mDimTargetAlpha != 0) {
10376 mLastDimAnimTime = currentTime;
10377 mDimTargetAlpha = 0;
10378 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10379 }
10380 }
10381
10382 boolean animating = false;
10383 if (mLastDimAnimTime != 0) {
10384 mDimCurrentAlpha += mDimDeltaPerMs
10385 * (currentTime-mLastDimAnimTime);
10386 boolean more = true;
10387 if (displayFrozen) {
10388 // If the display is frozen, there is no reason to animate.
10389 more = false;
10390 } else if (mDimDeltaPerMs > 0) {
10391 if (mDimCurrentAlpha > mDimTargetAlpha) {
10392 more = false;
10393 }
10394 } else if (mDimDeltaPerMs < 0) {
10395 if (mDimCurrentAlpha < mDimTargetAlpha) {
10396 more = false;
10397 }
10398 } else {
10399 more = false;
10400 }
10401
10402 // Do we need to continue animating?
10403 if (more) {
10404 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10405 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10406 mLastDimAnimTime = currentTime;
10407 mDimSurface.setAlpha(mDimCurrentAlpha);
10408 animating = true;
10409 } else {
10410 mDimCurrentAlpha = mDimTargetAlpha;
10411 mLastDimAnimTime = 0;
10412 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10413 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10414 mDimSurface.setAlpha(mDimCurrentAlpha);
10415 if (!dimming) {
10416 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10417 + ": HIDE");
10418 try {
10419 mDimSurface.hide();
10420 } catch (RuntimeException e) {
10421 Log.w(TAG, "Illegal argument exception hiding dim surface");
10422 }
10423 mDimShown = false;
10424 }
10425 }
10426 }
10427 return animating;
10428 }
10429
10430 public void printTo(PrintWriter pw) {
10431 pw.print(" mDimShown="); pw.print(mDimShown);
10432 pw.print(" current="); pw.print(mDimCurrentAlpha);
10433 pw.print(" target="); pw.print(mDimTargetAlpha);
10434 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10435 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10436 }
10437 }
10438
10439 /**
10440 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10441 * This is used for opening/closing transition for apps in compatible mode.
10442 */
10443 private static class FadeInOutAnimation extends Animation {
10444 int mWidth;
10445 boolean mFadeIn;
10446
10447 public FadeInOutAnimation(boolean fadeIn) {
10448 setInterpolator(new AccelerateInterpolator());
10449 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10450 mFadeIn = fadeIn;
10451 }
10452
10453 @Override
10454 protected void applyTransformation(float interpolatedTime, Transformation t) {
10455 float x = interpolatedTime;
10456 if (!mFadeIn) {
10457 x = 1.0f - x; // reverse the interpolation for fade out
10458 }
10459 if (x < 0.5) {
10460 // move the window out of the screen.
10461 t.getMatrix().setTranslate(mWidth, 0);
10462 } else {
10463 t.getMatrix().setTranslate(0, 0);// show
10464 t.setAlpha((x - 0.5f) * 2);
10465 }
10466 }
10467
10468 @Override
10469 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10470 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10471 mWidth = width;
10472 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010473
10474 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010475 public int getZAdjustment() {
10476 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010477 }
10478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479}