blob: b4f47681f665d4b781eb5cced52552df3c45f1f2 [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 Hackborn759a39e2009-08-09 17:20:27 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 AppWindowToken mFocusedApp = null;
423
424 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 float mWindowAnimationScale = 1.0f;
427 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 final KeyWaiter mKeyWaiter = new KeyWaiter();
430 final KeyQ mQueue;
431 final InputDispatcherThread mInputThread;
432
433 // Who is holding the screen on.
434 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Whether the UI is currently running in touch mode (not showing
438 * navigational focus because the user is directly pressing the screen).
439 */
440 boolean mInTouchMode = false;
441
442 private ViewServer mViewServer;
443
444 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700445
Dianne Hackbornc485a602009-03-24 22:39:49 -0700446 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700447 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700448
449 // The frame use to limit the size of the app running in compatibility mode.
450 Rect mCompatibleScreenFrame = new Rect();
451 // The surface used to fill the outer rim of the app running in compatibility mode.
452 Surface mBackgroundFillerSurface = null;
453 boolean mBackgroundFillerShown = false;
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public static WindowManagerService main(Context context,
456 PowerManagerService pm, boolean haveInputMethods) {
457 WMThread thr = new WMThread(context, pm, haveInputMethods);
458 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (thr) {
461 while (thr.mService == null) {
462 try {
463 thr.wait();
464 } catch (InterruptedException e) {
465 }
466 }
467 }
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 return thr.mService;
470 }
Romain Guy06882f82009-06-10 13:36:04 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 static class WMThread extends Thread {
473 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 private final Context mContext;
476 private final PowerManagerService mPM;
477 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 public WMThread(Context context, PowerManagerService pm,
480 boolean haveInputMethods) {
481 super("WindowManager");
482 mContext = context;
483 mPM = pm;
484 mHaveInputMethods = haveInputMethods;
485 }
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public void run() {
488 Looper.prepare();
489 WindowManagerService s = new WindowManagerService(mContext, mPM,
490 mHaveInputMethods);
491 android.os.Process.setThreadPriority(
492 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 synchronized (this) {
495 mService = s;
496 notifyAll();
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 Looper.loop();
500 }
501 }
502
503 static class PolicyThread extends Thread {
504 private final WindowManagerPolicy mPolicy;
505 private final WindowManagerService mService;
506 private final Context mContext;
507 private final PowerManagerService mPM;
508 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public PolicyThread(WindowManagerPolicy policy,
511 WindowManagerService service, Context context,
512 PowerManagerService pm) {
513 super("WindowManagerPolicy");
514 mPolicy = policy;
515 mService = service;
516 mContext = context;
517 mPM = pm;
518 }
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public void run() {
521 Looper.prepare();
522 //Looper.myLooper().setMessageLogging(new LogPrinter(
523 // Log.VERBOSE, "WindowManagerPolicy"));
524 android.os.Process.setThreadPriority(
525 android.os.Process.THREAD_PRIORITY_FOREGROUND);
526 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 synchronized (this) {
529 mRunning = true;
530 notifyAll();
531 }
Romain Guy06882f82009-06-10 13:36:04 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 Looper.loop();
534 }
535 }
536
537 private WindowManagerService(Context context, PowerManagerService pm,
538 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700539 if (MEASURE_LATENCY) {
540 lt = new LatencyTimer(100, 1000);
541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mContext = context;
544 mHaveInputMethods = haveInputMethods;
545 mLimitedAlphaCompositing = context.getResources().getBoolean(
546 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 mPowerManager = pm;
549 mPowerManager.setPolicy(mPolicy);
550 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
551 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
552 "SCREEN_FROZEN");
553 mScreenFrozenLock.setReferenceCounted(false);
554
555 mActivityManager = ActivityManagerNative.getDefault();
556 mBatteryStats = BatteryStatsService.getService();
557
558 // Get persisted window scale setting
559 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
560 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
561 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
562 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700563
Michael Chan9f028e62009-08-04 17:37:46 -0700564 int max_events_per_sec = 35;
565 try {
566 max_events_per_sec = Integer.parseInt(SystemProperties
567 .get("windowsmgr.max_events_per_sec"));
568 if (max_events_per_sec < 1) {
569 max_events_per_sec = 35;
570 }
571 } catch (NumberFormatException e) {
572 }
573 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mQueue = new KeyQ();
576
577 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
580 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 synchronized (thr) {
583 while (!thr.mRunning) {
584 try {
585 thr.wait();
586 } catch (InterruptedException e) {
587 }
588 }
589 }
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 // Add ourself to the Watchdog monitors.
594 Watchdog.getInstance().addMonitor(this);
595 }
596
597 @Override
598 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
599 throws RemoteException {
600 try {
601 return super.onTransact(code, data, reply, flags);
602 } catch (RuntimeException e) {
603 // The window manager only throws security exceptions, so let's
604 // log all others.
605 if (!(e instanceof SecurityException)) {
606 Log.e(TAG, "Window Manager Crash", e);
607 }
608 throw e;
609 }
610 }
611
612 private void placeWindowAfter(Object pos, WindowState window) {
613 final int i = mWindows.indexOf(pos);
614 if (localLOGV || DEBUG_FOCUS) Log.v(
615 TAG, "Adding window " + window + " at "
616 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
617 mWindows.add(i+1, window);
618 }
619
620 private void placeWindowBefore(Object pos, WindowState window) {
621 final int i = mWindows.indexOf(pos);
622 if (localLOGV || DEBUG_FOCUS) Log.v(
623 TAG, "Adding window " + window + " at "
624 + i + " of " + mWindows.size() + " (before " + pos + ")");
625 mWindows.add(i, window);
626 }
627
628 //This method finds out the index of a window that has the same app token as
629 //win. used for z ordering the windows in mWindows
630 private int findIdxBasedOnAppTokens(WindowState win) {
631 //use a local variable to cache mWindows
632 ArrayList localmWindows = mWindows;
633 int jmax = localmWindows.size();
634 if(jmax == 0) {
635 return -1;
636 }
637 for(int j = (jmax-1); j >= 0; j--) {
638 WindowState wentry = (WindowState)localmWindows.get(j);
639 if(wentry.mAppToken == win.mAppToken) {
640 return j;
641 }
642 }
643 return -1;
644 }
Romain Guy06882f82009-06-10 13:36:04 -0700645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
647 final IWindow client = win.mClient;
648 final WindowToken token = win.mToken;
649 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 final int N = localmWindows.size();
652 final WindowState attached = win.mAttachedWindow;
653 int i;
654 if (attached == null) {
655 int tokenWindowsPos = token.windows.size();
656 if (token.appWindowToken != null) {
657 int index = tokenWindowsPos-1;
658 if (index >= 0) {
659 // If this application has existing windows, we
660 // simply place the new window on top of them... but
661 // keep the starting window on top.
662 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
663 // Base windows go behind everything else.
664 placeWindowBefore(token.windows.get(0), win);
665 tokenWindowsPos = 0;
666 } else {
667 AppWindowToken atoken = win.mAppToken;
668 if (atoken != null &&
669 token.windows.get(index) == atoken.startingWindow) {
670 placeWindowBefore(token.windows.get(index), win);
671 tokenWindowsPos--;
672 } else {
673 int newIdx = findIdxBasedOnAppTokens(win);
674 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700675 //there is a window above this one associated with the same
676 //apptoken note that the window could be a floating window
677 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 //windows associated with this token.
679 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
683 } else {
684 if (localLOGV) Log.v(
685 TAG, "Figuring out where to add app window "
686 + client.asBinder() + " (token=" + token + ")");
687 // Figure out where the window should go, based on the
688 // order of applications.
689 final int NA = mAppTokens.size();
690 Object pos = null;
691 for (i=NA-1; i>=0; i--) {
692 AppWindowToken t = mAppTokens.get(i);
693 if (t == token) {
694 i--;
695 break;
696 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700697
698 // We haven't reached the token yet; if this token
699 // is not going to the bottom and has windows, we can
700 // use it as an anchor for when we do reach the token.
701 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 pos = t.windows.get(0);
703 }
704 }
705 // We now know the index into the apps. If we found
706 // an app window above, that gives us the position; else
707 // we need to look some more.
708 if (pos != null) {
709 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700710 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 mTokenMap.get(((WindowState)pos).mClient.asBinder());
712 if (atoken != null) {
713 final int NC = atoken.windows.size();
714 if (NC > 0) {
715 WindowState bottom = atoken.windows.get(0);
716 if (bottom.mSubLayer < 0) {
717 pos = bottom;
718 }
719 }
720 }
721 placeWindowBefore(pos, win);
722 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700723 // Continue looking down until we find the first
724 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 while (i >= 0) {
726 AppWindowToken t = mAppTokens.get(i);
727 final int NW = t.windows.size();
728 if (NW > 0) {
729 pos = t.windows.get(NW-1);
730 break;
731 }
732 i--;
733 }
734 if (pos != null) {
735 // Move in front of any windows attached to this
736 // one.
737 WindowToken atoken =
738 mTokenMap.get(((WindowState)pos).mClient.asBinder());
739 if (atoken != null) {
740 final int NC = atoken.windows.size();
741 if (NC > 0) {
742 WindowState top = atoken.windows.get(NC-1);
743 if (top.mSubLayer >= 0) {
744 pos = top;
745 }
746 }
747 }
748 placeWindowAfter(pos, win);
749 } else {
750 // Just search for the start of this layer.
751 final int myLayer = win.mBaseLayer;
752 for (i=0; i<N; i++) {
753 WindowState w = (WindowState)localmWindows.get(i);
754 if (w.mBaseLayer > myLayer) {
755 break;
756 }
757 }
758 if (localLOGV || DEBUG_FOCUS) Log.v(
759 TAG, "Adding window " + win + " at "
760 + i + " of " + N);
761 localmWindows.add(i, win);
762 }
763 }
764 }
765 } else {
766 // Figure out where window should go, based on layer.
767 final int myLayer = win.mBaseLayer;
768 for (i=N-1; i>=0; i--) {
769 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
770 i++;
771 break;
772 }
773 }
774 if (i < 0) i = 0;
775 if (localLOGV || DEBUG_FOCUS) Log.v(
776 TAG, "Adding window " + win + " at "
777 + i + " of " + N);
778 localmWindows.add(i, win);
779 }
780 if (addToToken) {
781 token.windows.add(tokenWindowsPos, win);
782 }
783
784 } else {
785 // Figure out this window's ordering relative to the window
786 // it is attached to.
787 final int NA = token.windows.size();
788 final int sublayer = win.mSubLayer;
789 int largestSublayer = Integer.MIN_VALUE;
790 WindowState windowWithLargestSublayer = null;
791 for (i=0; i<NA; i++) {
792 WindowState w = token.windows.get(i);
793 final int wSublayer = w.mSubLayer;
794 if (wSublayer >= largestSublayer) {
795 largestSublayer = wSublayer;
796 windowWithLargestSublayer = w;
797 }
798 if (sublayer < 0) {
799 // For negative sublayers, we go below all windows
800 // in the same sublayer.
801 if (wSublayer >= sublayer) {
802 if (addToToken) {
803 token.windows.add(i, win);
804 }
805 placeWindowBefore(
806 wSublayer >= 0 ? attached : w, win);
807 break;
808 }
809 } else {
810 // For positive sublayers, we go above all windows
811 // in the same sublayer.
812 if (wSublayer > sublayer) {
813 if (addToToken) {
814 token.windows.add(i, win);
815 }
816 placeWindowBefore(w, win);
817 break;
818 }
819 }
820 }
821 if (i >= NA) {
822 if (addToToken) {
823 token.windows.add(win);
824 }
825 if (sublayer < 0) {
826 placeWindowBefore(attached, win);
827 } else {
828 placeWindowAfter(largestSublayer >= 0
829 ? windowWithLargestSublayer
830 : attached,
831 win);
832 }
833 }
834 }
Romain Guy06882f82009-06-10 13:36:04 -0700835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 if (win.mAppToken != null && addToToken) {
837 win.mAppToken.allAppWindows.add(win);
838 }
839 }
Romain Guy06882f82009-06-10 13:36:04 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 static boolean canBeImeTarget(WindowState w) {
842 final int fl = w.mAttrs.flags
843 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
844 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
845 return w.isVisibleOrAdding();
846 }
847 return false;
848 }
Romain Guy06882f82009-06-10 13:36:04 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
851 final ArrayList localmWindows = mWindows;
852 final int N = localmWindows.size();
853 WindowState w = null;
854 int i = N;
855 while (i > 0) {
856 i--;
857 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
860 // + Integer.toHexString(w.mAttrs.flags));
861 if (canBeImeTarget(w)) {
862 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 // Yet more tricksyness! If this window is a "starting"
865 // window, we do actually want to be on top of it, but
866 // it is not -really- where input will go. So if the caller
867 // is not actually looking to move the IME, look down below
868 // for a real window to target...
869 if (!willMove
870 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
871 && i > 0) {
872 WindowState wb = (WindowState)localmWindows.get(i-1);
873 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
874 i--;
875 w = wb;
876 }
877 }
878 break;
879 }
880 }
Romain Guy06882f82009-06-10 13:36:04 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
885 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 if (willMove && w != null) {
888 final WindowState curTarget = mInputMethodTarget;
889 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 // Now some fun for dealing with window animations that
892 // modify the Z order. We need to look at all windows below
893 // the current target that are in this app, finding the highest
894 // visible one in layering.
895 AppWindowToken token = curTarget.mAppToken;
896 WindowState highestTarget = null;
897 int highestPos = 0;
898 if (token.animating || token.animation != null) {
899 int pos = 0;
900 pos = localmWindows.indexOf(curTarget);
901 while (pos >= 0) {
902 WindowState win = (WindowState)localmWindows.get(pos);
903 if (win.mAppToken != token) {
904 break;
905 }
906 if (!win.mRemoved) {
907 if (highestTarget == null || win.mAnimLayer >
908 highestTarget.mAnimLayer) {
909 highestTarget = win;
910 highestPos = pos;
911 }
912 }
913 pos--;
914 }
915 }
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700918 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 + mNextAppTransition + " " + highestTarget
920 + " animating=" + highestTarget.isAnimating()
921 + " layer=" + highestTarget.mAnimLayer
922 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
925 // If we are currently setting up for an animation,
926 // hold everything until we can find out what will happen.
927 mInputMethodTargetWaitingAnim = true;
928 mInputMethodTarget = highestTarget;
929 return highestPos + 1;
930 } else if (highestTarget.isAnimating() &&
931 highestTarget.mAnimLayer > w.mAnimLayer) {
932 // If the window we are currently targeting is involved
933 // with an animation, and it is on top of the next target
934 // we will be over, then hold off on moving until
935 // that is done.
936 mInputMethodTarget = highestTarget;
937 return highestPos + 1;
938 }
939 }
940 }
941 }
Romain Guy06882f82009-06-10 13:36:04 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 //Log.i(TAG, "Placing input method @" + (i+1));
944 if (w != null) {
945 if (willMove) {
946 RuntimeException e = new RuntimeException();
947 e.fillInStackTrace();
948 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
949 + mInputMethodTarget + " to " + w, e);
950 mInputMethodTarget = w;
951 if (w.mAppToken != null) {
952 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
953 } else {
954 setInputMethodAnimLayerAdjustment(0);
955 }
956 }
957 return i+1;
958 }
959 if (willMove) {
960 RuntimeException e = new RuntimeException();
961 e.fillInStackTrace();
962 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
963 + mInputMethodTarget + " to null", e);
964 mInputMethodTarget = null;
965 setInputMethodAnimLayerAdjustment(0);
966 }
967 return -1;
968 }
Romain Guy06882f82009-06-10 13:36:04 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 void addInputMethodWindowToListLocked(WindowState win) {
971 int pos = findDesiredInputMethodWindowIndexLocked(true);
972 if (pos >= 0) {
973 win.mTargetAppToken = mInputMethodTarget.mAppToken;
974 mWindows.add(pos, win);
975 moveInputMethodDialogsLocked(pos+1);
976 return;
977 }
978 win.mTargetAppToken = null;
979 addWindowToListInOrderLocked(win, true);
980 moveInputMethodDialogsLocked(pos);
981 }
Romain Guy06882f82009-06-10 13:36:04 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 void setInputMethodAnimLayerAdjustment(int adj) {
984 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
985 mInputMethodAnimLayerAdjustment = adj;
986 WindowState imw = mInputMethodWindow;
987 if (imw != null) {
988 imw.mAnimLayer = imw.mLayer + adj;
989 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
990 + " anim layer: " + imw.mAnimLayer);
991 int wi = imw.mChildWindows.size();
992 while (wi > 0) {
993 wi--;
994 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
995 cw.mAnimLayer = cw.mLayer + adj;
996 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
997 + " anim layer: " + cw.mAnimLayer);
998 }
999 }
1000 int di = mInputMethodDialogs.size();
1001 while (di > 0) {
1002 di --;
1003 imw = mInputMethodDialogs.get(di);
1004 imw.mAnimLayer = imw.mLayer + adj;
1005 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1006 + " anim layer: " + imw.mAnimLayer);
1007 }
1008 }
Romain Guy06882f82009-06-10 13:36:04 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1011 int wpos = mWindows.indexOf(win);
1012 if (wpos >= 0) {
1013 if (wpos < interestingPos) interestingPos--;
1014 mWindows.remove(wpos);
1015 int NC = win.mChildWindows.size();
1016 while (NC > 0) {
1017 NC--;
1018 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1019 int cpos = mWindows.indexOf(cw);
1020 if (cpos >= 0) {
1021 if (cpos < interestingPos) interestingPos--;
1022 mWindows.remove(cpos);
1023 }
1024 }
1025 }
1026 return interestingPos;
1027 }
Romain Guy06882f82009-06-10 13:36:04 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 private void reAddWindowToListInOrderLocked(WindowState win) {
1030 addWindowToListInOrderLocked(win, false);
1031 // This is a hack to get all of the child windows added as well
1032 // at the right position. Child windows should be rare and
1033 // this case should be rare, so it shouldn't be that big a deal.
1034 int wpos = mWindows.indexOf(win);
1035 if (wpos >= 0) {
1036 mWindows.remove(wpos);
1037 reAddWindowLocked(wpos, win);
1038 }
1039 }
Romain Guy06882f82009-06-10 13:36:04 -07001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 void logWindowList(String prefix) {
1042 int N = mWindows.size();
1043 while (N > 0) {
1044 N--;
1045 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1046 }
1047 }
Romain Guy06882f82009-06-10 13:36:04 -07001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 void moveInputMethodDialogsLocked(int pos) {
1050 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 final int N = dialogs.size();
1053 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1054 for (int i=0; i<N; i++) {
1055 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1056 }
1057 if (DEBUG_INPUT_METHOD) {
1058 Log.v(TAG, "Window list w/pos=" + pos);
1059 logWindowList(" ");
1060 }
Romain Guy06882f82009-06-10 13:36:04 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 if (pos >= 0) {
1063 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1064 if (pos < mWindows.size()) {
1065 WindowState wp = (WindowState)mWindows.get(pos);
1066 if (wp == mInputMethodWindow) {
1067 pos++;
1068 }
1069 }
1070 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1071 for (int i=0; i<N; i++) {
1072 WindowState win = dialogs.get(i);
1073 win.mTargetAppToken = targetAppToken;
1074 pos = reAddWindowLocked(pos, win);
1075 }
1076 if (DEBUG_INPUT_METHOD) {
1077 Log.v(TAG, "Final window list:");
1078 logWindowList(" ");
1079 }
1080 return;
1081 }
1082 for (int i=0; i<N; i++) {
1083 WindowState win = dialogs.get(i);
1084 win.mTargetAppToken = null;
1085 reAddWindowToListInOrderLocked(win);
1086 if (DEBUG_INPUT_METHOD) {
1087 Log.v(TAG, "No IM target, final list:");
1088 logWindowList(" ");
1089 }
1090 }
1091 }
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1094 final WindowState imWin = mInputMethodWindow;
1095 final int DN = mInputMethodDialogs.size();
1096 if (imWin == null && DN == 0) {
1097 return false;
1098 }
Romain Guy06882f82009-06-10 13:36:04 -07001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1101 if (imPos >= 0) {
1102 // In this case, the input method windows are to be placed
1103 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 // First check to see if the input method windows are already
1106 // located here, and contiguous.
1107 final int N = mWindows.size();
1108 WindowState firstImWin = imPos < N
1109 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 // Figure out the actual input method window that should be
1112 // at the bottom of their stack.
1113 WindowState baseImWin = imWin != null
1114 ? imWin : mInputMethodDialogs.get(0);
1115 if (baseImWin.mChildWindows.size() > 0) {
1116 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1117 if (cw.mSubLayer < 0) baseImWin = cw;
1118 }
Romain Guy06882f82009-06-10 13:36:04 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (firstImWin == baseImWin) {
1121 // The windows haven't moved... but are they still contiguous?
1122 // First find the top IM window.
1123 int pos = imPos+1;
1124 while (pos < N) {
1125 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1126 break;
1127 }
1128 pos++;
1129 }
1130 pos++;
1131 // Now there should be no more input method windows above.
1132 while (pos < N) {
1133 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1134 break;
1135 }
1136 pos++;
1137 }
1138 if (pos >= N) {
1139 // All is good!
1140 return false;
1141 }
1142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 if (imWin != null) {
1145 if (DEBUG_INPUT_METHOD) {
1146 Log.v(TAG, "Moving IM from " + imPos);
1147 logWindowList(" ");
1148 }
1149 imPos = tmpRemoveWindowLocked(imPos, imWin);
1150 if (DEBUG_INPUT_METHOD) {
1151 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1152 logWindowList(" ");
1153 }
1154 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1155 reAddWindowLocked(imPos, imWin);
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "List after moving IM to " + imPos + ":");
1158 logWindowList(" ");
1159 }
1160 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1161 } else {
1162 moveInputMethodDialogsLocked(imPos);
1163 }
Romain Guy06882f82009-06-10 13:36:04 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 } else {
1166 // In this case, the input method windows go in a fixed layer,
1167 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 if (imWin != null) {
1170 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1171 tmpRemoveWindowLocked(0, imWin);
1172 imWin.mTargetAppToken = null;
1173 reAddWindowToListInOrderLocked(imWin);
1174 if (DEBUG_INPUT_METHOD) {
1175 Log.v(TAG, "List with no IM target:");
1176 logWindowList(" ");
1177 }
1178 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1179 } else {
1180 moveInputMethodDialogsLocked(-1);;
1181 }
Romain Guy06882f82009-06-10 13:36:04 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
Romain Guy06882f82009-06-10 13:36:04 -07001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (needAssignLayers) {
1186 assignLayersLocked();
1187 }
Romain Guy06882f82009-06-10 13:36:04 -07001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 return true;
1190 }
Romain Guy06882f82009-06-10 13:36:04 -07001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 void adjustInputMethodDialogsLocked() {
1193 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1194 }
Romain Guy06882f82009-06-10 13:36:04 -07001195
Dianne Hackborn25994b42009-09-04 14:21:19 -07001196 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1197 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1198 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1199 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1200 ? wallpaperTarget.mAppToken.animation : null)
1201 + " upper=" + mUpperWallpaperTarget
1202 + " lower=" + mLowerWallpaperTarget);
1203 return (wallpaperTarget != null
1204 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1205 && wallpaperTarget.mAppToken.animation != null)))
1206 || mUpperWallpaperTarget != null
1207 || mLowerWallpaperTarget != null;
1208 }
1209
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001210 boolean adjustWallpaperWindowsLocked() {
1211 boolean changed = false;
1212
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001213 final int dw = mDisplay.getWidth();
1214 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001215
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001216 // First find top-most window that has asked to be on top of the
1217 // wallpaper; all wallpapers go behind it.
1218 final ArrayList localmWindows = mWindows;
1219 int N = localmWindows.size();
1220 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001221 WindowState foundW = null;
1222 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001223 int i = N;
1224 while (i > 0) {
1225 i--;
1226 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001227 if (w.mAppToken != null) {
1228 // If this window's app token is hidden and not animating,
1229 // it is of no interest to us.
1230 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1231 if (DEBUG_WALLPAPER) Log.v(TAG,
1232 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001233 continue;
1234 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001235 // If this window's app token is ot fullscreen, also irrelevant.
1236 if (!w.mAppToken.appFullscreen) {
1237 if (DEBUG_WALLPAPER) Log.v(TAG,
1238 "Skipping non-fullscreen token: " + w);
1239 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001240 }
1241 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001242 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1243 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1244 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001245 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001246 && (mWallpaperTarget == w
1247 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001248 if (DEBUG_WALLPAPER) Log.v(TAG,
1249 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001250 foundW = w;
1251 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001252 if (w == mWallpaperTarget && w.mAppToken != null
1253 && w.mAppToken.animation != null) {
1254 // The current wallpaper target is animating, so we'll
1255 // look behind it for another possible target and figure
1256 // out what is going on below.
1257 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1258 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001259 continue;
1260 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001261 break;
1262 }
1263 }
1264
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001265 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1266 // If we are currently waiting for an app transition, and either
1267 // the current target or the next target are involved with it,
1268 // then hold off on doing anything with the wallpaper.
1269 // Note that we are checking here for just whether the target
1270 // is part of an app token... which is potentially overly aggressive
1271 // (the app token may not be involved in the transition), but good
1272 // enough (we'll just wait until whatever transition is pending
1273 // executes).
1274 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001275 if (DEBUG_WALLPAPER) Log.v(TAG,
1276 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001277 return false;
1278 }
1279 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001280 if (DEBUG_WALLPAPER) Log.v(TAG,
1281 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001282 return false;
1283 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001284 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001285
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001287 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001288 Log.v(TAG, "New wallpaper target: " + foundW
1289 + " oldTarget: " + mWallpaperTarget);
1290 }
1291
1292 mLowerWallpaperTarget = null;
1293 mUpperWallpaperTarget = null;
1294
1295 WindowState oldW = mWallpaperTarget;
1296 mWallpaperTarget = foundW;
1297
1298 // Now what is happening... if the current and new targets are
1299 // animating, then we are in our super special mode!
1300 if (foundW != null && foundW.mAppToken != null && oldW != null
1301 && oldW.mAppToken != null) {
1302 if (DEBUG_WALLPAPER) {
1303 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1304 + " old animation: " + oldW.mAppToken.animation);
1305 }
1306 if (foundW.mAppToken.animation != null
1307 && oldW.mAppToken.animation != null) {
1308 int oldI = localmWindows.indexOf(oldW);
1309 if (DEBUG_WALLPAPER) {
1310 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1311 }
1312 if (oldI >= 0) {
1313 if (DEBUG_WALLPAPER) {
1314 Log.v(TAG, "Animating wallpapers: old#" + oldI
1315 + "=" + oldW + "; new#" + foundI
1316 + "=" + foundW);
1317 }
1318
1319 // Set the new target correctly.
1320 if (foundW.mAppToken.hiddenRequested) {
1321 if (DEBUG_WALLPAPER) {
1322 Log.v(TAG, "Old wallpaper still the target.");
1323 }
1324 mWallpaperTarget = oldW;
1325 }
1326
1327 // Now set the upper and lower wallpaper targets
1328 // correctly, and make sure that we are positioning
1329 // the wallpaper below the lower.
1330 if (foundI > oldI) {
1331 // The new target is on top of the old one.
1332 if (DEBUG_WALLPAPER) {
1333 Log.v(TAG, "Found target above old target.");
1334 }
1335 mUpperWallpaperTarget = foundW;
1336 mLowerWallpaperTarget = oldW;
1337 foundW = oldW;
1338 foundI = oldI;
1339 } else {
1340 // The new target is below the old one.
1341 if (DEBUG_WALLPAPER) {
1342 Log.v(TAG, "Found target below old target.");
1343 }
1344 mUpperWallpaperTarget = oldW;
1345 mLowerWallpaperTarget = foundW;
1346 }
1347 }
1348 }
1349 }
1350
1351 } else {
1352 // Is it time to stop animating?
1353 if (mLowerWallpaperTarget == null
1354 || mLowerWallpaperTarget.mAppToken.animation == null
1355 || mUpperWallpaperTarget == null
1356 || mUpperWallpaperTarget.mAppToken.animation == null) {
1357 if (DEBUG_WALLPAPER) {
1358 Log.v(TAG, "No longer animating wallpaper targets!");
1359 }
1360 mLowerWallpaperTarget = null;
1361 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001362 }
1363 }
1364
1365 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001366 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001367 // The window is visible to the compositor... but is it visible
1368 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001369 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001370 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001371
1372 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001373 // its layer adjustment. Only do this if we are not transfering
1374 // between two wallpaper targets.
1375 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001376 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001377 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001378
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001379 // Now w is the window we are supposed to be behind... but we
1380 // need to be sure to also be behind any of its attached windows,
1381 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001382 while (foundI > 0) {
1383 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1384 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001385 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001386 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001387 // This window is not related to the previous one in any
1388 // interesting way, so stop here.
1389 break;
1390 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001391 foundW = wb;
1392 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001393 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001394 } else {
1395 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001396 }
1397
1398 // Okay i is the position immediately above the wallpaper. Look at
1399 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001400 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001401
Dianne Hackborn284ac932009-08-28 10:34:25 -07001402 if (visible) {
1403 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1404 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1405 }
1406
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001407 // Start stepping backwards from here, ensuring that our wallpaper windows
1408 // are correctly placed.
1409 int curTokenIndex = mWallpaperTokens.size();
1410 while (curTokenIndex > 0) {
1411 curTokenIndex--;
1412 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn25994b42009-09-04 14:21:19 -07001413 token.hidden = !visible;
1414
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001415 int curWallpaperIndex = token.windows.size();
1416 while (curWallpaperIndex > 0) {
1417 curWallpaperIndex--;
1418 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001419
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001420 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001421 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001422 }
1423
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001424 // First, make sure the client has the current visibility
1425 // state.
1426 if (wallpaper.mWallpaperVisible != visible) {
1427 wallpaper.mWallpaperVisible = visible;
1428 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001429 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001430 "Setting visibility of wallpaper " + wallpaper
1431 + ": " + visible);
1432 wallpaper.mClient.dispatchAppVisibility(visible);
1433 } catch (RemoteException e) {
1434 }
1435 }
1436
1437 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001438 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1439 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001440
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001441 // First, if this window is at the current index, then all
1442 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001443 if (wallpaper == foundW) {
1444 foundI--;
1445 foundW = foundI > 0
1446 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001447 continue;
1448 }
1449
1450 // The window didn't match... the current wallpaper window,
1451 // wherever it is, is in the wrong place, so make sure it is
1452 // not in the list.
1453 int oldIndex = localmWindows.indexOf(wallpaper);
1454 if (oldIndex >= 0) {
1455 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001456 if (oldIndex < foundI) {
1457 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001458 }
1459 }
1460
1461 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001462 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001463 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001464
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001465 localmWindows.add(foundI, wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001466 changed = true;
1467 }
1468 }
1469
1470 return changed;
1471 }
1472
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001473 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001474 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1475 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001476 mWallpaperAnimLayerAdjustment = adj;
1477 int curTokenIndex = mWallpaperTokens.size();
1478 while (curTokenIndex > 0) {
1479 curTokenIndex--;
1480 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1481 int curWallpaperIndex = token.windows.size();
1482 while (curWallpaperIndex > 0) {
1483 curWallpaperIndex--;
1484 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1485 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001486 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1487 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001488 }
1489 }
1490 }
1491
Dianne Hackborn284ac932009-08-28 10:34:25 -07001492 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001493 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001494 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001495 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001496 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001497 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001498 changed = wallpaperWin.mXOffset != offset;
1499 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001500 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1501 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001502 wallpaperWin.mXOffset = offset;
1503 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001504 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1505 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001506 rawChanged = true;
1507 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001508 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001509
Dianne Hackborn284ac932009-08-28 10:34:25 -07001510 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001511 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001512 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001513 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001514 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1515 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001516 changed = true;
1517 wallpaperWin.mYOffset = offset;
1518 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001519 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1520 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001521 rawChanged = true;
1522 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001523 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001524
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001525 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001526 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001527 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1528 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1529 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001530 wallpaperWin.mClient.dispatchWallpaperOffsets(
1531 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1532 } catch (RemoteException e) {
1533 }
1534 }
1535
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001536 return changed;
1537 }
1538
1539 boolean updateWallpaperOffsetLocked() {
1540 final int dw = mDisplay.getWidth();
1541 final int dh = mDisplay.getHeight();
1542
1543 boolean changed = false;
1544
1545 WindowState target = mWallpaperTarget;
1546 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001547 mLastWallpaperX = target.mWallpaperX;
1548 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001549 int curTokenIndex = mWallpaperTokens.size();
1550 while (curTokenIndex > 0) {
1551 curTokenIndex--;
1552 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1553 int curWallpaperIndex = token.windows.size();
1554 while (curWallpaperIndex > 0) {
1555 curWallpaperIndex--;
1556 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn284ac932009-08-28 10:34:25 -07001557 if (updateWallpaperOffsetLocked(wallpaper, dw, dh)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001558 wallpaper.computeShownFrameLocked();
1559 changed = true;
1560 }
1561 }
1562 }
1563 }
1564
1565 return changed;
1566 }
1567
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001568 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001569 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001570 final int dw = mDisplay.getWidth();
1571 final int dh = mDisplay.getHeight();
1572
1573 int curTokenIndex = mWallpaperTokens.size();
1574 while (curTokenIndex > 0) {
1575 curTokenIndex--;
1576 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn25994b42009-09-04 14:21:19 -07001577 token.hidden = !visible;
1578
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001579 int curWallpaperIndex = token.windows.size();
1580 while (curWallpaperIndex > 0) {
1581 curWallpaperIndex--;
1582 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1583 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001584 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001585 }
1586
1587 if (wallpaper.mWallpaperVisible != visible) {
1588 wallpaper.mWallpaperVisible = visible;
1589 try {
1590 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001591 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001592 + ": " + visible);
1593 wallpaper.mClient.dispatchAppVisibility(visible);
1594 } catch (RemoteException e) {
1595 }
1596 }
1597 }
1598 }
1599 }
1600
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001601 void sendPointerToWallpaperLocked(WindowState srcWin,
1602 MotionEvent pointer, long eventTime) {
1603 int curTokenIndex = mWallpaperTokens.size();
1604 while (curTokenIndex > 0) {
1605 curTokenIndex--;
1606 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1607 int curWallpaperIndex = token.windows.size();
1608 while (curWallpaperIndex > 0) {
1609 curWallpaperIndex--;
1610 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1611 if ((wallpaper.mAttrs.flags &
1612 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1613 continue;
1614 }
1615 try {
1616 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1617 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1618 srcWin.mFrame.top-wallpaper.mFrame.top);
1619 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1620 } catch (RemoteException e) {
1621 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1622 }
1623 }
1624 }
1625 }
1626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 public int addWindow(Session session, IWindow client,
1628 WindowManager.LayoutParams attrs, int viewVisibility,
1629 Rect outContentInsets) {
1630 int res = mPolicy.checkAddPermission(attrs);
1631 if (res != WindowManagerImpl.ADD_OKAY) {
1632 return res;
1633 }
Romain Guy06882f82009-06-10 13:36:04 -07001634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 boolean reportNewConfig = false;
1636 WindowState attachedWindow = null;
1637 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 synchronized(mWindowMap) {
1640 // Instantiating a Display requires talking with the simulator,
1641 // so don't do it until we know the system is mostly up and
1642 // running.
1643 if (mDisplay == null) {
1644 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1645 mDisplay = wm.getDefaultDisplay();
1646 mQueue.setDisplay(mDisplay);
1647 reportNewConfig = true;
1648 }
Romain Guy06882f82009-06-10 13:36:04 -07001649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (mWindowMap.containsKey(client.asBinder())) {
1651 Log.w(TAG, "Window " + client + " is already added");
1652 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1653 }
1654
1655 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001656 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 if (attachedWindow == null) {
1658 Log.w(TAG, "Attempted to add window with token that is not a window: "
1659 + attrs.token + ". Aborting.");
1660 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1661 }
1662 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1663 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1664 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1665 + attrs.token + ". Aborting.");
1666 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1667 }
1668 }
1669
1670 boolean addToken = false;
1671 WindowToken token = mTokenMap.get(attrs.token);
1672 if (token == null) {
1673 if (attrs.type >= FIRST_APPLICATION_WINDOW
1674 && attrs.type <= LAST_APPLICATION_WINDOW) {
1675 Log.w(TAG, "Attempted to add application window with unknown token "
1676 + attrs.token + ". Aborting.");
1677 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1678 }
1679 if (attrs.type == TYPE_INPUT_METHOD) {
1680 Log.w(TAG, "Attempted to add input method window with unknown token "
1681 + attrs.token + ". Aborting.");
1682 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1683 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001684 if (attrs.type == TYPE_WALLPAPER) {
1685 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1686 + attrs.token + ". Aborting.");
1687 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 token = new WindowToken(attrs.token, -1, false);
1690 addToken = true;
1691 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1692 && attrs.type <= LAST_APPLICATION_WINDOW) {
1693 AppWindowToken atoken = token.appWindowToken;
1694 if (atoken == null) {
1695 Log.w(TAG, "Attempted to add window with non-application token "
1696 + token + ". Aborting.");
1697 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1698 } else if (atoken.removed) {
1699 Log.w(TAG, "Attempted to add window with exiting application token "
1700 + token + ". Aborting.");
1701 return WindowManagerImpl.ADD_APP_EXITING;
1702 }
1703 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1704 // No need for this guy!
1705 if (localLOGV) Log.v(
1706 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1707 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1708 }
1709 } else if (attrs.type == TYPE_INPUT_METHOD) {
1710 if (token.windowType != TYPE_INPUT_METHOD) {
1711 Log.w(TAG, "Attempted to add input method window with bad token "
1712 + attrs.token + ". Aborting.");
1713 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1714 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001715 } else if (attrs.type == TYPE_WALLPAPER) {
1716 if (token.windowType != TYPE_WALLPAPER) {
1717 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1718 + attrs.token + ". Aborting.");
1719 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722
1723 win = new WindowState(session, client, token,
1724 attachedWindow, attrs, viewVisibility);
1725 if (win.mDeathRecipient == null) {
1726 // Client has apparently died, so there is no reason to
1727 // continue.
1728 Log.w(TAG, "Adding window client " + client.asBinder()
1729 + " that is dead, aborting.");
1730 return WindowManagerImpl.ADD_APP_EXITING;
1731 }
1732
1733 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 res = mPolicy.prepareAddWindowLw(win, attrs);
1736 if (res != WindowManagerImpl.ADD_OKAY) {
1737 return res;
1738 }
1739
1740 // From now on, no exceptions or errors allowed!
1741
1742 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 if (addToken) {
1747 mTokenMap.put(attrs.token, token);
1748 mTokenList.add(token);
1749 }
1750 win.attach();
1751 mWindowMap.put(client.asBinder(), win);
1752
1753 if (attrs.type == TYPE_APPLICATION_STARTING &&
1754 token.appWindowToken != null) {
1755 token.appWindowToken.startingWindow = win;
1756 }
1757
1758 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 if (attrs.type == TYPE_INPUT_METHOD) {
1761 mInputMethodWindow = win;
1762 addInputMethodWindowToListLocked(win);
1763 imMayMove = false;
1764 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1765 mInputMethodDialogs.add(win);
1766 addWindowToListInOrderLocked(win, true);
1767 adjustInputMethodDialogsLocked();
1768 imMayMove = false;
1769 } else {
1770 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001771 if (attrs.type == TYPE_WALLPAPER ||
1772 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1773 adjustWallpaperWindowsLocked();
1774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
Romain Guy06882f82009-06-10 13:36:04 -07001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 if (mInTouchMode) {
1782 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1783 }
1784 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1785 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1786 }
Romain Guy06882f82009-06-10 13:36:04 -07001787
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001788 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001790 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1791 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 imMayMove = false;
1793 }
1794 }
Romain Guy06882f82009-06-10 13:36:04 -07001795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001797 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
Romain Guy06882f82009-06-10 13:36:04 -07001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 assignLayersLocked();
1801 // Don't do layout here, the window must call
1802 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 //dump();
1805
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001806 if (focusChanged) {
1807 if (mCurrentFocus != null) {
1808 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1809 }
1810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 if (localLOGV) Log.v(
1812 TAG, "New client " + client.asBinder()
1813 + ": window=" + win);
1814 }
1815
1816 // sendNewConfiguration() checks caller permissions so we must call it with
1817 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1818 // identity anyway, so it's safe to just clear & restore around this whole
1819 // block.
1820 final long origId = Binder.clearCallingIdentity();
1821 if (reportNewConfig) {
1822 sendNewConfiguration();
1823 } else {
1824 // Update Orientation after adding a window, only if the window needs to be
1825 // displayed right away
1826 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001827 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 sendNewConfiguration();
1829 }
1830 }
1831 }
1832 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 return res;
1835 }
Romain Guy06882f82009-06-10 13:36:04 -07001836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 public void removeWindow(Session session, IWindow client) {
1838 synchronized(mWindowMap) {
1839 WindowState win = windowForClientLocked(session, client);
1840 if (win == null) {
1841 return;
1842 }
1843 removeWindowLocked(session, win);
1844 }
1845 }
Romain Guy06882f82009-06-10 13:36:04 -07001846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 public void removeWindowLocked(Session session, WindowState win) {
1848
1849 if (localLOGV || DEBUG_FOCUS) Log.v(
1850 TAG, "Remove " + win + " client="
1851 + Integer.toHexString(System.identityHashCode(
1852 win.mClient.asBinder()))
1853 + ", surface=" + win.mSurface);
1854
1855 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 if (DEBUG_APP_TRANSITIONS) Log.v(
1858 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1859 + " mExiting=" + win.mExiting
1860 + " isAnimating=" + win.isAnimating()
1861 + " app-animation="
1862 + (win.mAppToken != null ? win.mAppToken.animation : null)
1863 + " inPendingTransaction="
1864 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1865 + " mDisplayFrozen=" + mDisplayFrozen);
1866 // Visibility of the removed window. Will be used later to update orientation later on.
1867 boolean wasVisible = false;
1868 // First, see if we need to run an animation. If we do, we have
1869 // to hold off on removing the window until the animation is done.
1870 // If the display is frozen, just remove immediately, since the
1871 // animation wouldn't be seen.
1872 if (win.mSurface != null && !mDisplayFrozen) {
1873 // If we are not currently running the exit animation, we
1874 // need to see about starting one.
1875 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1878 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1879 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1880 }
1881 // Try starting an animation.
1882 if (applyAnimationLocked(win, transit, false)) {
1883 win.mExiting = true;
1884 }
1885 }
1886 if (win.mExiting || win.isAnimating()) {
1887 // The exit animation is running... wait for it!
1888 //Log.i(TAG, "*** Running exit animation...");
1889 win.mExiting = true;
1890 win.mRemoveOnExit = true;
1891 mLayoutNeeded = true;
1892 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1893 performLayoutAndPlaceSurfacesLocked();
1894 if (win.mAppToken != null) {
1895 win.mAppToken.updateReportedVisibilityLocked();
1896 }
1897 //dump();
1898 Binder.restoreCallingIdentity(origId);
1899 return;
1900 }
1901 }
1902
1903 removeWindowInnerLocked(session, win);
1904 // Removing a visible window will effect the computed orientation
1905 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001906 if (wasVisible && computeForcedAppOrientationLocked()
1907 != mForcedAppOrientation) {
1908 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1911 Binder.restoreCallingIdentity(origId);
1912 }
Romain Guy06882f82009-06-10 13:36:04 -07001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001915 mKeyWaiter.finishedKey(session, win.mClient, true,
1916 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1918 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (mInputMethodTarget == win) {
1923 moveInputMethodWindowsIfNeededLocked(false);
1924 }
Romain Guy06882f82009-06-10 13:36:04 -07001925
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001926 if (false) {
1927 RuntimeException e = new RuntimeException("here");
1928 e.fillInStackTrace();
1929 Log.w(TAG, "Removing window " + win, e);
1930 }
1931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 mPolicy.removeWindowLw(win);
1933 win.removeLocked();
1934
1935 mWindowMap.remove(win.mClient.asBinder());
1936 mWindows.remove(win);
1937
1938 if (mInputMethodWindow == win) {
1939 mInputMethodWindow = null;
1940 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1941 mInputMethodDialogs.remove(win);
1942 }
Romain Guy06882f82009-06-10 13:36:04 -07001943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 final WindowToken token = win.mToken;
1945 final AppWindowToken atoken = win.mAppToken;
1946 token.windows.remove(win);
1947 if (atoken != null) {
1948 atoken.allAppWindows.remove(win);
1949 }
1950 if (localLOGV) Log.v(
1951 TAG, "**** Removing window " + win + ": count="
1952 + token.windows.size());
1953 if (token.windows.size() == 0) {
1954 if (!token.explicit) {
1955 mTokenMap.remove(token.token);
1956 mTokenList.remove(token);
1957 } else if (atoken != null) {
1958 atoken.firstWindowDrawn = false;
1959 }
1960 }
1961
1962 if (atoken != null) {
1963 if (atoken.startingWindow == win) {
1964 atoken.startingWindow = null;
1965 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1966 // If this is the last window and we had requested a starting
1967 // transition window, well there is no point now.
1968 atoken.startingData = null;
1969 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1970 // If this is the last window except for a starting transition
1971 // window, we need to get rid of the starting transition.
1972 if (DEBUG_STARTING_WINDOW) {
1973 Log.v(TAG, "Schedule remove starting " + token
1974 + ": no more real windows");
1975 }
1976 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1977 mH.sendMessage(m);
1978 }
1979 }
Romain Guy06882f82009-06-10 13:36:04 -07001980
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001981 if (win.mAttrs.type == TYPE_WALLPAPER ||
1982 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1983 adjustWallpaperWindowsLocked();
1984 }
1985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 if (!mInLayout) {
1987 assignLayersLocked();
1988 mLayoutNeeded = true;
1989 performLayoutAndPlaceSurfacesLocked();
1990 if (win.mAppToken != null) {
1991 win.mAppToken.updateReportedVisibilityLocked();
1992 }
1993 }
1994 }
1995
1996 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1997 long origId = Binder.clearCallingIdentity();
1998 try {
1999 synchronized (mWindowMap) {
2000 WindowState w = windowForClientLocked(session, client);
2001 if ((w != null) && (w.mSurface != null)) {
2002 Surface.openTransaction();
2003 try {
2004 w.mSurface.setTransparentRegionHint(region);
2005 } finally {
2006 Surface.closeTransaction();
2007 }
2008 }
2009 }
2010 } finally {
2011 Binder.restoreCallingIdentity(origId);
2012 }
2013 }
2014
2015 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002016 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 Rect visibleInsets) {
2018 long origId = Binder.clearCallingIdentity();
2019 try {
2020 synchronized (mWindowMap) {
2021 WindowState w = windowForClientLocked(session, client);
2022 if (w != null) {
2023 w.mGivenInsetsPending = false;
2024 w.mGivenContentInsets.set(contentInsets);
2025 w.mGivenVisibleInsets.set(visibleInsets);
2026 w.mTouchableInsets = touchableInsets;
2027 mLayoutNeeded = true;
2028 performLayoutAndPlaceSurfacesLocked();
2029 }
2030 }
2031 } finally {
2032 Binder.restoreCallingIdentity(origId);
2033 }
2034 }
Romain Guy06882f82009-06-10 13:36:04 -07002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 public void getWindowDisplayFrame(Session session, IWindow client,
2037 Rect outDisplayFrame) {
2038 synchronized(mWindowMap) {
2039 WindowState win = windowForClientLocked(session, client);
2040 if (win == null) {
2041 outDisplayFrame.setEmpty();
2042 return;
2043 }
2044 outDisplayFrame.set(win.mDisplayFrame);
2045 }
2046 }
2047
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002048 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2049 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2050 window.mWallpaperX = x;
2051 window.mWallpaperY = y;
2052
2053 if (mWallpaperTarget == window) {
2054 if (updateWallpaperOffsetLocked()) {
2055 performLayoutAndPlaceSurfacesLocked();
2056 }
2057 }
2058 }
2059 }
2060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 public int relayoutWindow(Session session, IWindow client,
2062 WindowManager.LayoutParams attrs, int requestedWidth,
2063 int requestedHeight, int viewVisibility, boolean insetsPending,
2064 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2065 Surface outSurface) {
2066 boolean displayed = false;
2067 boolean inTouchMode;
2068 Configuration newConfig = null;
2069 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 synchronized(mWindowMap) {
2072 WindowState win = windowForClientLocked(session, client);
2073 if (win == null) {
2074 return 0;
2075 }
2076 win.mRequestedWidth = requestedWidth;
2077 win.mRequestedHeight = requestedHeight;
2078
2079 if (attrs != null) {
2080 mPolicy.adjustWindowParamsLw(attrs);
2081 }
Romain Guy06882f82009-06-10 13:36:04 -07002082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 int attrChanges = 0;
2084 int flagChanges = 0;
2085 if (attrs != null) {
2086 flagChanges = win.mAttrs.flags ^= attrs.flags;
2087 attrChanges = win.mAttrs.copyFrom(attrs);
2088 }
2089
2090 if (localLOGV) Log.v(
2091 TAG, "Relayout given client " + client.asBinder()
2092 + " (" + win.mAttrs.getTitle() + ")");
2093
2094
2095 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2096 win.mAlpha = attrs.alpha;
2097 }
2098
2099 final boolean scaledWindow =
2100 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2101
2102 if (scaledWindow) {
2103 // requested{Width|Height} Surface's physical size
2104 // attrs.{width|height} Size on screen
2105 win.mHScale = (attrs.width != requestedWidth) ?
2106 (attrs.width / (float)requestedWidth) : 1.0f;
2107 win.mVScale = (attrs.height != requestedHeight) ?
2108 (attrs.height / (float)requestedHeight) : 1.0f;
2109 }
2110
2111 boolean imMayMove = (flagChanges&(
2112 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2113 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 boolean focusMayChange = win.mViewVisibility != viewVisibility
2116 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2117 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002118
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002119 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2120 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 win.mRelayoutCalled = true;
2123 final int oldVisibility = win.mViewVisibility;
2124 win.mViewVisibility = viewVisibility;
2125 if (viewVisibility == View.VISIBLE &&
2126 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2127 displayed = !win.isVisibleLw();
2128 if (win.mExiting) {
2129 win.mExiting = false;
2130 win.mAnimation = null;
2131 }
2132 if (win.mDestroying) {
2133 win.mDestroying = false;
2134 mDestroySurface.remove(win);
2135 }
2136 if (oldVisibility == View.GONE) {
2137 win.mEnterAnimationPending = true;
2138 }
2139 if (displayed && win.mSurface != null && !win.mDrawPending
2140 && !win.mCommitDrawPending && !mDisplayFrozen) {
2141 applyEnterAnimationLocked(win);
2142 }
2143 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2144 // To change the format, we need to re-build the surface.
2145 win.destroySurfaceLocked();
2146 displayed = true;
2147 }
2148 try {
2149 Surface surface = win.createSurfaceLocked();
2150 if (surface != null) {
2151 outSurface.copyFrom(surface);
2152 } else {
2153 outSurface.clear();
2154 }
2155 } catch (Exception e) {
2156 Log.w(TAG, "Exception thrown when creating surface for client "
2157 + client + " (" + win.mAttrs.getTitle() + ")",
2158 e);
2159 Binder.restoreCallingIdentity(origId);
2160 return 0;
2161 }
2162 if (displayed) {
2163 focusMayChange = true;
2164 }
2165 if (win.mAttrs.type == TYPE_INPUT_METHOD
2166 && mInputMethodWindow == null) {
2167 mInputMethodWindow = win;
2168 imMayMove = true;
2169 }
2170 } else {
2171 win.mEnterAnimationPending = false;
2172 if (win.mSurface != null) {
2173 // If we are not currently running the exit animation, we
2174 // need to see about starting one.
2175 if (!win.mExiting) {
2176 // Try starting an animation; if there isn't one, we
2177 // can destroy the surface right away.
2178 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2179 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2180 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2181 }
2182 if (win.isWinVisibleLw() &&
2183 applyAnimationLocked(win, transit, false)) {
2184 win.mExiting = true;
2185 mKeyWaiter.finishedKey(session, client, true,
2186 KeyWaiter.RETURN_NOTHING);
2187 } else if (win.isAnimating()) {
2188 // Currently in a hide animation... turn this into
2189 // an exit.
2190 win.mExiting = true;
2191 } else {
2192 if (mInputMethodWindow == win) {
2193 mInputMethodWindow = null;
2194 }
2195 win.destroySurfaceLocked();
2196 }
2197 }
2198 }
2199 outSurface.clear();
2200 }
2201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 if (focusMayChange) {
2203 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2204 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 imMayMove = false;
2206 }
2207 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2208 }
Romain Guy06882f82009-06-10 13:36:04 -07002209
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002210 // updateFocusedWindowLocked() already assigned layers so we only need to
2211 // reassign them at this point if the IM window state gets shuffled
2212 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 if (imMayMove) {
2215 if (moveInputMethodWindowsIfNeededLocked(false)) {
2216 assignLayers = true;
2217 }
2218 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002219 if (wallpaperMayMove) {
2220 if (adjustWallpaperWindowsLocked()) {
2221 assignLayers = true;
2222 }
2223 }
Romain Guy06882f82009-06-10 13:36:04 -07002224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 mLayoutNeeded = true;
2226 win.mGivenInsetsPending = insetsPending;
2227 if (assignLayers) {
2228 assignLayersLocked();
2229 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002230 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002232 if (displayed && win.mIsWallpaper) {
2233 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
2234 mDisplay.getHeight());
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 if (win.mAppToken != null) {
2237 win.mAppToken.updateReportedVisibilityLocked();
2238 }
2239 outFrame.set(win.mFrame);
2240 outContentInsets.set(win.mContentInsets);
2241 outVisibleInsets.set(win.mVisibleInsets);
2242 if (localLOGV) Log.v(
2243 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002244 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 + ", requestedHeight=" + requestedHeight
2246 + ", viewVisibility=" + viewVisibility
2247 + "\nRelayout returning frame=" + outFrame
2248 + ", surface=" + outSurface);
2249
2250 if (localLOGV || DEBUG_FOCUS) Log.v(
2251 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2252
2253 inTouchMode = mInTouchMode;
2254 }
2255
2256 if (newConfig != null) {
2257 sendNewConfiguration();
2258 }
Romain Guy06882f82009-06-10 13:36:04 -07002259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2263 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2264 }
2265
2266 public void finishDrawingWindow(Session session, IWindow client) {
2267 final long origId = Binder.clearCallingIdentity();
2268 synchronized(mWindowMap) {
2269 WindowState win = windowForClientLocked(session, client);
2270 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002271 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2272 adjustWallpaperWindowsLocked();
2273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 mLayoutNeeded = true;
2275 performLayoutAndPlaceSurfacesLocked();
2276 }
2277 }
2278 Binder.restoreCallingIdentity(origId);
2279 }
2280
2281 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2282 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2283 + (lp != null ? lp.packageName : null)
2284 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2285 if (lp != null && lp.windowAnimations != 0) {
2286 // If this is a system resource, don't try to load it from the
2287 // application resources. It is nice to avoid loading application
2288 // resources if we can.
2289 String packageName = lp.packageName != null ? lp.packageName : "android";
2290 int resId = lp.windowAnimations;
2291 if ((resId&0xFF000000) == 0x01000000) {
2292 packageName = "android";
2293 }
2294 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2295 + packageName);
2296 return AttributeCache.instance().get(packageName, resId,
2297 com.android.internal.R.styleable.WindowAnimation);
2298 }
2299 return null;
2300 }
Romain Guy06882f82009-06-10 13:36:04 -07002301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 private void applyEnterAnimationLocked(WindowState win) {
2303 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2304 if (win.mEnterAnimationPending) {
2305 win.mEnterAnimationPending = false;
2306 transit = WindowManagerPolicy.TRANSIT_ENTER;
2307 }
2308
2309 applyAnimationLocked(win, transit, true);
2310 }
2311
2312 private boolean applyAnimationLocked(WindowState win,
2313 int transit, boolean isEntrance) {
2314 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2315 // If we are trying to apply an animation, but already running
2316 // an animation of the same type, then just leave that one alone.
2317 return true;
2318 }
Romain Guy06882f82009-06-10 13:36:04 -07002319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 // Only apply an animation if the display isn't frozen. If it is
2321 // frozen, there is no reason to animate and it can cause strange
2322 // artifacts when we unfreeze the display if some different animation
2323 // is running.
2324 if (!mDisplayFrozen) {
2325 int anim = mPolicy.selectAnimationLw(win, transit);
2326 int attr = -1;
2327 Animation a = null;
2328 if (anim != 0) {
2329 a = AnimationUtils.loadAnimation(mContext, anim);
2330 } else {
2331 switch (transit) {
2332 case WindowManagerPolicy.TRANSIT_ENTER:
2333 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2334 break;
2335 case WindowManagerPolicy.TRANSIT_EXIT:
2336 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2337 break;
2338 case WindowManagerPolicy.TRANSIT_SHOW:
2339 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2340 break;
2341 case WindowManagerPolicy.TRANSIT_HIDE:
2342 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2343 break;
2344 }
2345 if (attr >= 0) {
2346 a = loadAnimation(win.mAttrs, attr);
2347 }
2348 }
2349 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2350 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2351 + " mAnimation=" + win.mAnimation
2352 + " isEntrance=" + isEntrance);
2353 if (a != null) {
2354 if (DEBUG_ANIM) {
2355 RuntimeException e = new RuntimeException();
2356 e.fillInStackTrace();
2357 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2358 }
2359 win.setAnimation(a);
2360 win.mAnimationIsEntrance = isEntrance;
2361 }
2362 } else {
2363 win.clearAnimation();
2364 }
2365
2366 return win.mAnimation != null;
2367 }
2368
2369 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2370 int anim = 0;
2371 Context context = mContext;
2372 if (animAttr >= 0) {
2373 AttributeCache.Entry ent = getCachedAnimations(lp);
2374 if (ent != null) {
2375 context = ent.context;
2376 anim = ent.array.getResourceId(animAttr, 0);
2377 }
2378 }
2379 if (anim != 0) {
2380 return AnimationUtils.loadAnimation(context, anim);
2381 }
2382 return null;
2383 }
Romain Guy06882f82009-06-10 13:36:04 -07002384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 private boolean applyAnimationLocked(AppWindowToken wtoken,
2386 WindowManager.LayoutParams lp, int transit, boolean enter) {
2387 // Only apply an animation if the display isn't frozen. If it is
2388 // frozen, there is no reason to animate and it can cause strange
2389 // artifacts when we unfreeze the display if some different animation
2390 // is running.
2391 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002392 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002393 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002394 a = new FadeInOutAnimation(enter);
2395 if (DEBUG_ANIM) Log.v(TAG,
2396 "applying FadeInOutAnimation for a window in compatibility mode");
2397 } else {
2398 int animAttr = 0;
2399 switch (transit) {
2400 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2401 animAttr = enter
2402 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2403 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2404 break;
2405 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2406 animAttr = enter
2407 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2408 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2409 break;
2410 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2411 animAttr = enter
2412 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2413 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2414 break;
2415 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2416 animAttr = enter
2417 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2418 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2419 break;
2420 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2421 animAttr = enter
2422 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2423 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2424 break;
2425 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2426 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002427 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002428 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2429 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002430 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002431 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002432 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2433 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002434 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002435 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002436 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002437 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2438 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2439 break;
2440 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2441 animAttr = enter
2442 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2443 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2444 break;
2445 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2446 animAttr = enter
2447 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2448 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002449 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002450 }
2451 a = loadAnimation(lp, animAttr);
2452 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2453 + " anim=" + a
2454 + " animAttr=0x" + Integer.toHexString(animAttr)
2455 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 if (a != null) {
2458 if (DEBUG_ANIM) {
2459 RuntimeException e = new RuntimeException();
2460 e.fillInStackTrace();
2461 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2462 }
2463 wtoken.setAnimation(a);
2464 }
2465 } else {
2466 wtoken.clearAnimation();
2467 }
2468
2469 return wtoken.animation != null;
2470 }
2471
2472 // -------------------------------------------------------------
2473 // Application Window Tokens
2474 // -------------------------------------------------------------
2475
2476 public void validateAppTokens(List tokens) {
2477 int v = tokens.size()-1;
2478 int m = mAppTokens.size()-1;
2479 while (v >= 0 && m >= 0) {
2480 AppWindowToken wtoken = mAppTokens.get(m);
2481 if (wtoken.removed) {
2482 m--;
2483 continue;
2484 }
2485 if (tokens.get(v) != wtoken.token) {
2486 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2487 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2488 }
2489 v--;
2490 m--;
2491 }
2492 while (v >= 0) {
2493 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2494 v--;
2495 }
2496 while (m >= 0) {
2497 AppWindowToken wtoken = mAppTokens.get(m);
2498 if (!wtoken.removed) {
2499 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2500 }
2501 m--;
2502 }
2503 }
2504
2505 boolean checkCallingPermission(String permission, String func) {
2506 // Quick check: if the calling permission is me, it's all okay.
2507 if (Binder.getCallingPid() == Process.myPid()) {
2508 return true;
2509 }
Romain Guy06882f82009-06-10 13:36:04 -07002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 if (mContext.checkCallingPermission(permission)
2512 == PackageManager.PERMISSION_GRANTED) {
2513 return true;
2514 }
2515 String msg = "Permission Denial: " + func + " from pid="
2516 + Binder.getCallingPid()
2517 + ", uid=" + Binder.getCallingUid()
2518 + " requires " + permission;
2519 Log.w(TAG, msg);
2520 return false;
2521 }
Romain Guy06882f82009-06-10 13:36:04 -07002522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 AppWindowToken findAppWindowToken(IBinder token) {
2524 WindowToken wtoken = mTokenMap.get(token);
2525 if (wtoken == null) {
2526 return null;
2527 }
2528 return wtoken.appWindowToken;
2529 }
Romain Guy06882f82009-06-10 13:36:04 -07002530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 public void addWindowToken(IBinder token, int type) {
2532 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2533 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002534 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 synchronized(mWindowMap) {
2538 WindowToken wtoken = mTokenMap.get(token);
2539 if (wtoken != null) {
2540 Log.w(TAG, "Attempted to add existing input method token: " + token);
2541 return;
2542 }
2543 wtoken = new WindowToken(token, type, true);
2544 mTokenMap.put(token, wtoken);
2545 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002546 if (type == TYPE_WALLPAPER) {
2547 mWallpaperTokens.add(wtoken);
2548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
2550 }
Romain Guy06882f82009-06-10 13:36:04 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 public void removeWindowToken(IBinder token) {
2553 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2554 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002555 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557
2558 final long origId = Binder.clearCallingIdentity();
2559 synchronized(mWindowMap) {
2560 WindowToken wtoken = mTokenMap.remove(token);
2561 mTokenList.remove(wtoken);
2562 if (wtoken != null) {
2563 boolean delayed = false;
2564 if (!wtoken.hidden) {
2565 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 final int N = wtoken.windows.size();
2568 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 for (int i=0; i<N; i++) {
2571 WindowState win = wtoken.windows.get(i);
2572
2573 if (win.isAnimating()) {
2574 delayed = true;
2575 }
Romain Guy06882f82009-06-10 13:36:04 -07002576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 if (win.isVisibleNow()) {
2578 applyAnimationLocked(win,
2579 WindowManagerPolicy.TRANSIT_EXIT, false);
2580 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2581 KeyWaiter.RETURN_NOTHING);
2582 changed = true;
2583 }
2584 }
2585
2586 if (changed) {
2587 mLayoutNeeded = true;
2588 performLayoutAndPlaceSurfacesLocked();
2589 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2590 }
Romain Guy06882f82009-06-10 13:36:04 -07002591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 if (delayed) {
2593 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002594 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2595 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597 }
Romain Guy06882f82009-06-10 13:36:04 -07002598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 } else {
2600 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2601 }
2602 }
2603 Binder.restoreCallingIdentity(origId);
2604 }
2605
2606 public void addAppToken(int addPos, IApplicationToken token,
2607 int groupId, int requestedOrientation, boolean fullscreen) {
2608 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2609 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002610 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 }
Romain Guy06882f82009-06-10 13:36:04 -07002612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 synchronized(mWindowMap) {
2614 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2615 if (wtoken != null) {
2616 Log.w(TAG, "Attempted to add existing app token: " + token);
2617 return;
2618 }
2619 wtoken = new AppWindowToken(token);
2620 wtoken.groupId = groupId;
2621 wtoken.appFullscreen = fullscreen;
2622 wtoken.requestedOrientation = requestedOrientation;
2623 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002624 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 mTokenMap.put(token.asBinder(), wtoken);
2626 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 // Application tokens start out hidden.
2629 wtoken.hidden = true;
2630 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 //dump();
2633 }
2634 }
Romain Guy06882f82009-06-10 13:36:04 -07002635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 public void setAppGroupId(IBinder token, int groupId) {
2637 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2638 "setAppStartingIcon()")) {
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 synchronized(mWindowMap) {
2643 AppWindowToken wtoken = findAppWindowToken(token);
2644 if (wtoken == null) {
2645 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2646 return;
2647 }
2648 wtoken.groupId = groupId;
2649 }
2650 }
Romain Guy06882f82009-06-10 13:36:04 -07002651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 public int getOrientationFromWindowsLocked() {
2653 int pos = mWindows.size() - 1;
2654 while (pos >= 0) {
2655 WindowState wtoken = (WindowState) mWindows.get(pos);
2656 pos--;
2657 if (wtoken.mAppToken != null) {
2658 // We hit an application window. so the orientation will be determined by the
2659 // app window. No point in continuing further.
2660 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2661 }
2662 if (!wtoken.isVisibleLw()) {
2663 continue;
2664 }
2665 int req = wtoken.mAttrs.screenOrientation;
2666 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2667 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2668 continue;
2669 } else {
2670 return req;
2671 }
2672 }
2673 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2674 }
Romain Guy06882f82009-06-10 13:36:04 -07002675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 public int getOrientationFromAppTokensLocked() {
2677 int pos = mAppTokens.size() - 1;
2678 int curGroup = 0;
2679 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002680 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002682 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 while (pos >= 0) {
2684 AppWindowToken wtoken = mAppTokens.get(pos);
2685 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002686 // if we're about to tear down this window and not seek for
2687 // the behind activity, don't use it for orientation
2688 if (!findingBehind
2689 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002690 continue;
2691 }
2692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 if (!haveGroup) {
2694 // We ignore any hidden applications on the top.
2695 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2696 continue;
2697 }
2698 haveGroup = true;
2699 curGroup = wtoken.groupId;
2700 lastOrientation = wtoken.requestedOrientation;
2701 } else if (curGroup != wtoken.groupId) {
2702 // If we have hit a new application group, and the bottom
2703 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002704 // the orientation behind it, and the last app was
2705 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002707 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2708 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 return lastOrientation;
2710 }
2711 }
2712 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002713 // If this application is fullscreen, and didn't explicitly say
2714 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002716 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002717 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002718 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 return or;
2720 }
2721 // If this application has requested an explicit orientation,
2722 // then use it.
2723 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2724 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2725 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2726 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2727 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2728 return or;
2729 }
Owen Lin3413b892009-05-01 17:12:32 -07002730 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 }
2732 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2733 }
Romain Guy06882f82009-06-10 13:36:04 -07002734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002736 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002737 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2738 "updateOrientationFromAppTokens()")) {
2739 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2740 }
2741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 Configuration config;
2743 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002744 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2745 freezeThisOneIfNeeded);
2746 Binder.restoreCallingIdentity(ident);
2747 return config;
2748 }
2749
2750 Configuration updateOrientationFromAppTokensUnchecked(
2751 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2752 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002754 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002755 if (config != null) {
2756 mLayoutNeeded = true;
2757 performLayoutAndPlaceSurfacesLocked();
2758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 return config;
2761 }
Romain Guy06882f82009-06-10 13:36:04 -07002762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 /*
2764 * The orientation is computed from non-application windows first. If none of
2765 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002766 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2768 * android.os.IBinder)
2769 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002770 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002771 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 long ident = Binder.clearCallingIdentity();
2774 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002775 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 if (req != mForcedAppOrientation) {
2778 changed = true;
2779 mForcedAppOrientation = req;
2780 //send a message to Policy indicating orientation change to take
2781 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002782 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
Romain Guy06882f82009-06-10 13:36:04 -07002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 if (changed) {
2786 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002787 WindowManagerPolicy.USE_LAST_ROTATION,
2788 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 if (changed) {
2790 if (freezeThisOneIfNeeded != null) {
2791 AppWindowToken wtoken = findAppWindowToken(
2792 freezeThisOneIfNeeded);
2793 if (wtoken != null) {
2794 startAppFreezingScreenLocked(wtoken,
2795 ActivityInfo.CONFIG_ORIENTATION);
2796 }
2797 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002798 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 }
2800 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002801
2802 // No obvious action we need to take, but if our current
2803 // state mismatches the activity maanager's, update it
2804 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002805 mTempConfiguration.setToDefaults();
2806 if (computeNewConfigurationLocked(mTempConfiguration)) {
2807 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002808 return new Configuration(mTempConfiguration);
2809 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002810 }
2811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 } finally {
2813 Binder.restoreCallingIdentity(ident);
2814 }
Romain Guy06882f82009-06-10 13:36:04 -07002815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 return null;
2817 }
Romain Guy06882f82009-06-10 13:36:04 -07002818
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002819 int computeForcedAppOrientationLocked() {
2820 int req = getOrientationFromWindowsLocked();
2821 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2822 req = getOrientationFromAppTokensLocked();
2823 }
2824 return req;
2825 }
Romain Guy06882f82009-06-10 13:36:04 -07002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2828 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2829 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002830 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 }
Romain Guy06882f82009-06-10 13:36:04 -07002832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 synchronized(mWindowMap) {
2834 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2835 if (wtoken == null) {
2836 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2837 return;
2838 }
Romain Guy06882f82009-06-10 13:36:04 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 wtoken.requestedOrientation = requestedOrientation;
2841 }
2842 }
Romain Guy06882f82009-06-10 13:36:04 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public int getAppOrientation(IApplicationToken token) {
2845 synchronized(mWindowMap) {
2846 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2847 if (wtoken == null) {
2848 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2849 }
Romain Guy06882f82009-06-10 13:36:04 -07002850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return wtoken.requestedOrientation;
2852 }
2853 }
Romain Guy06882f82009-06-10 13:36:04 -07002854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2856 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2857 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002858 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
2861 synchronized(mWindowMap) {
2862 boolean changed = false;
2863 if (token == null) {
2864 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2865 changed = mFocusedApp != null;
2866 mFocusedApp = null;
2867 mKeyWaiter.tickle();
2868 } else {
2869 AppWindowToken newFocus = findAppWindowToken(token);
2870 if (newFocus == null) {
2871 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2872 return;
2873 }
2874 changed = mFocusedApp != newFocus;
2875 mFocusedApp = newFocus;
2876 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2877 mKeyWaiter.tickle();
2878 }
2879
2880 if (moveFocusNow && changed) {
2881 final long origId = Binder.clearCallingIdentity();
2882 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2883 Binder.restoreCallingIdentity(origId);
2884 }
2885 }
2886 }
2887
2888 public void prepareAppTransition(int transit) {
2889 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2890 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002891 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
Romain Guy06882f82009-06-10 13:36:04 -07002893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 synchronized(mWindowMap) {
2895 if (DEBUG_APP_TRANSITIONS) Log.v(
2896 TAG, "Prepare app transition: transit=" + transit
2897 + " mNextAppTransition=" + mNextAppTransition);
2898 if (!mDisplayFrozen) {
2899 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2900 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002901 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2902 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2903 // Opening a new task always supersedes a close for the anim.
2904 mNextAppTransition = transit;
2905 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2906 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2907 // Opening a new activity always supersedes a close for the anim.
2908 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
2910 mAppTransitionReady = false;
2911 mAppTransitionTimeout = false;
2912 mStartingIconInTransition = false;
2913 mSkipAppTransitionAnimation = false;
2914 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2915 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2916 5000);
2917 }
2918 }
2919 }
2920
2921 public int getPendingAppTransition() {
2922 return mNextAppTransition;
2923 }
Romain Guy06882f82009-06-10 13:36:04 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 public void executeAppTransition() {
2926 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2927 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002928 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
Romain Guy06882f82009-06-10 13:36:04 -07002930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 synchronized(mWindowMap) {
2932 if (DEBUG_APP_TRANSITIONS) Log.v(
2933 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2934 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2935 mAppTransitionReady = true;
2936 final long origId = Binder.clearCallingIdentity();
2937 performLayoutAndPlaceSurfacesLocked();
2938 Binder.restoreCallingIdentity(origId);
2939 }
2940 }
2941 }
2942
2943 public void setAppStartingWindow(IBinder token, String pkg,
2944 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2945 IBinder transferFrom, boolean createIfNeeded) {
2946 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2947 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002948 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
2950
2951 synchronized(mWindowMap) {
2952 if (DEBUG_STARTING_WINDOW) Log.v(
2953 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2954 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 AppWindowToken wtoken = findAppWindowToken(token);
2957 if (wtoken == null) {
2958 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2959 return;
2960 }
2961
2962 // If the display is frozen, we won't do anything until the
2963 // actual window is displayed so there is no reason to put in
2964 // the starting window.
2965 if (mDisplayFrozen) {
2966 return;
2967 }
Romain Guy06882f82009-06-10 13:36:04 -07002968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 if (wtoken.startingData != null) {
2970 return;
2971 }
Romain Guy06882f82009-06-10 13:36:04 -07002972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 if (transferFrom != null) {
2974 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2975 if (ttoken != null) {
2976 WindowState startingWindow = ttoken.startingWindow;
2977 if (startingWindow != null) {
2978 if (mStartingIconInTransition) {
2979 // In this case, the starting icon has already
2980 // been displayed, so start letting windows get
2981 // shown immediately without any more transitions.
2982 mSkipAppTransitionAnimation = true;
2983 }
2984 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2985 "Moving existing starting from " + ttoken
2986 + " to " + wtoken);
2987 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 // Transfer the starting window over to the new
2990 // token.
2991 wtoken.startingData = ttoken.startingData;
2992 wtoken.startingView = ttoken.startingView;
2993 wtoken.startingWindow = startingWindow;
2994 ttoken.startingData = null;
2995 ttoken.startingView = null;
2996 ttoken.startingWindow = null;
2997 ttoken.startingMoved = true;
2998 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002999 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 startingWindow.mAppToken = wtoken;
3001 mWindows.remove(startingWindow);
3002 ttoken.windows.remove(startingWindow);
3003 ttoken.allAppWindows.remove(startingWindow);
3004 addWindowToListInOrderLocked(startingWindow, true);
3005 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 // Propagate other interesting state between the
3008 // tokens. If the old token is displayed, we should
3009 // immediately force the new one to be displayed. If
3010 // it is animating, we need to move that animation to
3011 // the new one.
3012 if (ttoken.allDrawn) {
3013 wtoken.allDrawn = true;
3014 }
3015 if (ttoken.firstWindowDrawn) {
3016 wtoken.firstWindowDrawn = true;
3017 }
3018 if (!ttoken.hidden) {
3019 wtoken.hidden = false;
3020 wtoken.hiddenRequested = false;
3021 wtoken.willBeHidden = false;
3022 }
3023 if (wtoken.clientHidden != ttoken.clientHidden) {
3024 wtoken.clientHidden = ttoken.clientHidden;
3025 wtoken.sendAppVisibilityToClients();
3026 }
3027 if (ttoken.animation != null) {
3028 wtoken.animation = ttoken.animation;
3029 wtoken.animating = ttoken.animating;
3030 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3031 ttoken.animation = null;
3032 ttoken.animLayerAdjustment = 0;
3033 wtoken.updateLayers();
3034 ttoken.updateLayers();
3035 }
Romain Guy06882f82009-06-10 13:36:04 -07003036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 mLayoutNeeded = true;
3039 performLayoutAndPlaceSurfacesLocked();
3040 Binder.restoreCallingIdentity(origId);
3041 return;
3042 } else if (ttoken.startingData != null) {
3043 // The previous app was getting ready to show a
3044 // starting window, but hasn't yet done so. Steal it!
3045 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3046 "Moving pending starting from " + ttoken
3047 + " to " + wtoken);
3048 wtoken.startingData = ttoken.startingData;
3049 ttoken.startingData = null;
3050 ttoken.startingMoved = true;
3051 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3052 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3053 // want to process the message ASAP, before any other queued
3054 // messages.
3055 mH.sendMessageAtFrontOfQueue(m);
3056 return;
3057 }
3058 }
3059 }
3060
3061 // There is no existing starting window, and the caller doesn't
3062 // want us to create one, so that's it!
3063 if (!createIfNeeded) {
3064 return;
3065 }
Romain Guy06882f82009-06-10 13:36:04 -07003066
Dianne Hackborn284ac932009-08-28 10:34:25 -07003067 // If this is a translucent or wallpaper window, then don't
3068 // show a starting window -- the current effect (a full-screen
3069 // opaque starting window that fades away to the real contents
3070 // when it is ready) does not work for this.
3071 if (theme != 0) {
3072 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3073 com.android.internal.R.styleable.Window);
3074 if (ent.array.getBoolean(
3075 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3076 return;
3077 }
3078 if (ent.array.getBoolean(
3079 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3080 return;
3081 }
3082 }
3083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 mStartingIconInTransition = true;
3085 wtoken.startingData = new StartingData(
3086 pkg, theme, nonLocalizedLabel,
3087 labelRes, icon);
3088 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3089 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3090 // want to process the message ASAP, before any other queued
3091 // messages.
3092 mH.sendMessageAtFrontOfQueue(m);
3093 }
3094 }
3095
3096 public void setAppWillBeHidden(IBinder token) {
3097 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3098 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003099 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
3101
3102 AppWindowToken wtoken;
3103
3104 synchronized(mWindowMap) {
3105 wtoken = findAppWindowToken(token);
3106 if (wtoken == null) {
3107 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3108 return;
3109 }
3110 wtoken.willBeHidden = true;
3111 }
3112 }
Romain Guy06882f82009-06-10 13:36:04 -07003113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3115 boolean visible, int transit, boolean performLayout) {
3116 boolean delayed = false;
3117
3118 if (wtoken.clientHidden == visible) {
3119 wtoken.clientHidden = !visible;
3120 wtoken.sendAppVisibilityToClients();
3121 }
Romain Guy06882f82009-06-10 13:36:04 -07003122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 wtoken.willBeHidden = false;
3124 if (wtoken.hidden == visible) {
3125 final int N = wtoken.allAppWindows.size();
3126 boolean changed = false;
3127 if (DEBUG_APP_TRANSITIONS) Log.v(
3128 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3129 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3134 if (wtoken.animation == sDummyAnimation) {
3135 wtoken.animation = null;
3136 }
3137 applyAnimationLocked(wtoken, lp, transit, visible);
3138 changed = true;
3139 if (wtoken.animation != null) {
3140 delayed = runningAppAnimation = true;
3141 }
3142 }
Romain Guy06882f82009-06-10 13:36:04 -07003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 for (int i=0; i<N; i++) {
3145 WindowState win = wtoken.allAppWindows.get(i);
3146 if (win == wtoken.startingWindow) {
3147 continue;
3148 }
3149
3150 if (win.isAnimating()) {
3151 delayed = true;
3152 }
Romain Guy06882f82009-06-10 13:36:04 -07003153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3155 //win.dump(" ");
3156 if (visible) {
3157 if (!win.isVisibleNow()) {
3158 if (!runningAppAnimation) {
3159 applyAnimationLocked(win,
3160 WindowManagerPolicy.TRANSIT_ENTER, true);
3161 }
3162 changed = true;
3163 }
3164 } else if (win.isVisibleNow()) {
3165 if (!runningAppAnimation) {
3166 applyAnimationLocked(win,
3167 WindowManagerPolicy.TRANSIT_EXIT, false);
3168 }
3169 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3170 KeyWaiter.RETURN_NOTHING);
3171 changed = true;
3172 }
3173 }
3174
3175 wtoken.hidden = wtoken.hiddenRequested = !visible;
3176 if (!visible) {
3177 unsetAppFreezingScreenLocked(wtoken, true, true);
3178 } else {
3179 // If we are being set visible, and the starting window is
3180 // not yet displayed, then make sure it doesn't get displayed.
3181 WindowState swin = wtoken.startingWindow;
3182 if (swin != null && (swin.mDrawPending
3183 || swin.mCommitDrawPending)) {
3184 swin.mPolicyVisibility = false;
3185 swin.mPolicyVisibilityAfterAnim = false;
3186 }
3187 }
Romain Guy06882f82009-06-10 13:36:04 -07003188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3190 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3191 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 if (changed && performLayout) {
3194 mLayoutNeeded = true;
3195 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 performLayoutAndPlaceSurfacesLocked();
3197 }
3198 }
3199
3200 if (wtoken.animation != null) {
3201 delayed = true;
3202 }
Romain Guy06882f82009-06-10 13:36:04 -07003203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 return delayed;
3205 }
3206
3207 public void setAppVisibility(IBinder token, boolean visible) {
3208 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3209 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003210 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 }
3212
3213 AppWindowToken wtoken;
3214
3215 synchronized(mWindowMap) {
3216 wtoken = findAppWindowToken(token);
3217 if (wtoken == null) {
3218 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3219 return;
3220 }
3221
3222 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3223 RuntimeException e = new RuntimeException();
3224 e.fillInStackTrace();
3225 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3226 + "): mNextAppTransition=" + mNextAppTransition
3227 + " hidden=" + wtoken.hidden
3228 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3229 }
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 // If we are preparing an app transition, then delay changing
3232 // the visibility of this token until we execute that transition.
3233 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3234 // Already in requested state, don't do anything more.
3235 if (wtoken.hiddenRequested != visible) {
3236 return;
3237 }
3238 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 if (DEBUG_APP_TRANSITIONS) Log.v(
3241 TAG, "Setting dummy animation on: " + wtoken);
3242 wtoken.setDummyAnimation();
3243 mOpeningApps.remove(wtoken);
3244 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003245 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 wtoken.inPendingTransaction = true;
3247 if (visible) {
3248 mOpeningApps.add(wtoken);
3249 wtoken.allDrawn = false;
3250 wtoken.startingDisplayed = false;
3251 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003252 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 if (wtoken.clientHidden) {
3255 // In the case where we are making an app visible
3256 // but holding off for a transition, we still need
3257 // to tell the client to make its windows visible so
3258 // they get drawn. Otherwise, we will wait on
3259 // performing the transition until all windows have
3260 // been drawn, they never will be, and we are sad.
3261 wtoken.clientHidden = false;
3262 wtoken.sendAppVisibilityToClients();
3263 }
3264 } else {
3265 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003266 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268 return;
3269 }
Romain Guy06882f82009-06-10 13:36:04 -07003270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 final long origId = Binder.clearCallingIdentity();
3272 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3273 wtoken.updateReportedVisibilityLocked();
3274 Binder.restoreCallingIdentity(origId);
3275 }
3276 }
3277
3278 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3279 boolean unfreezeSurfaceNow, boolean force) {
3280 if (wtoken.freezingScreen) {
3281 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3282 + " force=" + force);
3283 final int N = wtoken.allAppWindows.size();
3284 boolean unfrozeWindows = false;
3285 for (int i=0; i<N; i++) {
3286 WindowState w = wtoken.allAppWindows.get(i);
3287 if (w.mAppFreezing) {
3288 w.mAppFreezing = false;
3289 if (w.mSurface != null && !w.mOrientationChanging) {
3290 w.mOrientationChanging = true;
3291 }
3292 unfrozeWindows = true;
3293 }
3294 }
3295 if (force || unfrozeWindows) {
3296 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3297 wtoken.freezingScreen = false;
3298 mAppsFreezingScreen--;
3299 }
3300 if (unfreezeSurfaceNow) {
3301 if (unfrozeWindows) {
3302 mLayoutNeeded = true;
3303 performLayoutAndPlaceSurfacesLocked();
3304 }
3305 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3306 stopFreezingDisplayLocked();
3307 }
3308 }
3309 }
3310 }
Romain Guy06882f82009-06-10 13:36:04 -07003311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3313 int configChanges) {
3314 if (DEBUG_ORIENTATION) {
3315 RuntimeException e = new RuntimeException();
3316 e.fillInStackTrace();
3317 Log.i(TAG, "Set freezing of " + wtoken.appToken
3318 + ": hidden=" + wtoken.hidden + " freezing="
3319 + wtoken.freezingScreen, e);
3320 }
3321 if (!wtoken.hiddenRequested) {
3322 if (!wtoken.freezingScreen) {
3323 wtoken.freezingScreen = true;
3324 mAppsFreezingScreen++;
3325 if (mAppsFreezingScreen == 1) {
3326 startFreezingDisplayLocked();
3327 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3328 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3329 5000);
3330 }
3331 }
3332 final int N = wtoken.allAppWindows.size();
3333 for (int i=0; i<N; i++) {
3334 WindowState w = wtoken.allAppWindows.get(i);
3335 w.mAppFreezing = true;
3336 }
3337 }
3338 }
Romain Guy06882f82009-06-10 13:36:04 -07003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 public void startAppFreezingScreen(IBinder token, int configChanges) {
3341 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3342 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003343 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
3345
3346 synchronized(mWindowMap) {
3347 if (configChanges == 0 && !mDisplayFrozen) {
3348 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3349 return;
3350 }
Romain Guy06882f82009-06-10 13:36:04 -07003351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 AppWindowToken wtoken = findAppWindowToken(token);
3353 if (wtoken == null || wtoken.appToken == null) {
3354 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3355 return;
3356 }
3357 final long origId = Binder.clearCallingIdentity();
3358 startAppFreezingScreenLocked(wtoken, configChanges);
3359 Binder.restoreCallingIdentity(origId);
3360 }
3361 }
Romain Guy06882f82009-06-10 13:36:04 -07003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 public void stopAppFreezingScreen(IBinder token, boolean force) {
3364 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3365 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003366 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
3368
3369 synchronized(mWindowMap) {
3370 AppWindowToken wtoken = findAppWindowToken(token);
3371 if (wtoken == null || wtoken.appToken == null) {
3372 return;
3373 }
3374 final long origId = Binder.clearCallingIdentity();
3375 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3376 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3377 unsetAppFreezingScreenLocked(wtoken, true, force);
3378 Binder.restoreCallingIdentity(origId);
3379 }
3380 }
Romain Guy06882f82009-06-10 13:36:04 -07003381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 public void removeAppToken(IBinder token) {
3383 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3384 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003385 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 }
3387
3388 AppWindowToken wtoken = null;
3389 AppWindowToken startingToken = null;
3390 boolean delayed = false;
3391
3392 final long origId = Binder.clearCallingIdentity();
3393 synchronized(mWindowMap) {
3394 WindowToken basewtoken = mTokenMap.remove(token);
3395 mTokenList.remove(basewtoken);
3396 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3397 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3398 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3399 wtoken.inPendingTransaction = false;
3400 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003401 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 if (mClosingApps.contains(wtoken)) {
3403 delayed = true;
3404 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3405 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003406 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 delayed = true;
3408 }
3409 if (DEBUG_APP_TRANSITIONS) Log.v(
3410 TAG, "Removing app " + wtoken + " delayed=" + delayed
3411 + " animation=" + wtoken.animation
3412 + " animating=" + wtoken.animating);
3413 if (delayed) {
3414 // set the token aside because it has an active animation to be finished
3415 mExitingAppTokens.add(wtoken);
3416 }
3417 mAppTokens.remove(wtoken);
3418 wtoken.removed = true;
3419 if (wtoken.startingData != null) {
3420 startingToken = wtoken;
3421 }
3422 unsetAppFreezingScreenLocked(wtoken, true, true);
3423 if (mFocusedApp == wtoken) {
3424 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3425 mFocusedApp = null;
3426 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3427 mKeyWaiter.tickle();
3428 }
3429 } else {
3430 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3431 }
Romain Guy06882f82009-06-10 13:36:04 -07003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 if (!delayed && wtoken != null) {
3434 wtoken.updateReportedVisibilityLocked();
3435 }
3436 }
3437 Binder.restoreCallingIdentity(origId);
3438
3439 if (startingToken != null) {
3440 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3441 + startingToken + ": app token removed");
3442 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3443 mH.sendMessage(m);
3444 }
3445 }
3446
3447 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3448 final int NW = token.windows.size();
3449 for (int i=0; i<NW; i++) {
3450 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003451 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 mWindows.remove(win);
3453 int j = win.mChildWindows.size();
3454 while (j > 0) {
3455 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003456 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3457 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3458 "Tmp removing child window " + cwin);
3459 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461 }
3462 return NW > 0;
3463 }
3464
3465 void dumpAppTokensLocked() {
3466 for (int i=mAppTokens.size()-1; i>=0; i--) {
3467 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3468 }
3469 }
Romain Guy06882f82009-06-10 13:36:04 -07003470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 void dumpWindowsLocked() {
3472 for (int i=mWindows.size()-1; i>=0; i--) {
3473 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3474 }
3475 }
Romain Guy06882f82009-06-10 13:36:04 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 private int findWindowOffsetLocked(int tokenPos) {
3478 final int NW = mWindows.size();
3479
3480 if (tokenPos >= mAppTokens.size()) {
3481 int i = NW;
3482 while (i > 0) {
3483 i--;
3484 WindowState win = (WindowState)mWindows.get(i);
3485 if (win.getAppToken() != null) {
3486 return i+1;
3487 }
3488 }
3489 }
3490
3491 while (tokenPos > 0) {
3492 // Find the first app token below the new position that has
3493 // a window displayed.
3494 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3495 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3496 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003497 if (wtoken.sendingToBottom) {
3498 if (DEBUG_REORDER) Log.v(TAG,
3499 "Skipping token -- currently sending to bottom");
3500 tokenPos--;
3501 continue;
3502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 int i = wtoken.windows.size();
3504 while (i > 0) {
3505 i--;
3506 WindowState win = wtoken.windows.get(i);
3507 int j = win.mChildWindows.size();
3508 while (j > 0) {
3509 j--;
3510 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003511 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 for (int pos=NW-1; pos>=0; pos--) {
3513 if (mWindows.get(pos) == cwin) {
3514 if (DEBUG_REORDER) Log.v(TAG,
3515 "Found child win @" + (pos+1));
3516 return pos+1;
3517 }
3518 }
3519 }
3520 }
3521 for (int pos=NW-1; pos>=0; pos--) {
3522 if (mWindows.get(pos) == win) {
3523 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3524 return pos+1;
3525 }
3526 }
3527 }
3528 tokenPos--;
3529 }
3530
3531 return 0;
3532 }
3533
3534 private final int reAddWindowLocked(int index, WindowState win) {
3535 final int NCW = win.mChildWindows.size();
3536 boolean added = false;
3537 for (int j=0; j<NCW; j++) {
3538 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3539 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003540 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3541 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 mWindows.add(index, win);
3543 index++;
3544 added = true;
3545 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003546 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3547 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 mWindows.add(index, cwin);
3549 index++;
3550 }
3551 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003552 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3553 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 mWindows.add(index, win);
3555 index++;
3556 }
3557 return index;
3558 }
Romain Guy06882f82009-06-10 13:36:04 -07003559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3561 final int NW = token.windows.size();
3562 for (int i=0; i<NW; i++) {
3563 index = reAddWindowLocked(index, token.windows.get(i));
3564 }
3565 return index;
3566 }
3567
3568 public void moveAppToken(int index, IBinder token) {
3569 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3570 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003571 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 }
3573
3574 synchronized(mWindowMap) {
3575 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3576 if (DEBUG_REORDER) dumpAppTokensLocked();
3577 final AppWindowToken wtoken = findAppWindowToken(token);
3578 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3579 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3580 + token + " (" + wtoken + ")");
3581 return;
3582 }
3583 mAppTokens.add(index, wtoken);
3584 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3585 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 final long origId = Binder.clearCallingIdentity();
3588 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3589 if (DEBUG_REORDER) dumpWindowsLocked();
3590 if (tmpRemoveAppWindowsLocked(wtoken)) {
3591 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3592 if (DEBUG_REORDER) dumpWindowsLocked();
3593 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3594 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3595 if (DEBUG_REORDER) dumpWindowsLocked();
3596 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 mLayoutNeeded = true;
3598 performLayoutAndPlaceSurfacesLocked();
3599 }
3600 Binder.restoreCallingIdentity(origId);
3601 }
3602 }
3603
3604 private void removeAppTokensLocked(List<IBinder> tokens) {
3605 // XXX This should be done more efficiently!
3606 // (take advantage of the fact that both lists should be
3607 // ordered in the same way.)
3608 int N = tokens.size();
3609 for (int i=0; i<N; i++) {
3610 IBinder token = tokens.get(i);
3611 final AppWindowToken wtoken = findAppWindowToken(token);
3612 if (!mAppTokens.remove(wtoken)) {
3613 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3614 + token + " (" + wtoken + ")");
3615 i--;
3616 N--;
3617 }
3618 }
3619 }
3620
Dianne Hackborna8f60182009-09-01 19:01:50 -07003621 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3622 boolean updateFocusAndLayout) {
3623 // First remove all of the windows from the list.
3624 tmpRemoveAppWindowsLocked(wtoken);
3625
3626 // Where to start adding?
3627 int pos = findWindowOffsetLocked(tokenPos);
3628
3629 // And now add them back at the correct place.
3630 pos = reAddAppWindowsLocked(pos, wtoken);
3631
3632 if (updateFocusAndLayout) {
3633 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3634 assignLayersLocked();
3635 }
3636 mLayoutNeeded = true;
3637 performLayoutAndPlaceSurfacesLocked();
3638 }
3639 }
3640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3642 // First remove all of the windows from the list.
3643 final int N = tokens.size();
3644 int i;
3645 for (i=0; i<N; i++) {
3646 WindowToken token = mTokenMap.get(tokens.get(i));
3647 if (token != null) {
3648 tmpRemoveAppWindowsLocked(token);
3649 }
3650 }
3651
3652 // Where to start adding?
3653 int pos = findWindowOffsetLocked(tokenPos);
3654
3655 // And now add them back at the correct place.
3656 for (i=0; i<N; i++) {
3657 WindowToken token = mTokenMap.get(tokens.get(i));
3658 if (token != null) {
3659 pos = reAddAppWindowsLocked(pos, token);
3660 }
3661 }
3662
Dianne Hackborna8f60182009-09-01 19:01:50 -07003663 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3664 assignLayersLocked();
3665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 mLayoutNeeded = true;
3667 performLayoutAndPlaceSurfacesLocked();
3668
3669 //dump();
3670 }
3671
3672 public void moveAppTokensToTop(List<IBinder> tokens) {
3673 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3674 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003675 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 }
3677
3678 final long origId = Binder.clearCallingIdentity();
3679 synchronized(mWindowMap) {
3680 removeAppTokensLocked(tokens);
3681 final int N = tokens.size();
3682 for (int i=0; i<N; i++) {
3683 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3684 if (wt != null) {
3685 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003686 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3687 mToTopApps.remove(wt);
3688 mToBottomApps.remove(wt);
3689 mToTopApps.add(wt);
3690 wt.sendingToBottom = false;
3691 wt.sendingToTop = true;
3692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
3694 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003695
3696 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3697 moveAppWindowsLocked(tokens, mAppTokens.size());
3698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 }
3700 Binder.restoreCallingIdentity(origId);
3701 }
3702
3703 public void moveAppTokensToBottom(List<IBinder> tokens) {
3704 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3705 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003706 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 }
3708
3709 final long origId = Binder.clearCallingIdentity();
3710 synchronized(mWindowMap) {
3711 removeAppTokensLocked(tokens);
3712 final int N = tokens.size();
3713 int pos = 0;
3714 for (int i=0; i<N; i++) {
3715 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3716 if (wt != null) {
3717 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003718 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3719 mToTopApps.remove(wt);
3720 mToBottomApps.remove(wt);
3721 mToBottomApps.add(i, wt);
3722 wt.sendingToTop = false;
3723 wt.sendingToBottom = true;
3724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 pos++;
3726 }
3727 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003728
3729 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3730 moveAppWindowsLocked(tokens, 0);
3731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 }
3733 Binder.restoreCallingIdentity(origId);
3734 }
3735
3736 // -------------------------------------------------------------
3737 // Misc IWindowSession methods
3738 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 public void disableKeyguard(IBinder token, String tag) {
3741 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3742 != PackageManager.PERMISSION_GRANTED) {
3743 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3744 }
3745 mKeyguardDisabled.acquire(token, tag);
3746 }
3747
3748 public void reenableKeyguard(IBinder token) {
3749 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3750 != PackageManager.PERMISSION_GRANTED) {
3751 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3752 }
3753 synchronized (mKeyguardDisabled) {
3754 mKeyguardDisabled.release(token);
3755
3756 if (!mKeyguardDisabled.isAcquired()) {
3757 // if we are the last one to reenable the keyguard wait until
3758 // we have actaully finished reenabling until returning
3759 mWaitingUntilKeyguardReenabled = true;
3760 while (mWaitingUntilKeyguardReenabled) {
3761 try {
3762 mKeyguardDisabled.wait();
3763 } catch (InterruptedException e) {
3764 Thread.currentThread().interrupt();
3765 }
3766 }
3767 }
3768 }
3769 }
3770
3771 /**
3772 * @see android.app.KeyguardManager#exitKeyguardSecurely
3773 */
3774 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3775 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3776 != PackageManager.PERMISSION_GRANTED) {
3777 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3778 }
3779 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3780 public void onKeyguardExitResult(boolean success) {
3781 try {
3782 callback.onKeyguardExitResult(success);
3783 } catch (RemoteException e) {
3784 // Client has died, we don't care.
3785 }
3786 }
3787 });
3788 }
3789
3790 public boolean inKeyguardRestrictedInputMode() {
3791 return mPolicy.inKeyguardRestrictedKeyInputMode();
3792 }
Romain Guy06882f82009-06-10 13:36:04 -07003793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 static float fixScale(float scale) {
3795 if (scale < 0) scale = 0;
3796 else if (scale > 20) scale = 20;
3797 return Math.abs(scale);
3798 }
Romain Guy06882f82009-06-10 13:36:04 -07003799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 public void setAnimationScale(int which, float scale) {
3801 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3802 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003803 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 }
3805
3806 if (scale < 0) scale = 0;
3807 else if (scale > 20) scale = 20;
3808 scale = Math.abs(scale);
3809 switch (which) {
3810 case 0: mWindowAnimationScale = fixScale(scale); break;
3811 case 1: mTransitionAnimationScale = fixScale(scale); break;
3812 }
Romain Guy06882f82009-06-10 13:36:04 -07003813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 // Persist setting
3815 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3816 }
Romain Guy06882f82009-06-10 13:36:04 -07003817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 public void setAnimationScales(float[] scales) {
3819 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3820 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003821 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
3823
3824 if (scales != null) {
3825 if (scales.length >= 1) {
3826 mWindowAnimationScale = fixScale(scales[0]);
3827 }
3828 if (scales.length >= 2) {
3829 mTransitionAnimationScale = fixScale(scales[1]);
3830 }
3831 }
Romain Guy06882f82009-06-10 13:36:04 -07003832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 // Persist setting
3834 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3835 }
Romain Guy06882f82009-06-10 13:36:04 -07003836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 public float getAnimationScale(int which) {
3838 switch (which) {
3839 case 0: return mWindowAnimationScale;
3840 case 1: return mTransitionAnimationScale;
3841 }
3842 return 0;
3843 }
Romain Guy06882f82009-06-10 13:36:04 -07003844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 public float[] getAnimationScales() {
3846 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3847 }
Romain Guy06882f82009-06-10 13:36:04 -07003848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 public int getSwitchState(int sw) {
3850 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3851 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003852 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 }
3854 return KeyInputQueue.getSwitchState(sw);
3855 }
Romain Guy06882f82009-06-10 13:36:04 -07003856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 public int getSwitchStateForDevice(int devid, int sw) {
3858 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3859 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003860 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 }
3862 return KeyInputQueue.getSwitchState(devid, sw);
3863 }
Romain Guy06882f82009-06-10 13:36:04 -07003864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 public int getScancodeState(int sw) {
3866 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3867 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003868 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 }
3870 return KeyInputQueue.getScancodeState(sw);
3871 }
Romain Guy06882f82009-06-10 13:36:04 -07003872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 public int getScancodeStateForDevice(int devid, int sw) {
3874 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3875 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003876 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 }
3878 return KeyInputQueue.getScancodeState(devid, sw);
3879 }
Romain Guy06882f82009-06-10 13:36:04 -07003880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 public int getKeycodeState(int sw) {
3882 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3883 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003884 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 }
3886 return KeyInputQueue.getKeycodeState(sw);
3887 }
Romain Guy06882f82009-06-10 13:36:04 -07003888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 public int getKeycodeStateForDevice(int devid, int sw) {
3890 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3891 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003892 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 }
3894 return KeyInputQueue.getKeycodeState(devid, sw);
3895 }
Romain Guy06882f82009-06-10 13:36:04 -07003896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3898 return KeyInputQueue.hasKeys(keycodes, keyExists);
3899 }
Romain Guy06882f82009-06-10 13:36:04 -07003900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 public void enableScreenAfterBoot() {
3902 synchronized(mWindowMap) {
3903 if (mSystemBooted) {
3904 return;
3905 }
3906 mSystemBooted = true;
3907 }
Romain Guy06882f82009-06-10 13:36:04 -07003908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 performEnableScreen();
3910 }
Romain Guy06882f82009-06-10 13:36:04 -07003911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 public void enableScreenIfNeededLocked() {
3913 if (mDisplayEnabled) {
3914 return;
3915 }
3916 if (!mSystemBooted) {
3917 return;
3918 }
3919 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3920 }
Romain Guy06882f82009-06-10 13:36:04 -07003921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 public void performEnableScreen() {
3923 synchronized(mWindowMap) {
3924 if (mDisplayEnabled) {
3925 return;
3926 }
3927 if (!mSystemBooted) {
3928 return;
3929 }
Romain Guy06882f82009-06-10 13:36:04 -07003930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 // Don't enable the screen until all existing windows
3932 // have been drawn.
3933 final int N = mWindows.size();
3934 for (int i=0; i<N; i++) {
3935 WindowState w = (WindowState)mWindows.get(i);
3936 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3937 return;
3938 }
3939 }
Romain Guy06882f82009-06-10 13:36:04 -07003940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 mDisplayEnabled = true;
3942 if (false) {
3943 Log.i(TAG, "ENABLING SCREEN!");
3944 StringWriter sw = new StringWriter();
3945 PrintWriter pw = new PrintWriter(sw);
3946 this.dump(null, pw, null);
3947 Log.i(TAG, sw.toString());
3948 }
3949 try {
3950 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3951 if (surfaceFlinger != null) {
3952 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3953 Parcel data = Parcel.obtain();
3954 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3955 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3956 data, null, 0);
3957 data.recycle();
3958 }
3959 } catch (RemoteException ex) {
3960 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3961 }
3962 }
Romain Guy06882f82009-06-10 13:36:04 -07003963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003967 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3968 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 }
Romain Guy06882f82009-06-10 13:36:04 -07003970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 public void setInTouchMode(boolean mode) {
3972 synchronized(mWindowMap) {
3973 mInTouchMode = mode;
3974 }
3975 }
3976
Romain Guy06882f82009-06-10 13:36:04 -07003977 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003978 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003980 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003981 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 }
3983
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003984 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 }
Romain Guy06882f82009-06-10 13:36:04 -07003986
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003987 public void setRotationUnchecked(int rotation,
3988 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 if(DEBUG_ORIENTATION) Log.v(TAG,
3990 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 long origId = Binder.clearCallingIdentity();
3993 boolean changed;
3994 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003995 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 }
Romain Guy06882f82009-06-10 13:36:04 -07003997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 if (changed) {
3999 sendNewConfiguration();
4000 synchronized(mWindowMap) {
4001 mLayoutNeeded = true;
4002 performLayoutAndPlaceSurfacesLocked();
4003 }
4004 } else if (alwaysSendConfiguration) {
4005 //update configuration ignoring orientation change
4006 sendNewConfiguration();
4007 }
Romain Guy06882f82009-06-10 13:36:04 -07004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 Binder.restoreCallingIdentity(origId);
4010 }
Romain Guy06882f82009-06-10 13:36:04 -07004011
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004012 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 boolean changed;
4014 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4015 rotation = mRequestedRotation;
4016 } else {
4017 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004018 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 }
4020 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004021 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 mRotation, mDisplayEnabled);
4023 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4024 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004027 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 "Rotation changed to " + rotation
4029 + " from " + mRotation
4030 + " (forceApp=" + mForcedAppOrientation
4031 + ", req=" + mRequestedRotation + ")");
4032 mRotation = rotation;
4033 mWindowsFreezingScreen = true;
4034 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4035 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4036 2000);
4037 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004038 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 mQueue.setOrientation(rotation);
4040 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004041 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 }
4043 for (int i=mWindows.size()-1; i>=0; i--) {
4044 WindowState w = (WindowState)mWindows.get(i);
4045 if (w.mSurface != null) {
4046 w.mOrientationChanging = true;
4047 }
4048 }
4049 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4050 try {
4051 mRotationWatchers.get(i).onRotationChanged(rotation);
4052 } catch (RemoteException e) {
4053 }
4054 }
4055 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 return changed;
4058 }
Romain Guy06882f82009-06-10 13:36:04 -07004059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 public int getRotation() {
4061 return mRotation;
4062 }
4063
4064 public int watchRotation(IRotationWatcher watcher) {
4065 final IBinder watcherBinder = watcher.asBinder();
4066 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4067 public void binderDied() {
4068 synchronized (mWindowMap) {
4069 for (int i=0; i<mRotationWatchers.size(); i++) {
4070 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004071 IRotationWatcher removed = mRotationWatchers.remove(i);
4072 if (removed != null) {
4073 removed.asBinder().unlinkToDeath(this, 0);
4074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 i--;
4076 }
4077 }
4078 }
4079 }
4080 };
Romain Guy06882f82009-06-10 13:36:04 -07004081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 synchronized (mWindowMap) {
4083 try {
4084 watcher.asBinder().linkToDeath(dr, 0);
4085 mRotationWatchers.add(watcher);
4086 } catch (RemoteException e) {
4087 // Client died, no cleanup needed.
4088 }
Romain Guy06882f82009-06-10 13:36:04 -07004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 return mRotation;
4091 }
4092 }
4093
4094 /**
4095 * Starts the view server on the specified port.
4096 *
4097 * @param port The port to listener to.
4098 *
4099 * @return True if the server was successfully started, false otherwise.
4100 *
4101 * @see com.android.server.ViewServer
4102 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4103 */
4104 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004105 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 return false;
4107 }
4108
4109 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4110 return false;
4111 }
4112
4113 if (port < 1024) {
4114 return false;
4115 }
4116
4117 if (mViewServer != null) {
4118 if (!mViewServer.isRunning()) {
4119 try {
4120 return mViewServer.start();
4121 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004122 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 }
4124 }
4125 return false;
4126 }
4127
4128 try {
4129 mViewServer = new ViewServer(this, port);
4130 return mViewServer.start();
4131 } catch (IOException e) {
4132 Log.w(TAG, "View server did not start");
4133 }
4134 return false;
4135 }
4136
Romain Guy06882f82009-06-10 13:36:04 -07004137 private boolean isSystemSecure() {
4138 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4139 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4140 }
4141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 /**
4143 * Stops the view server if it exists.
4144 *
4145 * @return True if the server stopped, false if it wasn't started or
4146 * couldn't be stopped.
4147 *
4148 * @see com.android.server.ViewServer
4149 */
4150 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004151 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 return false;
4153 }
4154
4155 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4156 return false;
4157 }
4158
4159 if (mViewServer != null) {
4160 return mViewServer.stop();
4161 }
4162 return false;
4163 }
4164
4165 /**
4166 * Indicates whether the view server is running.
4167 *
4168 * @return True if the server is running, false otherwise.
4169 *
4170 * @see com.android.server.ViewServer
4171 */
4172 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004173 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 return false;
4175 }
4176
4177 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4178 return false;
4179 }
4180
4181 return mViewServer != null && mViewServer.isRunning();
4182 }
4183
4184 /**
4185 * Lists all availble windows in the system. The listing is written in the
4186 * specified Socket's output stream with the following syntax:
4187 * windowHashCodeInHexadecimal windowName
4188 * Each line of the ouput represents a different window.
4189 *
4190 * @param client The remote client to send the listing to.
4191 * @return False if an error occured, true otherwise.
4192 */
4193 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004194 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 return false;
4196 }
4197
4198 boolean result = true;
4199
4200 Object[] windows;
4201 synchronized (mWindowMap) {
4202 windows = new Object[mWindows.size()];
4203 //noinspection unchecked
4204 windows = mWindows.toArray(windows);
4205 }
4206
4207 BufferedWriter out = null;
4208
4209 // Any uncaught exception will crash the system process
4210 try {
4211 OutputStream clientStream = client.getOutputStream();
4212 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4213
4214 final int count = windows.length;
4215 for (int i = 0; i < count; i++) {
4216 final WindowState w = (WindowState) windows[i];
4217 out.write(Integer.toHexString(System.identityHashCode(w)));
4218 out.write(' ');
4219 out.append(w.mAttrs.getTitle());
4220 out.write('\n');
4221 }
4222
4223 out.write("DONE.\n");
4224 out.flush();
4225 } catch (Exception e) {
4226 result = false;
4227 } finally {
4228 if (out != null) {
4229 try {
4230 out.close();
4231 } catch (IOException e) {
4232 result = false;
4233 }
4234 }
4235 }
4236
4237 return result;
4238 }
4239
4240 /**
4241 * Sends a command to a target window. The result of the command, if any, will be
4242 * written in the output stream of the specified socket.
4243 *
4244 * The parameters must follow this syntax:
4245 * windowHashcode extra
4246 *
4247 * Where XX is the length in characeters of the windowTitle.
4248 *
4249 * The first parameter is the target window. The window with the specified hashcode
4250 * will be the target. If no target can be found, nothing happens. The extra parameters
4251 * will be delivered to the target window and as parameters to the command itself.
4252 *
4253 * @param client The remote client to sent the result, if any, to.
4254 * @param command The command to execute.
4255 * @param parameters The command parameters.
4256 *
4257 * @return True if the command was successfully delivered, false otherwise. This does
4258 * not indicate whether the command itself was successful.
4259 */
4260 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004261 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 return false;
4263 }
4264
4265 boolean success = true;
4266 Parcel data = null;
4267 Parcel reply = null;
4268
4269 // Any uncaught exception will crash the system process
4270 try {
4271 // Find the hashcode of the window
4272 int index = parameters.indexOf(' ');
4273 if (index == -1) {
4274 index = parameters.length();
4275 }
4276 final String code = parameters.substring(0, index);
4277 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4278
4279 // Extract the command's parameter after the window description
4280 if (index < parameters.length()) {
4281 parameters = parameters.substring(index + 1);
4282 } else {
4283 parameters = "";
4284 }
4285
4286 final WindowManagerService.WindowState window = findWindow(hashCode);
4287 if (window == null) {
4288 return false;
4289 }
4290
4291 data = Parcel.obtain();
4292 data.writeInterfaceToken("android.view.IWindow");
4293 data.writeString(command);
4294 data.writeString(parameters);
4295 data.writeInt(1);
4296 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4297
4298 reply = Parcel.obtain();
4299
4300 final IBinder binder = window.mClient.asBinder();
4301 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4302 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4303
4304 reply.readException();
4305
4306 } catch (Exception e) {
4307 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4308 success = false;
4309 } finally {
4310 if (data != null) {
4311 data.recycle();
4312 }
4313 if (reply != null) {
4314 reply.recycle();
4315 }
4316 }
4317
4318 return success;
4319 }
4320
4321 private WindowState findWindow(int hashCode) {
4322 if (hashCode == -1) {
4323 return getFocusedWindow();
4324 }
4325
4326 synchronized (mWindowMap) {
4327 final ArrayList windows = mWindows;
4328 final int count = windows.size();
4329
4330 for (int i = 0; i < count; i++) {
4331 WindowState w = (WindowState) windows.get(i);
4332 if (System.identityHashCode(w) == hashCode) {
4333 return w;
4334 }
4335 }
4336 }
4337
4338 return null;
4339 }
4340
4341 /*
4342 * Instruct the Activity Manager to fetch the current configuration and broadcast
4343 * that to config-changed listeners if appropriate.
4344 */
4345 void sendNewConfiguration() {
4346 try {
4347 mActivityManager.updateConfiguration(null);
4348 } catch (RemoteException e) {
4349 }
4350 }
Romain Guy06882f82009-06-10 13:36:04 -07004351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 public Configuration computeNewConfiguration() {
4353 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004354 return computeNewConfigurationLocked();
4355 }
4356 }
Romain Guy06882f82009-06-10 13:36:04 -07004357
Dianne Hackbornc485a602009-03-24 22:39:49 -07004358 Configuration computeNewConfigurationLocked() {
4359 Configuration config = new Configuration();
4360 if (!computeNewConfigurationLocked(config)) {
4361 return null;
4362 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004363 return config;
4364 }
Romain Guy06882f82009-06-10 13:36:04 -07004365
Dianne Hackbornc485a602009-03-24 22:39:49 -07004366 boolean computeNewConfigurationLocked(Configuration config) {
4367 if (mDisplay == null) {
4368 return false;
4369 }
4370 mQueue.getInputConfiguration(config);
4371 final int dw = mDisplay.getWidth();
4372 final int dh = mDisplay.getHeight();
4373 int orientation = Configuration.ORIENTATION_SQUARE;
4374 if (dw < dh) {
4375 orientation = Configuration.ORIENTATION_PORTRAIT;
4376 } else if (dw > dh) {
4377 orientation = Configuration.ORIENTATION_LANDSCAPE;
4378 }
4379 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004380
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004381 DisplayMetrics dm = new DisplayMetrics();
4382 mDisplay.getMetrics(dm);
4383 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4384
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004385 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004386 // Note we only do this once because at this point we don't
4387 // expect the screen to change in this way at runtime, and want
4388 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004389 int longSize = dw;
4390 int shortSize = dh;
4391 if (longSize < shortSize) {
4392 int tmp = longSize;
4393 longSize = shortSize;
4394 shortSize = tmp;
4395 }
4396 longSize = (int)(longSize/dm.density);
4397 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004398
Dianne Hackborn723738c2009-06-25 19:48:04 -07004399 // These semi-magic numbers define our compatibility modes for
4400 // applications with different screens. Don't change unless you
4401 // make sure to test lots and lots of apps!
4402 if (longSize < 470) {
4403 // This is shorter than an HVGA normal density screen (which
4404 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004405 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4406 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004407 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004408 // Is this a large screen?
4409 if (longSize > 640 && shortSize >= 480) {
4410 // VGA or larger screens at medium density are the point
4411 // at which we consider it to be a large screen.
4412 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4413 } else {
4414 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4415
4416 // If this screen is wider than normal HVGA, or taller
4417 // than FWVGA, then for old apps we want to run in size
4418 // compatibility mode.
4419 if (shortSize > 321 || longSize > 570) {
4420 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4421 }
4422 }
4423
4424 // Is this a long screen?
4425 if (((longSize*3)/5) >= (shortSize-1)) {
4426 // Anything wider than WVGA (5:3) is considering to be long.
4427 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4428 } else {
4429 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4430 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004431 }
4432 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004433 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004434
Dianne Hackbornc485a602009-03-24 22:39:49 -07004435 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4436 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4437 mPolicy.adjustConfigurationLw(config);
4438 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 }
Romain Guy06882f82009-06-10 13:36:04 -07004440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 // -------------------------------------------------------------
4442 // Input Events and Focus Management
4443 // -------------------------------------------------------------
4444
4445 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004446 long curTime = SystemClock.uptimeMillis();
4447
Michael Chane10de972009-05-18 11:24:50 -07004448 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004449 if (mLastTouchEventType == eventType &&
4450 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4451 return;
4452 }
4453 mLastUserActivityCallTime = curTime;
4454 mLastTouchEventType = eventType;
4455 }
4456
4457 if (targetWin == null
4458 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4459 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 }
4461 }
4462
4463 // tells if it's a cheek event or not -- this function is stateful
4464 private static final int EVENT_NONE = 0;
4465 private static final int EVENT_UNKNOWN = 0;
4466 private static final int EVENT_CHEEK = 0;
4467 private static final int EVENT_IGNORE_DURATION = 300; // ms
4468 private static final float CHEEK_THRESHOLD = 0.6f;
4469 private int mEventState = EVENT_NONE;
4470 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 private int eventType(MotionEvent ev) {
4473 float size = ev.getSize();
4474 switch (ev.getAction()) {
4475 case MotionEvent.ACTION_DOWN:
4476 mEventSize = size;
4477 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4478 case MotionEvent.ACTION_UP:
4479 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004480 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 case MotionEvent.ACTION_MOVE:
4482 final int N = ev.getHistorySize();
4483 if (size > mEventSize) mEventSize = size;
4484 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4485 for (int i=0; i<N; i++) {
4486 size = ev.getHistoricalSize(i);
4487 if (size > mEventSize) mEventSize = size;
4488 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4489 }
4490 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4491 return TOUCH_EVENT;
4492 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004493 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
4495 default:
4496 // not good
4497 return OTHER_EVENT;
4498 }
4499 }
4500
4501 /**
4502 * @return Returns true if event was dispatched, false if it was dropped for any reason
4503 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004504 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4506 "dispatchPointer " + ev);
4507
Michael Chan53071d62009-05-13 17:29:48 -07004508 if (MEASURE_LATENCY) {
4509 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4510 }
4511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004513 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514
Michael Chan53071d62009-05-13 17:29:48 -07004515 if (MEASURE_LATENCY) {
4516 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4517 }
4518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 if (action == MotionEvent.ACTION_UP) {
4522 // let go of our target
4523 mKeyWaiter.mMotionTarget = null;
4524 mPowerManager.logPointerUpEvent();
4525 } else if (action == MotionEvent.ACTION_DOWN) {
4526 mPowerManager.logPointerDownEvent();
4527 }
4528
4529 if (targetObj == null) {
4530 // In this case we are either dropping the event, or have received
4531 // a move or up without a down. It is common to receive move
4532 // events in such a way, since this means the user is moving the
4533 // pointer without actually pressing down. All other cases should
4534 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004535 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4537 }
4538 if (qev != null) {
4539 mQueue.recycleEvent(qev);
4540 }
4541 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004542 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 }
4544 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4545 if (qev != null) {
4546 mQueue.recycleEvent(qev);
4547 }
4548 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004549 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
Romain Guy06882f82009-06-10 13:36:04 -07004551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004555 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556
4557 //Log.i(TAG, "Sending " + ev + " to " + target);
4558
4559 if (uid != 0 && uid != target.mSession.mUid) {
4560 if (mContext.checkPermission(
4561 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4562 != PackageManager.PERMISSION_GRANTED) {
4563 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4564 + pid + " uid " + uid + " to window " + target
4565 + " owned by uid " + target.mSession.mUid);
4566 if (qev != null) {
4567 mQueue.recycleEvent(qev);
4568 }
4569 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004570 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 }
4572 }
4573
Michael Chan53071d62009-05-13 17:29:48 -07004574 if (MEASURE_LATENCY) {
4575 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4576 }
4577
Romain Guy06882f82009-06-10 13:36:04 -07004578 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4580 //target wants to ignore fat touch events
4581 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4582 //explicit flag to return without processing event further
4583 boolean returnFlag = false;
4584 if((action == MotionEvent.ACTION_DOWN)) {
4585 mFatTouch = false;
4586 if(cheekPress) {
4587 mFatTouch = true;
4588 returnFlag = true;
4589 }
4590 } else {
4591 if(action == MotionEvent.ACTION_UP) {
4592 if(mFatTouch) {
4593 //earlier even was invalid doesnt matter if current up is cheekpress or not
4594 mFatTouch = false;
4595 returnFlag = true;
4596 } else if(cheekPress) {
4597 //cancel the earlier event
4598 ev.setAction(MotionEvent.ACTION_CANCEL);
4599 action = MotionEvent.ACTION_CANCEL;
4600 }
4601 } else if(action == MotionEvent.ACTION_MOVE) {
4602 if(mFatTouch) {
4603 //two cases here
4604 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004605 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606 returnFlag = true;
4607 } else if(cheekPress) {
4608 //valid down followed by invalid moves
4609 //an invalid move have to cancel earlier action
4610 ev.setAction(MotionEvent.ACTION_CANCEL);
4611 action = MotionEvent.ACTION_CANCEL;
4612 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4613 //note that the subsequent invalid moves will not get here
4614 mFatTouch = true;
4615 }
4616 }
4617 } //else if action
4618 if(returnFlag) {
4619 //recycle que, ev
4620 if (qev != null) {
4621 mQueue.recycleEvent(qev);
4622 }
4623 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004624 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 }
4626 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004627
Michael Chan9f028e62009-08-04 17:37:46 -07004628 // Enable this for testing the "right" value
4629 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004630 int max_events_per_sec = 35;
4631 try {
4632 max_events_per_sec = Integer.parseInt(SystemProperties
4633 .get("windowsmgr.max_events_per_sec"));
4634 if (max_events_per_sec < 1) {
4635 max_events_per_sec = 35;
4636 }
4637 } catch (NumberFormatException e) {
4638 }
4639 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4640 }
4641
4642 /*
4643 * Throttle events to minimize CPU usage when there's a flood of events
4644 * e.g. constant contact with the screen
4645 */
4646 if (action == MotionEvent.ACTION_MOVE) {
4647 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4648 long now = SystemClock.uptimeMillis();
4649 if (now < nextEventTime) {
4650 try {
4651 Thread.sleep(nextEventTime - now);
4652 } catch (InterruptedException e) {
4653 }
4654 mLastTouchEventTime = nextEventTime;
4655 } else {
4656 mLastTouchEventTime = now;
4657 }
4658 }
4659
Michael Chan53071d62009-05-13 17:29:48 -07004660 if (MEASURE_LATENCY) {
4661 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4662 }
4663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 synchronized(mWindowMap) {
4665 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4666 mKeyWaiter.bindTargetWindowLocked(target,
4667 KeyWaiter.RETURN_PENDING_POINTER, qev);
4668 ev = null;
4669 } else {
4670 if (action == MotionEvent.ACTION_DOWN) {
4671 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4672 if (out != null) {
4673 MotionEvent oev = MotionEvent.obtain(ev);
4674 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4675 do {
4676 final Rect frame = out.mFrame;
4677 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4678 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004679 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 } catch (android.os.RemoteException e) {
4681 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4682 }
4683 oev.offsetLocation((float)frame.left, (float)frame.top);
4684 out = out.mNextOutsideTouch;
4685 } while (out != null);
4686 mKeyWaiter.mOutsideTouchTargets = null;
4687 }
4688 }
4689 final Rect frame = target.mFrame;
4690 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4691 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004692
4693 // If we are on top of the wallpaper, then the wallpaper also
4694 // gets to see this movement.
4695 if (mWallpaperTarget == target) {
4696 sendPointerToWallpaperLocked(target, ev, eventTime);
4697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 }
4699 }
Romain Guy06882f82009-06-10 13:36:04 -07004700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 // finally offset the event to the target's coordinate system and
4702 // dispatch the event.
4703 try {
4704 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4705 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4706 }
Michael Chan53071d62009-05-13 17:29:48 -07004707
4708 if (MEASURE_LATENCY) {
4709 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4710 }
4711
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004712 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004713
4714 if (MEASURE_LATENCY) {
4715 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4716 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004717 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 } catch (android.os.RemoteException e) {
4719 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4720 mKeyWaiter.mMotionTarget = null;
4721 try {
4722 removeWindow(target.mSession, target.mClient);
4723 } catch (java.util.NoSuchElementException ex) {
4724 // This will happen if the window has already been
4725 // removed.
4726 }
4727 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004728 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 }
Romain Guy06882f82009-06-10 13:36:04 -07004730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 /**
4732 * @return Returns true if event was dispatched, false if it was dropped for any reason
4733 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004734 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 if (DEBUG_INPUT) Log.v(
4736 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004739 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 if (focusObj == null) {
4741 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4742 if (qev != null) {
4743 mQueue.recycleEvent(qev);
4744 }
4745 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004746 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 }
4748 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4749 if (qev != null) {
4750 mQueue.recycleEvent(qev);
4751 }
4752 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004753 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 }
Romain Guy06882f82009-06-10 13:36:04 -07004755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 if (uid != 0 && uid != focus.mSession.mUid) {
4759 if (mContext.checkPermission(
4760 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4761 != PackageManager.PERMISSION_GRANTED) {
4762 Log.w(TAG, "Permission denied: injecting key event from pid "
4763 + pid + " uid " + uid + " to window " + focus
4764 + " owned by uid " + focus.mSession.mUid);
4765 if (qev != null) {
4766 mQueue.recycleEvent(qev);
4767 }
4768 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004769 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 }
4771 }
Romain Guy06882f82009-06-10 13:36:04 -07004772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 synchronized(mWindowMap) {
4776 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4777 mKeyWaiter.bindTargetWindowLocked(focus,
4778 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4779 // We don't deliver movement events to the client, we hold
4780 // them and wait for them to call back.
4781 ev = null;
4782 } else {
4783 mKeyWaiter.bindTargetWindowLocked(focus);
4784 }
4785 }
Romain Guy06882f82009-06-10 13:36:04 -07004786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004788 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004789 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 } catch (android.os.RemoteException e) {
4791 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4792 try {
4793 removeWindow(focus.mSession, focus.mClient);
4794 } catch (java.util.NoSuchElementException ex) {
4795 // This will happen if the window has already been
4796 // removed.
4797 }
4798 }
Romain Guy06882f82009-06-10 13:36:04 -07004799
Dianne Hackborncfaef692009-06-15 14:24:44 -07004800 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 }
Romain Guy06882f82009-06-10 13:36:04 -07004802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 /**
4804 * @return Returns true if event was dispatched, false if it was dropped for any reason
4805 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004806 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4808
4809 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004810 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 if (focusObj == null) {
4812 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004813 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 }
4815 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004816 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 }
Romain Guy06882f82009-06-10 13:36:04 -07004818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 if (DEBUG_INPUT) Log.v(
4822 TAG, "Dispatching to " + focus + ": " + event);
4823
4824 if (uid != 0 && uid != focus.mSession.mUid) {
4825 if (mContext.checkPermission(
4826 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4827 != PackageManager.PERMISSION_GRANTED) {
4828 Log.w(TAG, "Permission denied: injecting key event from pid "
4829 + pid + " uid " + uid + " to window " + focus
4830 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004831 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 }
4833 }
Romain Guy06882f82009-06-10 13:36:04 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 synchronized(mWindowMap) {
4836 mKeyWaiter.bindTargetWindowLocked(focus);
4837 }
4838
4839 // NOSHIP extra state logging
4840 mKeyWaiter.recordDispatchState(event, focus);
4841 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 try {
4844 if (DEBUG_INPUT || DEBUG_FOCUS) {
4845 Log.v(TAG, "Delivering key " + event.getKeyCode()
4846 + " to " + focus);
4847 }
4848 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004849 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 } catch (android.os.RemoteException e) {
4851 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4852 try {
4853 removeWindow(focus.mSession, focus.mClient);
4854 } catch (java.util.NoSuchElementException ex) {
4855 // This will happen if the window has already been
4856 // removed.
4857 }
4858 }
Romain Guy06882f82009-06-10 13:36:04 -07004859
Dianne Hackborncfaef692009-06-15 14:24:44 -07004860 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004861 }
Romain Guy06882f82009-06-10 13:36:04 -07004862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 public void pauseKeyDispatching(IBinder _token) {
4864 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4865 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004866 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 }
4868
4869 synchronized (mWindowMap) {
4870 WindowToken token = mTokenMap.get(_token);
4871 if (token != null) {
4872 mKeyWaiter.pauseDispatchingLocked(token);
4873 }
4874 }
4875 }
4876
4877 public void resumeKeyDispatching(IBinder _token) {
4878 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4879 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004880 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 }
4882
4883 synchronized (mWindowMap) {
4884 WindowToken token = mTokenMap.get(_token);
4885 if (token != null) {
4886 mKeyWaiter.resumeDispatchingLocked(token);
4887 }
4888 }
4889 }
4890
4891 public void setEventDispatching(boolean enabled) {
4892 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4893 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004894 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 }
4896
4897 synchronized (mWindowMap) {
4898 mKeyWaiter.setEventDispatchingLocked(enabled);
4899 }
4900 }
Romain Guy06882f82009-06-10 13:36:04 -07004901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 /**
4903 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004904 *
4905 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 * {@link SystemClock#uptimeMillis()} as the timebase.)
4907 * @param sync If true, wait for the event to be completed before returning to the caller.
4908 * @return Returns true if event was dispatched, false if it was dropped for any reason
4909 */
4910 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4911 long downTime = ev.getDownTime();
4912 long eventTime = ev.getEventTime();
4913
4914 int action = ev.getAction();
4915 int code = ev.getKeyCode();
4916 int repeatCount = ev.getRepeatCount();
4917 int metaState = ev.getMetaState();
4918 int deviceId = ev.getDeviceId();
4919 int scancode = ev.getScanCode();
4920
4921 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4922 if (downTime == 0) downTime = eventTime;
4923
4924 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004925 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004927 final int pid = Binder.getCallingPid();
4928 final int uid = Binder.getCallingUid();
4929 final long ident = Binder.clearCallingIdentity();
4930 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004932 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004934 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004935 switch (result) {
4936 case INJECT_NO_PERMISSION:
4937 throw new SecurityException(
4938 "Injecting to another application requires INJECT_EVENT permission");
4939 case INJECT_SUCCEEDED:
4940 return true;
4941 }
4942 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 }
4944
4945 /**
4946 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004947 *
4948 * @param ev A motion event describing the pointer (touch) action. (As noted in
4949 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950 * {@link SystemClock#uptimeMillis()} as the timebase.)
4951 * @param sync If true, wait for the event to be completed before returning to the caller.
4952 * @return Returns true if event was dispatched, false if it was dropped for any reason
4953 */
4954 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004955 final int pid = Binder.getCallingPid();
4956 final int uid = Binder.getCallingUid();
4957 final long ident = Binder.clearCallingIdentity();
4958 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004960 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004962 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004963 switch (result) {
4964 case INJECT_NO_PERMISSION:
4965 throw new SecurityException(
4966 "Injecting to another application requires INJECT_EVENT permission");
4967 case INJECT_SUCCEEDED:
4968 return true;
4969 }
4970 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 }
Romain Guy06882f82009-06-10 13:36:04 -07004972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 /**
4974 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004975 *
4976 * @param ev A motion event describing the trackball action. (As noted in
4977 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 * {@link SystemClock#uptimeMillis()} as the timebase.)
4979 * @param sync If true, wait for the event to be completed before returning to the caller.
4980 * @return Returns true if event was dispatched, false if it was dropped for any reason
4981 */
4982 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004983 final int pid = Binder.getCallingPid();
4984 final int uid = Binder.getCallingUid();
4985 final long ident = Binder.clearCallingIdentity();
4986 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004988 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004990 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004991 switch (result) {
4992 case INJECT_NO_PERMISSION:
4993 throw new SecurityException(
4994 "Injecting to another application requires INJECT_EVENT permission");
4995 case INJECT_SUCCEEDED:
4996 return true;
4997 }
4998 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
Romain Guy06882f82009-06-10 13:36:04 -07005000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 private WindowState getFocusedWindow() {
5002 synchronized (mWindowMap) {
5003 return getFocusedWindowLocked();
5004 }
5005 }
5006
5007 private WindowState getFocusedWindowLocked() {
5008 return mCurrentFocus;
5009 }
Romain Guy06882f82009-06-10 13:36:04 -07005010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 /**
5012 * This class holds the state for dispatching key events. This state
5013 * is protected by the KeyWaiter instance, NOT by the window lock. You
5014 * can be holding the main window lock while acquire the KeyWaiter lock,
5015 * but not the other way around.
5016 */
5017 final class KeyWaiter {
5018 // NOSHIP debugging
5019 public class DispatchState {
5020 private KeyEvent event;
5021 private WindowState focus;
5022 private long time;
5023 private WindowState lastWin;
5024 private IBinder lastBinder;
5025 private boolean finished;
5026 private boolean gotFirstWindow;
5027 private boolean eventDispatching;
5028 private long timeToSwitch;
5029 private boolean wasFrozen;
5030 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005031 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5034 focus = theFocus;
5035 event = theEvent;
5036 time = System.currentTimeMillis();
5037 // snapshot KeyWaiter state
5038 lastWin = mLastWin;
5039 lastBinder = mLastBinder;
5040 finished = mFinished;
5041 gotFirstWindow = mGotFirstWindow;
5042 eventDispatching = mEventDispatching;
5043 timeToSwitch = mTimeToSwitch;
5044 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005045 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 // cache the paused state at ctor time as well
5047 if (theFocus == null || theFocus.mToken == null) {
5048 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5049 focusPaused = false;
5050 } else {
5051 focusPaused = theFocus.mToken.paused;
5052 }
5053 }
Romain Guy06882f82009-06-10 13:36:04 -07005054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 public String toString() {
5056 return "{{" + event + " to " + focus + " @ " + time
5057 + " lw=" + lastWin + " lb=" + lastBinder
5058 + " fin=" + finished + " gfw=" + gotFirstWindow
5059 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005060 + " wf=" + wasFrozen + " fp=" + focusPaused
5061 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 }
5063 };
5064 private DispatchState mDispatchState = null;
5065 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5066 mDispatchState = new DispatchState(theEvent, theFocus);
5067 }
5068 // END NOSHIP
5069
5070 public static final int RETURN_NOTHING = 0;
5071 public static final int RETURN_PENDING_POINTER = 1;
5072 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 final Object SKIP_TARGET_TOKEN = new Object();
5075 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 private WindowState mLastWin = null;
5078 private IBinder mLastBinder = null;
5079 private boolean mFinished = true;
5080 private boolean mGotFirstWindow = false;
5081 private boolean mEventDispatching = true;
5082 private long mTimeToSwitch = 0;
5083 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 // Target of Motion events
5086 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 // Windows above the target who would like to receive an "outside"
5089 // touch event for any down events outside of them.
5090 WindowState mOutsideTouchTargets;
5091
5092 /**
5093 * Wait for the last event dispatch to complete, then find the next
5094 * target that should receive the given event and wait for that one
5095 * to be ready to receive it.
5096 */
5097 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5098 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005099 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 long startTime = SystemClock.uptimeMillis();
5101 long keyDispatchingTimeout = 5 * 1000;
5102 long waitedFor = 0;
5103
5104 while (true) {
5105 // Figure out which window we care about. It is either the
5106 // last window we are waiting to have process the event or,
5107 // if none, then the next window we think the event should go
5108 // to. Note: we retrieve mLastWin outside of the lock, so
5109 // it may change before we lock. Thus we must check it again.
5110 WindowState targetWin = mLastWin;
5111 boolean targetIsNew = targetWin == null;
5112 if (DEBUG_INPUT) Log.v(
5113 TAG, "waitForLastKey: mFinished=" + mFinished +
5114 ", mLastWin=" + mLastWin);
5115 if (targetIsNew) {
5116 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005117 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 if (target == SKIP_TARGET_TOKEN) {
5119 // The user has pressed a special key, and we are
5120 // dropping all pending events before it.
5121 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5122 + " " + nextMotion);
5123 return null;
5124 }
5125 if (target == CONSUMED_EVENT_TOKEN) {
5126 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5127 + " " + nextMotion);
5128 return target;
5129 }
5130 targetWin = (WindowState)target;
5131 }
Romain Guy06882f82009-06-10 13:36:04 -07005132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 // Now: is it okay to send the next event to this window?
5136 synchronized (this) {
5137 // First: did we come here based on the last window not
5138 // being null, but it changed by the time we got here?
5139 // If so, try again.
5140 if (!targetIsNew && mLastWin == null) {
5141 continue;
5142 }
Romain Guy06882f82009-06-10 13:36:04 -07005143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 // We never dispatch events if not finished with the
5145 // last one, or the display is frozen.
5146 if (mFinished && !mDisplayFrozen) {
5147 // If event dispatching is disabled, then we
5148 // just consume the events.
5149 if (!mEventDispatching) {
5150 if (DEBUG_INPUT) Log.v(TAG,
5151 "Skipping event; dispatching disabled: "
5152 + nextKey + " " + nextMotion);
5153 return null;
5154 }
5155 if (targetWin != null) {
5156 // If this is a new target, and that target is not
5157 // paused or unresponsive, then all looks good to
5158 // handle the event.
5159 if (targetIsNew && !targetWin.mToken.paused) {
5160 return targetWin;
5161 }
Romain Guy06882f82009-06-10 13:36:04 -07005162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 // If we didn't find a target window, and there is no
5164 // focused app window, then just eat the events.
5165 } else if (mFocusedApp == null) {
5166 if (DEBUG_INPUT) Log.v(TAG,
5167 "Skipping event; no focused app: "
5168 + nextKey + " " + nextMotion);
5169 return null;
5170 }
5171 }
Romain Guy06882f82009-06-10 13:36:04 -07005172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 if (DEBUG_INPUT) Log.v(
5174 TAG, "Waiting for last key in " + mLastBinder
5175 + " target=" + targetWin
5176 + " mFinished=" + mFinished
5177 + " mDisplayFrozen=" + mDisplayFrozen
5178 + " targetIsNew=" + targetIsNew
5179 + " paused="
5180 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005181 + " mFocusedApp=" + mFocusedApp
5182 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 targetApp = targetWin != null
5185 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 long curTimeout = keyDispatchingTimeout;
5188 if (mTimeToSwitch != 0) {
5189 long now = SystemClock.uptimeMillis();
5190 if (mTimeToSwitch <= now) {
5191 // If an app switch key has been pressed, and we have
5192 // waited too long for the current app to finish
5193 // processing keys, then wait no more!
5194 doFinishedKeyLocked(true);
5195 continue;
5196 }
5197 long switchTimeout = mTimeToSwitch - now;
5198 if (curTimeout > switchTimeout) {
5199 curTimeout = switchTimeout;
5200 }
5201 }
Romain Guy06882f82009-06-10 13:36:04 -07005202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 try {
5204 // after that continue
5205 // processing keys, so we don't get stuck.
5206 if (DEBUG_INPUT) Log.v(
5207 TAG, "Waiting for key dispatch: " + curTimeout);
5208 wait(curTimeout);
5209 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5210 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005211 + startTime + " switchTime=" + mTimeToSwitch
5212 + " target=" + targetWin + " mLW=" + mLastWin
5213 + " mLB=" + mLastBinder + " fin=" + mFinished
5214 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 } catch (InterruptedException e) {
5216 }
5217 }
5218
5219 // If we were frozen during configuration change, restart the
5220 // timeout checks from now; otherwise look at whether we timed
5221 // out before awakening.
5222 if (mWasFrozen) {
5223 waitedFor = 0;
5224 mWasFrozen = false;
5225 } else {
5226 waitedFor = SystemClock.uptimeMillis() - startTime;
5227 }
5228
5229 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5230 IApplicationToken at = null;
5231 synchronized (this) {
5232 Log.w(TAG, "Key dispatching timed out sending to " +
5233 (targetWin != null ? targetWin.mAttrs.getTitle()
5234 : "<null>"));
5235 // NOSHIP debugging
5236 Log.w(TAG, "Dispatch state: " + mDispatchState);
5237 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5238 // END NOSHIP
5239 //dump();
5240 if (targetWin != null) {
5241 at = targetWin.getAppToken();
5242 } else if (targetApp != null) {
5243 at = targetApp.appToken;
5244 }
5245 }
5246
5247 boolean abort = true;
5248 if (at != null) {
5249 try {
5250 long timeout = at.getKeyDispatchingTimeout();
5251 if (timeout > waitedFor) {
5252 // we did not wait the proper amount of time for this application.
5253 // set the timeout to be the real timeout and wait again.
5254 keyDispatchingTimeout = timeout - waitedFor;
5255 continue;
5256 } else {
5257 abort = at.keyDispatchingTimedOut();
5258 }
5259 } catch (RemoteException ex) {
5260 }
5261 }
5262
5263 synchronized (this) {
5264 if (abort && (mLastWin == targetWin || targetWin == null)) {
5265 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005266 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 if (DEBUG_INPUT) Log.v(TAG,
5268 "Window " + mLastWin +
5269 " timed out on key input");
5270 if (mLastWin.mToken.paused) {
5271 Log.w(TAG, "Un-pausing dispatching to this window");
5272 mLastWin.mToken.paused = false;
5273 }
5274 }
5275 if (mMotionTarget == targetWin) {
5276 mMotionTarget = null;
5277 }
5278 mLastWin = null;
5279 mLastBinder = null;
5280 if (failIfTimeout || targetWin == null) {
5281 return null;
5282 }
5283 } else {
5284 Log.w(TAG, "Continuing to wait for key to be dispatched");
5285 startTime = SystemClock.uptimeMillis();
5286 }
5287 }
5288 }
5289 }
5290 }
Romain Guy06882f82009-06-10 13:36:04 -07005291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005293 MotionEvent nextMotion, boolean isPointerEvent,
5294 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 if (nextKey != null) {
5298 // Find the target window for a normal key event.
5299 final int keycode = nextKey.getKeyCode();
5300 final int repeatCount = nextKey.getRepeatCount();
5301 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5302 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005305 if (callingUid == 0 ||
5306 mContext.checkPermission(
5307 android.Manifest.permission.INJECT_EVENTS,
5308 callingPid, callingUid)
5309 == PackageManager.PERMISSION_GRANTED) {
5310 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005311 nextKey.getMetaState(), down, repeatCount,
5312 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 Log.w(TAG, "Event timeout during app switch: dropping "
5315 + nextKey);
5316 return SKIP_TARGET_TOKEN;
5317 }
Romain Guy06882f82009-06-10 13:36:04 -07005318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 WindowState focus = null;
5322 synchronized(mWindowMap) {
5323 focus = getFocusedWindowLocked();
5324 }
Romain Guy06882f82009-06-10 13:36:04 -07005325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005327
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005328 if (callingUid == 0 ||
5329 (focus != null && callingUid == focus.mSession.mUid) ||
5330 mContext.checkPermission(
5331 android.Manifest.permission.INJECT_EVENTS,
5332 callingPid, callingUid)
5333 == PackageManager.PERMISSION_GRANTED) {
5334 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005335 keycode, nextKey.getMetaState(), down, repeatCount,
5336 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005337 return CONSUMED_EVENT_TOKEN;
5338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 }
Romain Guy06882f82009-06-10 13:36:04 -07005340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 } else if (!isPointerEvent) {
5344 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5345 if (!dispatch) {
5346 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5347 + nextMotion);
5348 return SKIP_TARGET_TOKEN;
5349 }
Romain Guy06882f82009-06-10 13:36:04 -07005350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 WindowState focus = null;
5352 synchronized(mWindowMap) {
5353 focus = getFocusedWindowLocked();
5354 }
Romain Guy06882f82009-06-10 13:36:04 -07005355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5357 return focus;
5358 }
Romain Guy06882f82009-06-10 13:36:04 -07005359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 if (nextMotion == null) {
5361 return SKIP_TARGET_TOKEN;
5362 }
Romain Guy06882f82009-06-10 13:36:04 -07005363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5365 KeyEvent.KEYCODE_UNKNOWN);
5366 if (!dispatch) {
5367 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5368 + nextMotion);
5369 return SKIP_TARGET_TOKEN;
5370 }
Romain Guy06882f82009-06-10 13:36:04 -07005371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 // Find the target window for a pointer event.
5373 int action = nextMotion.getAction();
5374 final float xf = nextMotion.getX();
5375 final float yf = nextMotion.getY();
5376 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 final boolean screenWasOff = qev != null
5379 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 synchronized(mWindowMap) {
5384 synchronized (this) {
5385 if (action == MotionEvent.ACTION_DOWN) {
5386 if (mMotionTarget != null) {
5387 // this is weird, we got a pen down, but we thought it was
5388 // already down!
5389 // XXX: We should probably send an ACTION_UP to the current
5390 // target.
5391 Log.w(TAG, "Pointer down received while already down in: "
5392 + mMotionTarget);
5393 mMotionTarget = null;
5394 }
Romain Guy06882f82009-06-10 13:36:04 -07005395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 // ACTION_DOWN is special, because we need to lock next events to
5397 // the window we'll land onto.
5398 final int x = (int)xf;
5399 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 final ArrayList windows = mWindows;
5402 final int N = windows.size();
5403 WindowState topErrWindow = null;
5404 final Rect tmpRect = mTempRect;
5405 for (int i=N-1; i>=0; i--) {
5406 WindowState child = (WindowState)windows.get(i);
5407 //Log.i(TAG, "Checking dispatch to: " + child);
5408 final int flags = child.mAttrs.flags;
5409 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5410 if (topErrWindow == null) {
5411 topErrWindow = child;
5412 }
5413 }
5414 if (!child.isVisibleLw()) {
5415 //Log.i(TAG, "Not visible!");
5416 continue;
5417 }
5418 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5419 //Log.i(TAG, "Not touchable!");
5420 if ((flags & WindowManager.LayoutParams
5421 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5422 child.mNextOutsideTouch = mOutsideTouchTargets;
5423 mOutsideTouchTargets = child;
5424 }
5425 continue;
5426 }
5427 tmpRect.set(child.mFrame);
5428 if (child.mTouchableInsets == ViewTreeObserver
5429 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5430 // The touch is inside of the window if it is
5431 // inside the frame, AND the content part of that
5432 // frame that was given by the application.
5433 tmpRect.left += child.mGivenContentInsets.left;
5434 tmpRect.top += child.mGivenContentInsets.top;
5435 tmpRect.right -= child.mGivenContentInsets.right;
5436 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5437 } else if (child.mTouchableInsets == ViewTreeObserver
5438 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5439 // The touch is inside of the window if it is
5440 // inside the frame, AND the visible part of that
5441 // frame that was given by the application.
5442 tmpRect.left += child.mGivenVisibleInsets.left;
5443 tmpRect.top += child.mGivenVisibleInsets.top;
5444 tmpRect.right -= child.mGivenVisibleInsets.right;
5445 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5446 }
5447 final int touchFlags = flags &
5448 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5449 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5450 if (tmpRect.contains(x, y) || touchFlags == 0) {
5451 //Log.i(TAG, "Using this target!");
5452 if (!screenWasOff || (flags &
5453 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5454 mMotionTarget = child;
5455 } else {
5456 //Log.i(TAG, "Waking, skip!");
5457 mMotionTarget = null;
5458 }
5459 break;
5460 }
Romain Guy06882f82009-06-10 13:36:04 -07005461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 if ((flags & WindowManager.LayoutParams
5463 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5464 child.mNextOutsideTouch = mOutsideTouchTargets;
5465 mOutsideTouchTargets = child;
5466 //Log.i(TAG, "Adding to outside target list: " + child);
5467 }
5468 }
5469
5470 // if there's an error window but it's not accepting
5471 // focus (typically because it is not yet visible) just
5472 // wait for it -- any other focused window may in fact
5473 // be in ANR state.
5474 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5475 mMotionTarget = null;
5476 }
5477 }
Romain Guy06882f82009-06-10 13:36:04 -07005478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 target = mMotionTarget;
5480 }
5481 }
Romain Guy06882f82009-06-10 13:36:04 -07005482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 // Pointer events are a little different -- if there isn't a
5486 // target found for any event, then just drop it.
5487 return target != null ? target : SKIP_TARGET_TOKEN;
5488 }
Romain Guy06882f82009-06-10 13:36:04 -07005489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 boolean checkShouldDispatchKey(int keycode) {
5491 synchronized (this) {
5492 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5493 mTimeToSwitch = 0;
5494 return true;
5495 }
5496 if (mTimeToSwitch != 0
5497 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5498 return false;
5499 }
5500 return true;
5501 }
5502 }
Romain Guy06882f82009-06-10 13:36:04 -07005503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 void bindTargetWindowLocked(WindowState win,
5505 int pendingWhat, QueuedEvent pendingMotion) {
5506 synchronized (this) {
5507 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5508 }
5509 }
Romain Guy06882f82009-06-10 13:36:04 -07005510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 void bindTargetWindowLocked(WindowState win) {
5512 synchronized (this) {
5513 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5514 }
5515 }
5516
5517 void bindTargetWindowLockedLocked(WindowState win,
5518 int pendingWhat, QueuedEvent pendingMotion) {
5519 mLastWin = win;
5520 mLastBinder = win.mClient.asBinder();
5521 mFinished = false;
5522 if (pendingMotion != null) {
5523 final Session s = win.mSession;
5524 if (pendingWhat == RETURN_PENDING_POINTER) {
5525 releasePendingPointerLocked(s);
5526 s.mPendingPointerMove = pendingMotion;
5527 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005528 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 "bindTargetToWindow " + s.mPendingPointerMove);
5530 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5531 releasePendingTrackballLocked(s);
5532 s.mPendingTrackballMove = pendingMotion;
5533 s.mPendingTrackballWindow = win;
5534 }
5535 }
5536 }
Romain Guy06882f82009-06-10 13:36:04 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 void releasePendingPointerLocked(Session s) {
5539 if (DEBUG_INPUT) Log.v(TAG,
5540 "releasePendingPointer " + s.mPendingPointerMove);
5541 if (s.mPendingPointerMove != null) {
5542 mQueue.recycleEvent(s.mPendingPointerMove);
5543 s.mPendingPointerMove = null;
5544 }
5545 }
Romain Guy06882f82009-06-10 13:36:04 -07005546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 void releasePendingTrackballLocked(Session s) {
5548 if (s.mPendingTrackballMove != null) {
5549 mQueue.recycleEvent(s.mPendingTrackballMove);
5550 s.mPendingTrackballMove = null;
5551 }
5552 }
Romain Guy06882f82009-06-10 13:36:04 -07005553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5555 int returnWhat) {
5556 if (DEBUG_INPUT) Log.v(
5557 TAG, "finishedKey: client=" + client + ", force=" + force);
5558
5559 if (client == null) {
5560 return null;
5561 }
5562
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005563 MotionEvent res = null;
5564 QueuedEvent qev = null;
5565 WindowState win = null;
5566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 synchronized (this) {
5568 if (DEBUG_INPUT) Log.v(
5569 TAG, "finishedKey: client=" + client.asBinder()
5570 + ", force=" + force + ", last=" + mLastBinder
5571 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 if (returnWhat == RETURN_PENDING_POINTER) {
5574 qev = session.mPendingPointerMove;
5575 win = session.mPendingPointerWindow;
5576 session.mPendingPointerMove = null;
5577 session.mPendingPointerWindow = null;
5578 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5579 qev = session.mPendingTrackballMove;
5580 win = session.mPendingTrackballWindow;
5581 session.mPendingTrackballMove = null;
5582 session.mPendingTrackballWindow = null;
5583 }
Romain Guy06882f82009-06-10 13:36:04 -07005584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 if (mLastBinder == client.asBinder()) {
5586 if (DEBUG_INPUT) Log.v(
5587 TAG, "finishedKey: last paused="
5588 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5589 if (mLastWin != null && (!mLastWin.mToken.paused || force
5590 || !mEventDispatching)) {
5591 doFinishedKeyLocked(false);
5592 } else {
5593 // Make sure to wake up anyone currently waiting to
5594 // dispatch a key, so they can re-evaluate their
5595 // current situation.
5596 mFinished = true;
5597 notifyAll();
5598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 }
Romain Guy06882f82009-06-10 13:36:04 -07005600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005602 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 if (DEBUG_INPUT) Log.v(TAG,
5604 "Returning pending motion: " + res);
5605 mQueue.recycleEvent(qev);
5606 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5607 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005611
5612 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5613 synchronized (mWindowMap) {
5614 if (mWallpaperTarget == win) {
5615 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5616 }
5617 }
5618 }
5619
5620 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 }
5622
5623 void tickle() {
5624 synchronized (this) {
5625 notifyAll();
5626 }
5627 }
Romain Guy06882f82009-06-10 13:36:04 -07005628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 void handleNewWindowLocked(WindowState newWindow) {
5630 if (!newWindow.canReceiveKeys()) {
5631 return;
5632 }
5633 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005634 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 TAG, "New key dispatch window: win="
5636 + newWindow.mClient.asBinder()
5637 + ", last=" + mLastBinder
5638 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5639 + "), finished=" + mFinished + ", paused="
5640 + newWindow.mToken.paused);
5641
5642 // Displaying a window implicitly causes dispatching to
5643 // be unpaused. (This is to protect against bugs if someone
5644 // pauses dispatching but forgets to resume.)
5645 newWindow.mToken.paused = false;
5646
5647 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648
5649 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5650 if (DEBUG_INPUT) Log.v(TAG,
5651 "New SYSTEM_ERROR window; resetting state");
5652 mLastWin = null;
5653 mLastBinder = null;
5654 mMotionTarget = null;
5655 mFinished = true;
5656 } else if (mLastWin != null) {
5657 // If the new window is above the window we are
5658 // waiting on, then stop waiting and let key dispatching
5659 // start on the new guy.
5660 if (DEBUG_INPUT) Log.v(
5661 TAG, "Last win layer=" + mLastWin.mLayer
5662 + ", new win layer=" + newWindow.mLayer);
5663 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005664 // The new window is above the old; finish pending input to the last
5665 // window and start directing it to the new one.
5666 mLastWin.mToken.paused = false;
5667 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005669 // Either the new window is lower, so there is no need to wake key waiters,
5670 // or we just finished key input to the previous window, which implicitly
5671 // notified the key waiters. In both cases, we don't need to issue the
5672 // notification here.
5673 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 }
5675
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005676 // Now that we've put a new window state in place, make the event waiter
5677 // take notice and retarget its attentions.
5678 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005679 }
5680 }
5681
5682 void pauseDispatchingLocked(WindowToken token) {
5683 synchronized (this)
5684 {
5685 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5686 token.paused = true;
5687
5688 /*
5689 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5690 mPaused = true;
5691 } else {
5692 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005693 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005695 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005697 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005698 }
5699 }
5700 */
5701 }
5702 }
5703
5704 void resumeDispatchingLocked(WindowToken token) {
5705 synchronized (this) {
5706 if (token.paused) {
5707 if (DEBUG_INPUT) Log.v(
5708 TAG, "Resuming WindowToken " + token
5709 + ", last=" + mLastBinder
5710 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5711 + "), finished=" + mFinished + ", paused="
5712 + token.paused);
5713 token.paused = false;
5714 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5715 doFinishedKeyLocked(true);
5716 } else {
5717 notifyAll();
5718 }
5719 }
5720 }
5721 }
5722
5723 void setEventDispatchingLocked(boolean enabled) {
5724 synchronized (this) {
5725 mEventDispatching = enabled;
5726 notifyAll();
5727 }
5728 }
Romain Guy06882f82009-06-10 13:36:04 -07005729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 void appSwitchComing() {
5731 synchronized (this) {
5732 // Don't wait for more than .5 seconds for app to finish
5733 // processing the pending events.
5734 long now = SystemClock.uptimeMillis() + 500;
5735 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5736 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5737 mTimeToSwitch = now;
5738 }
5739 notifyAll();
5740 }
5741 }
Romain Guy06882f82009-06-10 13:36:04 -07005742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 private final void doFinishedKeyLocked(boolean doRecycle) {
5744 if (mLastWin != null) {
5745 releasePendingPointerLocked(mLastWin.mSession);
5746 releasePendingTrackballLocked(mLastWin.mSession);
5747 }
Romain Guy06882f82009-06-10 13:36:04 -07005748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 if (mLastWin == null || !mLastWin.mToken.paused
5750 || !mLastWin.isVisibleLw()) {
5751 // If the current window has been paused, we aren't -really-
5752 // finished... so let the waiters still wait.
5753 mLastWin = null;
5754 mLastBinder = null;
5755 }
5756 mFinished = true;
5757 notifyAll();
5758 }
5759 }
5760
5761 private class KeyQ extends KeyInputQueue
5762 implements KeyInputQueue.FilterCallback {
5763 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005766 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5768 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5769 "KEEP_SCREEN_ON_FLAG");
5770 mHoldingScreen.setReferenceCounted(false);
5771 }
5772
5773 @Override
5774 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5775 if (mPolicy.preprocessInputEventTq(event)) {
5776 return true;
5777 }
Romain Guy06882f82009-06-10 13:36:04 -07005778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 switch (event.type) {
5780 case RawInputEvent.EV_KEY: {
5781 // XXX begin hack
5782 if (DEBUG) {
5783 if (event.keycode == KeyEvent.KEYCODE_G) {
5784 if (event.value != 0) {
5785 // G down
5786 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5787 }
5788 return false;
5789 }
5790 if (event.keycode == KeyEvent.KEYCODE_D) {
5791 if (event.value != 0) {
5792 //dump();
5793 }
5794 return false;
5795 }
5796 }
5797 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 boolean screenIsOff = !mPowerManager.screenIsOn();
5800 boolean screenIsDim = !mPowerManager.screenIsBright();
5801 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5804 mPowerManager.goToSleep(event.when);
5805 }
5806
5807 if (screenIsOff) {
5808 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5809 }
5810 if (screenIsDim) {
5811 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5812 }
5813 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5814 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005815 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 }
Romain Guy06882f82009-06-10 13:36:04 -07005817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5819 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5820 filterQueue(this);
5821 mKeyWaiter.appSwitchComing();
5822 }
5823 return true;
5824 } else {
5825 return false;
5826 }
5827 }
Romain Guy06882f82009-06-10 13:36:04 -07005828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 case RawInputEvent.EV_REL: {
5830 boolean screenIsOff = !mPowerManager.screenIsOn();
5831 boolean screenIsDim = !mPowerManager.screenIsBright();
5832 if (screenIsOff) {
5833 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5834 device.classes, event)) {
5835 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5836 return false;
5837 }
5838 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5839 }
5840 if (screenIsDim) {
5841 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5842 }
5843 return true;
5844 }
Romain Guy06882f82009-06-10 13:36:04 -07005845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 case RawInputEvent.EV_ABS: {
5847 boolean screenIsOff = !mPowerManager.screenIsOn();
5848 boolean screenIsDim = !mPowerManager.screenIsBright();
5849 if (screenIsOff) {
5850 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5851 device.classes, event)) {
5852 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5853 return false;
5854 }
5855 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5856 }
5857 if (screenIsDim) {
5858 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5859 }
5860 return true;
5861 }
Romain Guy06882f82009-06-10 13:36:04 -07005862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 default:
5864 return true;
5865 }
5866 }
5867
5868 public int filterEvent(QueuedEvent ev) {
5869 switch (ev.classType) {
5870 case RawInputEvent.CLASS_KEYBOARD:
5871 KeyEvent ke = (KeyEvent)ev.event;
5872 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5873 Log.w(TAG, "Dropping movement key during app switch: "
5874 + ke.getKeyCode() + ", action=" + ke.getAction());
5875 return FILTER_REMOVE;
5876 }
5877 return FILTER_ABORT;
5878 default:
5879 return FILTER_KEEP;
5880 }
5881 }
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 /**
5884 * Must be called with the main window manager lock held.
5885 */
5886 void setHoldScreenLocked(boolean holding) {
5887 boolean state = mHoldingScreen.isHeld();
5888 if (holding != state) {
5889 if (holding) {
5890 mHoldingScreen.acquire();
5891 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005892 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 mHoldingScreen.release();
5894 }
5895 }
5896 }
Michael Chan53071d62009-05-13 17:29:48 -07005897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898
5899 public boolean detectSafeMode() {
5900 mSafeMode = mPolicy.detectSafeMode();
5901 return mSafeMode;
5902 }
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 public void systemReady() {
5905 mPolicy.systemReady();
5906 }
Romain Guy06882f82009-06-10 13:36:04 -07005907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 private final class InputDispatcherThread extends Thread {
5909 // Time to wait when there is nothing to do: 9999 seconds.
5910 static final int LONG_WAIT=9999*1000;
5911
5912 public InputDispatcherThread() {
5913 super("InputDispatcher");
5914 }
Romain Guy06882f82009-06-10 13:36:04 -07005915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 @Override
5917 public void run() {
5918 while (true) {
5919 try {
5920 process();
5921 } catch (Exception e) {
5922 Log.e(TAG, "Exception in input dispatcher", e);
5923 }
5924 }
5925 }
Romain Guy06882f82009-06-10 13:36:04 -07005926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 private void process() {
5928 android.os.Process.setThreadPriority(
5929 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 // The last key event we saw
5932 KeyEvent lastKey = null;
5933
5934 // Last keydown time for auto-repeating keys
5935 long lastKeyTime = SystemClock.uptimeMillis();
5936 long nextKeyTime = lastKeyTime+LONG_WAIT;
5937
Romain Guy06882f82009-06-10 13:36:04 -07005938 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 int keyRepeatCount = 0;
5940
5941 // Need to report that configuration has changed?
5942 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 while (true) {
5945 long curTime = SystemClock.uptimeMillis();
5946
5947 if (DEBUG_INPUT) Log.v(
5948 TAG, "Waiting for next key: now=" + curTime
5949 + ", repeat @ " + nextKeyTime);
5950
5951 // Retrieve next event, waiting only as long as the next
5952 // repeat timeout. If the configuration has changed, then
5953 // don't wait at all -- we'll report the change as soon as
5954 // we have processed all events.
5955 QueuedEvent ev = mQueue.getEvent(
5956 (int)((!configChanged && curTime < nextKeyTime)
5957 ? (nextKeyTime-curTime) : 0));
5958
5959 if (DEBUG_INPUT && ev != null) Log.v(
5960 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5961
Michael Chan53071d62009-05-13 17:29:48 -07005962 if (MEASURE_LATENCY) {
5963 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5964 }
5965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 try {
5967 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005968 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 int eventType;
5970 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5971 eventType = eventType((MotionEvent)ev.event);
5972 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5973 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5974 eventType = LocalPowerManager.BUTTON_EVENT;
5975 } else {
5976 eventType = LocalPowerManager.OTHER_EVENT;
5977 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005978 try {
Michael Chan53071d62009-05-13 17:29:48 -07005979 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005980 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005981 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005982 mBatteryStats.noteInputEvent();
5983 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005984 } catch (RemoteException e) {
5985 // Ignore
5986 }
Michael Chane10de972009-05-18 11:24:50 -07005987
5988 if (eventType != TOUCH_EVENT
5989 && eventType != LONG_TOUCH_EVENT
5990 && eventType != CHEEK_EVENT) {
5991 mPowerManager.userActivity(curTime, false,
5992 eventType, false);
5993 } else if (mLastTouchEventType != eventType
5994 || (curTime - mLastUserActivityCallTime)
5995 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5996 mLastUserActivityCallTime = curTime;
5997 mLastTouchEventType = eventType;
5998 mPowerManager.userActivity(curTime, false,
5999 eventType, false);
6000 }
6001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 switch (ev.classType) {
6003 case RawInputEvent.CLASS_KEYBOARD:
6004 KeyEvent ke = (KeyEvent)ev.event;
6005 if (ke.isDown()) {
6006 lastKey = ke;
6007 keyRepeatCount = 0;
6008 lastKeyTime = curTime;
6009 nextKeyTime = lastKeyTime
6010 + KEY_REPEAT_FIRST_DELAY;
6011 if (DEBUG_INPUT) Log.v(
6012 TAG, "Received key down: first repeat @ "
6013 + nextKeyTime);
6014 } else {
6015 lastKey = null;
6016 // Arbitrary long timeout.
6017 lastKeyTime = curTime;
6018 nextKeyTime = curTime + LONG_WAIT;
6019 if (DEBUG_INPUT) Log.v(
6020 TAG, "Received key up: ignore repeat @ "
6021 + nextKeyTime);
6022 }
6023 dispatchKey((KeyEvent)ev.event, 0, 0);
6024 mQueue.recycleEvent(ev);
6025 break;
6026 case RawInputEvent.CLASS_TOUCHSCREEN:
6027 //Log.i(TAG, "Read next event " + ev);
6028 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6029 break;
6030 case RawInputEvent.CLASS_TRACKBALL:
6031 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6032 break;
6033 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6034 configChanged = true;
6035 break;
6036 default:
6037 mQueue.recycleEvent(ev);
6038 break;
6039 }
Romain Guy06882f82009-06-10 13:36:04 -07006040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 } else if (configChanged) {
6042 configChanged = false;
6043 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006045 } else if (lastKey != null) {
6046 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 // Timeout occurred while key was down. If it is at or
6049 // past the key repeat time, dispatch the repeat.
6050 if (DEBUG_INPUT) Log.v(
6051 TAG, "Key timeout: repeat=" + nextKeyTime
6052 + ", now=" + curTime);
6053 if (curTime < nextKeyTime) {
6054 continue;
6055 }
Romain Guy06882f82009-06-10 13:36:04 -07006056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 lastKeyTime = nextKeyTime;
6058 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6059 keyRepeatCount++;
6060 if (DEBUG_INPUT) Log.v(
6061 TAG, "Key repeat: count=" + keyRepeatCount
6062 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006063 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 } else {
6066 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 lastKeyTime = curTime;
6069 nextKeyTime = curTime + LONG_WAIT;
6070 }
Romain Guy06882f82009-06-10 13:36:04 -07006071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 } catch (Exception e) {
6073 Log.e(TAG,
6074 "Input thread received uncaught exception: " + e, e);
6075 }
6076 }
6077 }
6078 }
6079
6080 // -------------------------------------------------------------
6081 // Client Session State
6082 // -------------------------------------------------------------
6083
6084 private final class Session extends IWindowSession.Stub
6085 implements IBinder.DeathRecipient {
6086 final IInputMethodClient mClient;
6087 final IInputContext mInputContext;
6088 final int mUid;
6089 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006090 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 SurfaceSession mSurfaceSession;
6092 int mNumWindow = 0;
6093 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 /**
6096 * Current pointer move event being dispatched to client window... must
6097 * hold key lock to access.
6098 */
6099 QueuedEvent mPendingPointerMove;
6100 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 /**
6103 * Current trackball move event being dispatched to client window... must
6104 * hold key lock to access.
6105 */
6106 QueuedEvent mPendingTrackballMove;
6107 WindowState mPendingTrackballWindow;
6108
6109 public Session(IInputMethodClient client, IInputContext inputContext) {
6110 mClient = client;
6111 mInputContext = inputContext;
6112 mUid = Binder.getCallingUid();
6113 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006114 StringBuilder sb = new StringBuilder();
6115 sb.append("Session{");
6116 sb.append(Integer.toHexString(System.identityHashCode(this)));
6117 sb.append(" uid ");
6118 sb.append(mUid);
6119 sb.append("}");
6120 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 synchronized (mWindowMap) {
6123 if (mInputMethodManager == null && mHaveInputMethods) {
6124 IBinder b = ServiceManager.getService(
6125 Context.INPUT_METHOD_SERVICE);
6126 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6127 }
6128 }
6129 long ident = Binder.clearCallingIdentity();
6130 try {
6131 // Note: it is safe to call in to the input method manager
6132 // here because we are not holding our lock.
6133 if (mInputMethodManager != null) {
6134 mInputMethodManager.addClient(client, inputContext,
6135 mUid, mPid);
6136 } else {
6137 client.setUsingInputMethod(false);
6138 }
6139 client.asBinder().linkToDeath(this, 0);
6140 } catch (RemoteException e) {
6141 // The caller has died, so we can just forget about this.
6142 try {
6143 if (mInputMethodManager != null) {
6144 mInputMethodManager.removeClient(client);
6145 }
6146 } catch (RemoteException ee) {
6147 }
6148 } finally {
6149 Binder.restoreCallingIdentity(ident);
6150 }
6151 }
Romain Guy06882f82009-06-10 13:36:04 -07006152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 @Override
6154 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6155 throws RemoteException {
6156 try {
6157 return super.onTransact(code, data, reply, flags);
6158 } catch (RuntimeException e) {
6159 // Log all 'real' exceptions thrown to the caller
6160 if (!(e instanceof SecurityException)) {
6161 Log.e(TAG, "Window Session Crash", e);
6162 }
6163 throw e;
6164 }
6165 }
6166
6167 public void binderDied() {
6168 // Note: it is safe to call in to the input method manager
6169 // here because we are not holding our lock.
6170 try {
6171 if (mInputMethodManager != null) {
6172 mInputMethodManager.removeClient(mClient);
6173 }
6174 } catch (RemoteException e) {
6175 }
6176 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006177 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 mClientDead = true;
6179 killSessionLocked();
6180 }
6181 }
6182
6183 public int add(IWindow window, WindowManager.LayoutParams attrs,
6184 int viewVisibility, Rect outContentInsets) {
6185 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6186 }
Romain Guy06882f82009-06-10 13:36:04 -07006187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 public void remove(IWindow window) {
6189 removeWindow(this, window);
6190 }
Romain Guy06882f82009-06-10 13:36:04 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6193 int requestedWidth, int requestedHeight, int viewFlags,
6194 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6195 Rect outVisibleInsets, Surface outSurface) {
6196 return relayoutWindow(this, window, attrs,
6197 requestedWidth, requestedHeight, viewFlags, insetsPending,
6198 outFrame, outContentInsets, outVisibleInsets, outSurface);
6199 }
Romain Guy06882f82009-06-10 13:36:04 -07006200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 public void setTransparentRegion(IWindow window, Region region) {
6202 setTransparentRegionWindow(this, window, region);
6203 }
Romain Guy06882f82009-06-10 13:36:04 -07006204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 public void setInsets(IWindow window, int touchableInsets,
6206 Rect contentInsets, Rect visibleInsets) {
6207 setInsetsWindow(this, window, touchableInsets, contentInsets,
6208 visibleInsets);
6209 }
Romain Guy06882f82009-06-10 13:36:04 -07006210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6212 getWindowDisplayFrame(this, window, outDisplayFrame);
6213 }
Romain Guy06882f82009-06-10 13:36:04 -07006214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 public void finishDrawing(IWindow window) {
6216 if (localLOGV) Log.v(
6217 TAG, "IWindow finishDrawing called for " + window);
6218 finishDrawingWindow(this, window);
6219 }
6220
6221 public void finishKey(IWindow window) {
6222 if (localLOGV) Log.v(
6223 TAG, "IWindow finishKey called for " + window);
6224 mKeyWaiter.finishedKey(this, window, false,
6225 KeyWaiter.RETURN_NOTHING);
6226 }
6227
6228 public MotionEvent getPendingPointerMove(IWindow window) {
6229 if (localLOGV) Log.v(
6230 TAG, "IWindow getPendingMotionEvent called for " + window);
6231 return mKeyWaiter.finishedKey(this, window, false,
6232 KeyWaiter.RETURN_PENDING_POINTER);
6233 }
Romain Guy06882f82009-06-10 13:36:04 -07006234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 public MotionEvent getPendingTrackballMove(IWindow window) {
6236 if (localLOGV) Log.v(
6237 TAG, "IWindow getPendingMotionEvent called for " + window);
6238 return mKeyWaiter.finishedKey(this, window, false,
6239 KeyWaiter.RETURN_PENDING_TRACKBALL);
6240 }
6241
6242 public void setInTouchMode(boolean mode) {
6243 synchronized(mWindowMap) {
6244 mInTouchMode = mode;
6245 }
6246 }
6247
6248 public boolean getInTouchMode() {
6249 synchronized(mWindowMap) {
6250 return mInTouchMode;
6251 }
6252 }
6253
6254 public boolean performHapticFeedback(IWindow window, int effectId,
6255 boolean always) {
6256 synchronized(mWindowMap) {
6257 long ident = Binder.clearCallingIdentity();
6258 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006259 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 windowForClientLocked(this, window), effectId, always);
6261 } finally {
6262 Binder.restoreCallingIdentity(ident);
6263 }
6264 }
6265 }
Romain Guy06882f82009-06-10 13:36:04 -07006266
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006267 public void setWallpaperPosition(IBinder window, float x, float y) {
6268 synchronized(mWindowMap) {
6269 long ident = Binder.clearCallingIdentity();
6270 try {
6271 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6272 x, y);
6273 } finally {
6274 Binder.restoreCallingIdentity(ident);
6275 }
6276 }
6277 }
6278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 void windowAddedLocked() {
6280 if (mSurfaceSession == null) {
6281 if (localLOGV) Log.v(
6282 TAG, "First window added to " + this + ", creating SurfaceSession");
6283 mSurfaceSession = new SurfaceSession();
6284 mSessions.add(this);
6285 }
6286 mNumWindow++;
6287 }
6288
6289 void windowRemovedLocked() {
6290 mNumWindow--;
6291 killSessionLocked();
6292 }
Romain Guy06882f82009-06-10 13:36:04 -07006293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006294 void killSessionLocked() {
6295 if (mNumWindow <= 0 && mClientDead) {
6296 mSessions.remove(this);
6297 if (mSurfaceSession != null) {
6298 if (localLOGV) Log.v(
6299 TAG, "Last window removed from " + this
6300 + ", destroying " + mSurfaceSession);
6301 try {
6302 mSurfaceSession.kill();
6303 } catch (Exception e) {
6304 Log.w(TAG, "Exception thrown when killing surface session "
6305 + mSurfaceSession + " in session " + this
6306 + ": " + e.toString());
6307 }
6308 mSurfaceSession = null;
6309 }
6310 }
6311 }
Romain Guy06882f82009-06-10 13:36:04 -07006312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006314 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6315 pw.print(" mClientDead="); pw.print(mClientDead);
6316 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6317 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6318 pw.print(prefix);
6319 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6320 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6321 }
6322 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6323 pw.print(prefix);
6324 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6325 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 }
6328
6329 @Override
6330 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006331 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 }
6333 }
6334
6335 // -------------------------------------------------------------
6336 // Client Window State
6337 // -------------------------------------------------------------
6338
6339 private final class WindowState implements WindowManagerPolicy.WindowState {
6340 final Session mSession;
6341 final IWindow mClient;
6342 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006343 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 AppWindowToken mAppToken;
6345 AppWindowToken mTargetAppToken;
6346 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6347 final DeathRecipient mDeathRecipient;
6348 final WindowState mAttachedWindow;
6349 final ArrayList mChildWindows = new ArrayList();
6350 final int mBaseLayer;
6351 final int mSubLayer;
6352 final boolean mLayoutAttached;
6353 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006354 final boolean mIsWallpaper;
6355 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 int mViewVisibility;
6357 boolean mPolicyVisibility = true;
6358 boolean mPolicyVisibilityAfterAnim = true;
6359 boolean mAppFreezing;
6360 Surface mSurface;
6361 boolean mAttachedHidden; // is our parent window hidden?
6362 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006363 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364 int mRequestedWidth;
6365 int mRequestedHeight;
6366 int mLastRequestedWidth;
6367 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 int mLayer;
6369 int mAnimLayer;
6370 int mLastLayer;
6371 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006372 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373
6374 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 // Actual frame shown on-screen (may be modified by animation)
6377 final Rect mShownFrame = new Rect();
6378 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 /**
6381 * Insets that determine the actually visible area
6382 */
6383 final Rect mVisibleInsets = new Rect();
6384 final Rect mLastVisibleInsets = new Rect();
6385 boolean mVisibleInsetsChanged;
6386
6387 /**
6388 * Insets that are covered by system windows
6389 */
6390 final Rect mContentInsets = new Rect();
6391 final Rect mLastContentInsets = new Rect();
6392 boolean mContentInsetsChanged;
6393
6394 /**
6395 * Set to true if we are waiting for this window to receive its
6396 * given internal insets before laying out other windows based on it.
6397 */
6398 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 /**
6401 * These are the content insets that were given during layout for
6402 * this window, to be applied to windows behind it.
6403 */
6404 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 /**
6407 * These are the visible insets that were given during layout for
6408 * this window, to be applied to windows behind it.
6409 */
6410 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 /**
6413 * Flag indicating whether the touchable region should be adjusted by
6414 * the visible insets; if false the area outside the visible insets is
6415 * NOT touchable, so we must use those to adjust the frame during hit
6416 * tests.
6417 */
6418 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 // Current transformation being applied.
6421 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6422 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6423 float mHScale=1, mVScale=1;
6424 float mLastHScale=1, mLastVScale=1;
6425 final Matrix mTmpMatrix = new Matrix();
6426
6427 // "Real" frame that the application sees.
6428 final Rect mFrame = new Rect();
6429 final Rect mLastFrame = new Rect();
6430
6431 final Rect mContainingFrame = new Rect();
6432 final Rect mDisplayFrame = new Rect();
6433 final Rect mContentFrame = new Rect();
6434 final Rect mVisibleFrame = new Rect();
6435
6436 float mShownAlpha = 1;
6437 float mAlpha = 1;
6438 float mLastAlpha = 1;
6439
6440 // Set to true if, when the window gets displayed, it should perform
6441 // an enter animation.
6442 boolean mEnterAnimationPending;
6443
6444 // Currently running animation.
6445 boolean mAnimating;
6446 boolean mLocalAnimating;
6447 Animation mAnimation;
6448 boolean mAnimationIsEntrance;
6449 boolean mHasTransformation;
6450 boolean mHasLocalTransformation;
6451 final Transformation mTransformation = new Transformation();
6452
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006453 // If a window showing a wallpaper: the requested offset for the
6454 // wallpaper; if a wallpaper window: the currently applied offset.
6455 float mWallpaperX = -1;
6456 float mWallpaperY = -1;
6457
6458 // Wallpaper windows: pixels offset based on above variables.
6459 int mXOffset;
6460 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // This is set after IWindowSession.relayout() has been called at
6463 // least once for the window. It allows us to detect the situation
6464 // where we don't yet have a surface, but should have one soon, so
6465 // we can give the window focus before waiting for the relayout.
6466 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 // This is set after the Surface has been created but before the
6469 // window has been drawn. During this time the surface is hidden.
6470 boolean mDrawPending;
6471
6472 // This is set after the window has finished drawing for the first
6473 // time but before its surface is shown. The surface will be
6474 // displayed when the next layout is run.
6475 boolean mCommitDrawPending;
6476
6477 // This is set during the time after the window's drawing has been
6478 // committed, and before its surface is actually shown. It is used
6479 // to delay showing the surface until all windows in a token are ready
6480 // to be shown.
6481 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 // Set when the window has been shown in the screen the first time.
6484 boolean mHasDrawn;
6485
6486 // Currently running an exit animation?
6487 boolean mExiting;
6488
6489 // Currently on the mDestroySurface list?
6490 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 // Completely remove from window manager after exit animation?
6493 boolean mRemoveOnExit;
6494
6495 // Set when the orientation is changing and this window has not yet
6496 // been updated for the new orientation.
6497 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 // Is this window now (or just being) removed?
6500 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 WindowState(Session s, IWindow c, WindowToken token,
6503 WindowState attachedWindow, WindowManager.LayoutParams a,
6504 int viewVisibility) {
6505 mSession = s;
6506 mClient = c;
6507 mToken = token;
6508 mAttrs.copyFrom(a);
6509 mViewVisibility = viewVisibility;
6510 DeathRecipient deathRecipient = new DeathRecipient();
6511 mAlpha = a.alpha;
6512 if (localLOGV) Log.v(
6513 TAG, "Window " + this + " client=" + c.asBinder()
6514 + " token=" + token + " (" + mAttrs.token + ")");
6515 try {
6516 c.asBinder().linkToDeath(deathRecipient, 0);
6517 } catch (RemoteException e) {
6518 mDeathRecipient = null;
6519 mAttachedWindow = null;
6520 mLayoutAttached = false;
6521 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006522 mIsWallpaper = false;
6523 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 mBaseLayer = 0;
6525 mSubLayer = 0;
6526 return;
6527 }
6528 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6531 mAttrs.type <= LAST_SUB_WINDOW)) {
6532 // The multiplier here is to reserve space for multiple
6533 // windows in the same type layer.
6534 mBaseLayer = mPolicy.windowTypeToLayerLw(
6535 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6536 + TYPE_LAYER_OFFSET;
6537 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6538 mAttachedWindow = attachedWindow;
6539 mAttachedWindow.mChildWindows.add(this);
6540 mLayoutAttached = mAttrs.type !=
6541 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6542 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6543 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006544 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6545 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 } else {
6547 // The multiplier here is to reserve space for multiple
6548 // windows in the same type layer.
6549 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6550 * TYPE_LAYER_MULTIPLIER
6551 + TYPE_LAYER_OFFSET;
6552 mSubLayer = 0;
6553 mAttachedWindow = null;
6554 mLayoutAttached = false;
6555 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6556 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006557 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6558 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 }
6560
6561 WindowState appWin = this;
6562 while (appWin.mAttachedWindow != null) {
6563 appWin = mAttachedWindow;
6564 }
6565 WindowToken appToken = appWin.mToken;
6566 while (appToken.appWindowToken == null) {
6567 WindowToken parent = mTokenMap.get(appToken.token);
6568 if (parent == null || appToken == parent) {
6569 break;
6570 }
6571 appToken = parent;
6572 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006573 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 mAppToken = appToken.appWindowToken;
6575
6576 mSurface = null;
6577 mRequestedWidth = 0;
6578 mRequestedHeight = 0;
6579 mLastRequestedWidth = 0;
6580 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006581 mXOffset = 0;
6582 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 mLayer = 0;
6584 mAnimLayer = 0;
6585 mLastLayer = 0;
6586 }
6587
6588 void attach() {
6589 if (localLOGV) Log.v(
6590 TAG, "Attaching " + this + " token=" + mToken
6591 + ", list=" + mToken.windows);
6592 mSession.windowAddedLocked();
6593 }
6594
6595 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6596 mHaveFrame = true;
6597
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006598 final Rect container = mContainingFrame;
6599 container.set(pf);
6600
6601 final Rect display = mDisplayFrame;
6602 display.set(df);
6603
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006604 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006605 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006606 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6607 display.intersect(mCompatibleScreenFrame);
6608 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006609 }
6610
6611 final int pw = container.right - container.left;
6612 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613
6614 int w,h;
6615 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6616 w = mAttrs.width < 0 ? pw : mAttrs.width;
6617 h = mAttrs.height< 0 ? ph : mAttrs.height;
6618 } else {
6619 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6620 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6621 }
Romain Guy06882f82009-06-10 13:36:04 -07006622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 final Rect content = mContentFrame;
6624 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 final Rect visible = mVisibleFrame;
6627 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006630 final int fw = frame.width();
6631 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6634 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6635
6636 Gravity.apply(mAttrs.gravity, w, h, container,
6637 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6638 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6639
6640 //System.out.println("Out: " + mFrame);
6641
6642 // Now make sure the window fits in the overall display.
6643 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 // Make sure the content and visible frames are inside of the
6646 // final window frame.
6647 if (content.left < frame.left) content.left = frame.left;
6648 if (content.top < frame.top) content.top = frame.top;
6649 if (content.right > frame.right) content.right = frame.right;
6650 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6651 if (visible.left < frame.left) visible.left = frame.left;
6652 if (visible.top < frame.top) visible.top = frame.top;
6653 if (visible.right > frame.right) visible.right = frame.right;
6654 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 final Rect contentInsets = mContentInsets;
6657 contentInsets.left = content.left-frame.left;
6658 contentInsets.top = content.top-frame.top;
6659 contentInsets.right = frame.right-content.right;
6660 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006662 final Rect visibleInsets = mVisibleInsets;
6663 visibleInsets.left = visible.left-frame.left;
6664 visibleInsets.top = visible.top-frame.top;
6665 visibleInsets.right = frame.right-visible.right;
6666 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006667
Dianne Hackborn284ac932009-08-28 10:34:25 -07006668 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6669 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
6670 mDisplay.getHeight());
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006671 }
6672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 if (localLOGV) {
6674 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6675 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6676 Log.v(TAG, "Resolving (mRequestedWidth="
6677 + mRequestedWidth + ", mRequestedheight="
6678 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6679 + "): frame=" + mFrame.toShortString()
6680 + " ci=" + contentInsets.toShortString()
6681 + " vi=" + visibleInsets.toShortString());
6682 //}
6683 }
6684 }
Romain Guy06882f82009-06-10 13:36:04 -07006685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 public Rect getFrameLw() {
6687 return mFrame;
6688 }
6689
6690 public Rect getShownFrameLw() {
6691 return mShownFrame;
6692 }
6693
6694 public Rect getDisplayFrameLw() {
6695 return mDisplayFrame;
6696 }
6697
6698 public Rect getContentFrameLw() {
6699 return mContentFrame;
6700 }
6701
6702 public Rect getVisibleFrameLw() {
6703 return mVisibleFrame;
6704 }
6705
6706 public boolean getGivenInsetsPendingLw() {
6707 return mGivenInsetsPending;
6708 }
6709
6710 public Rect getGivenContentInsetsLw() {
6711 return mGivenContentInsets;
6712 }
Romain Guy06882f82009-06-10 13:36:04 -07006713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006714 public Rect getGivenVisibleInsetsLw() {
6715 return mGivenVisibleInsets;
6716 }
Romain Guy06882f82009-06-10 13:36:04 -07006717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 public WindowManager.LayoutParams getAttrs() {
6719 return mAttrs;
6720 }
6721
6722 public int getSurfaceLayer() {
6723 return mLayer;
6724 }
Romain Guy06882f82009-06-10 13:36:04 -07006725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006726 public IApplicationToken getAppToken() {
6727 return mAppToken != null ? mAppToken.appToken : null;
6728 }
6729
6730 public boolean hasAppShownWindows() {
6731 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6732 }
6733
6734 public boolean hasAppStartingIcon() {
6735 return mAppToken != null ? (mAppToken.startingData != null) : false;
6736 }
6737
6738 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6739 return mAppToken != null ? mAppToken.startingWindow : null;
6740 }
6741
6742 public void setAnimation(Animation anim) {
6743 if (localLOGV) Log.v(
6744 TAG, "Setting animation in " + this + ": " + anim);
6745 mAnimating = false;
6746 mLocalAnimating = false;
6747 mAnimation = anim;
6748 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6749 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6750 }
6751
6752 public void clearAnimation() {
6753 if (mAnimation != null) {
6754 mAnimating = true;
6755 mLocalAnimating = false;
6756 mAnimation = null;
6757 }
6758 }
Romain Guy06882f82009-06-10 13:36:04 -07006759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760 Surface createSurfaceLocked() {
6761 if (mSurface == null) {
6762 mDrawPending = true;
6763 mCommitDrawPending = false;
6764 mReadyToShow = false;
6765 if (mAppToken != null) {
6766 mAppToken.allDrawn = false;
6767 }
6768
6769 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006770 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 flags |= Surface.PUSH_BUFFERS;
6772 }
6773
6774 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6775 flags |= Surface.SECURE;
6776 }
6777 if (DEBUG_VISIBILITY) Log.v(
6778 TAG, "Creating surface in session "
6779 + mSession.mSurfaceSession + " window " + this
6780 + " w=" + mFrame.width()
6781 + " h=" + mFrame.height() + " format="
6782 + mAttrs.format + " flags=" + flags);
6783
6784 int w = mFrame.width();
6785 int h = mFrame.height();
6786 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6787 // for a scaled surface, we always want the requested
6788 // size.
6789 w = mRequestedWidth;
6790 h = mRequestedHeight;
6791 }
6792
6793 try {
6794 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006795 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 0, w, h, mAttrs.format, flags);
6797 } catch (Surface.OutOfResourcesException e) {
6798 Log.w(TAG, "OutOfResourcesException creating surface");
6799 reclaimSomeSurfaceMemoryLocked(this, "create");
6800 return null;
6801 } catch (Exception e) {
6802 Log.e(TAG, "Exception creating surface", e);
6803 return null;
6804 }
Romain Guy06882f82009-06-10 13:36:04 -07006805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 if (localLOGV) Log.v(
6807 TAG, "Got surface: " + mSurface
6808 + ", set left=" + mFrame.left + " top=" + mFrame.top
6809 + ", animLayer=" + mAnimLayer);
6810 if (SHOW_TRANSACTIONS) {
6811 Log.i(TAG, ">>> OPEN TRANSACTION");
6812 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6813 + mAttrs.getTitle() + ") pos=(" +
6814 mFrame.left + "," + mFrame.top + ") (" +
6815 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6816 mAnimLayer + " HIDE");
6817 }
6818 Surface.openTransaction();
6819 try {
6820 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006821 mSurface.setPosition(mFrame.left + mXOffset,
6822 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 mSurface.setLayer(mAnimLayer);
6824 mSurface.hide();
6825 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6826 mSurface.setFlags(Surface.SURFACE_DITHER,
6827 Surface.SURFACE_DITHER);
6828 }
6829 } catch (RuntimeException e) {
6830 Log.w(TAG, "Error creating surface in " + w, e);
6831 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6832 }
6833 mLastHidden = true;
6834 } finally {
6835 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6836 Surface.closeTransaction();
6837 }
6838 if (localLOGV) Log.v(
6839 TAG, "Created surface " + this);
6840 }
6841 return mSurface;
6842 }
Romain Guy06882f82009-06-10 13:36:04 -07006843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 void destroySurfaceLocked() {
6845 // Window is no longer on-screen, so can no longer receive
6846 // key events... if we were waiting for it to finish
6847 // handling a key event, the wait is over!
6848 mKeyWaiter.finishedKey(mSession, mClient, true,
6849 KeyWaiter.RETURN_NOTHING);
6850 mKeyWaiter.releasePendingPointerLocked(mSession);
6851 mKeyWaiter.releasePendingTrackballLocked(mSession);
6852
6853 if (mAppToken != null && this == mAppToken.startingWindow) {
6854 mAppToken.startingDisplayed = false;
6855 }
Romain Guy06882f82009-06-10 13:36:04 -07006856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 if (mSurface != null) {
6858 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006859 if (DEBUG_VISIBILITY) {
6860 RuntimeException e = new RuntimeException();
6861 e.fillInStackTrace();
6862 Log.w(TAG, "Window " + this + " destroying surface "
6863 + mSurface + ", session " + mSession, e);
6864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 if (SHOW_TRANSACTIONS) {
6866 RuntimeException ex = new RuntimeException();
6867 ex.fillInStackTrace();
6868 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6869 + mAttrs.getTitle() + ")", ex);
6870 }
6871 mSurface.clear();
6872 } catch (RuntimeException e) {
6873 Log.w(TAG, "Exception thrown when destroying Window " + this
6874 + " surface " + mSurface + " session " + mSession
6875 + ": " + e.toString());
6876 }
6877 mSurface = null;
6878 mDrawPending = false;
6879 mCommitDrawPending = false;
6880 mReadyToShow = false;
6881
6882 int i = mChildWindows.size();
6883 while (i > 0) {
6884 i--;
6885 WindowState c = (WindowState)mChildWindows.get(i);
6886 c.mAttachedHidden = true;
6887 }
6888 }
6889 }
6890
6891 boolean finishDrawingLocked() {
6892 if (mDrawPending) {
6893 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6894 TAG, "finishDrawingLocked: " + mSurface);
6895 mCommitDrawPending = true;
6896 mDrawPending = false;
6897 return true;
6898 }
6899 return false;
6900 }
6901
6902 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006903 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6905 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006906 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 }
6908 mCommitDrawPending = false;
6909 mReadyToShow = true;
6910 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6911 final AppWindowToken atoken = mAppToken;
6912 if (atoken == null || atoken.allDrawn || starting) {
6913 performShowLocked();
6914 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006915 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 }
6917
6918 // This must be called while inside a transaction.
6919 boolean performShowLocked() {
6920 if (DEBUG_VISIBILITY) {
6921 RuntimeException e = new RuntimeException();
6922 e.fillInStackTrace();
6923 Log.v(TAG, "performShow on " + this
6924 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6925 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6926 }
6927 if (mReadyToShow && isReadyForDisplay()) {
6928 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6929 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6930 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6931 + " during animation: policyVis=" + mPolicyVisibility
6932 + " attHidden=" + mAttachedHidden
6933 + " tok.hiddenRequested="
6934 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6935 + " tok.idden="
6936 + (mAppToken != null ? mAppToken.hidden : false)
6937 + " animating=" + mAnimating
6938 + " tok animating="
6939 + (mAppToken != null ? mAppToken.animating : false));
6940 if (!showSurfaceRobustlyLocked(this)) {
6941 return false;
6942 }
6943 mLastAlpha = -1;
6944 mHasDrawn = true;
6945 mLastHidden = false;
6946 mReadyToShow = false;
6947 enableScreenIfNeededLocked();
6948
6949 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006951 int i = mChildWindows.size();
6952 while (i > 0) {
6953 i--;
6954 WindowState c = (WindowState)mChildWindows.get(i);
6955 if (c.mSurface != null && c.mAttachedHidden) {
6956 c.mAttachedHidden = false;
6957 c.performShowLocked();
6958 }
6959 }
Romain Guy06882f82009-06-10 13:36:04 -07006960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006961 if (mAttrs.type != TYPE_APPLICATION_STARTING
6962 && mAppToken != null) {
6963 mAppToken.firstWindowDrawn = true;
6964 if (mAnimation == null && mAppToken.startingData != null) {
6965 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6966 + mToken
6967 + ": first real window is shown, no animation");
6968 mFinishedStarting.add(mAppToken);
6969 mH.sendEmptyMessage(H.FINISHED_STARTING);
6970 }
6971 mAppToken.updateReportedVisibilityLocked();
6972 }
6973 }
6974 return true;
6975 }
Romain Guy06882f82009-06-10 13:36:04 -07006976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 // This must be called while inside a transaction. Returns true if
6978 // there is more animation to run.
6979 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6980 if (!mDisplayFrozen) {
6981 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6984 mHasTransformation = true;
6985 mHasLocalTransformation = true;
6986 if (!mLocalAnimating) {
6987 if (DEBUG_ANIM) Log.v(
6988 TAG, "Starting animation in " + this +
6989 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6990 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6991 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6992 mAnimation.setStartTime(currentTime);
6993 mLocalAnimating = true;
6994 mAnimating = true;
6995 }
6996 mTransformation.clear();
6997 final boolean more = mAnimation.getTransformation(
6998 currentTime, mTransformation);
6999 if (DEBUG_ANIM) Log.v(
7000 TAG, "Stepped animation in " + this +
7001 ": more=" + more + ", xform=" + mTransformation);
7002 if (more) {
7003 // we're not done!
7004 return true;
7005 }
7006 if (DEBUG_ANIM) Log.v(
7007 TAG, "Finished animation in " + this +
7008 " @ " + currentTime);
7009 mAnimation = null;
7010 //WindowManagerService.this.dump();
7011 }
7012 mHasLocalTransformation = false;
7013 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007014 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 // When our app token is animating, we kind-of pretend like
7016 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7017 // part of this check means that we will only do this if
7018 // our window is not currently exiting, or it is not
7019 // locally animating itself. The idea being that one that
7020 // is exiting and doing a local animation should be removed
7021 // once that animation is done.
7022 mAnimating = true;
7023 mHasTransformation = true;
7024 mTransformation.clear();
7025 return false;
7026 } else if (mHasTransformation) {
7027 // Little trick to get through the path below to act like
7028 // we have finished an animation.
7029 mAnimating = true;
7030 } else if (isAnimating()) {
7031 mAnimating = true;
7032 }
7033 } else if (mAnimation != null) {
7034 // If the display is frozen, and there is a pending animation,
7035 // clear it and make sure we run the cleanup code.
7036 mAnimating = true;
7037 mLocalAnimating = true;
7038 mAnimation = null;
7039 }
Romain Guy06882f82009-06-10 13:36:04 -07007040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 if (!mAnimating && !mLocalAnimating) {
7042 return false;
7043 }
7044
7045 if (DEBUG_ANIM) Log.v(
7046 TAG, "Animation done in " + this + ": exiting=" + mExiting
7047 + ", reportedVisible="
7048 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 mAnimating = false;
7051 mLocalAnimating = false;
7052 mAnimation = null;
7053 mAnimLayer = mLayer;
7054 if (mIsImWindow) {
7055 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007056 } else if (mIsWallpaper) {
7057 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 }
7059 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7060 + " anim layer: " + mAnimLayer);
7061 mHasTransformation = false;
7062 mHasLocalTransformation = false;
7063 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7064 mTransformation.clear();
7065 if (mHasDrawn
7066 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7067 && mAppToken != null
7068 && mAppToken.firstWindowDrawn
7069 && mAppToken.startingData != null) {
7070 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7071 + mToken + ": first real window done animating");
7072 mFinishedStarting.add(mAppToken);
7073 mH.sendEmptyMessage(H.FINISHED_STARTING);
7074 }
Romain Guy06882f82009-06-10 13:36:04 -07007075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 finishExit();
7077
7078 if (mAppToken != null) {
7079 mAppToken.updateReportedVisibilityLocked();
7080 }
7081
7082 return false;
7083 }
7084
7085 void finishExit() {
7086 if (DEBUG_ANIM) Log.v(
7087 TAG, "finishExit in " + this
7088 + ": exiting=" + mExiting
7089 + " remove=" + mRemoveOnExit
7090 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007092 final int N = mChildWindows.size();
7093 for (int i=0; i<N; i++) {
7094 ((WindowState)mChildWindows.get(i)).finishExit();
7095 }
Romain Guy06882f82009-06-10 13:36:04 -07007096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 if (!mExiting) {
7098 return;
7099 }
Romain Guy06882f82009-06-10 13:36:04 -07007100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 if (isWindowAnimating()) {
7102 return;
7103 }
7104
7105 if (localLOGV) Log.v(
7106 TAG, "Exit animation finished in " + this
7107 + ": remove=" + mRemoveOnExit);
7108 if (mSurface != null) {
7109 mDestroySurface.add(this);
7110 mDestroying = true;
7111 if (SHOW_TRANSACTIONS) Log.i(
7112 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7113 try {
7114 mSurface.hide();
7115 } catch (RuntimeException e) {
7116 Log.w(TAG, "Error hiding surface in " + this, e);
7117 }
7118 mLastHidden = true;
7119 mKeyWaiter.releasePendingPointerLocked(mSession);
7120 }
7121 mExiting = false;
7122 if (mRemoveOnExit) {
7123 mPendingRemove.add(this);
7124 mRemoveOnExit = false;
7125 }
7126 }
Romain Guy06882f82009-06-10 13:36:04 -07007127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7129 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7130 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7131 if (dtdx < -.000001f || dtdx > .000001f) return false;
7132 if (dsdy < -.000001f || dsdy > .000001f) return false;
7133 return true;
7134 }
Romain Guy06882f82009-06-10 13:36:04 -07007135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 void computeShownFrameLocked() {
7137 final boolean selfTransformation = mHasLocalTransformation;
7138 Transformation attachedTransformation =
7139 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7140 ? mAttachedWindow.mTransformation : null;
7141 Transformation appTransformation =
7142 (mAppToken != null && mAppToken.hasTransformation)
7143 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007144
7145 // Wallpapers are animated based on the "real" window they
7146 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007147 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007148 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007149 if (mWallpaperTarget.mHasLocalTransformation) {
7150 attachedTransformation = mWallpaperTarget.mTransformation;
7151 }
7152 if (mWallpaperTarget.mAppToken != null &&
7153 mWallpaperTarget.mAppToken.hasTransformation) {
7154 appTransformation = mWallpaperTarget.mAppToken.transformation;
7155 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007156 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7157 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7158 }
7159 if (DEBUG_WALLPAPER && appTransformation != null) {
7160 Log.v(TAG, "WP target app xform: " + appTransformation);
7161 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007162 }
7163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 if (selfTransformation || attachedTransformation != null
7165 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007166 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 final Rect frame = mFrame;
7168 final float tmpFloats[] = mTmpFloats;
7169 final Matrix tmpMatrix = mTmpMatrix;
7170
7171 // Compute the desired transformation.
7172 tmpMatrix.setTranslate(frame.left, frame.top);
7173 if (selfTransformation) {
7174 tmpMatrix.preConcat(mTransformation.getMatrix());
7175 }
7176 if (attachedTransformation != null) {
7177 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7178 }
7179 if (appTransformation != null) {
7180 tmpMatrix.preConcat(appTransformation.getMatrix());
7181 }
7182
7183 // "convert" it into SurfaceFlinger's format
7184 // (a 2x2 matrix + an offset)
7185 // Here we must not transform the position of the surface
7186 // since it is already included in the transformation.
7187 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 tmpMatrix.getValues(tmpFloats);
7190 mDsDx = tmpFloats[Matrix.MSCALE_X];
7191 mDtDx = tmpFloats[Matrix.MSKEW_X];
7192 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7193 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007194 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7195 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 int w = frame.width();
7197 int h = frame.height();
7198 mShownFrame.set(x, y, x+w, y+h);
7199
7200 // Now set the alpha... but because our current hardware
7201 // can't do alpha transformation on a non-opaque surface,
7202 // turn it off if we are running an animation that is also
7203 // transforming since it is more important to have that
7204 // animation be smooth.
7205 mShownAlpha = mAlpha;
7206 if (!mLimitedAlphaCompositing
7207 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7208 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7209 && x == frame.left && y == frame.top))) {
7210 //Log.i(TAG, "Applying alpha transform");
7211 if (selfTransformation) {
7212 mShownAlpha *= mTransformation.getAlpha();
7213 }
7214 if (attachedTransformation != null) {
7215 mShownAlpha *= attachedTransformation.getAlpha();
7216 }
7217 if (appTransformation != null) {
7218 mShownAlpha *= appTransformation.getAlpha();
7219 }
7220 } else {
7221 //Log.i(TAG, "Not applying alpha transform");
7222 }
Romain Guy06882f82009-06-10 13:36:04 -07007223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 if (localLOGV) Log.v(
7225 TAG, "Continuing animation in " + this +
7226 ": " + mShownFrame +
7227 ", alpha=" + mTransformation.getAlpha());
7228 return;
7229 }
Romain Guy06882f82009-06-10 13:36:04 -07007230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007232 if (mXOffset != 0 || mYOffset != 0) {
7233 mShownFrame.offset(mXOffset, mYOffset);
7234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 mShownAlpha = mAlpha;
7236 mDsDx = 1;
7237 mDtDx = 0;
7238 mDsDy = 0;
7239 mDtDy = 1;
7240 }
Romain Guy06882f82009-06-10 13:36:04 -07007241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007242 /**
7243 * Is this window visible? It is not visible if there is no
7244 * surface, or we are in the process of running an exit animation
7245 * that will remove the surface, or its app token has been hidden.
7246 */
7247 public boolean isVisibleLw() {
7248 final AppWindowToken atoken = mAppToken;
7249 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7250 && (atoken == null || !atoken.hiddenRequested)
7251 && !mExiting && !mDestroying;
7252 }
7253
7254 /**
7255 * Is this window visible, ignoring its app token? It is not visible
7256 * if there is no surface, or we are in the process of running an exit animation
7257 * that will remove the surface.
7258 */
7259 public boolean isWinVisibleLw() {
7260 final AppWindowToken atoken = mAppToken;
7261 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7262 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7263 && !mExiting && !mDestroying;
7264 }
7265
7266 /**
7267 * The same as isVisible(), but follows the current hidden state of
7268 * the associated app token, not the pending requested hidden state.
7269 */
7270 boolean isVisibleNow() {
7271 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007272 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 }
7274
7275 /**
7276 * Same as isVisible(), but we also count it as visible between the
7277 * call to IWindowSession.add() and the first relayout().
7278 */
7279 boolean isVisibleOrAdding() {
7280 final AppWindowToken atoken = mAppToken;
7281 return (mSurface != null
7282 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7283 && mPolicyVisibility && !mAttachedHidden
7284 && (atoken == null || !atoken.hiddenRequested)
7285 && !mExiting && !mDestroying;
7286 }
7287
7288 /**
7289 * Is this window currently on-screen? It is on-screen either if it
7290 * is visible or it is currently running an animation before no longer
7291 * being visible.
7292 */
7293 boolean isOnScreen() {
7294 final AppWindowToken atoken = mAppToken;
7295 if (atoken != null) {
7296 return mSurface != null && mPolicyVisibility && !mDestroying
7297 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007298 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 } else {
7300 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007301 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 }
7303 }
Romain Guy06882f82009-06-10 13:36:04 -07007304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 /**
7306 * Like isOnScreen(), but we don't return true if the window is part
7307 * of a transition that has not yet been started.
7308 */
7309 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007310 if (mRootToken.waitingToShow &&
7311 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7312 return false;
7313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007315 final boolean animating = atoken != null
7316 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007318 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7319 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007320 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 }
7322
7323 /** Is the window or its container currently animating? */
7324 boolean isAnimating() {
7325 final WindowState attached = mAttachedWindow;
7326 final AppWindowToken atoken = mAppToken;
7327 return mAnimation != null
7328 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007329 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 (atoken.animation != null
7331 || atoken.inPendingTransaction));
7332 }
7333
7334 /** Is this window currently animating? */
7335 boolean isWindowAnimating() {
7336 return mAnimation != null;
7337 }
7338
7339 /**
7340 * Like isOnScreen, but returns false if the surface hasn't yet
7341 * been drawn.
7342 */
7343 public boolean isDisplayedLw() {
7344 final AppWindowToken atoken = mAppToken;
7345 return mSurface != null && mPolicyVisibility && !mDestroying
7346 && !mDrawPending && !mCommitDrawPending
7347 && ((!mAttachedHidden &&
7348 (atoken == null || !atoken.hiddenRequested))
7349 || mAnimating);
7350 }
7351
7352 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7353 boolean shownFrame, boolean onlyOpaque) {
7354 if (mSurface == null) {
7355 return false;
7356 }
7357 if (mAppToken != null && !mAppToken.appFullscreen) {
7358 return false;
7359 }
7360 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7361 return false;
7362 }
7363 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007364
7365 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7366 return frame.left <= mCompatibleScreenFrame.left &&
7367 frame.top <= mCompatibleScreenFrame.top &&
7368 frame.right >= mCompatibleScreenFrame.right &&
7369 frame.bottom >= mCompatibleScreenFrame.bottom;
7370 } else {
7371 return frame.left <= 0 && frame.top <= 0
7372 && frame.right >= screenWidth
7373 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 }
Romain Guy06882f82009-06-10 13:36:04 -07007376
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007377 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007378 * Return true if the window is opaque and fully drawn. This indicates
7379 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007380 */
7381 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007382 return (mAttrs.format == PixelFormat.OPAQUE
7383 || mAttrs.type == TYPE_WALLPAPER)
7384 && mSurface != null && mAnimation == null
7385 && (mAppToken == null || mAppToken.animation == null)
7386 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007387 }
7388
7389 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7390 return
7391 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007392 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7393 // only if it's visible
7394 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007395 // and only if the application fills the compatible screen
7396 mFrame.left <= mCompatibleScreenFrame.left &&
7397 mFrame.top <= mCompatibleScreenFrame.top &&
7398 mFrame.right >= mCompatibleScreenFrame.right &&
7399 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007400 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007401 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007402 }
7403
7404 boolean isFullscreen(int screenWidth, int screenHeight) {
7405 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007406 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 }
7408
7409 void removeLocked() {
7410 if (mAttachedWindow != null) {
7411 mAttachedWindow.mChildWindows.remove(this);
7412 }
7413 destroySurfaceLocked();
7414 mSession.windowRemovedLocked();
7415 try {
7416 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7417 } catch (RuntimeException e) {
7418 // Ignore if it has already been removed (usually because
7419 // we are doing this as part of processing a death note.)
7420 }
7421 }
7422
7423 private class DeathRecipient implements IBinder.DeathRecipient {
7424 public void binderDied() {
7425 try {
7426 synchronized(mWindowMap) {
7427 WindowState win = windowForClientLocked(mSession, mClient);
7428 Log.i(TAG, "WIN DEATH: " + win);
7429 if (win != null) {
7430 removeWindowLocked(mSession, win);
7431 }
7432 }
7433 } catch (IllegalArgumentException ex) {
7434 // This will happen if the window has already been
7435 // removed.
7436 }
7437 }
7438 }
7439
7440 /** Returns true if this window desires key events. */
7441 public final boolean canReceiveKeys() {
7442 return isVisibleOrAdding()
7443 && (mViewVisibility == View.VISIBLE)
7444 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7445 }
7446
7447 public boolean hasDrawnLw() {
7448 return mHasDrawn;
7449 }
7450
7451 public boolean showLw(boolean doAnimation) {
7452 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7453 mPolicyVisibility = true;
7454 mPolicyVisibilityAfterAnim = true;
7455 if (doAnimation) {
7456 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7457 }
7458 requestAnimationLocked(0);
7459 return true;
7460 }
7461 return false;
7462 }
7463
7464 public boolean hideLw(boolean doAnimation) {
7465 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7466 : mPolicyVisibility;
7467 if (current) {
7468 if (doAnimation) {
7469 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7470 if (mAnimation == null) {
7471 doAnimation = false;
7472 }
7473 }
7474 if (doAnimation) {
7475 mPolicyVisibilityAfterAnim = false;
7476 } else {
7477 mPolicyVisibilityAfterAnim = false;
7478 mPolicyVisibility = false;
7479 }
7480 requestAnimationLocked(0);
7481 return true;
7482 }
7483 return false;
7484 }
7485
7486 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007487 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007488
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007489 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7490 pw.print(" mClient="); pw.println(mClient.asBinder());
7491 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7492 if (mAttachedWindow != null || mLayoutAttached) {
7493 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7494 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7495 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007496 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7497 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7498 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007499 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7500 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007501 }
7502 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7503 pw.print(" mSubLayer="); pw.print(mSubLayer);
7504 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7505 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7506 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7507 pw.print("="); pw.print(mAnimLayer);
7508 pw.print(" mLastLayer="); pw.println(mLastLayer);
7509 if (mSurface != null) {
7510 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7511 }
7512 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7513 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7514 if (mAppToken != null) {
7515 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7516 }
7517 if (mTargetAppToken != null) {
7518 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7519 }
7520 pw.print(prefix); pw.print("mViewVisibility=0x");
7521 pw.print(Integer.toHexString(mViewVisibility));
7522 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007523 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7524 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007525 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7526 pw.print(prefix); pw.print("mPolicyVisibility=");
7527 pw.print(mPolicyVisibility);
7528 pw.print(" mPolicyVisibilityAfterAnim=");
7529 pw.print(mPolicyVisibilityAfterAnim);
7530 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7531 }
7532 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007533 pw.print(" h="); pw.println(mRequestedHeight);
7534 if (mXOffset != 0 || mYOffset != 0) {
7535 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7536 pw.print(" y="); pw.println(mYOffset);
7537 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007538 pw.print(prefix); pw.print("mGivenContentInsets=");
7539 mGivenContentInsets.printShortString(pw);
7540 pw.print(" mGivenVisibleInsets=");
7541 mGivenVisibleInsets.printShortString(pw);
7542 pw.println();
7543 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7544 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7545 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7546 }
7547 pw.print(prefix); pw.print("mShownFrame=");
7548 mShownFrame.printShortString(pw);
7549 pw.print(" last="); mLastShownFrame.printShortString(pw);
7550 pw.println();
7551 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7552 pw.print(" last="); mLastFrame.printShortString(pw);
7553 pw.println();
7554 pw.print(prefix); pw.print("mContainingFrame=");
7555 mContainingFrame.printShortString(pw);
7556 pw.print(" mDisplayFrame=");
7557 mDisplayFrame.printShortString(pw);
7558 pw.println();
7559 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7560 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7561 pw.println();
7562 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7563 pw.print(" last="); mLastContentInsets.printShortString(pw);
7564 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7565 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7566 pw.println();
7567 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7568 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7569 pw.print(" mAlpha="); pw.print(mAlpha);
7570 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7571 }
7572 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7573 || mAnimation != null) {
7574 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7575 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7576 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7577 pw.print(" mAnimation="); pw.println(mAnimation);
7578 }
7579 if (mHasTransformation || mHasLocalTransformation) {
7580 pw.print(prefix); pw.print("XForm: has=");
7581 pw.print(mHasTransformation);
7582 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7583 pw.print(" "); mTransformation.printShortString(pw);
7584 pw.println();
7585 }
7586 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7587 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7588 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7589 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7590 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7591 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7592 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7593 pw.print(" mDestroying="); pw.print(mDestroying);
7594 pw.print(" mRemoved="); pw.println(mRemoved);
7595 }
7596 if (mOrientationChanging || mAppFreezing) {
7597 pw.print(prefix); pw.print("mOrientationChanging=");
7598 pw.print(mOrientationChanging);
7599 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7600 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007601 if (mHScale != 1 || mVScale != 1) {
7602 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7603 pw.print(" mVScale="); pw.println(mVScale);
7604 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007605 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007606 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7607 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 }
7610
7611 @Override
7612 public String toString() {
7613 return "Window{"
7614 + Integer.toHexString(System.identityHashCode(this))
7615 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7616 }
7617 }
Romain Guy06882f82009-06-10 13:36:04 -07007618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 // -------------------------------------------------------------
7620 // Window Token State
7621 // -------------------------------------------------------------
7622
7623 class WindowToken {
7624 // The actual token.
7625 final IBinder token;
7626
7627 // The type of window this token is for, as per WindowManager.LayoutParams.
7628 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 // Set if this token was explicitly added by a client, so should
7631 // not be removed when all windows are removed.
7632 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007633
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007634 // For printing.
7635 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 // If this is an AppWindowToken, this is non-null.
7638 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 // All of the windows associated with this token.
7641 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7642
7643 // Is key dispatching paused for this token?
7644 boolean paused = false;
7645
7646 // Should this token's windows be hidden?
7647 boolean hidden;
7648
7649 // Temporary for finding which tokens no longer have visible windows.
7650 boolean hasVisible;
7651
Dianne Hackborna8f60182009-09-01 19:01:50 -07007652 // Set to true when this token is in a pending transaction where it
7653 // will be shown.
7654 boolean waitingToShow;
7655
7656 // Set to true when this token is in a pending transaction where it
7657 // will be hidden.
7658 boolean waitingToHide;
7659
7660 // Set to true when this token is in a pending transaction where its
7661 // windows will be put to the bottom of the list.
7662 boolean sendingToBottom;
7663
7664 // Set to true when this token is in a pending transaction where its
7665 // windows will be put to the top of the list.
7666 boolean sendingToTop;
7667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 WindowToken(IBinder _token, int type, boolean _explicit) {
7669 token = _token;
7670 windowType = type;
7671 explicit = _explicit;
7672 }
7673
7674 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007675 pw.print(prefix); pw.print("token="); pw.println(token);
7676 pw.print(prefix); pw.print("windows="); pw.println(windows);
7677 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7678 pw.print(" hidden="); pw.print(hidden);
7679 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007680 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7681 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7682 pw.print(" waitingToHide="); pw.print(waitingToHide);
7683 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7684 pw.print(" sendingToTop="); pw.println(sendingToTop);
7685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 }
7687
7688 @Override
7689 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007690 if (stringName == null) {
7691 StringBuilder sb = new StringBuilder();
7692 sb.append("WindowToken{");
7693 sb.append(Integer.toHexString(System.identityHashCode(this)));
7694 sb.append(" token="); sb.append(token); sb.append('}');
7695 stringName = sb.toString();
7696 }
7697 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 }
7699 };
7700
7701 class AppWindowToken extends WindowToken {
7702 // Non-null only for application tokens.
7703 final IApplicationToken appToken;
7704
7705 // All of the windows and child windows that are included in this
7706 // application token. Note this list is NOT sorted!
7707 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7708
7709 int groupId = -1;
7710 boolean appFullscreen;
7711 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 // These are used for determining when all windows associated with
7714 // an activity have been drawn, so they can be made visible together
7715 // at the same time.
7716 int lastTransactionSequence = mTransactionSequence-1;
7717 int numInterestingWindows;
7718 int numDrawnWindows;
7719 boolean inPendingTransaction;
7720 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 // Is this token going to be hidden in a little while? If so, it
7723 // won't be taken into account for setting the screen orientation.
7724 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 // Is this window's surface needed? This is almost like hidden, except
7727 // it will sometimes be true a little earlier: when the token has
7728 // been shown, but is still waiting for its app transition to execute
7729 // before making its windows shown.
7730 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 // Have we told the window clients to hide themselves?
7733 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 // Last visibility state we reported to the app token.
7736 boolean reportedVisible;
7737
7738 // Set to true when the token has been removed from the window mgr.
7739 boolean removed;
7740
7741 // Have we been asked to have this token keep the screen frozen?
7742 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 boolean animating;
7745 Animation animation;
7746 boolean hasTransformation;
7747 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 // Offset to the window of all layers in the token, for use by
7750 // AppWindowToken animations.
7751 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 // Information about an application starting window if displayed.
7754 StartingData startingData;
7755 WindowState startingWindow;
7756 View startingView;
7757 boolean startingDisplayed;
7758 boolean startingMoved;
7759 boolean firstWindowDrawn;
7760
7761 AppWindowToken(IApplicationToken _token) {
7762 super(_token.asBinder(),
7763 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7764 appWindowToken = this;
7765 appToken = _token;
7766 }
Romain Guy06882f82009-06-10 13:36:04 -07007767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 public void setAnimation(Animation anim) {
7769 if (localLOGV) Log.v(
7770 TAG, "Setting animation in " + this + ": " + anim);
7771 animation = anim;
7772 animating = false;
7773 anim.restrictDuration(MAX_ANIMATION_DURATION);
7774 anim.scaleCurrentDuration(mTransitionAnimationScale);
7775 int zorder = anim.getZAdjustment();
7776 int adj = 0;
7777 if (zorder == Animation.ZORDER_TOP) {
7778 adj = TYPE_LAYER_OFFSET;
7779 } else if (zorder == Animation.ZORDER_BOTTOM) {
7780 adj = -TYPE_LAYER_OFFSET;
7781 }
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 if (animLayerAdjustment != adj) {
7784 animLayerAdjustment = adj;
7785 updateLayers();
7786 }
7787 }
Romain Guy06882f82009-06-10 13:36:04 -07007788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 public void setDummyAnimation() {
7790 if (animation == null) {
7791 if (localLOGV) Log.v(
7792 TAG, "Setting dummy animation in " + this);
7793 animation = sDummyAnimation;
7794 }
7795 }
7796
7797 public void clearAnimation() {
7798 if (animation != null) {
7799 animation = null;
7800 animating = true;
7801 }
7802 }
Romain Guy06882f82009-06-10 13:36:04 -07007803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 void updateLayers() {
7805 final int N = allAppWindows.size();
7806 final int adj = animLayerAdjustment;
7807 for (int i=0; i<N; i++) {
7808 WindowState w = allAppWindows.get(i);
7809 w.mAnimLayer = w.mLayer + adj;
7810 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7811 + w.mAnimLayer);
7812 if (w == mInputMethodTarget) {
7813 setInputMethodAnimLayerAdjustment(adj);
7814 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007815 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007816 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 }
7819 }
Romain Guy06882f82009-06-10 13:36:04 -07007820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007821 void sendAppVisibilityToClients() {
7822 final int N = allAppWindows.size();
7823 for (int i=0; i<N; i++) {
7824 WindowState win = allAppWindows.get(i);
7825 if (win == startingWindow && clientHidden) {
7826 // Don't hide the starting window.
7827 continue;
7828 }
7829 try {
7830 if (DEBUG_VISIBILITY) Log.v(TAG,
7831 "Setting visibility of " + win + ": " + (!clientHidden));
7832 win.mClient.dispatchAppVisibility(!clientHidden);
7833 } catch (RemoteException e) {
7834 }
7835 }
7836 }
Romain Guy06882f82009-06-10 13:36:04 -07007837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 void showAllWindowsLocked() {
7839 final int NW = allAppWindows.size();
7840 for (int i=0; i<NW; i++) {
7841 WindowState w = allAppWindows.get(i);
7842 if (DEBUG_VISIBILITY) Log.v(TAG,
7843 "performing show on: " + w);
7844 w.performShowLocked();
7845 }
7846 }
Romain Guy06882f82009-06-10 13:36:04 -07007847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 // This must be called while inside a transaction.
7849 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7850 if (!mDisplayFrozen) {
7851 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 if (animation == sDummyAnimation) {
7854 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007855 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007856 // when it is really time to animate, this will be set to
7857 // a real animation and the next call will execute normally.
7858 return false;
7859 }
Romain Guy06882f82009-06-10 13:36:04 -07007860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7862 if (!animating) {
7863 if (DEBUG_ANIM) Log.v(
7864 TAG, "Starting animation in " + this +
7865 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7866 + " scale=" + mTransitionAnimationScale
7867 + " allDrawn=" + allDrawn + " animating=" + animating);
7868 animation.initialize(dw, dh, dw, dh);
7869 animation.setStartTime(currentTime);
7870 animating = true;
7871 }
7872 transformation.clear();
7873 final boolean more = animation.getTransformation(
7874 currentTime, transformation);
7875 if (DEBUG_ANIM) Log.v(
7876 TAG, "Stepped animation in " + this +
7877 ": more=" + more + ", xform=" + transformation);
7878 if (more) {
7879 // we're done!
7880 hasTransformation = true;
7881 return true;
7882 }
7883 if (DEBUG_ANIM) Log.v(
7884 TAG, "Finished animation in " + this +
7885 " @ " + currentTime);
7886 animation = null;
7887 }
7888 } else if (animation != null) {
7889 // If the display is frozen, and there is a pending animation,
7890 // clear it and make sure we run the cleanup code.
7891 animating = true;
7892 animation = null;
7893 }
7894
7895 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 if (!animating) {
7898 return false;
7899 }
7900
7901 clearAnimation();
7902 animating = false;
7903 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7904 moveInputMethodWindowsIfNeededLocked(true);
7905 }
Romain Guy06882f82009-06-10 13:36:04 -07007906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 if (DEBUG_ANIM) Log.v(
7908 TAG, "Animation done in " + this
7909 + ": reportedVisible=" + reportedVisible);
7910
7911 transformation.clear();
7912 if (animLayerAdjustment != 0) {
7913 animLayerAdjustment = 0;
7914 updateLayers();
7915 }
Romain Guy06882f82009-06-10 13:36:04 -07007916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 final int N = windows.size();
7918 for (int i=0; i<N; i++) {
7919 ((WindowState)windows.get(i)).finishExit();
7920 }
7921 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 return false;
7924 }
7925
7926 void updateReportedVisibilityLocked() {
7927 if (appToken == null) {
7928 return;
7929 }
Romain Guy06882f82009-06-10 13:36:04 -07007930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 int numInteresting = 0;
7932 int numVisible = 0;
7933 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7936 final int N = allAppWindows.size();
7937 for (int i=0; i<N; i++) {
7938 WindowState win = allAppWindows.get(i);
7939 if (win == startingWindow || win.mAppFreezing) {
7940 continue;
7941 }
7942 if (DEBUG_VISIBILITY) {
7943 Log.v(TAG, "Win " + win + ": isDisplayed="
7944 + win.isDisplayedLw()
7945 + ", isAnimating=" + win.isAnimating());
7946 if (!win.isDisplayedLw()) {
7947 Log.v(TAG, "Not displayed: s=" + win.mSurface
7948 + " pv=" + win.mPolicyVisibility
7949 + " dp=" + win.mDrawPending
7950 + " cdp=" + win.mCommitDrawPending
7951 + " ah=" + win.mAttachedHidden
7952 + " th="
7953 + (win.mAppToken != null
7954 ? win.mAppToken.hiddenRequested : false)
7955 + " a=" + win.mAnimating);
7956 }
7957 }
7958 numInteresting++;
7959 if (win.isDisplayedLw()) {
7960 if (!win.isAnimating()) {
7961 numVisible++;
7962 }
7963 nowGone = false;
7964 } else if (win.isAnimating()) {
7965 nowGone = false;
7966 }
7967 }
Romain Guy06882f82009-06-10 13:36:04 -07007968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7970 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7971 + numInteresting + " visible=" + numVisible);
7972 if (nowVisible != reportedVisible) {
7973 if (DEBUG_VISIBILITY) Log.v(
7974 TAG, "Visibility changed in " + this
7975 + ": vis=" + nowVisible);
7976 reportedVisible = nowVisible;
7977 Message m = mH.obtainMessage(
7978 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7979 nowVisible ? 1 : 0,
7980 nowGone ? 1 : 0,
7981 this);
7982 mH.sendMessage(m);
7983 }
7984 }
Romain Guy06882f82009-06-10 13:36:04 -07007985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 void dump(PrintWriter pw, String prefix) {
7987 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007988 if (appToken != null) {
7989 pw.print(prefix); pw.println("app=true");
7990 }
7991 if (allAppWindows.size() > 0) {
7992 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7993 }
7994 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007995 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007996 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7997 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7998 pw.print(" clientHidden="); pw.print(clientHidden);
7999 pw.print(" willBeHidden="); pw.print(willBeHidden);
8000 pw.print(" reportedVisible="); pw.println(reportedVisible);
8001 if (paused || freezingScreen) {
8002 pw.print(prefix); pw.print("paused="); pw.print(paused);
8003 pw.print(" freezingScreen="); pw.println(freezingScreen);
8004 }
8005 if (numInterestingWindows != 0 || numDrawnWindows != 0
8006 || inPendingTransaction || allDrawn) {
8007 pw.print(prefix); pw.print("numInterestingWindows=");
8008 pw.print(numInterestingWindows);
8009 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8010 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8011 pw.print(" allDrawn="); pw.println(allDrawn);
8012 }
8013 if (animating || animation != null) {
8014 pw.print(prefix); pw.print("animating="); pw.print(animating);
8015 pw.print(" animation="); pw.println(animation);
8016 }
8017 if (animLayerAdjustment != 0) {
8018 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8019 }
8020 if (hasTransformation) {
8021 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8022 pw.print(" transformation="); transformation.printShortString(pw);
8023 pw.println();
8024 }
8025 if (startingData != null || removed || firstWindowDrawn) {
8026 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8027 pw.print(" removed="); pw.print(removed);
8028 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8029 }
8030 if (startingWindow != null || startingView != null
8031 || startingDisplayed || startingMoved) {
8032 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8033 pw.print(" startingView="); pw.print(startingView);
8034 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8035 pw.print(" startingMoved"); pw.println(startingMoved);
8036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 }
8038
8039 @Override
8040 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008041 if (stringName == null) {
8042 StringBuilder sb = new StringBuilder();
8043 sb.append("AppWindowToken{");
8044 sb.append(Integer.toHexString(System.identityHashCode(this)));
8045 sb.append(" token="); sb.append(token); sb.append('}');
8046 stringName = sb.toString();
8047 }
8048 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049 }
8050 }
Romain Guy06882f82009-06-10 13:36:04 -07008051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 public static WindowManager.LayoutParams findAnimations(
8053 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008054 ArrayList<AppWindowToken> openingTokenList1,
8055 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008057
8058 // First, check if there is a compatible window in opening/closing
8059 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 WindowManager.LayoutParams animParams = null;
8061 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008062 animParams = findCompatibleWindowParams(openingTokenList1);
8063 if (animParams == null) {
8064 animParams = findCompatibleWindowParams(closingTokenList2);
8065 }
8066 if (animParams != null) {
8067 return animParams;
8068 }
8069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008070 //Log.i(TAG, "Looking for animations...");
8071 for (int i=order.size()-1; i>=0; i--) {
8072 AppWindowToken wtoken = order.get(i);
8073 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008074 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 int j = wtoken.windows.size();
8076 while (j > 0) {
8077 j--;
8078 WindowState win = wtoken.windows.get(j);
8079 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
8080 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8081 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8082 //Log.i(TAG, "Found base or application window, done!");
8083 if (wtoken.appFullscreen) {
8084 return win.mAttrs;
8085 }
8086 if (animSrc < 2) {
8087 animParams = win.mAttrs;
8088 animSrc = 2;
8089 }
8090 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
8091 //Log.i(TAG, "Found normal window, we may use this...");
8092 animParams = win.mAttrs;
8093 animSrc = 1;
8094 }
8095 }
8096 }
8097 }
Romain Guy06882f82009-06-10 13:36:04 -07008098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 return animParams;
8100 }
Romain Guy06882f82009-06-10 13:36:04 -07008101
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008102 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
8103 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
8104 AppWindowToken wtoken = tokenList.get(appCount);
8105 // Just checking one window is sufficient as all windows have the compatible flag
8106 // if the application is in compatibility mode.
8107 if (wtoken.windows.size() > 0) {
8108 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
8109 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8110 return params;
8111 }
8112 }
8113 }
8114 return null;
8115 }
8116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 // -------------------------------------------------------------
8118 // DummyAnimation
8119 // -------------------------------------------------------------
8120
8121 // This is an animation that does nothing: it just immediately finishes
8122 // itself every time it is called. It is used as a stub animation in cases
8123 // where we want to synchronize multiple things that may be animating.
8124 static final class DummyAnimation extends Animation {
8125 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8126 return false;
8127 }
8128 }
8129 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 // -------------------------------------------------------------
8132 // Async Handler
8133 // -------------------------------------------------------------
8134
8135 static final class StartingData {
8136 final String pkg;
8137 final int theme;
8138 final CharSequence nonLocalizedLabel;
8139 final int labelRes;
8140 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8143 int _labelRes, int _icon) {
8144 pkg = _pkg;
8145 theme = _theme;
8146 nonLocalizedLabel = _nonLocalizedLabel;
8147 labelRes = _labelRes;
8148 icon = _icon;
8149 }
8150 }
8151
8152 private final class H extends Handler {
8153 public static final int REPORT_FOCUS_CHANGE = 2;
8154 public static final int REPORT_LOSING_FOCUS = 3;
8155 public static final int ANIMATE = 4;
8156 public static final int ADD_STARTING = 5;
8157 public static final int REMOVE_STARTING = 6;
8158 public static final int FINISHED_STARTING = 7;
8159 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8161 public static final int HOLD_SCREEN_CHANGED = 12;
8162 public static final int APP_TRANSITION_TIMEOUT = 13;
8163 public static final int PERSIST_ANIMATION_SCALE = 14;
8164 public static final int FORCE_GC = 15;
8165 public static final int ENABLE_SCREEN = 16;
8166 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008167 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 public H() {
8172 }
Romain Guy06882f82009-06-10 13:36:04 -07008173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 @Override
8175 public void handleMessage(Message msg) {
8176 switch (msg.what) {
8177 case REPORT_FOCUS_CHANGE: {
8178 WindowState lastFocus;
8179 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181 synchronized(mWindowMap) {
8182 lastFocus = mLastFocus;
8183 newFocus = mCurrentFocus;
8184 if (lastFocus == newFocus) {
8185 // Focus is not changing, so nothing to do.
8186 return;
8187 }
8188 mLastFocus = newFocus;
8189 //Log.i(TAG, "Focus moving from " + lastFocus
8190 // + " to " + newFocus);
8191 if (newFocus != null && lastFocus != null
8192 && !newFocus.isDisplayedLw()) {
8193 //Log.i(TAG, "Delaying loss of focus...");
8194 mLosingFocus.add(lastFocus);
8195 lastFocus = null;
8196 }
8197 }
8198
8199 if (lastFocus != newFocus) {
8200 //System.out.println("Changing focus from " + lastFocus
8201 // + " to " + newFocus);
8202 if (newFocus != null) {
8203 try {
8204 //Log.i(TAG, "Gaining focus: " + newFocus);
8205 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8206 } catch (RemoteException e) {
8207 // Ignore if process has died.
8208 }
8209 }
8210
8211 if (lastFocus != null) {
8212 try {
8213 //Log.i(TAG, "Losing focus: " + lastFocus);
8214 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8215 } catch (RemoteException e) {
8216 // Ignore if process has died.
8217 }
8218 }
8219 }
8220 } break;
8221
8222 case REPORT_LOSING_FOCUS: {
8223 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 synchronized(mWindowMap) {
8226 losers = mLosingFocus;
8227 mLosingFocus = new ArrayList<WindowState>();
8228 }
8229
8230 final int N = losers.size();
8231 for (int i=0; i<N; i++) {
8232 try {
8233 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8234 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8235 } catch (RemoteException e) {
8236 // Ignore if process has died.
8237 }
8238 }
8239 } break;
8240
8241 case ANIMATE: {
8242 synchronized(mWindowMap) {
8243 mAnimationPending = false;
8244 performLayoutAndPlaceSurfacesLocked();
8245 }
8246 } break;
8247
8248 case ADD_STARTING: {
8249 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8250 final StartingData sd = wtoken.startingData;
8251
8252 if (sd == null) {
8253 // Animation has been canceled... do nothing.
8254 return;
8255 }
Romain Guy06882f82009-06-10 13:36:04 -07008256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8258 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 View view = null;
8261 try {
8262 view = mPolicy.addStartingWindow(
8263 wtoken.token, sd.pkg,
8264 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8265 sd.icon);
8266 } catch (Exception e) {
8267 Log.w(TAG, "Exception when adding starting window", e);
8268 }
8269
8270 if (view != null) {
8271 boolean abort = false;
8272
8273 synchronized(mWindowMap) {
8274 if (wtoken.removed || wtoken.startingData == null) {
8275 // If the window was successfully added, then
8276 // we need to remove it.
8277 if (wtoken.startingWindow != null) {
8278 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8279 "Aborted starting " + wtoken
8280 + ": removed=" + wtoken.removed
8281 + " startingData=" + wtoken.startingData);
8282 wtoken.startingWindow = null;
8283 wtoken.startingData = null;
8284 abort = true;
8285 }
8286 } else {
8287 wtoken.startingView = view;
8288 }
8289 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8290 "Added starting " + wtoken
8291 + ": startingWindow="
8292 + wtoken.startingWindow + " startingView="
8293 + wtoken.startingView);
8294 }
8295
8296 if (abort) {
8297 try {
8298 mPolicy.removeStartingWindow(wtoken.token, view);
8299 } catch (Exception e) {
8300 Log.w(TAG, "Exception when removing starting window", e);
8301 }
8302 }
8303 }
8304 } break;
8305
8306 case REMOVE_STARTING: {
8307 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8308 IBinder token = null;
8309 View view = null;
8310 synchronized (mWindowMap) {
8311 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8312 + wtoken + ": startingWindow="
8313 + wtoken.startingWindow + " startingView="
8314 + wtoken.startingView);
8315 if (wtoken.startingWindow != null) {
8316 view = wtoken.startingView;
8317 token = wtoken.token;
8318 wtoken.startingData = null;
8319 wtoken.startingView = null;
8320 wtoken.startingWindow = null;
8321 }
8322 }
8323 if (view != null) {
8324 try {
8325 mPolicy.removeStartingWindow(token, view);
8326 } catch (Exception e) {
8327 Log.w(TAG, "Exception when removing starting window", e);
8328 }
8329 }
8330 } break;
8331
8332 case FINISHED_STARTING: {
8333 IBinder token = null;
8334 View view = null;
8335 while (true) {
8336 synchronized (mWindowMap) {
8337 final int N = mFinishedStarting.size();
8338 if (N <= 0) {
8339 break;
8340 }
8341 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8342
8343 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8344 "Finished starting " + wtoken
8345 + ": startingWindow=" + wtoken.startingWindow
8346 + " startingView=" + wtoken.startingView);
8347
8348 if (wtoken.startingWindow == null) {
8349 continue;
8350 }
8351
8352 view = wtoken.startingView;
8353 token = wtoken.token;
8354 wtoken.startingData = null;
8355 wtoken.startingView = null;
8356 wtoken.startingWindow = null;
8357 }
8358
8359 try {
8360 mPolicy.removeStartingWindow(token, view);
8361 } catch (Exception e) {
8362 Log.w(TAG, "Exception when removing starting window", e);
8363 }
8364 }
8365 } break;
8366
8367 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8368 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8369
8370 boolean nowVisible = msg.arg1 != 0;
8371 boolean nowGone = msg.arg2 != 0;
8372
8373 try {
8374 if (DEBUG_VISIBILITY) Log.v(
8375 TAG, "Reporting visible in " + wtoken
8376 + " visible=" + nowVisible
8377 + " gone=" + nowGone);
8378 if (nowVisible) {
8379 wtoken.appToken.windowsVisible();
8380 } else {
8381 wtoken.appToken.windowsGone();
8382 }
8383 } catch (RemoteException ex) {
8384 }
8385 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 case WINDOW_FREEZE_TIMEOUT: {
8388 synchronized (mWindowMap) {
8389 Log.w(TAG, "Window freeze timeout expired.");
8390 int i = mWindows.size();
8391 while (i > 0) {
8392 i--;
8393 WindowState w = (WindowState)mWindows.get(i);
8394 if (w.mOrientationChanging) {
8395 w.mOrientationChanging = false;
8396 Log.w(TAG, "Force clearing orientation change: " + w);
8397 }
8398 }
8399 performLayoutAndPlaceSurfacesLocked();
8400 }
8401 break;
8402 }
Romain Guy06882f82009-06-10 13:36:04 -07008403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 case HOLD_SCREEN_CHANGED: {
8405 Session oldHold;
8406 Session newHold;
8407 synchronized (mWindowMap) {
8408 oldHold = mLastReportedHold;
8409 newHold = (Session)msg.obj;
8410 mLastReportedHold = newHold;
8411 }
Romain Guy06882f82009-06-10 13:36:04 -07008412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 if (oldHold != newHold) {
8414 try {
8415 if (oldHold != null) {
8416 mBatteryStats.noteStopWakelock(oldHold.mUid,
8417 "window",
8418 BatteryStats.WAKE_TYPE_WINDOW);
8419 }
8420 if (newHold != null) {
8421 mBatteryStats.noteStartWakelock(newHold.mUid,
8422 "window",
8423 BatteryStats.WAKE_TYPE_WINDOW);
8424 }
8425 } catch (RemoteException e) {
8426 }
8427 }
8428 break;
8429 }
Romain Guy06882f82009-06-10 13:36:04 -07008430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 case APP_TRANSITION_TIMEOUT: {
8432 synchronized (mWindowMap) {
8433 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8434 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8435 "*** APP TRANSITION TIMEOUT");
8436 mAppTransitionReady = true;
8437 mAppTransitionTimeout = true;
8438 performLayoutAndPlaceSurfacesLocked();
8439 }
8440 }
8441 break;
8442 }
Romain Guy06882f82009-06-10 13:36:04 -07008443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 case PERSIST_ANIMATION_SCALE: {
8445 Settings.System.putFloat(mContext.getContentResolver(),
8446 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8447 Settings.System.putFloat(mContext.getContentResolver(),
8448 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8449 break;
8450 }
Romain Guy06882f82009-06-10 13:36:04 -07008451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 case FORCE_GC: {
8453 synchronized(mWindowMap) {
8454 if (mAnimationPending) {
8455 // If we are animating, don't do the gc now but
8456 // delay a bit so we don't interrupt the animation.
8457 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8458 2000);
8459 return;
8460 }
8461 // If we are currently rotating the display, it will
8462 // schedule a new message when done.
8463 if (mDisplayFrozen) {
8464 return;
8465 }
8466 mFreezeGcPending = 0;
8467 }
8468 Runtime.getRuntime().gc();
8469 break;
8470 }
Romain Guy06882f82009-06-10 13:36:04 -07008471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 case ENABLE_SCREEN: {
8473 performEnableScreen();
8474 break;
8475 }
Romain Guy06882f82009-06-10 13:36:04 -07008476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 case APP_FREEZE_TIMEOUT: {
8478 synchronized (mWindowMap) {
8479 Log.w(TAG, "App freeze timeout expired.");
8480 int i = mAppTokens.size();
8481 while (i > 0) {
8482 i--;
8483 AppWindowToken tok = mAppTokens.get(i);
8484 if (tok.freezingScreen) {
8485 Log.w(TAG, "Force clearing freeze: " + tok);
8486 unsetAppFreezingScreenLocked(tok, true, true);
8487 }
8488 }
8489 }
8490 break;
8491 }
Romain Guy06882f82009-06-10 13:36:04 -07008492
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008493 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008494 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008495 sendNewConfiguration();
8496 }
8497 break;
8498 }
Romain Guy06882f82009-06-10 13:36:04 -07008499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 }
8501 }
8502 }
8503
8504 // -------------------------------------------------------------
8505 // IWindowManager API
8506 // -------------------------------------------------------------
8507
8508 public IWindowSession openSession(IInputMethodClient client,
8509 IInputContext inputContext) {
8510 if (client == null) throw new IllegalArgumentException("null client");
8511 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8512 return new Session(client, inputContext);
8513 }
8514
8515 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8516 synchronized (mWindowMap) {
8517 // The focus for the client is the window immediately below
8518 // where we would place the input method window.
8519 int idx = findDesiredInputMethodWindowIndexLocked(false);
8520 WindowState imFocus;
8521 if (idx > 0) {
8522 imFocus = (WindowState)mWindows.get(idx-1);
8523 if (imFocus != null) {
8524 if (imFocus.mSession.mClient != null &&
8525 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8526 return true;
8527 }
8528 }
8529 }
8530 }
8531 return false;
8532 }
Romain Guy06882f82009-06-10 13:36:04 -07008533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 // -------------------------------------------------------------
8535 // Internals
8536 // -------------------------------------------------------------
8537
8538 final WindowState windowForClientLocked(Session session, IWindow client) {
8539 return windowForClientLocked(session, client.asBinder());
8540 }
Romain Guy06882f82009-06-10 13:36:04 -07008541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 final WindowState windowForClientLocked(Session session, IBinder client) {
8543 WindowState win = mWindowMap.get(client);
8544 if (localLOGV) Log.v(
8545 TAG, "Looking up client " + client + ": " + win);
8546 if (win == null) {
8547 RuntimeException ex = new RuntimeException();
8548 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8549 return null;
8550 }
8551 if (session != null && win.mSession != session) {
8552 RuntimeException ex = new RuntimeException();
8553 Log.w(TAG, "Requested window " + client + " is in session " +
8554 win.mSession + ", not " + session, ex);
8555 return null;
8556 }
8557
8558 return win;
8559 }
8560
Dianne Hackborna8f60182009-09-01 19:01:50 -07008561 final void rebuildAppWindowListLocked() {
8562 int NW = mWindows.size();
8563 int i;
8564
8565 // First remove all existing app windows.
8566 i=0;
8567 while (i < NW) {
8568 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008569 WindowState win = (WindowState)mWindows.remove(i);
8570 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8571 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008572 NW--;
8573 continue;
8574 }
8575 i++;
8576 }
8577
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008578 // First add all of the exiting app tokens... these are no longer
8579 // in the main app list, but still have windows shown. We put them
8580 // in the back because now that the animation is over we no longer
8581 // will care about them.
8582 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008583 i = 0;
8584 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008585 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8586 }
8587
8588 // And add in the still active app tokens in Z order.
8589 NT = mAppTokens.size();
8590 for (int j=0; j<NT; j++) {
8591 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008592 }
8593 }
8594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 private final void assignLayersLocked() {
8596 int N = mWindows.size();
8597 int curBaseLayer = 0;
8598 int curLayer = 0;
8599 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 for (i=0; i<N; i++) {
8602 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008603 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8604 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008605 curLayer += WINDOW_LAYER_MULTIPLIER;
8606 w.mLayer = curLayer;
8607 } else {
8608 curBaseLayer = curLayer = w.mBaseLayer;
8609 w.mLayer = curLayer;
8610 }
8611 if (w.mTargetAppToken != null) {
8612 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8613 } else if (w.mAppToken != null) {
8614 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8615 } else {
8616 w.mAnimLayer = w.mLayer;
8617 }
8618 if (w.mIsImWindow) {
8619 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008620 } else if (w.mIsWallpaper) {
8621 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008622 }
8623 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8624 + w.mAnimLayer);
8625 //System.out.println(
8626 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8627 }
8628 }
8629
8630 private boolean mInLayout = false;
8631 private final void performLayoutAndPlaceSurfacesLocked() {
8632 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008633 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 throw new RuntimeException("Recursive call!");
8635 }
8636 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8637 return;
8638 }
8639
8640 boolean recoveringMemory = false;
8641 if (mForceRemoves != null) {
8642 recoveringMemory = true;
8643 // Wait a little it for things to settle down, and off we go.
8644 for (int i=0; i<mForceRemoves.size(); i++) {
8645 WindowState ws = mForceRemoves.get(i);
8646 Log.i(TAG, "Force removing: " + ws);
8647 removeWindowInnerLocked(ws.mSession, ws);
8648 }
8649 mForceRemoves = null;
8650 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8651 Object tmp = new Object();
8652 synchronized (tmp) {
8653 try {
8654 tmp.wait(250);
8655 } catch (InterruptedException e) {
8656 }
8657 }
8658 }
Romain Guy06882f82009-06-10 13:36:04 -07008659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 mInLayout = true;
8661 try {
8662 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 int i = mPendingRemove.size()-1;
8665 if (i >= 0) {
8666 while (i >= 0) {
8667 WindowState w = mPendingRemove.get(i);
8668 removeWindowInnerLocked(w.mSession, w);
8669 i--;
8670 }
8671 mPendingRemove.clear();
8672
8673 mInLayout = false;
8674 assignLayersLocked();
8675 mLayoutNeeded = true;
8676 performLayoutAndPlaceSurfacesLocked();
8677
8678 } else {
8679 mInLayout = false;
8680 if (mLayoutNeeded) {
8681 requestAnimationLocked(0);
8682 }
8683 }
8684 } catch (RuntimeException e) {
8685 mInLayout = false;
8686 Log.e(TAG, "Unhandled exception while layout out windows", e);
8687 }
8688 }
8689
8690 private final void performLayoutLockedInner() {
8691 final int dw = mDisplay.getWidth();
8692 final int dh = mDisplay.getHeight();
8693
8694 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008695 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 int i;
8697
8698 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008699
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008700 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 mPolicy.beginLayoutLw(dw, dh);
8702
8703 // First perform layout of any root windows (not attached
8704 // to another window).
8705 int topAttached = -1;
8706 for (i = N-1; i >= 0; i--) {
8707 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008708
8709 // Don't do layout of a window if it is not visible, or
8710 // soon won't be visible, to avoid wasting time and funky
8711 // changes while a window is animating away.
8712 final AppWindowToken atoken = win.mAppToken;
8713 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008715 || win.mRootToken.hidden
8716 || (atoken != null && atoken.hiddenRequested)
8717 || !win.mPolicyVisibility
8718 || win.mAttachedHidden
8719 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720
8721 // If this view is GONE, then skip it -- keep the current
8722 // frame, and let the caller know so they can ignore it
8723 // if they want. (We do the normal layout for INVISIBLE
8724 // windows, since that means "perform layout as normal,
8725 // just don't display").
8726 if (!gone || !win.mHaveFrame) {
8727 if (!win.mLayoutAttached) {
8728 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8729 } else {
8730 if (topAttached < 0) topAttached = i;
8731 }
8732 }
8733 }
Romain Guy06882f82009-06-10 13:36:04 -07008734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 // Now perform layout of attached windows, which usually
8736 // depend on the position of the window they are attached to.
8737 // XXX does not deal with windows that are attached to windows
8738 // that are themselves attached.
8739 for (i = topAttached; i >= 0; i--) {
8740 WindowState win = (WindowState) mWindows.get(i);
8741
8742 // If this view is GONE, then skip it -- keep the current
8743 // frame, and let the caller know so they can ignore it
8744 // if they want. (We do the normal layout for INVISIBLE
8745 // windows, since that means "perform layout as normal,
8746 // just don't display").
8747 if (win.mLayoutAttached) {
8748 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8749 || !win.mHaveFrame) {
8750 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8751 }
8752 }
8753 }
8754
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008755 if (!mPolicy.finishLayoutLw()) {
8756 mLayoutNeeded = false;
8757 } else if (repeats > 2) {
8758 Log.w(TAG, "Layout repeat aborted after too many iterations");
8759 mLayoutNeeded = false;
8760 } else {
8761 repeats++;
8762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 }
8764 }
Romain Guy06882f82009-06-10 13:36:04 -07008765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 private final void performLayoutAndPlaceSurfacesLockedInner(
8767 boolean recoveringMemory) {
8768 final long currentTime = SystemClock.uptimeMillis();
8769 final int dw = mDisplay.getWidth();
8770 final int dh = mDisplay.getHeight();
8771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 int i;
8773
8774 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008775 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 if (mFxSession == null) {
8778 mFxSession = new SurfaceSession();
8779 }
Romain Guy06882f82009-06-10 13:36:04 -07008780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8782
8783 // Initialize state of exiting tokens.
8784 for (i=mExitingTokens.size()-1; i>=0; i--) {
8785 mExitingTokens.get(i).hasVisible = false;
8786 }
8787
8788 // Initialize state of exiting applications.
8789 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8790 mExitingAppTokens.get(i).hasVisible = false;
8791 }
8792
8793 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008794 boolean orientationChangeComplete = true;
8795 Session holdScreen = null;
8796 float screenBrightness = -1;
8797 boolean focusDisplayed = false;
8798 boolean animating = false;
8799
8800 Surface.openTransaction();
8801 try {
8802 boolean restart;
8803
8804 do {
8805 final int transactionSequence = ++mTransactionSequence;
8806
8807 // Update animations of all applications, including those
8808 // associated with exiting/removed apps
8809 boolean tokensAnimating = false;
8810 final int NAT = mAppTokens.size();
8811 for (i=0; i<NAT; i++) {
8812 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8813 tokensAnimating = true;
8814 }
8815 }
8816 final int NEAT = mExitingAppTokens.size();
8817 for (i=0; i<NEAT; i++) {
8818 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8819 tokensAnimating = true;
8820 }
8821 }
8822
8823 animating = tokensAnimating;
8824 restart = false;
8825
8826 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008827 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828
8829 mPolicy.beginAnimationLw(dw, dh);
8830
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008831 final int N = mWindows.size();
8832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 for (i=N-1; i>=0; i--) {
8834 WindowState w = (WindowState)mWindows.get(i);
8835
8836 final WindowManager.LayoutParams attrs = w.mAttrs;
8837
8838 if (w.mSurface != null) {
8839 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008840 if (w.commitFinishDrawingLocked(currentTime)) {
8841 if ((w.mAttrs.flags
8842 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
8843 wallpaperMayChange = true;
8844 }
8845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8847 animating = true;
8848 //w.dump(" ");
8849 }
8850
8851 mPolicy.animatingWindowLw(w, attrs);
8852 }
8853
8854 final AppWindowToken atoken = w.mAppToken;
8855 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8856 if (atoken.lastTransactionSequence != transactionSequence) {
8857 atoken.lastTransactionSequence = transactionSequence;
8858 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8859 atoken.startingDisplayed = false;
8860 }
8861 if ((w.isOnScreen() || w.mAttrs.type
8862 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8863 && !w.mExiting && !w.mDestroying) {
8864 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8865 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8866 + w.isDisplayedLw()
8867 + ", isAnimating=" + w.isAnimating());
8868 if (!w.isDisplayedLw()) {
8869 Log.v(TAG, "Not displayed: s=" + w.mSurface
8870 + " pv=" + w.mPolicyVisibility
8871 + " dp=" + w.mDrawPending
8872 + " cdp=" + w.mCommitDrawPending
8873 + " ah=" + w.mAttachedHidden
8874 + " th=" + atoken.hiddenRequested
8875 + " a=" + w.mAnimating);
8876 }
8877 }
8878 if (w != atoken.startingWindow) {
8879 if (!atoken.freezingScreen || !w.mAppFreezing) {
8880 atoken.numInterestingWindows++;
8881 if (w.isDisplayedLw()) {
8882 atoken.numDrawnWindows++;
8883 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8884 "tokenMayBeDrawn: " + atoken
8885 + " freezingScreen=" + atoken.freezingScreen
8886 + " mAppFreezing=" + w.mAppFreezing);
8887 tokenMayBeDrawn = true;
8888 }
8889 }
8890 } else if (w.isDisplayedLw()) {
8891 atoken.startingDisplayed = true;
8892 }
8893 }
8894 } else if (w.mReadyToShow) {
8895 w.performShowLocked();
8896 }
8897 }
8898
8899 if (mPolicy.finishAnimationLw()) {
8900 restart = true;
8901 }
8902
8903 if (tokenMayBeDrawn) {
8904 // See if any windows have been drawn, so they (and others
8905 // associated with them) can now be shown.
8906 final int NT = mTokenList.size();
8907 for (i=0; i<NT; i++) {
8908 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8909 if (wtoken == null) {
8910 continue;
8911 }
8912 if (wtoken.freezingScreen) {
8913 int numInteresting = wtoken.numInterestingWindows;
8914 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8915 if (DEBUG_VISIBILITY) Log.v(TAG,
8916 "allDrawn: " + wtoken
8917 + " interesting=" + numInteresting
8918 + " drawn=" + wtoken.numDrawnWindows);
8919 wtoken.showAllWindowsLocked();
8920 unsetAppFreezingScreenLocked(wtoken, false, true);
8921 orientationChangeComplete = true;
8922 }
8923 } else if (!wtoken.allDrawn) {
8924 int numInteresting = wtoken.numInterestingWindows;
8925 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8926 if (DEBUG_VISIBILITY) Log.v(TAG,
8927 "allDrawn: " + wtoken
8928 + " interesting=" + numInteresting
8929 + " drawn=" + wtoken.numDrawnWindows);
8930 wtoken.allDrawn = true;
8931 restart = true;
8932
8933 // We can now show all of the drawn windows!
8934 if (!mOpeningApps.contains(wtoken)) {
8935 wtoken.showAllWindowsLocked();
8936 }
8937 }
8938 }
8939 }
8940 }
8941
8942 // If we are ready to perform an app transition, check through
8943 // all of the app tokens to be shown and see if they are ready
8944 // to go.
8945 if (mAppTransitionReady) {
8946 int NN = mOpeningApps.size();
8947 boolean goodToGo = true;
8948 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8949 "Checking " + NN + " opening apps (frozen="
8950 + mDisplayFrozen + " timeout="
8951 + mAppTransitionTimeout + ")...");
8952 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8953 // If the display isn't frozen, wait to do anything until
8954 // all of the apps are ready. Otherwise just go because
8955 // we'll unfreeze the display when everyone is ready.
8956 for (i=0; i<NN && goodToGo; i++) {
8957 AppWindowToken wtoken = mOpeningApps.get(i);
8958 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8959 "Check opening app" + wtoken + ": allDrawn="
8960 + wtoken.allDrawn + " startingDisplayed="
8961 + wtoken.startingDisplayed);
8962 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8963 && !wtoken.startingMoved) {
8964 goodToGo = false;
8965 }
8966 }
8967 }
8968 if (goodToGo) {
8969 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8970 int transit = mNextAppTransition;
8971 if (mSkipAppTransitionAnimation) {
8972 transit = WindowManagerPolicy.TRANSIT_NONE;
8973 }
8974 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8975 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008976 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 mAppTransitionTimeout = false;
8978 mStartingIconInTransition = false;
8979 mSkipAppTransitionAnimation = false;
8980
8981 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8982
Dianne Hackborna8f60182009-09-01 19:01:50 -07008983 // If there are applications waiting to come to the
8984 // top of the stack, now is the time to move their windows.
8985 // (Note that we don't do apps going to the bottom
8986 // here -- we want to keep their windows in the old
8987 // Z-order until the animation completes.)
8988 if (mToTopApps.size() > 0) {
8989 NN = mAppTokens.size();
8990 for (i=0; i<NN; i++) {
8991 AppWindowToken wtoken = mAppTokens.get(i);
8992 if (wtoken.sendingToTop) {
8993 wtoken.sendingToTop = false;
8994 moveAppWindowsLocked(wtoken, NN, false);
8995 }
8996 }
8997 mToTopApps.clear();
8998 }
8999
Dianne Hackborn25994b42009-09-04 14:21:19 -07009000 WindowState oldWallpaper = mWallpaperTarget;
9001
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009002 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009003 wallpaperMayChange = false;
9004
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009005 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009006 "New wallpaper target=" + mWallpaperTarget
9007 + ", lower target=" + mLowerWallpaperTarget
9008 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009009 int foundWallpapers = 0;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009010 if (mLowerWallpaperTarget != null) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009011 // Need to determine if both the closing and
9012 // opening app token sets are wallpaper targets,
9013 // in which case special animations are needed
9014 // (since the wallpaper needs to stay static
9015 // behind them).
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009016 NN = mClosingApps.size();
9017 for (i=0; i<NN; i++) {
9018 AppWindowToken wtoken = mClosingApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009019 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackborn25994b42009-09-04 14:21:19 -07009020 foundWallpapers |= 1;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009021 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009022 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackborn25994b42009-09-04 14:21:19 -07009023 foundWallpapers |= 1;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009024 }
9025 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009026 NN = mOpeningApps.size();
9027 for (i=0; i<NN; i++) {
9028 AppWindowToken wtoken = mOpeningApps.get(i);
9029 if (mLowerWallpaperTarget.mAppToken == wtoken) {
9030 foundWallpapers |= 2;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009031 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009032 if (mUpperWallpaperTarget.mAppToken == wtoken) {
9033 foundWallpapers |= 2;
9034 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009035 }
9036 }
9037
Dianne Hackborn25994b42009-09-04 14:21:19 -07009038 if (foundWallpapers == 3) {
9039 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9040 "Wallpaper animation!");
9041 switch (transit) {
9042 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9043 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9044 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9045 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9046 break;
9047 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9048 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9049 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9050 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9051 break;
9052 }
9053 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9054 "New transit: " + transit);
9055 } else if (oldWallpaper != null) {
9056 // We are transitioning from an activity with
9057 // a wallpaper to one without.
9058 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9059 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9060 "New transit away from wallpaper: " + transit);
9061 } else if (mWallpaperTarget != null) {
9062 // We are transitioning from an activity without
9063 // a wallpaper to now showing the wallpaper
9064 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9065 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9066 "New transit into wallpaper: " + transit);
9067 }
9068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 // We need to figure out which animation to use...
9070 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
9071 mOpeningApps, mClosingApps);
9072
9073 NN = mOpeningApps.size();
9074 for (i=0; i<NN; i++) {
9075 AppWindowToken wtoken = mOpeningApps.get(i);
9076 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9077 "Now opening app" + wtoken);
9078 wtoken.reportedVisible = false;
9079 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009080 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
9082 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009083 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 wtoken.showAllWindowsLocked();
9085 }
9086 NN = mClosingApps.size();
9087 for (i=0; i<NN; i++) {
9088 AppWindowToken wtoken = mClosingApps.get(i);
9089 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9090 "Now closing app" + wtoken);
9091 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009092 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
9094 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009095 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 // Force the allDrawn flag, because we want to start
9097 // this guy's animations regardless of whether it's
9098 // gotten drawn.
9099 wtoken.allDrawn = true;
9100 }
9101
9102 mOpeningApps.clear();
9103 mClosingApps.clear();
9104
9105 // This has changed the visibility of windows, so perform
9106 // a new layout to get them all up-to-date.
9107 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009108 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9109 assignLayersLocked();
9110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 performLayoutLockedInner();
9112 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9113
9114 restart = true;
9115 }
9116 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009117
Dianne Hackborna8f60182009-09-01 19:01:50 -07009118 if (!animating && mAppTransitionRunning) {
9119 // We have finished the animation of an app transition. To do
9120 // this, we have delayed a lot of operations like showing and
9121 // hiding apps, moving apps in Z-order, etc. The app token list
9122 // reflects the correct Z-order, but the window list may now
9123 // be out of sync with it. So here we will just rebuild the
9124 // entire app window list. Fun!
9125 mAppTransitionRunning = false;
9126 // Clear information about apps that were moving.
9127 mToBottomApps.clear();
9128
9129 rebuildAppWindowListLocked();
9130 restart = true;
9131 moveInputMethodWindowsIfNeededLocked(false);
9132 wallpaperMayChange = true;
9133 mLayoutNeeded = true;
9134 }
9135
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009136 if (wallpaperMayChange) {
9137 if (adjustWallpaperWindowsLocked()) {
9138 assignLayersLocked();
9139 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009140 if (mLayoutNeeded) {
9141 performLayoutLockedInner();
9142 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009143 }
9144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 } while (restart);
9146
9147 // THIRD LOOP: Update the surfaces of all windows.
9148
9149 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9150
9151 boolean obscured = false;
9152 boolean blurring = false;
9153 boolean dimming = false;
9154 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009155 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009156 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009158 final int N = mWindows.size();
9159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009160 for (i=N-1; i>=0; i--) {
9161 WindowState w = (WindowState)mWindows.get(i);
9162
9163 boolean displayed = false;
9164 final WindowManager.LayoutParams attrs = w.mAttrs;
9165 final int attrFlags = attrs.flags;
9166
9167 if (w.mSurface != null) {
9168 w.computeShownFrameLocked();
9169 if (localLOGV) Log.v(
9170 TAG, "Placing surface #" + i + " " + w.mSurface
9171 + ": new=" + w.mShownFrame + ", old="
9172 + w.mLastShownFrame);
9173
9174 boolean resize;
9175 int width, height;
9176 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9177 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9178 w.mLastRequestedHeight != w.mRequestedHeight;
9179 // for a scaled surface, we just want to use
9180 // the requested size.
9181 width = w.mRequestedWidth;
9182 height = w.mRequestedHeight;
9183 w.mLastRequestedWidth = width;
9184 w.mLastRequestedHeight = height;
9185 w.mLastShownFrame.set(w.mShownFrame);
9186 try {
9187 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9188 } catch (RuntimeException e) {
9189 Log.w(TAG, "Error positioning surface in " + w, e);
9190 if (!recoveringMemory) {
9191 reclaimSomeSurfaceMemoryLocked(w, "position");
9192 }
9193 }
9194 } else {
9195 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9196 width = w.mShownFrame.width();
9197 height = w.mShownFrame.height();
9198 w.mLastShownFrame.set(w.mShownFrame);
9199 if (resize) {
9200 if (SHOW_TRANSACTIONS) Log.i(
9201 TAG, " SURFACE " + w.mSurface + ": ("
9202 + w.mShownFrame.left + ","
9203 + w.mShownFrame.top + ") ("
9204 + w.mShownFrame.width() + "x"
9205 + w.mShownFrame.height() + ")");
9206 }
9207 }
9208
9209 if (resize) {
9210 if (width < 1) width = 1;
9211 if (height < 1) height = 1;
9212 if (w.mSurface != null) {
9213 try {
9214 w.mSurface.setSize(width, height);
9215 w.mSurface.setPosition(w.mShownFrame.left,
9216 w.mShownFrame.top);
9217 } catch (RuntimeException e) {
9218 // If something goes wrong with the surface (such
9219 // as running out of memory), don't take down the
9220 // entire system.
9221 Log.e(TAG, "Failure updating surface of " + w
9222 + "size=(" + width + "x" + height
9223 + "), pos=(" + w.mShownFrame.left
9224 + "," + w.mShownFrame.top + ")", e);
9225 if (!recoveringMemory) {
9226 reclaimSomeSurfaceMemoryLocked(w, "size");
9227 }
9228 }
9229 }
9230 }
9231 if (!w.mAppFreezing) {
9232 w.mContentInsetsChanged =
9233 !w.mLastContentInsets.equals(w.mContentInsets);
9234 w.mVisibleInsetsChanged =
9235 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009236 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 || w.mContentInsetsChanged
9238 || w.mVisibleInsetsChanged) {
9239 w.mLastFrame.set(w.mFrame);
9240 w.mLastContentInsets.set(w.mContentInsets);
9241 w.mLastVisibleInsets.set(w.mVisibleInsets);
9242 // If the orientation is changing, then we need to
9243 // hold off on unfreezing the display until this
9244 // window has been redrawn; to do that, we need
9245 // to go through the process of getting informed
9246 // by the application when it has finished drawing.
9247 if (w.mOrientationChanging) {
9248 if (DEBUG_ORIENTATION) Log.v(TAG,
9249 "Orientation start waiting for draw in "
9250 + w + ", surface " + w.mSurface);
9251 w.mDrawPending = true;
9252 w.mCommitDrawPending = false;
9253 w.mReadyToShow = false;
9254 if (w.mAppToken != null) {
9255 w.mAppToken.allDrawn = false;
9256 }
9257 }
Romain Guy06882f82009-06-10 13:36:04 -07009258 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 "Resizing window " + w + " to " + w.mFrame);
9260 mResizingWindows.add(w);
9261 } else if (w.mOrientationChanging) {
9262 if (!w.mDrawPending && !w.mCommitDrawPending) {
9263 if (DEBUG_ORIENTATION) Log.v(TAG,
9264 "Orientation not waiting for draw in "
9265 + w + ", surface " + w.mSurface);
9266 w.mOrientationChanging = false;
9267 }
9268 }
9269 }
9270
9271 if (w.mAttachedHidden) {
9272 if (!w.mLastHidden) {
9273 //dump();
9274 w.mLastHidden = true;
9275 if (SHOW_TRANSACTIONS) Log.i(
9276 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9277 if (w.mSurface != null) {
9278 try {
9279 w.mSurface.hide();
9280 } catch (RuntimeException e) {
9281 Log.w(TAG, "Exception hiding surface in " + w);
9282 }
9283 }
9284 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9285 }
9286 // If we are waiting for this window to handle an
9287 // orientation change, well, it is hidden, so
9288 // doesn't really matter. Note that this does
9289 // introduce a potential glitch if the window
9290 // becomes unhidden before it has drawn for the
9291 // new orientation.
9292 if (w.mOrientationChanging) {
9293 w.mOrientationChanging = false;
9294 if (DEBUG_ORIENTATION) Log.v(TAG,
9295 "Orientation change skips hidden " + w);
9296 }
9297 } else if (!w.isReadyForDisplay()) {
9298 if (!w.mLastHidden) {
9299 //dump();
9300 w.mLastHidden = true;
9301 if (SHOW_TRANSACTIONS) Log.i(
9302 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9303 if (w.mSurface != null) {
9304 try {
9305 w.mSurface.hide();
9306 } catch (RuntimeException e) {
9307 Log.w(TAG, "Exception exception hiding surface in " + w);
9308 }
9309 }
9310 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9311 }
9312 // If we are waiting for this window to handle an
9313 // orientation change, well, it is hidden, so
9314 // doesn't really matter. Note that this does
9315 // introduce a potential glitch if the window
9316 // becomes unhidden before it has drawn for the
9317 // new orientation.
9318 if (w.mOrientationChanging) {
9319 w.mOrientationChanging = false;
9320 if (DEBUG_ORIENTATION) Log.v(TAG,
9321 "Orientation change skips hidden " + w);
9322 }
9323 } else if (w.mLastLayer != w.mAnimLayer
9324 || w.mLastAlpha != w.mShownAlpha
9325 || w.mLastDsDx != w.mDsDx
9326 || w.mLastDtDx != w.mDtDx
9327 || w.mLastDsDy != w.mDsDy
9328 || w.mLastDtDy != w.mDtDy
9329 || w.mLastHScale != w.mHScale
9330 || w.mLastVScale != w.mVScale
9331 || w.mLastHidden) {
9332 displayed = true;
9333 w.mLastAlpha = w.mShownAlpha;
9334 w.mLastLayer = w.mAnimLayer;
9335 w.mLastDsDx = w.mDsDx;
9336 w.mLastDtDx = w.mDtDx;
9337 w.mLastDsDy = w.mDsDy;
9338 w.mLastDtDy = w.mDtDy;
9339 w.mLastHScale = w.mHScale;
9340 w.mLastVScale = w.mVScale;
9341 if (SHOW_TRANSACTIONS) Log.i(
9342 TAG, " SURFACE " + w.mSurface + ": alpha="
9343 + w.mShownAlpha + " layer=" + w.mAnimLayer);
9344 if (w.mSurface != null) {
9345 try {
9346 w.mSurface.setAlpha(w.mShownAlpha);
9347 w.mSurface.setLayer(w.mAnimLayer);
9348 w.mSurface.setMatrix(
9349 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9350 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9351 } catch (RuntimeException e) {
9352 Log.w(TAG, "Error updating surface in " + w, e);
9353 if (!recoveringMemory) {
9354 reclaimSomeSurfaceMemoryLocked(w, "update");
9355 }
9356 }
9357 }
9358
9359 if (w.mLastHidden && !w.mDrawPending
9360 && !w.mCommitDrawPending
9361 && !w.mReadyToShow) {
9362 if (SHOW_TRANSACTIONS) Log.i(
9363 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9364 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9365 + " during relayout");
9366 if (showSurfaceRobustlyLocked(w)) {
9367 w.mHasDrawn = true;
9368 w.mLastHidden = false;
9369 } else {
9370 w.mOrientationChanging = false;
9371 }
9372 }
9373 if (w.mSurface != null) {
9374 w.mToken.hasVisible = true;
9375 }
9376 } else {
9377 displayed = true;
9378 }
9379
9380 if (displayed) {
9381 if (!covered) {
9382 if (attrs.width == LayoutParams.FILL_PARENT
9383 && attrs.height == LayoutParams.FILL_PARENT) {
9384 covered = true;
9385 }
9386 }
9387 if (w.mOrientationChanging) {
9388 if (w.mDrawPending || w.mCommitDrawPending) {
9389 orientationChangeComplete = false;
9390 if (DEBUG_ORIENTATION) Log.v(TAG,
9391 "Orientation continue waiting for draw in " + w);
9392 } else {
9393 w.mOrientationChanging = false;
9394 if (DEBUG_ORIENTATION) Log.v(TAG,
9395 "Orientation change complete in " + w);
9396 }
9397 }
9398 w.mToken.hasVisible = true;
9399 }
9400 } else if (w.mOrientationChanging) {
9401 if (DEBUG_ORIENTATION) Log.v(TAG,
9402 "Orientation change skips hidden " + w);
9403 w.mOrientationChanging = false;
9404 }
9405
9406 final boolean canBeSeen = w.isDisplayedLw();
9407
9408 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9409 focusDisplayed = true;
9410 }
9411
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009412 final boolean obscuredChanged = w.mObscured != obscured;
9413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009415 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 if (w.mSurface != null) {
9417 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9418 holdScreen = w.mSession;
9419 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009420 if (!syswin && w.mAttrs.screenBrightness >= 0
9421 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 screenBrightness = w.mAttrs.screenBrightness;
9423 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009424 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9425 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9426 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9427 syswin = true;
9428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009430
Dianne Hackborn25994b42009-09-04 14:21:19 -07009431 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9432 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 // This window completely covers everything behind it,
9434 // so we want to leave all of them as unblurred (for
9435 // performance reasons).
9436 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009437 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9438 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009439 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009440 obscured = true;
9441 if (mBackgroundFillerSurface == null) {
9442 try {
9443 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9444 0, dw, dh,
9445 PixelFormat.OPAQUE,
9446 Surface.FX_SURFACE_NORMAL);
9447 } catch (Exception e) {
9448 Log.e(TAG, "Exception creating filler surface", e);
9449 }
9450 }
9451 try {
9452 mBackgroundFillerSurface.setPosition(0, 0);
9453 mBackgroundFillerSurface.setSize(dw, dh);
9454 // Using the same layer as Dim because they will never be shown at the
9455 // same time.
9456 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9457 mBackgroundFillerSurface.show();
9458 } catch (RuntimeException e) {
9459 Log.e(TAG, "Exception showing filler surface");
9460 }
9461 backgroundFillerShown = true;
9462 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 } else if (canBeSeen && !obscured &&
9464 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9465 if (localLOGV) Log.v(TAG, "Win " + w
9466 + ": blurring=" + blurring
9467 + " obscured=" + obscured
9468 + " displayed=" + displayed);
9469 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9470 if (!dimming) {
9471 //Log.i(TAG, "DIM BEHIND: " + w);
9472 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009473 if (mDimAnimator == null) {
9474 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009476 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009477 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009478 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 }
9480 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9481 if (!blurring) {
9482 //Log.i(TAG, "BLUR BEHIND: " + w);
9483 blurring = true;
9484 mBlurShown = true;
9485 if (mBlurSurface == null) {
9486 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9487 + mBlurSurface + ": CREATE");
9488 try {
Romain Guy06882f82009-06-10 13:36:04 -07009489 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 -1, 16, 16,
9491 PixelFormat.OPAQUE,
9492 Surface.FX_SURFACE_BLUR);
9493 } catch (Exception e) {
9494 Log.e(TAG, "Exception creating Blur surface", e);
9495 }
9496 }
9497 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9498 + mBlurSurface + ": SHOW pos=(0,0) (" +
9499 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9500 if (mBlurSurface != null) {
9501 mBlurSurface.setPosition(0, 0);
9502 mBlurSurface.setSize(dw, dh);
9503 try {
9504 mBlurSurface.show();
9505 } catch (RuntimeException e) {
9506 Log.w(TAG, "Failure showing blur surface", e);
9507 }
9508 }
9509 }
9510 mBlurSurface.setLayer(w.mAnimLayer-2);
9511 }
9512 }
9513 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009514
9515 if (obscuredChanged && mWallpaperTarget == w) {
9516 // This is the wallpaper target and its obscured state
9517 // changed... make sure the current wallaper's visibility
9518 // has been updated accordingly.
9519 updateWallpaperVisibilityLocked();
9520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009522
9523 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9524 mBackgroundFillerShown = false;
9525 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9526 try {
9527 mBackgroundFillerSurface.hide();
9528 } catch (RuntimeException e) {
9529 Log.e(TAG, "Exception hiding filler surface", e);
9530 }
9531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009532
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009533 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9534 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 }
Romain Guy06882f82009-06-10 13:36:04 -07009536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 if (!blurring && mBlurShown) {
9538 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9539 + ": HIDE");
9540 try {
9541 mBlurSurface.hide();
9542 } catch (IllegalArgumentException e) {
9543 Log.w(TAG, "Illegal argument exception hiding blur surface");
9544 }
9545 mBlurShown = false;
9546 }
9547
9548 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9549 } catch (RuntimeException e) {
9550 Log.e(TAG, "Unhandled exception in Window Manager", e);
9551 }
9552
9553 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9556 "With display frozen, orientationChangeComplete="
9557 + orientationChangeComplete);
9558 if (orientationChangeComplete) {
9559 if (mWindowsFreezingScreen) {
9560 mWindowsFreezingScreen = false;
9561 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9562 }
9563 if (mAppsFreezingScreen == 0) {
9564 stopFreezingDisplayLocked();
9565 }
9566 }
Romain Guy06882f82009-06-10 13:36:04 -07009567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 i = mResizingWindows.size();
9569 if (i > 0) {
9570 do {
9571 i--;
9572 WindowState win = mResizingWindows.get(i);
9573 try {
9574 win.mClient.resized(win.mFrame.width(),
9575 win.mFrame.height(), win.mLastContentInsets,
9576 win.mLastVisibleInsets, win.mDrawPending);
9577 win.mContentInsetsChanged = false;
9578 win.mVisibleInsetsChanged = false;
9579 } catch (RemoteException e) {
9580 win.mOrientationChanging = false;
9581 }
9582 } while (i > 0);
9583 mResizingWindows.clear();
9584 }
Romain Guy06882f82009-06-10 13:36:04 -07009585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009587 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 i = mDestroySurface.size();
9589 if (i > 0) {
9590 do {
9591 i--;
9592 WindowState win = mDestroySurface.get(i);
9593 win.mDestroying = false;
9594 if (mInputMethodWindow == win) {
9595 mInputMethodWindow = null;
9596 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009597 if (win == mWallpaperTarget) {
9598 wallpaperDestroyed = true;
9599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 win.destroySurfaceLocked();
9601 } while (i > 0);
9602 mDestroySurface.clear();
9603 }
9604
9605 // Time to remove any exiting tokens?
9606 for (i=mExitingTokens.size()-1; i>=0; i--) {
9607 WindowToken token = mExitingTokens.get(i);
9608 if (!token.hasVisible) {
9609 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009610 if (token.windowType == TYPE_WALLPAPER) {
9611 mWallpaperTokens.remove(token);
9612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 }
9614 }
9615
9616 // Time to remove any exiting applications?
9617 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9618 AppWindowToken token = mExitingAppTokens.get(i);
9619 if (!token.hasVisible && !mClosingApps.contains(token)) {
9620 mAppTokens.remove(token);
9621 mExitingAppTokens.remove(i);
9622 }
9623 }
9624
Dianne Hackborna8f60182009-09-01 19:01:50 -07009625 boolean needRelayout = false;
9626
9627 if (!animating && mAppTransitionRunning) {
9628 // We have finished the animation of an app transition. To do
9629 // this, we have delayed a lot of operations like showing and
9630 // hiding apps, moving apps in Z-order, etc. The app token list
9631 // reflects the correct Z-order, but the window list may now
9632 // be out of sync with it. So here we will just rebuild the
9633 // entire app window list. Fun!
9634 mAppTransitionRunning = false;
9635 needRelayout = true;
9636 rebuildAppWindowListLocked();
9637 // Clear information about apps that were moving.
9638 mToBottomApps.clear();
9639 }
9640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 if (focusDisplayed) {
9642 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9643 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009644 if (wallpaperDestroyed) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07009645 needRelayout = adjustWallpaperWindowsLocked();
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009646 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009647 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009648 requestAnimationLocked(0);
9649 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9651 }
9652 mQueue.setHoldScreenLocked(holdScreen != null);
9653 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9654 mPowerManager.setScreenBrightnessOverride(-1);
9655 } else {
9656 mPowerManager.setScreenBrightnessOverride((int)
9657 (screenBrightness * Power.BRIGHTNESS_ON));
9658 }
9659 if (holdScreen != mHoldingScreenOn) {
9660 mHoldingScreenOn = holdScreen;
9661 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9662 mH.sendMessage(m);
9663 }
9664 }
9665
9666 void requestAnimationLocked(long delay) {
9667 if (!mAnimationPending) {
9668 mAnimationPending = true;
9669 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9670 }
9671 }
Romain Guy06882f82009-06-10 13:36:04 -07009672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009673 /**
9674 * Have the surface flinger show a surface, robustly dealing with
9675 * error conditions. In particular, if there is not enough memory
9676 * to show the surface, then we will try to get rid of other surfaces
9677 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009678 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 * @return Returns true if the surface was successfully shown.
9680 */
9681 boolean showSurfaceRobustlyLocked(WindowState win) {
9682 try {
9683 if (win.mSurface != null) {
9684 win.mSurface.show();
9685 }
9686 return true;
9687 } catch (RuntimeException e) {
9688 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9689 }
Romain Guy06882f82009-06-10 13:36:04 -07009690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009691 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 return false;
9694 }
Romain Guy06882f82009-06-10 13:36:04 -07009695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9697 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9700 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 if (mForceRemoves == null) {
9703 mForceRemoves = new ArrayList<WindowState>();
9704 }
Romain Guy06882f82009-06-10 13:36:04 -07009705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 long callingIdentity = Binder.clearCallingIdentity();
9707 try {
9708 // There was some problem... first, do a sanity check of the
9709 // window list to make sure we haven't left any dangling surfaces
9710 // around.
9711 int N = mWindows.size();
9712 boolean leakedSurface = false;
9713 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9714 for (int i=0; i<N; i++) {
9715 WindowState ws = (WindowState)mWindows.get(i);
9716 if (ws.mSurface != null) {
9717 if (!mSessions.contains(ws.mSession)) {
9718 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9719 + ws + " surface=" + ws.mSurface
9720 + " token=" + win.mToken
9721 + " pid=" + ws.mSession.mPid
9722 + " uid=" + ws.mSession.mUid);
9723 ws.mSurface.clear();
9724 ws.mSurface = null;
9725 mForceRemoves.add(ws);
9726 i--;
9727 N--;
9728 leakedSurface = true;
9729 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9730 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9731 + ws + " surface=" + ws.mSurface
9732 + " token=" + win.mAppToken);
9733 ws.mSurface.clear();
9734 ws.mSurface = null;
9735 leakedSurface = true;
9736 }
9737 }
9738 }
Romain Guy06882f82009-06-10 13:36:04 -07009739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 boolean killedApps = false;
9741 if (!leakedSurface) {
9742 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9743 SparseIntArray pidCandidates = new SparseIntArray();
9744 for (int i=0; i<N; i++) {
9745 WindowState ws = (WindowState)mWindows.get(i);
9746 if (ws.mSurface != null) {
9747 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9748 }
9749 }
9750 if (pidCandidates.size() > 0) {
9751 int[] pids = new int[pidCandidates.size()];
9752 for (int i=0; i<pids.length; i++) {
9753 pids[i] = pidCandidates.keyAt(i);
9754 }
9755 try {
9756 if (mActivityManager.killPidsForMemory(pids)) {
9757 killedApps = true;
9758 }
9759 } catch (RemoteException e) {
9760 }
9761 }
9762 }
Romain Guy06882f82009-06-10 13:36:04 -07009763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 if (leakedSurface || killedApps) {
9765 // We managed to reclaim some memory, so get rid of the trouble
9766 // surface and ask the app to request another one.
9767 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9768 if (surface != null) {
9769 surface.clear();
9770 win.mSurface = null;
9771 }
Romain Guy06882f82009-06-10 13:36:04 -07009772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 try {
9774 win.mClient.dispatchGetNewSurface();
9775 } catch (RemoteException e) {
9776 }
9777 }
9778 } finally {
9779 Binder.restoreCallingIdentity(callingIdentity);
9780 }
9781 }
Romain Guy06882f82009-06-10 13:36:04 -07009782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 private boolean updateFocusedWindowLocked(int mode) {
9784 WindowState newFocus = computeFocusedWindowLocked();
9785 if (mCurrentFocus != newFocus) {
9786 // This check makes sure that we don't already have the focus
9787 // change message pending.
9788 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9789 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9790 if (localLOGV) Log.v(
9791 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9792 final WindowState oldFocus = mCurrentFocus;
9793 mCurrentFocus = newFocus;
9794 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 final WindowState imWindow = mInputMethodWindow;
9797 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009798 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009800 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9801 mLayoutNeeded = true;
9802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9804 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009805 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9806 // Client will do the layout, but we need to assign layers
9807 // for handleNewWindowLocked() below.
9808 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 }
9810 }
Romain Guy06882f82009-06-10 13:36:04 -07009811
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009812 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9813 mKeyWaiter.handleNewWindowLocked(newFocus);
9814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 return true;
9816 }
9817 return false;
9818 }
9819
9820 private WindowState computeFocusedWindowLocked() {
9821 WindowState result = null;
9822 WindowState win;
9823
9824 int i = mWindows.size() - 1;
9825 int nextAppIndex = mAppTokens.size()-1;
9826 WindowToken nextApp = nextAppIndex >= 0
9827 ? mAppTokens.get(nextAppIndex) : null;
9828
9829 while (i >= 0) {
9830 win = (WindowState)mWindows.get(i);
9831
9832 if (localLOGV || DEBUG_FOCUS) Log.v(
9833 TAG, "Looking for focus: " + i
9834 + " = " + win
9835 + ", flags=" + win.mAttrs.flags
9836 + ", canReceive=" + win.canReceiveKeys());
9837
9838 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 // If this window's application has been removed, just skip it.
9841 if (thisApp != null && thisApp.removed) {
9842 i--;
9843 continue;
9844 }
Romain Guy06882f82009-06-10 13:36:04 -07009845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 // If there is a focused app, don't allow focus to go to any
9847 // windows below it. If this is an application window, step
9848 // through the app tokens until we find its app.
9849 if (thisApp != null && nextApp != null && thisApp != nextApp
9850 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9851 int origAppIndex = nextAppIndex;
9852 while (nextAppIndex > 0) {
9853 if (nextApp == mFocusedApp) {
9854 // Whoops, we are below the focused app... no focus
9855 // for you!
9856 if (localLOGV || DEBUG_FOCUS) Log.v(
9857 TAG, "Reached focused app: " + mFocusedApp);
9858 return null;
9859 }
9860 nextAppIndex--;
9861 nextApp = mAppTokens.get(nextAppIndex);
9862 if (nextApp == thisApp) {
9863 break;
9864 }
9865 }
9866 if (thisApp != nextApp) {
9867 // Uh oh, the app token doesn't exist! This shouldn't
9868 // happen, but if it does we can get totally hosed...
9869 // so restart at the original app.
9870 nextAppIndex = origAppIndex;
9871 nextApp = mAppTokens.get(nextAppIndex);
9872 }
9873 }
9874
9875 // Dispatch to this window if it is wants key events.
9876 if (win.canReceiveKeys()) {
9877 if (DEBUG_FOCUS) Log.v(
9878 TAG, "Found focus @ " + i + " = " + win);
9879 result = win;
9880 break;
9881 }
9882
9883 i--;
9884 }
9885
9886 return result;
9887 }
9888
9889 private void startFreezingDisplayLocked() {
9890 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009891 // Freezing the display also suspends key event delivery, to
9892 // keep events from going astray while the display is reconfigured.
9893 // If someone has changed orientation again while the screen is
9894 // still frozen, the events will continue to be blocked while the
9895 // successive orientation change is processed. To prevent spurious
9896 // ANRs, we reset the event dispatch timeout in this case.
9897 synchronized (mKeyWaiter) {
9898 mKeyWaiter.mWasFrozen = true;
9899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 return;
9901 }
Romain Guy06882f82009-06-10 13:36:04 -07009902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 long now = SystemClock.uptimeMillis();
9906 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9907 if (mFreezeGcPending != 0) {
9908 if (now > (mFreezeGcPending+1000)) {
9909 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9910 mH.removeMessages(H.FORCE_GC);
9911 Runtime.getRuntime().gc();
9912 mFreezeGcPending = now;
9913 }
9914 } else {
9915 mFreezeGcPending = now;
9916 }
Romain Guy06882f82009-06-10 13:36:04 -07009917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009918 mDisplayFrozen = true;
9919 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9920 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9921 mAppTransitionReady = true;
9922 }
Romain Guy06882f82009-06-10 13:36:04 -07009923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 if (PROFILE_ORIENTATION) {
9925 File file = new File("/data/system/frozen");
9926 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9927 }
9928 Surface.freezeDisplay(0);
9929 }
Romain Guy06882f82009-06-10 13:36:04 -07009930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 private void stopFreezingDisplayLocked() {
9932 if (!mDisplayFrozen) {
9933 return;
9934 }
Romain Guy06882f82009-06-10 13:36:04 -07009935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 mDisplayFrozen = false;
9937 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9938 if (PROFILE_ORIENTATION) {
9939 Debug.stopMethodTracing();
9940 }
9941 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009942
Chris Tate2ad63a92009-03-25 17:36:48 -07009943 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9944 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 synchronized (mKeyWaiter) {
9946 mKeyWaiter.mWasFrozen = true;
9947 mKeyWaiter.notifyAll();
9948 }
9949
9950 // A little kludge: a lot could have happened while the
9951 // display was frozen, so now that we are coming back we
9952 // do a gc so that any remote references the system
9953 // processes holds on others can be released if they are
9954 // no longer needed.
9955 mH.removeMessages(H.FORCE_GC);
9956 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9957 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 mScreenFrozenLock.release();
9960 }
Romain Guy06882f82009-06-10 13:36:04 -07009961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 @Override
9963 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9964 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9965 != PackageManager.PERMISSION_GRANTED) {
9966 pw.println("Permission Denial: can't dump WindowManager from from pid="
9967 + Binder.getCallingPid()
9968 + ", uid=" + Binder.getCallingUid());
9969 return;
9970 }
Romain Guy06882f82009-06-10 13:36:04 -07009971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 synchronized(mWindowMap) {
9973 pw.println("Current Window Manager state:");
9974 for (int i=mWindows.size()-1; i>=0; i--) {
9975 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009976 pw.print(" Window #"); pw.print(i); pw.print(' ');
9977 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 w.dump(pw, " ");
9979 }
9980 if (mInputMethodDialogs.size() > 0) {
9981 pw.println(" ");
9982 pw.println(" Input method dialogs:");
9983 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9984 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009985 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009986 }
9987 }
9988 if (mPendingRemove.size() > 0) {
9989 pw.println(" ");
9990 pw.println(" Remove pending for:");
9991 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9992 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009993 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9994 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 w.dump(pw, " ");
9996 }
9997 }
9998 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9999 pw.println(" ");
10000 pw.println(" Windows force removing:");
10001 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10002 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010003 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10004 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 w.dump(pw, " ");
10006 }
10007 }
10008 if (mDestroySurface.size() > 0) {
10009 pw.println(" ");
10010 pw.println(" Windows waiting to destroy their surface:");
10011 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10012 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010013 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10014 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 w.dump(pw, " ");
10016 }
10017 }
10018 if (mLosingFocus.size() > 0) {
10019 pw.println(" ");
10020 pw.println(" Windows losing focus:");
10021 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10022 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010023 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10024 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 w.dump(pw, " ");
10026 }
10027 }
10028 if (mSessions.size() > 0) {
10029 pw.println(" ");
10030 pw.println(" All active sessions:");
10031 Iterator<Session> it = mSessions.iterator();
10032 while (it.hasNext()) {
10033 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010034 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 s.dump(pw, " ");
10036 }
10037 }
10038 if (mTokenMap.size() > 0) {
10039 pw.println(" ");
10040 pw.println(" All tokens:");
10041 Iterator<WindowToken> it = mTokenMap.values().iterator();
10042 while (it.hasNext()) {
10043 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010044 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 token.dump(pw, " ");
10046 }
10047 }
10048 if (mTokenList.size() > 0) {
10049 pw.println(" ");
10050 pw.println(" Window token list:");
10051 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010052 pw.print(" #"); pw.print(i); pw.print(": ");
10053 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 }
10055 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010056 if (mWallpaperTokens.size() > 0) {
10057 pw.println(" ");
10058 pw.println(" Wallpaper tokens:");
10059 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10060 WindowToken token = mWallpaperTokens.get(i);
10061 pw.print(" Wallpaper #"); pw.print(i);
10062 pw.print(' '); pw.print(token); pw.println(':');
10063 token.dump(pw, " ");
10064 }
10065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 if (mAppTokens.size() > 0) {
10067 pw.println(" ");
10068 pw.println(" Application tokens in Z order:");
10069 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010070 pw.print(" App #"); pw.print(i); pw.print(": ");
10071 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 }
10073 }
10074 if (mFinishedStarting.size() > 0) {
10075 pw.println(" ");
10076 pw.println(" Finishing start of application tokens:");
10077 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10078 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010079 pw.print(" Finished Starting #"); pw.print(i);
10080 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 token.dump(pw, " ");
10082 }
10083 }
10084 if (mExitingTokens.size() > 0) {
10085 pw.println(" ");
10086 pw.println(" Exiting tokens:");
10087 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10088 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010089 pw.print(" Exiting #"); pw.print(i);
10090 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010091 token.dump(pw, " ");
10092 }
10093 }
10094 if (mExitingAppTokens.size() > 0) {
10095 pw.println(" ");
10096 pw.println(" Exiting application tokens:");
10097 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10098 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010099 pw.print(" Exiting App #"); pw.print(i);
10100 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 token.dump(pw, " ");
10102 }
10103 }
10104 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010105 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10106 pw.print(" mLastFocus="); pw.println(mLastFocus);
10107 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10108 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10109 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010110 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010111 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10112 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10113 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10114 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010115 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10116 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10117 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10118 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10119 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010120 if (mDimAnimator != null) {
10121 mDimAnimator.printTo(pw);
10122 } else {
10123 pw.print( " no DimAnimator ");
10124 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010125 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010126 pw.print(mInputMethodAnimLayerAdjustment);
10127 pw.print(" mWallpaperAnimLayerAdjustment=");
10128 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010129 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10130 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010131 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10132 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10133 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10134 pw.print(" mRotation="); pw.print(mRotation);
10135 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10136 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10137 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10138 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10139 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10140 pw.print(" mNextAppTransition=0x");
10141 pw.print(Integer.toHexString(mNextAppTransition));
10142 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010143 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010144 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10145 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10146 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10147 if (mOpeningApps.size() > 0) {
10148 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10149 }
10150 if (mClosingApps.size() > 0) {
10151 pw.print(" mClosingApps="); pw.println(mClosingApps);
10152 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010153 if (mToTopApps.size() > 0) {
10154 pw.print(" mToTopApps="); pw.println(mToTopApps);
10155 }
10156 if (mToBottomApps.size() > 0) {
10157 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10158 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010159 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10160 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010162 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10163 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10164 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10165 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10166 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10167 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 }
10169 }
10170
10171 public void monitor() {
10172 synchronized (mWindowMap) { }
10173 synchronized (mKeyguardDisabled) { }
10174 synchronized (mKeyWaiter) { }
10175 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010176
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010177 public void virtualKeyFeedback(KeyEvent event) {
10178 mPolicy.keyFeedbackFromInput(event);
10179 }
10180
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010181 /**
10182 * DimAnimator class that controls the dim animation. This holds the surface and
10183 * all state used for dim animation.
10184 */
10185 private static class DimAnimator {
10186 Surface mDimSurface;
10187 boolean mDimShown = false;
10188 float mDimCurrentAlpha;
10189 float mDimTargetAlpha;
10190 float mDimDeltaPerMs;
10191 long mLastDimAnimTime;
10192
10193 DimAnimator (SurfaceSession session) {
10194 if (mDimSurface == null) {
10195 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10196 + mDimSurface + ": CREATE");
10197 try {
10198 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10199 Surface.FX_SURFACE_DIM);
10200 } catch (Exception e) {
10201 Log.e(TAG, "Exception creating Dim surface", e);
10202 }
10203 }
10204 }
10205
10206 /**
10207 * Show the dim surface.
10208 */
10209 void show(int dw, int dh) {
10210 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10211 dw + "x" + dh + ")");
10212 mDimShown = true;
10213 try {
10214 mDimSurface.setPosition(0, 0);
10215 mDimSurface.setSize(dw, dh);
10216 mDimSurface.show();
10217 } catch (RuntimeException e) {
10218 Log.w(TAG, "Failure showing dim surface", e);
10219 }
10220 }
10221
10222 /**
10223 * Set's the dim surface's layer and update dim parameters that will be used in
10224 * {@link updateSurface} after all windows are examined.
10225 */
10226 void updateParameters(WindowState w, long currentTime) {
10227 mDimSurface.setLayer(w.mAnimLayer-1);
10228
10229 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
10230 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
10231 if (mDimTargetAlpha != target) {
10232 // If the desired dim level has changed, then
10233 // start an animation to it.
10234 mLastDimAnimTime = currentTime;
10235 long duration = (w.mAnimating && w.mAnimation != null)
10236 ? w.mAnimation.computeDurationHint()
10237 : DEFAULT_DIM_DURATION;
10238 if (target > mDimTargetAlpha) {
10239 // This is happening behind the activity UI,
10240 // so we can make it run a little longer to
10241 // give a stronger impression without disrupting
10242 // the user.
10243 duration *= DIM_DURATION_MULTIPLIER;
10244 }
10245 if (duration < 1) {
10246 // Don't divide by zero
10247 duration = 1;
10248 }
10249 mDimTargetAlpha = target;
10250 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10251 }
10252 }
10253
10254 /**
10255 * Updating the surface's alpha. Returns true if the animation continues, or returns
10256 * false when the animation is finished and the dim surface is hidden.
10257 */
10258 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10259 if (!dimming) {
10260 if (mDimTargetAlpha != 0) {
10261 mLastDimAnimTime = currentTime;
10262 mDimTargetAlpha = 0;
10263 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10264 }
10265 }
10266
10267 boolean animating = false;
10268 if (mLastDimAnimTime != 0) {
10269 mDimCurrentAlpha += mDimDeltaPerMs
10270 * (currentTime-mLastDimAnimTime);
10271 boolean more = true;
10272 if (displayFrozen) {
10273 // If the display is frozen, there is no reason to animate.
10274 more = false;
10275 } else if (mDimDeltaPerMs > 0) {
10276 if (mDimCurrentAlpha > mDimTargetAlpha) {
10277 more = false;
10278 }
10279 } else if (mDimDeltaPerMs < 0) {
10280 if (mDimCurrentAlpha < mDimTargetAlpha) {
10281 more = false;
10282 }
10283 } else {
10284 more = false;
10285 }
10286
10287 // Do we need to continue animating?
10288 if (more) {
10289 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10290 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10291 mLastDimAnimTime = currentTime;
10292 mDimSurface.setAlpha(mDimCurrentAlpha);
10293 animating = true;
10294 } else {
10295 mDimCurrentAlpha = mDimTargetAlpha;
10296 mLastDimAnimTime = 0;
10297 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10298 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10299 mDimSurface.setAlpha(mDimCurrentAlpha);
10300 if (!dimming) {
10301 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10302 + ": HIDE");
10303 try {
10304 mDimSurface.hide();
10305 } catch (RuntimeException e) {
10306 Log.w(TAG, "Illegal argument exception hiding dim surface");
10307 }
10308 mDimShown = false;
10309 }
10310 }
10311 }
10312 return animating;
10313 }
10314
10315 public void printTo(PrintWriter pw) {
10316 pw.print(" mDimShown="); pw.print(mDimShown);
10317 pw.print(" current="); pw.print(mDimCurrentAlpha);
10318 pw.print(" target="); pw.print(mDimTargetAlpha);
10319 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10320 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10321 }
10322 }
10323
10324 /**
10325 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10326 * This is used for opening/closing transition for apps in compatible mode.
10327 */
10328 private static class FadeInOutAnimation extends Animation {
10329 int mWidth;
10330 boolean mFadeIn;
10331
10332 public FadeInOutAnimation(boolean fadeIn) {
10333 setInterpolator(new AccelerateInterpolator());
10334 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10335 mFadeIn = fadeIn;
10336 }
10337
10338 @Override
10339 protected void applyTransformation(float interpolatedTime, Transformation t) {
10340 float x = interpolatedTime;
10341 if (!mFadeIn) {
10342 x = 1.0f - x; // reverse the interpolation for fade out
10343 }
10344 if (x < 0.5) {
10345 // move the window out of the screen.
10346 t.getMatrix().setTranslate(mWidth, 0);
10347 } else {
10348 t.getMatrix().setTranslate(0, 0);// show
10349 t.setAlpha((x - 0.5f) * 2);
10350 }
10351 }
10352
10353 @Override
10354 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10355 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10356 mWidth = width;
10357 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010358
10359 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010360 public int getZAdjustment() {
10361 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010362 }
10363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364}