blob: 1ea1b3193c2fae418493a8013621710bab477339 [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 Hackborn4c62fc02009-08-08 20:40:27 -07001196 boolean adjustWallpaperWindowsLocked() {
1197 boolean changed = false;
1198
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001199 final int dw = mDisplay.getWidth();
1200 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001201
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001202 // First find top-most window that has asked to be on top of the
1203 // wallpaper; all wallpapers go behind it.
1204 final ArrayList localmWindows = mWindows;
1205 int N = localmWindows.size();
1206 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001207 WindowState foundW = null;
1208 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001209 int i = N;
1210 while (i > 0) {
1211 i--;
1212 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001213 if (w.mAppToken != null) {
1214 // If this window's app token is hidden and not animating,
1215 // it is of no interest to us.
1216 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1217 if (DEBUG_WALLPAPER) Log.v(TAG,
1218 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001219 continue;
1220 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001221 // If this window's app token is ot fullscreen, also irrelevant.
1222 if (!w.mAppToken.appFullscreen) {
1223 if (DEBUG_WALLPAPER) Log.v(TAG,
1224 "Skipping non-fullscreen token: " + w);
1225 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001226 }
1227 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001228 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1229 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1230 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001231 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001232 && (mWallpaperTarget == w
1233 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001234 if (DEBUG_WALLPAPER) Log.v(TAG,
1235 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001236 foundW = w;
1237 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001238 if (w == mWallpaperTarget && w.mAppToken != null
1239 && w.mAppToken.animation != null) {
1240 // The current wallpaper target is animating, so we'll
1241 // look behind it for another possible target and figure
1242 // out what is going on below.
1243 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1244 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001245 continue;
1246 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001247 break;
1248 }
1249 }
1250
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001251 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1252 // If we are currently waiting for an app transition, and either
1253 // the current target or the next target are involved with it,
1254 // then hold off on doing anything with the wallpaper.
1255 // Note that we are checking here for just whether the target
1256 // is part of an app token... which is potentially overly aggressive
1257 // (the app token may not be involved in the transition), but good
1258 // enough (we'll just wait until whatever transition is pending
1259 // executes).
1260 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001261 if (DEBUG_WALLPAPER) Log.v(TAG,
1262 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001263 return false;
1264 }
1265 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001266 if (DEBUG_WALLPAPER) Log.v(TAG,
1267 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001268 return false;
1269 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001270 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001271
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001272 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001273 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001274 Log.v(TAG, "New wallpaper target: " + foundW
1275 + " oldTarget: " + mWallpaperTarget);
1276 }
1277
1278 mLowerWallpaperTarget = null;
1279 mUpperWallpaperTarget = null;
1280
1281 WindowState oldW = mWallpaperTarget;
1282 mWallpaperTarget = foundW;
1283
1284 // Now what is happening... if the current and new targets are
1285 // animating, then we are in our super special mode!
1286 if (foundW != null && foundW.mAppToken != null && oldW != null
1287 && oldW.mAppToken != null) {
1288 if (DEBUG_WALLPAPER) {
1289 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1290 + " old animation: " + oldW.mAppToken.animation);
1291 }
1292 if (foundW.mAppToken.animation != null
1293 && oldW.mAppToken.animation != null) {
1294 int oldI = localmWindows.indexOf(oldW);
1295 if (DEBUG_WALLPAPER) {
1296 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1297 }
1298 if (oldI >= 0) {
1299 if (DEBUG_WALLPAPER) {
1300 Log.v(TAG, "Animating wallpapers: old#" + oldI
1301 + "=" + oldW + "; new#" + foundI
1302 + "=" + foundW);
1303 }
1304
1305 // Set the new target correctly.
1306 if (foundW.mAppToken.hiddenRequested) {
1307 if (DEBUG_WALLPAPER) {
1308 Log.v(TAG, "Old wallpaper still the target.");
1309 }
1310 mWallpaperTarget = oldW;
1311 }
1312
1313 // Now set the upper and lower wallpaper targets
1314 // correctly, and make sure that we are positioning
1315 // the wallpaper below the lower.
1316 if (foundI > oldI) {
1317 // The new target is on top of the old one.
1318 if (DEBUG_WALLPAPER) {
1319 Log.v(TAG, "Found target above old target.");
1320 }
1321 mUpperWallpaperTarget = foundW;
1322 mLowerWallpaperTarget = oldW;
1323 foundW = oldW;
1324 foundI = oldI;
1325 } else {
1326 // The new target is below the old one.
1327 if (DEBUG_WALLPAPER) {
1328 Log.v(TAG, "Found target below old target.");
1329 }
1330 mUpperWallpaperTarget = oldW;
1331 mLowerWallpaperTarget = foundW;
1332 }
1333 }
1334 }
1335 }
1336
1337 } else {
1338 // Is it time to stop animating?
1339 if (mLowerWallpaperTarget == null
1340 || mLowerWallpaperTarget.mAppToken.animation == null
1341 || mUpperWallpaperTarget == null
1342 || mUpperWallpaperTarget.mAppToken.animation == null) {
1343 if (DEBUG_WALLPAPER) {
1344 Log.v(TAG, "No longer animating wallpaper targets!");
1345 }
1346 mLowerWallpaperTarget = null;
1347 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001348 }
1349 }
1350
1351 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001352 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001353 // The window is visible to the compositor... but is it visible
1354 // to the user? That is what the wallpaper cares about.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001355 visible = !foundW.mObscured;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001356 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001357
1358 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001359 // its layer adjustment. Only do this if we are not transfering
1360 // between two wallpaper targets.
1361 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001362 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001363 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001364
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001365 // Now w is the window we are supposed to be behind... but we
1366 // need to be sure to also be behind any of its attached windows,
1367 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001368 while (foundI > 0) {
1369 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1370 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001371 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001372 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001373 // This window is not related to the previous one in any
1374 // interesting way, so stop here.
1375 break;
1376 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001377 foundW = wb;
1378 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001379 }
1380 }
1381
1382 // Okay i is the position immediately above the wallpaper. Look at
1383 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001384 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001385
Dianne Hackborn284ac932009-08-28 10:34:25 -07001386 if (visible) {
1387 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1388 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1389 }
1390
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001391 // Start stepping backwards from here, ensuring that our wallpaper windows
1392 // are correctly placed.
1393 int curTokenIndex = mWallpaperTokens.size();
1394 while (curTokenIndex > 0) {
1395 curTokenIndex--;
1396 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1397 int curWallpaperIndex = token.windows.size();
1398 while (curWallpaperIndex > 0) {
1399 curWallpaperIndex--;
1400 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001401
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001402 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001403 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001404 }
1405
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001406 // First, make sure the client has the current visibility
1407 // state.
1408 if (wallpaper.mWallpaperVisible != visible) {
1409 wallpaper.mWallpaperVisible = visible;
1410 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001411 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001412 "Setting visibility of wallpaper " + wallpaper
1413 + ": " + visible);
1414 wallpaper.mClient.dispatchAppVisibility(visible);
1415 } catch (RemoteException e) {
1416 }
1417 }
1418
1419 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001420 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1421 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001422
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001423 // First, if this window is at the current index, then all
1424 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001425 if (wallpaper == foundW) {
1426 foundI--;
1427 foundW = foundI > 0
1428 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001429 continue;
1430 }
1431
1432 // The window didn't match... the current wallpaper window,
1433 // wherever it is, is in the wrong place, so make sure it is
1434 // not in the list.
1435 int oldIndex = localmWindows.indexOf(wallpaper);
1436 if (oldIndex >= 0) {
1437 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001438 if (oldIndex < foundI) {
1439 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001440 }
1441 }
1442
1443 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001444 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001445 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001446
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001447 localmWindows.add(foundI, wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001448 changed = true;
1449 }
1450 }
1451
1452 return changed;
1453 }
1454
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001455 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001456 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1457 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001458 mWallpaperAnimLayerAdjustment = adj;
1459 int curTokenIndex = mWallpaperTokens.size();
1460 while (curTokenIndex > 0) {
1461 curTokenIndex--;
1462 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1463 int curWallpaperIndex = token.windows.size();
1464 while (curWallpaperIndex > 0) {
1465 curWallpaperIndex--;
1466 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1467 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001468 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1469 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001470 }
1471 }
1472 }
1473
Dianne Hackborn284ac932009-08-28 10:34:25 -07001474 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001475 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001476 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001477 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001478 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001479 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001480 changed = wallpaperWin.mXOffset != offset;
1481 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001482 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1483 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001484 wallpaperWin.mXOffset = offset;
1485 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001486 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1487 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001488 rawChanged = true;
1489 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001490 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001491
Dianne Hackborn284ac932009-08-28 10:34:25 -07001492 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001493 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001494 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001495 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001496 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1497 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001498 changed = true;
1499 wallpaperWin.mYOffset = offset;
1500 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001501 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1502 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001503 rawChanged = true;
1504 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001505 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001506
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001507 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001508 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001509 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1510 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1511 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001512 wallpaperWin.mClient.dispatchWallpaperOffsets(
1513 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1514 } catch (RemoteException e) {
1515 }
1516 }
1517
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001518 return changed;
1519 }
1520
1521 boolean updateWallpaperOffsetLocked() {
1522 final int dw = mDisplay.getWidth();
1523 final int dh = mDisplay.getHeight();
1524
1525 boolean changed = false;
1526
1527 WindowState target = mWallpaperTarget;
1528 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001529 mLastWallpaperX = target.mWallpaperX;
1530 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001531 int curTokenIndex = mWallpaperTokens.size();
1532 while (curTokenIndex > 0) {
1533 curTokenIndex--;
1534 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1535 int curWallpaperIndex = token.windows.size();
1536 while (curWallpaperIndex > 0) {
1537 curWallpaperIndex--;
1538 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn284ac932009-08-28 10:34:25 -07001539 if (updateWallpaperOffsetLocked(wallpaper, dw, dh)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001540 wallpaper.computeShownFrameLocked();
1541 changed = true;
1542 }
1543 }
1544 }
1545 }
1546
1547 return changed;
1548 }
1549
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001550 void updateWallpaperVisibilityLocked() {
1551 final boolean visible = mWallpaperTarget != null
1552 && !mWallpaperTarget.mObscured;
1553 final int dw = mDisplay.getWidth();
1554 final int dh = mDisplay.getHeight();
1555
1556 int curTokenIndex = mWallpaperTokens.size();
1557 while (curTokenIndex > 0) {
1558 curTokenIndex--;
1559 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1560 int curWallpaperIndex = token.windows.size();
1561 while (curWallpaperIndex > 0) {
1562 curWallpaperIndex--;
1563 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1564 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001565 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001566 }
1567
1568 if (wallpaper.mWallpaperVisible != visible) {
1569 wallpaper.mWallpaperVisible = visible;
1570 try {
1571 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
1572 "Setting visibility of wallpaper " + wallpaper
1573 + ": " + visible);
1574 wallpaper.mClient.dispatchAppVisibility(visible);
1575 } catch (RemoteException e) {
1576 }
1577 }
1578 }
1579 }
1580 }
1581
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001582 void sendPointerToWallpaperLocked(WindowState srcWin,
1583 MotionEvent pointer, long eventTime) {
1584 int curTokenIndex = mWallpaperTokens.size();
1585 while (curTokenIndex > 0) {
1586 curTokenIndex--;
1587 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1588 int curWallpaperIndex = token.windows.size();
1589 while (curWallpaperIndex > 0) {
1590 curWallpaperIndex--;
1591 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1592 if ((wallpaper.mAttrs.flags &
1593 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1594 continue;
1595 }
1596 try {
1597 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1598 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1599 srcWin.mFrame.top-wallpaper.mFrame.top);
1600 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1601 } catch (RemoteException e) {
1602 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1603 }
1604 }
1605 }
1606 }
1607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 public int addWindow(Session session, IWindow client,
1609 WindowManager.LayoutParams attrs, int viewVisibility,
1610 Rect outContentInsets) {
1611 int res = mPolicy.checkAddPermission(attrs);
1612 if (res != WindowManagerImpl.ADD_OKAY) {
1613 return res;
1614 }
Romain Guy06882f82009-06-10 13:36:04 -07001615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 boolean reportNewConfig = false;
1617 WindowState attachedWindow = null;
1618 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 synchronized(mWindowMap) {
1621 // Instantiating a Display requires talking with the simulator,
1622 // so don't do it until we know the system is mostly up and
1623 // running.
1624 if (mDisplay == null) {
1625 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1626 mDisplay = wm.getDefaultDisplay();
1627 mQueue.setDisplay(mDisplay);
1628 reportNewConfig = true;
1629 }
Romain Guy06882f82009-06-10 13:36:04 -07001630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 if (mWindowMap.containsKey(client.asBinder())) {
1632 Log.w(TAG, "Window " + client + " is already added");
1633 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1634 }
1635
1636 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001637 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 if (attachedWindow == null) {
1639 Log.w(TAG, "Attempted to add window with token that is not a window: "
1640 + attrs.token + ". Aborting.");
1641 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1642 }
1643 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1644 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1645 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1646 + attrs.token + ". Aborting.");
1647 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1648 }
1649 }
1650
1651 boolean addToken = false;
1652 WindowToken token = mTokenMap.get(attrs.token);
1653 if (token == null) {
1654 if (attrs.type >= FIRST_APPLICATION_WINDOW
1655 && attrs.type <= LAST_APPLICATION_WINDOW) {
1656 Log.w(TAG, "Attempted to add application window with unknown token "
1657 + attrs.token + ". Aborting.");
1658 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1659 }
1660 if (attrs.type == TYPE_INPUT_METHOD) {
1661 Log.w(TAG, "Attempted to add input method window with unknown token "
1662 + attrs.token + ". Aborting.");
1663 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1664 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001665 if (attrs.type == TYPE_WALLPAPER) {
1666 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1667 + attrs.token + ". Aborting.");
1668 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 token = new WindowToken(attrs.token, -1, false);
1671 addToken = true;
1672 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1673 && attrs.type <= LAST_APPLICATION_WINDOW) {
1674 AppWindowToken atoken = token.appWindowToken;
1675 if (atoken == null) {
1676 Log.w(TAG, "Attempted to add window with non-application token "
1677 + token + ". Aborting.");
1678 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1679 } else if (atoken.removed) {
1680 Log.w(TAG, "Attempted to add window with exiting application token "
1681 + token + ". Aborting.");
1682 return WindowManagerImpl.ADD_APP_EXITING;
1683 }
1684 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1685 // No need for this guy!
1686 if (localLOGV) Log.v(
1687 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1688 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1689 }
1690 } else if (attrs.type == TYPE_INPUT_METHOD) {
1691 if (token.windowType != TYPE_INPUT_METHOD) {
1692 Log.w(TAG, "Attempted to add input method window with bad token "
1693 + attrs.token + ". Aborting.");
1694 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1695 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001696 } else if (attrs.type == TYPE_WALLPAPER) {
1697 if (token.windowType != TYPE_WALLPAPER) {
1698 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1699 + attrs.token + ". Aborting.");
1700 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703
1704 win = new WindowState(session, client, token,
1705 attachedWindow, attrs, viewVisibility);
1706 if (win.mDeathRecipient == null) {
1707 // Client has apparently died, so there is no reason to
1708 // continue.
1709 Log.w(TAG, "Adding window client " + client.asBinder()
1710 + " that is dead, aborting.");
1711 return WindowManagerImpl.ADD_APP_EXITING;
1712 }
1713
1714 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 res = mPolicy.prepareAddWindowLw(win, attrs);
1717 if (res != WindowManagerImpl.ADD_OKAY) {
1718 return res;
1719 }
1720
1721 // From now on, no exceptions or errors allowed!
1722
1723 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 if (addToken) {
1728 mTokenMap.put(attrs.token, token);
1729 mTokenList.add(token);
1730 }
1731 win.attach();
1732 mWindowMap.put(client.asBinder(), win);
1733
1734 if (attrs.type == TYPE_APPLICATION_STARTING &&
1735 token.appWindowToken != null) {
1736 token.appWindowToken.startingWindow = win;
1737 }
1738
1739 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 if (attrs.type == TYPE_INPUT_METHOD) {
1742 mInputMethodWindow = win;
1743 addInputMethodWindowToListLocked(win);
1744 imMayMove = false;
1745 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1746 mInputMethodDialogs.add(win);
1747 addWindowToListInOrderLocked(win, true);
1748 adjustInputMethodDialogsLocked();
1749 imMayMove = false;
1750 } else {
1751 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001752 if (attrs.type == TYPE_WALLPAPER ||
1753 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1754 adjustWallpaperWindowsLocked();
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
Romain Guy06882f82009-06-10 13:36:04 -07001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 if (mInTouchMode) {
1763 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1764 }
1765 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1766 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1767 }
Romain Guy06882f82009-06-10 13:36:04 -07001768
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001769 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001771 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1772 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 imMayMove = false;
1774 }
1775 }
Romain Guy06882f82009-06-10 13:36:04 -07001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001778 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
Romain Guy06882f82009-06-10 13:36:04 -07001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 assignLayersLocked();
1782 // Don't do layout here, the window must call
1783 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 //dump();
1786
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001787 if (focusChanged) {
1788 if (mCurrentFocus != null) {
1789 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1790 }
1791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 if (localLOGV) Log.v(
1793 TAG, "New client " + client.asBinder()
1794 + ": window=" + win);
1795 }
1796
1797 // sendNewConfiguration() checks caller permissions so we must call it with
1798 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1799 // identity anyway, so it's safe to just clear & restore around this whole
1800 // block.
1801 final long origId = Binder.clearCallingIdentity();
1802 if (reportNewConfig) {
1803 sendNewConfiguration();
1804 } else {
1805 // Update Orientation after adding a window, only if the window needs to be
1806 // displayed right away
1807 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001808 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 sendNewConfiguration();
1810 }
1811 }
1812 }
1813 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 return res;
1816 }
Romain Guy06882f82009-06-10 13:36:04 -07001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 public void removeWindow(Session session, IWindow client) {
1819 synchronized(mWindowMap) {
1820 WindowState win = windowForClientLocked(session, client);
1821 if (win == null) {
1822 return;
1823 }
1824 removeWindowLocked(session, win);
1825 }
1826 }
Romain Guy06882f82009-06-10 13:36:04 -07001827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 public void removeWindowLocked(Session session, WindowState win) {
1829
1830 if (localLOGV || DEBUG_FOCUS) Log.v(
1831 TAG, "Remove " + win + " client="
1832 + Integer.toHexString(System.identityHashCode(
1833 win.mClient.asBinder()))
1834 + ", surface=" + win.mSurface);
1835
1836 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 if (DEBUG_APP_TRANSITIONS) Log.v(
1839 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1840 + " mExiting=" + win.mExiting
1841 + " isAnimating=" + win.isAnimating()
1842 + " app-animation="
1843 + (win.mAppToken != null ? win.mAppToken.animation : null)
1844 + " inPendingTransaction="
1845 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1846 + " mDisplayFrozen=" + mDisplayFrozen);
1847 // Visibility of the removed window. Will be used later to update orientation later on.
1848 boolean wasVisible = false;
1849 // First, see if we need to run an animation. If we do, we have
1850 // to hold off on removing the window until the animation is done.
1851 // If the display is frozen, just remove immediately, since the
1852 // animation wouldn't be seen.
1853 if (win.mSurface != null && !mDisplayFrozen) {
1854 // If we are not currently running the exit animation, we
1855 // need to see about starting one.
1856 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1859 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1860 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1861 }
1862 // Try starting an animation.
1863 if (applyAnimationLocked(win, transit, false)) {
1864 win.mExiting = true;
1865 }
1866 }
1867 if (win.mExiting || win.isAnimating()) {
1868 // The exit animation is running... wait for it!
1869 //Log.i(TAG, "*** Running exit animation...");
1870 win.mExiting = true;
1871 win.mRemoveOnExit = true;
1872 mLayoutNeeded = true;
1873 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1874 performLayoutAndPlaceSurfacesLocked();
1875 if (win.mAppToken != null) {
1876 win.mAppToken.updateReportedVisibilityLocked();
1877 }
1878 //dump();
1879 Binder.restoreCallingIdentity(origId);
1880 return;
1881 }
1882 }
1883
1884 removeWindowInnerLocked(session, win);
1885 // Removing a visible window will effect the computed orientation
1886 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001887 if (wasVisible && computeForcedAppOrientationLocked()
1888 != mForcedAppOrientation) {
1889 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1892 Binder.restoreCallingIdentity(origId);
1893 }
Romain Guy06882f82009-06-10 13:36:04 -07001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001896 mKeyWaiter.finishedKey(session, win.mClient, true,
1897 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1899 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 if (mInputMethodTarget == win) {
1904 moveInputMethodWindowsIfNeededLocked(false);
1905 }
Romain Guy06882f82009-06-10 13:36:04 -07001906
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001907 if (false) {
1908 RuntimeException e = new RuntimeException("here");
1909 e.fillInStackTrace();
1910 Log.w(TAG, "Removing window " + win, e);
1911 }
1912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 mPolicy.removeWindowLw(win);
1914 win.removeLocked();
1915
1916 mWindowMap.remove(win.mClient.asBinder());
1917 mWindows.remove(win);
1918
1919 if (mInputMethodWindow == win) {
1920 mInputMethodWindow = null;
1921 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1922 mInputMethodDialogs.remove(win);
1923 }
Romain Guy06882f82009-06-10 13:36:04 -07001924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 final WindowToken token = win.mToken;
1926 final AppWindowToken atoken = win.mAppToken;
1927 token.windows.remove(win);
1928 if (atoken != null) {
1929 atoken.allAppWindows.remove(win);
1930 }
1931 if (localLOGV) Log.v(
1932 TAG, "**** Removing window " + win + ": count="
1933 + token.windows.size());
1934 if (token.windows.size() == 0) {
1935 if (!token.explicit) {
1936 mTokenMap.remove(token.token);
1937 mTokenList.remove(token);
1938 } else if (atoken != null) {
1939 atoken.firstWindowDrawn = false;
1940 }
1941 }
1942
1943 if (atoken != null) {
1944 if (atoken.startingWindow == win) {
1945 atoken.startingWindow = null;
1946 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1947 // If this is the last window and we had requested a starting
1948 // transition window, well there is no point now.
1949 atoken.startingData = null;
1950 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1951 // If this is the last window except for a starting transition
1952 // window, we need to get rid of the starting transition.
1953 if (DEBUG_STARTING_WINDOW) {
1954 Log.v(TAG, "Schedule remove starting " + token
1955 + ": no more real windows");
1956 }
1957 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1958 mH.sendMessage(m);
1959 }
1960 }
Romain Guy06882f82009-06-10 13:36:04 -07001961
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001962 if (win.mAttrs.type == TYPE_WALLPAPER ||
1963 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1964 adjustWallpaperWindowsLocked();
1965 }
1966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 if (!mInLayout) {
1968 assignLayersLocked();
1969 mLayoutNeeded = true;
1970 performLayoutAndPlaceSurfacesLocked();
1971 if (win.mAppToken != null) {
1972 win.mAppToken.updateReportedVisibilityLocked();
1973 }
1974 }
1975 }
1976
1977 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1978 long origId = Binder.clearCallingIdentity();
1979 try {
1980 synchronized (mWindowMap) {
1981 WindowState w = windowForClientLocked(session, client);
1982 if ((w != null) && (w.mSurface != null)) {
1983 Surface.openTransaction();
1984 try {
1985 w.mSurface.setTransparentRegionHint(region);
1986 } finally {
1987 Surface.closeTransaction();
1988 }
1989 }
1990 }
1991 } finally {
1992 Binder.restoreCallingIdentity(origId);
1993 }
1994 }
1995
1996 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001997 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 Rect visibleInsets) {
1999 long origId = Binder.clearCallingIdentity();
2000 try {
2001 synchronized (mWindowMap) {
2002 WindowState w = windowForClientLocked(session, client);
2003 if (w != null) {
2004 w.mGivenInsetsPending = false;
2005 w.mGivenContentInsets.set(contentInsets);
2006 w.mGivenVisibleInsets.set(visibleInsets);
2007 w.mTouchableInsets = touchableInsets;
2008 mLayoutNeeded = true;
2009 performLayoutAndPlaceSurfacesLocked();
2010 }
2011 }
2012 } finally {
2013 Binder.restoreCallingIdentity(origId);
2014 }
2015 }
Romain Guy06882f82009-06-10 13:36:04 -07002016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 public void getWindowDisplayFrame(Session session, IWindow client,
2018 Rect outDisplayFrame) {
2019 synchronized(mWindowMap) {
2020 WindowState win = windowForClientLocked(session, client);
2021 if (win == null) {
2022 outDisplayFrame.setEmpty();
2023 return;
2024 }
2025 outDisplayFrame.set(win.mDisplayFrame);
2026 }
2027 }
2028
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002029 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2030 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2031 window.mWallpaperX = x;
2032 window.mWallpaperY = y;
2033
2034 if (mWallpaperTarget == window) {
2035 if (updateWallpaperOffsetLocked()) {
2036 performLayoutAndPlaceSurfacesLocked();
2037 }
2038 }
2039 }
2040 }
2041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 public int relayoutWindow(Session session, IWindow client,
2043 WindowManager.LayoutParams attrs, int requestedWidth,
2044 int requestedHeight, int viewVisibility, boolean insetsPending,
2045 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2046 Surface outSurface) {
2047 boolean displayed = false;
2048 boolean inTouchMode;
2049 Configuration newConfig = null;
2050 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 synchronized(mWindowMap) {
2053 WindowState win = windowForClientLocked(session, client);
2054 if (win == null) {
2055 return 0;
2056 }
2057 win.mRequestedWidth = requestedWidth;
2058 win.mRequestedHeight = requestedHeight;
2059
2060 if (attrs != null) {
2061 mPolicy.adjustWindowParamsLw(attrs);
2062 }
Romain Guy06882f82009-06-10 13:36:04 -07002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 int attrChanges = 0;
2065 int flagChanges = 0;
2066 if (attrs != null) {
2067 flagChanges = win.mAttrs.flags ^= attrs.flags;
2068 attrChanges = win.mAttrs.copyFrom(attrs);
2069 }
2070
2071 if (localLOGV) Log.v(
2072 TAG, "Relayout given client " + client.asBinder()
2073 + " (" + win.mAttrs.getTitle() + ")");
2074
2075
2076 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2077 win.mAlpha = attrs.alpha;
2078 }
2079
2080 final boolean scaledWindow =
2081 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2082
2083 if (scaledWindow) {
2084 // requested{Width|Height} Surface's physical size
2085 // attrs.{width|height} Size on screen
2086 win.mHScale = (attrs.width != requestedWidth) ?
2087 (attrs.width / (float)requestedWidth) : 1.0f;
2088 win.mVScale = (attrs.height != requestedHeight) ?
2089 (attrs.height / (float)requestedHeight) : 1.0f;
2090 }
2091
2092 boolean imMayMove = (flagChanges&(
2093 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2094 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 boolean focusMayChange = win.mViewVisibility != viewVisibility
2097 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2098 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002099
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002100 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2101 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 win.mRelayoutCalled = true;
2104 final int oldVisibility = win.mViewVisibility;
2105 win.mViewVisibility = viewVisibility;
2106 if (viewVisibility == View.VISIBLE &&
2107 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2108 displayed = !win.isVisibleLw();
2109 if (win.mExiting) {
2110 win.mExiting = false;
2111 win.mAnimation = null;
2112 }
2113 if (win.mDestroying) {
2114 win.mDestroying = false;
2115 mDestroySurface.remove(win);
2116 }
2117 if (oldVisibility == View.GONE) {
2118 win.mEnterAnimationPending = true;
2119 }
2120 if (displayed && win.mSurface != null && !win.mDrawPending
2121 && !win.mCommitDrawPending && !mDisplayFrozen) {
2122 applyEnterAnimationLocked(win);
2123 }
2124 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2125 // To change the format, we need to re-build the surface.
2126 win.destroySurfaceLocked();
2127 displayed = true;
2128 }
2129 try {
2130 Surface surface = win.createSurfaceLocked();
2131 if (surface != null) {
2132 outSurface.copyFrom(surface);
2133 } else {
2134 outSurface.clear();
2135 }
2136 } catch (Exception e) {
2137 Log.w(TAG, "Exception thrown when creating surface for client "
2138 + client + " (" + win.mAttrs.getTitle() + ")",
2139 e);
2140 Binder.restoreCallingIdentity(origId);
2141 return 0;
2142 }
2143 if (displayed) {
2144 focusMayChange = true;
2145 }
2146 if (win.mAttrs.type == TYPE_INPUT_METHOD
2147 && mInputMethodWindow == null) {
2148 mInputMethodWindow = win;
2149 imMayMove = true;
2150 }
2151 } else {
2152 win.mEnterAnimationPending = false;
2153 if (win.mSurface != null) {
2154 // If we are not currently running the exit animation, we
2155 // need to see about starting one.
2156 if (!win.mExiting) {
2157 // Try starting an animation; if there isn't one, we
2158 // can destroy the surface right away.
2159 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2160 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2161 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2162 }
2163 if (win.isWinVisibleLw() &&
2164 applyAnimationLocked(win, transit, false)) {
2165 win.mExiting = true;
2166 mKeyWaiter.finishedKey(session, client, true,
2167 KeyWaiter.RETURN_NOTHING);
2168 } else if (win.isAnimating()) {
2169 // Currently in a hide animation... turn this into
2170 // an exit.
2171 win.mExiting = true;
2172 } else {
2173 if (mInputMethodWindow == win) {
2174 mInputMethodWindow = null;
2175 }
2176 win.destroySurfaceLocked();
2177 }
2178 }
2179 }
2180 outSurface.clear();
2181 }
2182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 if (focusMayChange) {
2184 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2185 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 imMayMove = false;
2187 }
2188 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2189 }
Romain Guy06882f82009-06-10 13:36:04 -07002190
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002191 // updateFocusedWindowLocked() already assigned layers so we only need to
2192 // reassign them at this point if the IM window state gets shuffled
2193 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 if (imMayMove) {
2196 if (moveInputMethodWindowsIfNeededLocked(false)) {
2197 assignLayers = true;
2198 }
2199 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002200 if (wallpaperMayMove) {
2201 if (adjustWallpaperWindowsLocked()) {
2202 assignLayers = true;
2203 }
2204 }
Romain Guy06882f82009-06-10 13:36:04 -07002205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 mLayoutNeeded = true;
2207 win.mGivenInsetsPending = insetsPending;
2208 if (assignLayers) {
2209 assignLayersLocked();
2210 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002211 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002213 if (displayed && win.mIsWallpaper) {
2214 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
2215 mDisplay.getHeight());
2216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 if (win.mAppToken != null) {
2218 win.mAppToken.updateReportedVisibilityLocked();
2219 }
2220 outFrame.set(win.mFrame);
2221 outContentInsets.set(win.mContentInsets);
2222 outVisibleInsets.set(win.mVisibleInsets);
2223 if (localLOGV) Log.v(
2224 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002225 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 + ", requestedHeight=" + requestedHeight
2227 + ", viewVisibility=" + viewVisibility
2228 + "\nRelayout returning frame=" + outFrame
2229 + ", surface=" + outSurface);
2230
2231 if (localLOGV || DEBUG_FOCUS) Log.v(
2232 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2233
2234 inTouchMode = mInTouchMode;
2235 }
2236
2237 if (newConfig != null) {
2238 sendNewConfiguration();
2239 }
Romain Guy06882f82009-06-10 13:36:04 -07002240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2244 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2245 }
2246
2247 public void finishDrawingWindow(Session session, IWindow client) {
2248 final long origId = Binder.clearCallingIdentity();
2249 synchronized(mWindowMap) {
2250 WindowState win = windowForClientLocked(session, client);
2251 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002252 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2253 adjustWallpaperWindowsLocked();
2254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 mLayoutNeeded = true;
2256 performLayoutAndPlaceSurfacesLocked();
2257 }
2258 }
2259 Binder.restoreCallingIdentity(origId);
2260 }
2261
2262 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2263 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2264 + (lp != null ? lp.packageName : null)
2265 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2266 if (lp != null && lp.windowAnimations != 0) {
2267 // If this is a system resource, don't try to load it from the
2268 // application resources. It is nice to avoid loading application
2269 // resources if we can.
2270 String packageName = lp.packageName != null ? lp.packageName : "android";
2271 int resId = lp.windowAnimations;
2272 if ((resId&0xFF000000) == 0x01000000) {
2273 packageName = "android";
2274 }
2275 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2276 + packageName);
2277 return AttributeCache.instance().get(packageName, resId,
2278 com.android.internal.R.styleable.WindowAnimation);
2279 }
2280 return null;
2281 }
Romain Guy06882f82009-06-10 13:36:04 -07002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 private void applyEnterAnimationLocked(WindowState win) {
2284 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2285 if (win.mEnterAnimationPending) {
2286 win.mEnterAnimationPending = false;
2287 transit = WindowManagerPolicy.TRANSIT_ENTER;
2288 }
2289
2290 applyAnimationLocked(win, transit, true);
2291 }
2292
2293 private boolean applyAnimationLocked(WindowState win,
2294 int transit, boolean isEntrance) {
2295 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2296 // If we are trying to apply an animation, but already running
2297 // an animation of the same type, then just leave that one alone.
2298 return true;
2299 }
Romain Guy06882f82009-06-10 13:36:04 -07002300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 // Only apply an animation if the display isn't frozen. If it is
2302 // frozen, there is no reason to animate and it can cause strange
2303 // artifacts when we unfreeze the display if some different animation
2304 // is running.
2305 if (!mDisplayFrozen) {
2306 int anim = mPolicy.selectAnimationLw(win, transit);
2307 int attr = -1;
2308 Animation a = null;
2309 if (anim != 0) {
2310 a = AnimationUtils.loadAnimation(mContext, anim);
2311 } else {
2312 switch (transit) {
2313 case WindowManagerPolicy.TRANSIT_ENTER:
2314 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2315 break;
2316 case WindowManagerPolicy.TRANSIT_EXIT:
2317 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2318 break;
2319 case WindowManagerPolicy.TRANSIT_SHOW:
2320 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2321 break;
2322 case WindowManagerPolicy.TRANSIT_HIDE:
2323 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2324 break;
2325 }
2326 if (attr >= 0) {
2327 a = loadAnimation(win.mAttrs, attr);
2328 }
2329 }
2330 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2331 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2332 + " mAnimation=" + win.mAnimation
2333 + " isEntrance=" + isEntrance);
2334 if (a != null) {
2335 if (DEBUG_ANIM) {
2336 RuntimeException e = new RuntimeException();
2337 e.fillInStackTrace();
2338 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2339 }
2340 win.setAnimation(a);
2341 win.mAnimationIsEntrance = isEntrance;
2342 }
2343 } else {
2344 win.clearAnimation();
2345 }
2346
2347 return win.mAnimation != null;
2348 }
2349
2350 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2351 int anim = 0;
2352 Context context = mContext;
2353 if (animAttr >= 0) {
2354 AttributeCache.Entry ent = getCachedAnimations(lp);
2355 if (ent != null) {
2356 context = ent.context;
2357 anim = ent.array.getResourceId(animAttr, 0);
2358 }
2359 }
2360 if (anim != 0) {
2361 return AnimationUtils.loadAnimation(context, anim);
2362 }
2363 return null;
2364 }
Romain Guy06882f82009-06-10 13:36:04 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 private boolean applyAnimationLocked(AppWindowToken wtoken,
2367 WindowManager.LayoutParams lp, int transit, boolean enter) {
2368 // Only apply an animation if the display isn't frozen. If it is
2369 // frozen, there is no reason to animate and it can cause strange
2370 // artifacts when we unfreeze the display if some different animation
2371 // is running.
2372 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002373 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002374 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002375 a = new FadeInOutAnimation(enter);
2376 if (DEBUG_ANIM) Log.v(TAG,
2377 "applying FadeInOutAnimation for a window in compatibility mode");
2378 } else {
2379 int animAttr = 0;
2380 switch (transit) {
2381 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2382 animAttr = enter
2383 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2384 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2385 break;
2386 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2387 animAttr = enter
2388 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2389 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2390 break;
2391 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2392 animAttr = enter
2393 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2394 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2395 break;
2396 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2397 animAttr = enter
2398 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2399 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2400 break;
2401 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2402 animAttr = enter
2403 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2404 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2405 break;
2406 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2407 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002408 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002409 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2410 break;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002411 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN:
2412 animAttr = enter
2413 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenEnterAnimation
2414 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenExitAnimation;
2415 break;
2416 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE:
2417 animAttr = enter
2418 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseEnterAnimation
2419 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseExitAnimation;
2420 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002421 }
2422 a = loadAnimation(lp, animAttr);
2423 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2424 + " anim=" + a
2425 + " animAttr=0x" + Integer.toHexString(animAttr)
2426 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 if (a != null) {
2429 if (DEBUG_ANIM) {
2430 RuntimeException e = new RuntimeException();
2431 e.fillInStackTrace();
2432 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2433 }
2434 wtoken.setAnimation(a);
2435 }
2436 } else {
2437 wtoken.clearAnimation();
2438 }
2439
2440 return wtoken.animation != null;
2441 }
2442
2443 // -------------------------------------------------------------
2444 // Application Window Tokens
2445 // -------------------------------------------------------------
2446
2447 public void validateAppTokens(List tokens) {
2448 int v = tokens.size()-1;
2449 int m = mAppTokens.size()-1;
2450 while (v >= 0 && m >= 0) {
2451 AppWindowToken wtoken = mAppTokens.get(m);
2452 if (wtoken.removed) {
2453 m--;
2454 continue;
2455 }
2456 if (tokens.get(v) != wtoken.token) {
2457 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2458 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2459 }
2460 v--;
2461 m--;
2462 }
2463 while (v >= 0) {
2464 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2465 v--;
2466 }
2467 while (m >= 0) {
2468 AppWindowToken wtoken = mAppTokens.get(m);
2469 if (!wtoken.removed) {
2470 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2471 }
2472 m--;
2473 }
2474 }
2475
2476 boolean checkCallingPermission(String permission, String func) {
2477 // Quick check: if the calling permission is me, it's all okay.
2478 if (Binder.getCallingPid() == Process.myPid()) {
2479 return true;
2480 }
Romain Guy06882f82009-06-10 13:36:04 -07002481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 if (mContext.checkCallingPermission(permission)
2483 == PackageManager.PERMISSION_GRANTED) {
2484 return true;
2485 }
2486 String msg = "Permission Denial: " + func + " from pid="
2487 + Binder.getCallingPid()
2488 + ", uid=" + Binder.getCallingUid()
2489 + " requires " + permission;
2490 Log.w(TAG, msg);
2491 return false;
2492 }
Romain Guy06882f82009-06-10 13:36:04 -07002493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 AppWindowToken findAppWindowToken(IBinder token) {
2495 WindowToken wtoken = mTokenMap.get(token);
2496 if (wtoken == null) {
2497 return null;
2498 }
2499 return wtoken.appWindowToken;
2500 }
Romain Guy06882f82009-06-10 13:36:04 -07002501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 public void addWindowToken(IBinder token, int type) {
2503 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2504 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002505 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
Romain Guy06882f82009-06-10 13:36:04 -07002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 synchronized(mWindowMap) {
2509 WindowToken wtoken = mTokenMap.get(token);
2510 if (wtoken != null) {
2511 Log.w(TAG, "Attempted to add existing input method token: " + token);
2512 return;
2513 }
2514 wtoken = new WindowToken(token, type, true);
2515 mTokenMap.put(token, wtoken);
2516 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002517 if (type == TYPE_WALLPAPER) {
2518 mWallpaperTokens.add(wtoken);
2519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
2521 }
Romain Guy06882f82009-06-10 13:36:04 -07002522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 public void removeWindowToken(IBinder token) {
2524 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2525 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002526 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
2528
2529 final long origId = Binder.clearCallingIdentity();
2530 synchronized(mWindowMap) {
2531 WindowToken wtoken = mTokenMap.remove(token);
2532 mTokenList.remove(wtoken);
2533 if (wtoken != null) {
2534 boolean delayed = false;
2535 if (!wtoken.hidden) {
2536 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 final int N = wtoken.windows.size();
2539 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 for (int i=0; i<N; i++) {
2542 WindowState win = wtoken.windows.get(i);
2543
2544 if (win.isAnimating()) {
2545 delayed = true;
2546 }
Romain Guy06882f82009-06-10 13:36:04 -07002547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 if (win.isVisibleNow()) {
2549 applyAnimationLocked(win,
2550 WindowManagerPolicy.TRANSIT_EXIT, false);
2551 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2552 KeyWaiter.RETURN_NOTHING);
2553 changed = true;
2554 }
2555 }
2556
2557 if (changed) {
2558 mLayoutNeeded = true;
2559 performLayoutAndPlaceSurfacesLocked();
2560 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2561 }
Romain Guy06882f82009-06-10 13:36:04 -07002562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 if (delayed) {
2564 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002565 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2566 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
2568 }
Romain Guy06882f82009-06-10 13:36:04 -07002569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 } else {
2571 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2572 }
2573 }
2574 Binder.restoreCallingIdentity(origId);
2575 }
2576
2577 public void addAppToken(int addPos, IApplicationToken token,
2578 int groupId, int requestedOrientation, boolean fullscreen) {
2579 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2580 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002581 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
Romain Guy06882f82009-06-10 13:36:04 -07002583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 synchronized(mWindowMap) {
2585 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2586 if (wtoken != null) {
2587 Log.w(TAG, "Attempted to add existing app token: " + token);
2588 return;
2589 }
2590 wtoken = new AppWindowToken(token);
2591 wtoken.groupId = groupId;
2592 wtoken.appFullscreen = fullscreen;
2593 wtoken.requestedOrientation = requestedOrientation;
2594 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002595 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 mTokenMap.put(token.asBinder(), wtoken);
2597 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 // Application tokens start out hidden.
2600 wtoken.hidden = true;
2601 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 //dump();
2604 }
2605 }
Romain Guy06882f82009-06-10 13:36:04 -07002606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 public void setAppGroupId(IBinder token, int groupId) {
2608 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2609 "setAppStartingIcon()")) {
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 }
2612
2613 synchronized(mWindowMap) {
2614 AppWindowToken wtoken = findAppWindowToken(token);
2615 if (wtoken == null) {
2616 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2617 return;
2618 }
2619 wtoken.groupId = groupId;
2620 }
2621 }
Romain Guy06882f82009-06-10 13:36:04 -07002622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 public int getOrientationFromWindowsLocked() {
2624 int pos = mWindows.size() - 1;
2625 while (pos >= 0) {
2626 WindowState wtoken = (WindowState) mWindows.get(pos);
2627 pos--;
2628 if (wtoken.mAppToken != null) {
2629 // We hit an application window. so the orientation will be determined by the
2630 // app window. No point in continuing further.
2631 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2632 }
2633 if (!wtoken.isVisibleLw()) {
2634 continue;
2635 }
2636 int req = wtoken.mAttrs.screenOrientation;
2637 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2638 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2639 continue;
2640 } else {
2641 return req;
2642 }
2643 }
2644 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2645 }
Romain Guy06882f82009-06-10 13:36:04 -07002646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 public int getOrientationFromAppTokensLocked() {
2648 int pos = mAppTokens.size() - 1;
2649 int curGroup = 0;
2650 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002651 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002653 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 while (pos >= 0) {
2655 AppWindowToken wtoken = mAppTokens.get(pos);
2656 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002657 // if we're about to tear down this window and not seek for
2658 // the behind activity, don't use it for orientation
2659 if (!findingBehind
2660 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002661 continue;
2662 }
2663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (!haveGroup) {
2665 // We ignore any hidden applications on the top.
2666 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2667 continue;
2668 }
2669 haveGroup = true;
2670 curGroup = wtoken.groupId;
2671 lastOrientation = wtoken.requestedOrientation;
2672 } else if (curGroup != wtoken.groupId) {
2673 // If we have hit a new application group, and the bottom
2674 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002675 // the orientation behind it, and the last app was
2676 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002678 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2679 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 return lastOrientation;
2681 }
2682 }
2683 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002684 // If this application is fullscreen, and didn't explicitly say
2685 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002687 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002688 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002689 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 return or;
2691 }
2692 // If this application has requested an explicit orientation,
2693 // then use it.
2694 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2695 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2696 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2697 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2698 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2699 return or;
2700 }
Owen Lin3413b892009-05-01 17:12:32 -07002701 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
2703 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2704 }
Romain Guy06882f82009-06-10 13:36:04 -07002705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002707 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002708 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2709 "updateOrientationFromAppTokens()")) {
2710 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2711 }
2712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 Configuration config;
2714 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002715 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2716 freezeThisOneIfNeeded);
2717 Binder.restoreCallingIdentity(ident);
2718 return config;
2719 }
2720
2721 Configuration updateOrientationFromAppTokensUnchecked(
2722 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2723 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002725 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002726 if (config != null) {
2727 mLayoutNeeded = true;
2728 performLayoutAndPlaceSurfacesLocked();
2729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 return config;
2732 }
Romain Guy06882f82009-06-10 13:36:04 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 /*
2735 * The orientation is computed from non-application windows first. If none of
2736 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002737 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2739 * android.os.IBinder)
2740 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002741 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002742 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 long ident = Binder.clearCallingIdentity();
2745 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002746 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 if (req != mForcedAppOrientation) {
2749 changed = true;
2750 mForcedAppOrientation = req;
2751 //send a message to Policy indicating orientation change to take
2752 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002753 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 }
Romain Guy06882f82009-06-10 13:36:04 -07002755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 if (changed) {
2757 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002758 WindowManagerPolicy.USE_LAST_ROTATION,
2759 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 if (changed) {
2761 if (freezeThisOneIfNeeded != null) {
2762 AppWindowToken wtoken = findAppWindowToken(
2763 freezeThisOneIfNeeded);
2764 if (wtoken != null) {
2765 startAppFreezingScreenLocked(wtoken,
2766 ActivityInfo.CONFIG_ORIENTATION);
2767 }
2768 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002769 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 }
2771 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002772
2773 // No obvious action we need to take, but if our current
2774 // state mismatches the activity maanager's, update it
2775 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002776 mTempConfiguration.setToDefaults();
2777 if (computeNewConfigurationLocked(mTempConfiguration)) {
2778 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002779 return new Configuration(mTempConfiguration);
2780 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002781 }
2782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 } finally {
2784 Binder.restoreCallingIdentity(ident);
2785 }
Romain Guy06882f82009-06-10 13:36:04 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 return null;
2788 }
Romain Guy06882f82009-06-10 13:36:04 -07002789
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002790 int computeForcedAppOrientationLocked() {
2791 int req = getOrientationFromWindowsLocked();
2792 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2793 req = getOrientationFromAppTokensLocked();
2794 }
2795 return req;
2796 }
Romain Guy06882f82009-06-10 13:36:04 -07002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2799 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2800 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002801 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 }
Romain Guy06882f82009-06-10 13:36:04 -07002803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 synchronized(mWindowMap) {
2805 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2806 if (wtoken == null) {
2807 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2808 return;
2809 }
Romain Guy06882f82009-06-10 13:36:04 -07002810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 wtoken.requestedOrientation = requestedOrientation;
2812 }
2813 }
Romain Guy06882f82009-06-10 13:36:04 -07002814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 public int getAppOrientation(IApplicationToken token) {
2816 synchronized(mWindowMap) {
2817 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2818 if (wtoken == null) {
2819 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2820 }
Romain Guy06882f82009-06-10 13:36:04 -07002821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 return wtoken.requestedOrientation;
2823 }
2824 }
Romain Guy06882f82009-06-10 13:36:04 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2827 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2828 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002829 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831
2832 synchronized(mWindowMap) {
2833 boolean changed = false;
2834 if (token == null) {
2835 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2836 changed = mFocusedApp != null;
2837 mFocusedApp = null;
2838 mKeyWaiter.tickle();
2839 } else {
2840 AppWindowToken newFocus = findAppWindowToken(token);
2841 if (newFocus == null) {
2842 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2843 return;
2844 }
2845 changed = mFocusedApp != newFocus;
2846 mFocusedApp = newFocus;
2847 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2848 mKeyWaiter.tickle();
2849 }
2850
2851 if (moveFocusNow && changed) {
2852 final long origId = Binder.clearCallingIdentity();
2853 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2854 Binder.restoreCallingIdentity(origId);
2855 }
2856 }
2857 }
2858
2859 public void prepareAppTransition(int transit) {
2860 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2861 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002862 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
Romain Guy06882f82009-06-10 13:36:04 -07002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 synchronized(mWindowMap) {
2866 if (DEBUG_APP_TRANSITIONS) Log.v(
2867 TAG, "Prepare app transition: transit=" + transit
2868 + " mNextAppTransition=" + mNextAppTransition);
2869 if (!mDisplayFrozen) {
2870 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2871 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002872 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2873 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2874 // Opening a new task always supersedes a close for the anim.
2875 mNextAppTransition = transit;
2876 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2877 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2878 // Opening a new activity always supersedes a close for the anim.
2879 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 }
2881 mAppTransitionReady = false;
2882 mAppTransitionTimeout = false;
2883 mStartingIconInTransition = false;
2884 mSkipAppTransitionAnimation = false;
2885 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2886 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2887 5000);
2888 }
2889 }
2890 }
2891
2892 public int getPendingAppTransition() {
2893 return mNextAppTransition;
2894 }
Romain Guy06882f82009-06-10 13:36:04 -07002895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 public void executeAppTransition() {
2897 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2898 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002899 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 synchronized(mWindowMap) {
2903 if (DEBUG_APP_TRANSITIONS) Log.v(
2904 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2905 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2906 mAppTransitionReady = true;
2907 final long origId = Binder.clearCallingIdentity();
2908 performLayoutAndPlaceSurfacesLocked();
2909 Binder.restoreCallingIdentity(origId);
2910 }
2911 }
2912 }
2913
2914 public void setAppStartingWindow(IBinder token, String pkg,
2915 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2916 IBinder transferFrom, boolean createIfNeeded) {
2917 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2918 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002919 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
2921
2922 synchronized(mWindowMap) {
2923 if (DEBUG_STARTING_WINDOW) Log.v(
2924 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2925 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 AppWindowToken wtoken = findAppWindowToken(token);
2928 if (wtoken == null) {
2929 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2930 return;
2931 }
2932
2933 // If the display is frozen, we won't do anything until the
2934 // actual window is displayed so there is no reason to put in
2935 // the starting window.
2936 if (mDisplayFrozen) {
2937 return;
2938 }
Romain Guy06882f82009-06-10 13:36:04 -07002939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 if (wtoken.startingData != null) {
2941 return;
2942 }
Romain Guy06882f82009-06-10 13:36:04 -07002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 if (transferFrom != null) {
2945 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2946 if (ttoken != null) {
2947 WindowState startingWindow = ttoken.startingWindow;
2948 if (startingWindow != null) {
2949 if (mStartingIconInTransition) {
2950 // In this case, the starting icon has already
2951 // been displayed, so start letting windows get
2952 // shown immediately without any more transitions.
2953 mSkipAppTransitionAnimation = true;
2954 }
2955 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2956 "Moving existing starting from " + ttoken
2957 + " to " + wtoken);
2958 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 // Transfer the starting window over to the new
2961 // token.
2962 wtoken.startingData = ttoken.startingData;
2963 wtoken.startingView = ttoken.startingView;
2964 wtoken.startingWindow = startingWindow;
2965 ttoken.startingData = null;
2966 ttoken.startingView = null;
2967 ttoken.startingWindow = null;
2968 ttoken.startingMoved = true;
2969 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002970 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 startingWindow.mAppToken = wtoken;
2972 mWindows.remove(startingWindow);
2973 ttoken.windows.remove(startingWindow);
2974 ttoken.allAppWindows.remove(startingWindow);
2975 addWindowToListInOrderLocked(startingWindow, true);
2976 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 // Propagate other interesting state between the
2979 // tokens. If the old token is displayed, we should
2980 // immediately force the new one to be displayed. If
2981 // it is animating, we need to move that animation to
2982 // the new one.
2983 if (ttoken.allDrawn) {
2984 wtoken.allDrawn = true;
2985 }
2986 if (ttoken.firstWindowDrawn) {
2987 wtoken.firstWindowDrawn = true;
2988 }
2989 if (!ttoken.hidden) {
2990 wtoken.hidden = false;
2991 wtoken.hiddenRequested = false;
2992 wtoken.willBeHidden = false;
2993 }
2994 if (wtoken.clientHidden != ttoken.clientHidden) {
2995 wtoken.clientHidden = ttoken.clientHidden;
2996 wtoken.sendAppVisibilityToClients();
2997 }
2998 if (ttoken.animation != null) {
2999 wtoken.animation = ttoken.animation;
3000 wtoken.animating = ttoken.animating;
3001 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3002 ttoken.animation = null;
3003 ttoken.animLayerAdjustment = 0;
3004 wtoken.updateLayers();
3005 ttoken.updateLayers();
3006 }
Romain Guy06882f82009-06-10 13:36:04 -07003007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 mLayoutNeeded = true;
3010 performLayoutAndPlaceSurfacesLocked();
3011 Binder.restoreCallingIdentity(origId);
3012 return;
3013 } else if (ttoken.startingData != null) {
3014 // The previous app was getting ready to show a
3015 // starting window, but hasn't yet done so. Steal it!
3016 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3017 "Moving pending starting from " + ttoken
3018 + " to " + wtoken);
3019 wtoken.startingData = ttoken.startingData;
3020 ttoken.startingData = null;
3021 ttoken.startingMoved = true;
3022 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3023 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3024 // want to process the message ASAP, before any other queued
3025 // messages.
3026 mH.sendMessageAtFrontOfQueue(m);
3027 return;
3028 }
3029 }
3030 }
3031
3032 // There is no existing starting window, and the caller doesn't
3033 // want us to create one, so that's it!
3034 if (!createIfNeeded) {
3035 return;
3036 }
Romain Guy06882f82009-06-10 13:36:04 -07003037
Dianne Hackborn284ac932009-08-28 10:34:25 -07003038 // If this is a translucent or wallpaper window, then don't
3039 // show a starting window -- the current effect (a full-screen
3040 // opaque starting window that fades away to the real contents
3041 // when it is ready) does not work for this.
3042 if (theme != 0) {
3043 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3044 com.android.internal.R.styleable.Window);
3045 if (ent.array.getBoolean(
3046 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3047 return;
3048 }
3049 if (ent.array.getBoolean(
3050 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3051 return;
3052 }
3053 }
3054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 mStartingIconInTransition = true;
3056 wtoken.startingData = new StartingData(
3057 pkg, theme, nonLocalizedLabel,
3058 labelRes, icon);
3059 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3060 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3061 // want to process the message ASAP, before any other queued
3062 // messages.
3063 mH.sendMessageAtFrontOfQueue(m);
3064 }
3065 }
3066
3067 public void setAppWillBeHidden(IBinder token) {
3068 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3069 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003070 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
3072
3073 AppWindowToken wtoken;
3074
3075 synchronized(mWindowMap) {
3076 wtoken = findAppWindowToken(token);
3077 if (wtoken == null) {
3078 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3079 return;
3080 }
3081 wtoken.willBeHidden = true;
3082 }
3083 }
Romain Guy06882f82009-06-10 13:36:04 -07003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3086 boolean visible, int transit, boolean performLayout) {
3087 boolean delayed = false;
3088
3089 if (wtoken.clientHidden == visible) {
3090 wtoken.clientHidden = !visible;
3091 wtoken.sendAppVisibilityToClients();
3092 }
Romain Guy06882f82009-06-10 13:36:04 -07003093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 wtoken.willBeHidden = false;
3095 if (wtoken.hidden == visible) {
3096 final int N = wtoken.allAppWindows.size();
3097 boolean changed = false;
3098 if (DEBUG_APP_TRANSITIONS) Log.v(
3099 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3100 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3105 if (wtoken.animation == sDummyAnimation) {
3106 wtoken.animation = null;
3107 }
3108 applyAnimationLocked(wtoken, lp, transit, visible);
3109 changed = true;
3110 if (wtoken.animation != null) {
3111 delayed = runningAppAnimation = true;
3112 }
3113 }
Romain Guy06882f82009-06-10 13:36:04 -07003114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 for (int i=0; i<N; i++) {
3116 WindowState win = wtoken.allAppWindows.get(i);
3117 if (win == wtoken.startingWindow) {
3118 continue;
3119 }
3120
3121 if (win.isAnimating()) {
3122 delayed = true;
3123 }
Romain Guy06882f82009-06-10 13:36:04 -07003124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3126 //win.dump(" ");
3127 if (visible) {
3128 if (!win.isVisibleNow()) {
3129 if (!runningAppAnimation) {
3130 applyAnimationLocked(win,
3131 WindowManagerPolicy.TRANSIT_ENTER, true);
3132 }
3133 changed = true;
3134 }
3135 } else if (win.isVisibleNow()) {
3136 if (!runningAppAnimation) {
3137 applyAnimationLocked(win,
3138 WindowManagerPolicy.TRANSIT_EXIT, false);
3139 }
3140 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3141 KeyWaiter.RETURN_NOTHING);
3142 changed = true;
3143 }
3144 }
3145
3146 wtoken.hidden = wtoken.hiddenRequested = !visible;
3147 if (!visible) {
3148 unsetAppFreezingScreenLocked(wtoken, true, true);
3149 } else {
3150 // If we are being set visible, and the starting window is
3151 // not yet displayed, then make sure it doesn't get displayed.
3152 WindowState swin = wtoken.startingWindow;
3153 if (swin != null && (swin.mDrawPending
3154 || swin.mCommitDrawPending)) {
3155 swin.mPolicyVisibility = false;
3156 swin.mPolicyVisibilityAfterAnim = false;
3157 }
3158 }
Romain Guy06882f82009-06-10 13:36:04 -07003159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3161 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3162 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 if (changed && performLayout) {
3165 mLayoutNeeded = true;
3166 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 performLayoutAndPlaceSurfacesLocked();
3168 }
3169 }
3170
3171 if (wtoken.animation != null) {
3172 delayed = true;
3173 }
Romain Guy06882f82009-06-10 13:36:04 -07003174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 return delayed;
3176 }
3177
3178 public void setAppVisibility(IBinder token, boolean visible) {
3179 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3180 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003181 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
3183
3184 AppWindowToken wtoken;
3185
3186 synchronized(mWindowMap) {
3187 wtoken = findAppWindowToken(token);
3188 if (wtoken == null) {
3189 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3190 return;
3191 }
3192
3193 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3194 RuntimeException e = new RuntimeException();
3195 e.fillInStackTrace();
3196 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3197 + "): mNextAppTransition=" + mNextAppTransition
3198 + " hidden=" + wtoken.hidden
3199 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3200 }
Romain Guy06882f82009-06-10 13:36:04 -07003201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 // If we are preparing an app transition, then delay changing
3203 // the visibility of this token until we execute that transition.
3204 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3205 // Already in requested state, don't do anything more.
3206 if (wtoken.hiddenRequested != visible) {
3207 return;
3208 }
3209 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 if (DEBUG_APP_TRANSITIONS) Log.v(
3212 TAG, "Setting dummy animation on: " + wtoken);
3213 wtoken.setDummyAnimation();
3214 mOpeningApps.remove(wtoken);
3215 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003216 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 wtoken.inPendingTransaction = true;
3218 if (visible) {
3219 mOpeningApps.add(wtoken);
3220 wtoken.allDrawn = false;
3221 wtoken.startingDisplayed = false;
3222 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003223 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 if (wtoken.clientHidden) {
3226 // In the case where we are making an app visible
3227 // but holding off for a transition, we still need
3228 // to tell the client to make its windows visible so
3229 // they get drawn. Otherwise, we will wait on
3230 // performing the transition until all windows have
3231 // been drawn, they never will be, and we are sad.
3232 wtoken.clientHidden = false;
3233 wtoken.sendAppVisibilityToClients();
3234 }
3235 } else {
3236 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003237 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239 return;
3240 }
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 final long origId = Binder.clearCallingIdentity();
3243 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3244 wtoken.updateReportedVisibilityLocked();
3245 Binder.restoreCallingIdentity(origId);
3246 }
3247 }
3248
3249 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3250 boolean unfreezeSurfaceNow, boolean force) {
3251 if (wtoken.freezingScreen) {
3252 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3253 + " force=" + force);
3254 final int N = wtoken.allAppWindows.size();
3255 boolean unfrozeWindows = false;
3256 for (int i=0; i<N; i++) {
3257 WindowState w = wtoken.allAppWindows.get(i);
3258 if (w.mAppFreezing) {
3259 w.mAppFreezing = false;
3260 if (w.mSurface != null && !w.mOrientationChanging) {
3261 w.mOrientationChanging = true;
3262 }
3263 unfrozeWindows = true;
3264 }
3265 }
3266 if (force || unfrozeWindows) {
3267 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3268 wtoken.freezingScreen = false;
3269 mAppsFreezingScreen--;
3270 }
3271 if (unfreezeSurfaceNow) {
3272 if (unfrozeWindows) {
3273 mLayoutNeeded = true;
3274 performLayoutAndPlaceSurfacesLocked();
3275 }
3276 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3277 stopFreezingDisplayLocked();
3278 }
3279 }
3280 }
3281 }
Romain Guy06882f82009-06-10 13:36:04 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3284 int configChanges) {
3285 if (DEBUG_ORIENTATION) {
3286 RuntimeException e = new RuntimeException();
3287 e.fillInStackTrace();
3288 Log.i(TAG, "Set freezing of " + wtoken.appToken
3289 + ": hidden=" + wtoken.hidden + " freezing="
3290 + wtoken.freezingScreen, e);
3291 }
3292 if (!wtoken.hiddenRequested) {
3293 if (!wtoken.freezingScreen) {
3294 wtoken.freezingScreen = true;
3295 mAppsFreezingScreen++;
3296 if (mAppsFreezingScreen == 1) {
3297 startFreezingDisplayLocked();
3298 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3299 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3300 5000);
3301 }
3302 }
3303 final int N = wtoken.allAppWindows.size();
3304 for (int i=0; i<N; i++) {
3305 WindowState w = wtoken.allAppWindows.get(i);
3306 w.mAppFreezing = true;
3307 }
3308 }
3309 }
Romain Guy06882f82009-06-10 13:36:04 -07003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 public void startAppFreezingScreen(IBinder token, int configChanges) {
3312 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3313 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003314 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 }
3316
3317 synchronized(mWindowMap) {
3318 if (configChanges == 0 && !mDisplayFrozen) {
3319 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3320 return;
3321 }
Romain Guy06882f82009-06-10 13:36:04 -07003322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 AppWindowToken wtoken = findAppWindowToken(token);
3324 if (wtoken == null || wtoken.appToken == null) {
3325 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3326 return;
3327 }
3328 final long origId = Binder.clearCallingIdentity();
3329 startAppFreezingScreenLocked(wtoken, configChanges);
3330 Binder.restoreCallingIdentity(origId);
3331 }
3332 }
Romain Guy06882f82009-06-10 13:36:04 -07003333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 public void stopAppFreezingScreen(IBinder token, boolean force) {
3335 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3336 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003337 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339
3340 synchronized(mWindowMap) {
3341 AppWindowToken wtoken = findAppWindowToken(token);
3342 if (wtoken == null || wtoken.appToken == null) {
3343 return;
3344 }
3345 final long origId = Binder.clearCallingIdentity();
3346 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3347 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3348 unsetAppFreezingScreenLocked(wtoken, true, force);
3349 Binder.restoreCallingIdentity(origId);
3350 }
3351 }
Romain Guy06882f82009-06-10 13:36:04 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 public void removeAppToken(IBinder token) {
3354 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3355 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003356 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
3358
3359 AppWindowToken wtoken = null;
3360 AppWindowToken startingToken = null;
3361 boolean delayed = false;
3362
3363 final long origId = Binder.clearCallingIdentity();
3364 synchronized(mWindowMap) {
3365 WindowToken basewtoken = mTokenMap.remove(token);
3366 mTokenList.remove(basewtoken);
3367 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3368 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3369 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3370 wtoken.inPendingTransaction = false;
3371 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003372 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 if (mClosingApps.contains(wtoken)) {
3374 delayed = true;
3375 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3376 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003377 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 delayed = true;
3379 }
3380 if (DEBUG_APP_TRANSITIONS) Log.v(
3381 TAG, "Removing app " + wtoken + " delayed=" + delayed
3382 + " animation=" + wtoken.animation
3383 + " animating=" + wtoken.animating);
3384 if (delayed) {
3385 // set the token aside because it has an active animation to be finished
3386 mExitingAppTokens.add(wtoken);
3387 }
3388 mAppTokens.remove(wtoken);
3389 wtoken.removed = true;
3390 if (wtoken.startingData != null) {
3391 startingToken = wtoken;
3392 }
3393 unsetAppFreezingScreenLocked(wtoken, true, true);
3394 if (mFocusedApp == wtoken) {
3395 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3396 mFocusedApp = null;
3397 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3398 mKeyWaiter.tickle();
3399 }
3400 } else {
3401 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3402 }
Romain Guy06882f82009-06-10 13:36:04 -07003403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (!delayed && wtoken != null) {
3405 wtoken.updateReportedVisibilityLocked();
3406 }
3407 }
3408 Binder.restoreCallingIdentity(origId);
3409
3410 if (startingToken != null) {
3411 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3412 + startingToken + ": app token removed");
3413 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3414 mH.sendMessage(m);
3415 }
3416 }
3417
3418 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3419 final int NW = token.windows.size();
3420 for (int i=0; i<NW; i++) {
3421 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003422 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 mWindows.remove(win);
3424 int j = win.mChildWindows.size();
3425 while (j > 0) {
3426 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003427 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3428 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3429 "Tmp removing child window " + cwin);
3430 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 }
3432 }
3433 return NW > 0;
3434 }
3435
3436 void dumpAppTokensLocked() {
3437 for (int i=mAppTokens.size()-1; i>=0; i--) {
3438 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3439 }
3440 }
Romain Guy06882f82009-06-10 13:36:04 -07003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 void dumpWindowsLocked() {
3443 for (int i=mWindows.size()-1; i>=0; i--) {
3444 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3445 }
3446 }
Romain Guy06882f82009-06-10 13:36:04 -07003447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 private int findWindowOffsetLocked(int tokenPos) {
3449 final int NW = mWindows.size();
3450
3451 if (tokenPos >= mAppTokens.size()) {
3452 int i = NW;
3453 while (i > 0) {
3454 i--;
3455 WindowState win = (WindowState)mWindows.get(i);
3456 if (win.getAppToken() != null) {
3457 return i+1;
3458 }
3459 }
3460 }
3461
3462 while (tokenPos > 0) {
3463 // Find the first app token below the new position that has
3464 // a window displayed.
3465 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3466 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3467 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003468 if (wtoken.sendingToBottom) {
3469 if (DEBUG_REORDER) Log.v(TAG,
3470 "Skipping token -- currently sending to bottom");
3471 tokenPos--;
3472 continue;
3473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 int i = wtoken.windows.size();
3475 while (i > 0) {
3476 i--;
3477 WindowState win = wtoken.windows.get(i);
3478 int j = win.mChildWindows.size();
3479 while (j > 0) {
3480 j--;
3481 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003482 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 for (int pos=NW-1; pos>=0; pos--) {
3484 if (mWindows.get(pos) == cwin) {
3485 if (DEBUG_REORDER) Log.v(TAG,
3486 "Found child win @" + (pos+1));
3487 return pos+1;
3488 }
3489 }
3490 }
3491 }
3492 for (int pos=NW-1; pos>=0; pos--) {
3493 if (mWindows.get(pos) == win) {
3494 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3495 return pos+1;
3496 }
3497 }
3498 }
3499 tokenPos--;
3500 }
3501
3502 return 0;
3503 }
3504
3505 private final int reAddWindowLocked(int index, WindowState win) {
3506 final int NCW = win.mChildWindows.size();
3507 boolean added = false;
3508 for (int j=0; j<NCW; j++) {
3509 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3510 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003511 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3512 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 mWindows.add(index, win);
3514 index++;
3515 added = true;
3516 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003517 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3518 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 mWindows.add(index, cwin);
3520 index++;
3521 }
3522 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003523 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3524 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 mWindows.add(index, win);
3526 index++;
3527 }
3528 return index;
3529 }
Romain Guy06882f82009-06-10 13:36:04 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3532 final int NW = token.windows.size();
3533 for (int i=0; i<NW; i++) {
3534 index = reAddWindowLocked(index, token.windows.get(i));
3535 }
3536 return index;
3537 }
3538
3539 public void moveAppToken(int index, IBinder token) {
3540 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3541 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003542 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 }
3544
3545 synchronized(mWindowMap) {
3546 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3547 if (DEBUG_REORDER) dumpAppTokensLocked();
3548 final AppWindowToken wtoken = findAppWindowToken(token);
3549 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3550 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3551 + token + " (" + wtoken + ")");
3552 return;
3553 }
3554 mAppTokens.add(index, wtoken);
3555 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3556 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 final long origId = Binder.clearCallingIdentity();
3559 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3560 if (DEBUG_REORDER) dumpWindowsLocked();
3561 if (tmpRemoveAppWindowsLocked(wtoken)) {
3562 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3563 if (DEBUG_REORDER) dumpWindowsLocked();
3564 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3565 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3566 if (DEBUG_REORDER) dumpWindowsLocked();
3567 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 mLayoutNeeded = true;
3569 performLayoutAndPlaceSurfacesLocked();
3570 }
3571 Binder.restoreCallingIdentity(origId);
3572 }
3573 }
3574
3575 private void removeAppTokensLocked(List<IBinder> tokens) {
3576 // XXX This should be done more efficiently!
3577 // (take advantage of the fact that both lists should be
3578 // ordered in the same way.)
3579 int N = tokens.size();
3580 for (int i=0; i<N; i++) {
3581 IBinder token = tokens.get(i);
3582 final AppWindowToken wtoken = findAppWindowToken(token);
3583 if (!mAppTokens.remove(wtoken)) {
3584 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3585 + token + " (" + wtoken + ")");
3586 i--;
3587 N--;
3588 }
3589 }
3590 }
3591
Dianne Hackborna8f60182009-09-01 19:01:50 -07003592 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3593 boolean updateFocusAndLayout) {
3594 // First remove all of the windows from the list.
3595 tmpRemoveAppWindowsLocked(wtoken);
3596
3597 // Where to start adding?
3598 int pos = findWindowOffsetLocked(tokenPos);
3599
3600 // And now add them back at the correct place.
3601 pos = reAddAppWindowsLocked(pos, wtoken);
3602
3603 if (updateFocusAndLayout) {
3604 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3605 assignLayersLocked();
3606 }
3607 mLayoutNeeded = true;
3608 performLayoutAndPlaceSurfacesLocked();
3609 }
3610 }
3611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3613 // First remove all of the windows from the list.
3614 final int N = tokens.size();
3615 int i;
3616 for (i=0; i<N; i++) {
3617 WindowToken token = mTokenMap.get(tokens.get(i));
3618 if (token != null) {
3619 tmpRemoveAppWindowsLocked(token);
3620 }
3621 }
3622
3623 // Where to start adding?
3624 int pos = findWindowOffsetLocked(tokenPos);
3625
3626 // And now add them back at the correct place.
3627 for (i=0; i<N; i++) {
3628 WindowToken token = mTokenMap.get(tokens.get(i));
3629 if (token != null) {
3630 pos = reAddAppWindowsLocked(pos, token);
3631 }
3632 }
3633
Dianne Hackborna8f60182009-09-01 19:01:50 -07003634 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3635 assignLayersLocked();
3636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 mLayoutNeeded = true;
3638 performLayoutAndPlaceSurfacesLocked();
3639
3640 //dump();
3641 }
3642
3643 public void moveAppTokensToTop(List<IBinder> tokens) {
3644 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3645 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003646 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
3648
3649 final long origId = Binder.clearCallingIdentity();
3650 synchronized(mWindowMap) {
3651 removeAppTokensLocked(tokens);
3652 final int N = tokens.size();
3653 for (int i=0; i<N; i++) {
3654 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3655 if (wt != null) {
3656 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003657 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3658 mToTopApps.remove(wt);
3659 mToBottomApps.remove(wt);
3660 mToTopApps.add(wt);
3661 wt.sendingToBottom = false;
3662 wt.sendingToTop = true;
3663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 }
3665 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003666
3667 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3668 moveAppWindowsLocked(tokens, mAppTokens.size());
3669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 }
3671 Binder.restoreCallingIdentity(origId);
3672 }
3673
3674 public void moveAppTokensToBottom(List<IBinder> tokens) {
3675 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3676 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003677 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
3679
3680 final long origId = Binder.clearCallingIdentity();
3681 synchronized(mWindowMap) {
3682 removeAppTokensLocked(tokens);
3683 final int N = tokens.size();
3684 int pos = 0;
3685 for (int i=0; i<N; i++) {
3686 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3687 if (wt != null) {
3688 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003689 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3690 mToTopApps.remove(wt);
3691 mToBottomApps.remove(wt);
3692 mToBottomApps.add(i, wt);
3693 wt.sendingToTop = false;
3694 wt.sendingToBottom = true;
3695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 pos++;
3697 }
3698 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003699
3700 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3701 moveAppWindowsLocked(tokens, 0);
3702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 }
3704 Binder.restoreCallingIdentity(origId);
3705 }
3706
3707 // -------------------------------------------------------------
3708 // Misc IWindowSession methods
3709 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 public void disableKeyguard(IBinder token, String tag) {
3712 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3713 != PackageManager.PERMISSION_GRANTED) {
3714 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3715 }
3716 mKeyguardDisabled.acquire(token, tag);
3717 }
3718
3719 public void reenableKeyguard(IBinder token) {
3720 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3721 != PackageManager.PERMISSION_GRANTED) {
3722 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3723 }
3724 synchronized (mKeyguardDisabled) {
3725 mKeyguardDisabled.release(token);
3726
3727 if (!mKeyguardDisabled.isAcquired()) {
3728 // if we are the last one to reenable the keyguard wait until
3729 // we have actaully finished reenabling until returning
3730 mWaitingUntilKeyguardReenabled = true;
3731 while (mWaitingUntilKeyguardReenabled) {
3732 try {
3733 mKeyguardDisabled.wait();
3734 } catch (InterruptedException e) {
3735 Thread.currentThread().interrupt();
3736 }
3737 }
3738 }
3739 }
3740 }
3741
3742 /**
3743 * @see android.app.KeyguardManager#exitKeyguardSecurely
3744 */
3745 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3746 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3747 != PackageManager.PERMISSION_GRANTED) {
3748 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3749 }
3750 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3751 public void onKeyguardExitResult(boolean success) {
3752 try {
3753 callback.onKeyguardExitResult(success);
3754 } catch (RemoteException e) {
3755 // Client has died, we don't care.
3756 }
3757 }
3758 });
3759 }
3760
3761 public boolean inKeyguardRestrictedInputMode() {
3762 return mPolicy.inKeyguardRestrictedKeyInputMode();
3763 }
Romain Guy06882f82009-06-10 13:36:04 -07003764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 static float fixScale(float scale) {
3766 if (scale < 0) scale = 0;
3767 else if (scale > 20) scale = 20;
3768 return Math.abs(scale);
3769 }
Romain Guy06882f82009-06-10 13:36:04 -07003770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 public void setAnimationScale(int which, float scale) {
3772 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3773 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003774 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
3776
3777 if (scale < 0) scale = 0;
3778 else if (scale > 20) scale = 20;
3779 scale = Math.abs(scale);
3780 switch (which) {
3781 case 0: mWindowAnimationScale = fixScale(scale); break;
3782 case 1: mTransitionAnimationScale = fixScale(scale); break;
3783 }
Romain Guy06882f82009-06-10 13:36:04 -07003784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 // Persist setting
3786 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3787 }
Romain Guy06882f82009-06-10 13:36:04 -07003788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 public void setAnimationScales(float[] scales) {
3790 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3791 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003792 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 }
3794
3795 if (scales != null) {
3796 if (scales.length >= 1) {
3797 mWindowAnimationScale = fixScale(scales[0]);
3798 }
3799 if (scales.length >= 2) {
3800 mTransitionAnimationScale = fixScale(scales[1]);
3801 }
3802 }
Romain Guy06882f82009-06-10 13:36:04 -07003803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 // Persist setting
3805 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3806 }
Romain Guy06882f82009-06-10 13:36:04 -07003807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 public float getAnimationScale(int which) {
3809 switch (which) {
3810 case 0: return mWindowAnimationScale;
3811 case 1: return mTransitionAnimationScale;
3812 }
3813 return 0;
3814 }
Romain Guy06882f82009-06-10 13:36:04 -07003815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 public float[] getAnimationScales() {
3817 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3818 }
Romain Guy06882f82009-06-10 13:36:04 -07003819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 public int getSwitchState(int sw) {
3821 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3822 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003823 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 }
3825 return KeyInputQueue.getSwitchState(sw);
3826 }
Romain Guy06882f82009-06-10 13:36:04 -07003827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 public int getSwitchStateForDevice(int devid, int sw) {
3829 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3830 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003831 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 }
3833 return KeyInputQueue.getSwitchState(devid, sw);
3834 }
Romain Guy06882f82009-06-10 13:36:04 -07003835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 public int getScancodeState(int sw) {
3837 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3838 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003839 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 }
3841 return KeyInputQueue.getScancodeState(sw);
3842 }
Romain Guy06882f82009-06-10 13:36:04 -07003843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 public int getScancodeStateForDevice(int devid, int sw) {
3845 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3846 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003847 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 }
3849 return KeyInputQueue.getScancodeState(devid, sw);
3850 }
Romain Guy06882f82009-06-10 13:36:04 -07003851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 public int getKeycodeState(int sw) {
3853 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3854 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003855 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003856 }
3857 return KeyInputQueue.getKeycodeState(sw);
3858 }
Romain Guy06882f82009-06-10 13:36:04 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 public int getKeycodeStateForDevice(int devid, int sw) {
3861 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3862 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003863 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 }
3865 return KeyInputQueue.getKeycodeState(devid, sw);
3866 }
Romain Guy06882f82009-06-10 13:36:04 -07003867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3869 return KeyInputQueue.hasKeys(keycodes, keyExists);
3870 }
Romain Guy06882f82009-06-10 13:36:04 -07003871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 public void enableScreenAfterBoot() {
3873 synchronized(mWindowMap) {
3874 if (mSystemBooted) {
3875 return;
3876 }
3877 mSystemBooted = true;
3878 }
Romain Guy06882f82009-06-10 13:36:04 -07003879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 performEnableScreen();
3881 }
Romain Guy06882f82009-06-10 13:36:04 -07003882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 public void enableScreenIfNeededLocked() {
3884 if (mDisplayEnabled) {
3885 return;
3886 }
3887 if (!mSystemBooted) {
3888 return;
3889 }
3890 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3891 }
Romain Guy06882f82009-06-10 13:36:04 -07003892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 public void performEnableScreen() {
3894 synchronized(mWindowMap) {
3895 if (mDisplayEnabled) {
3896 return;
3897 }
3898 if (!mSystemBooted) {
3899 return;
3900 }
Romain Guy06882f82009-06-10 13:36:04 -07003901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 // Don't enable the screen until all existing windows
3903 // have been drawn.
3904 final int N = mWindows.size();
3905 for (int i=0; i<N; i++) {
3906 WindowState w = (WindowState)mWindows.get(i);
3907 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3908 return;
3909 }
3910 }
Romain Guy06882f82009-06-10 13:36:04 -07003911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 mDisplayEnabled = true;
3913 if (false) {
3914 Log.i(TAG, "ENABLING SCREEN!");
3915 StringWriter sw = new StringWriter();
3916 PrintWriter pw = new PrintWriter(sw);
3917 this.dump(null, pw, null);
3918 Log.i(TAG, sw.toString());
3919 }
3920 try {
3921 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3922 if (surfaceFlinger != null) {
3923 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3924 Parcel data = Parcel.obtain();
3925 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3926 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3927 data, null, 0);
3928 data.recycle();
3929 }
3930 } catch (RemoteException ex) {
3931 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3932 }
3933 }
Romain Guy06882f82009-06-10 13:36:04 -07003934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003938 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3939 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 }
Romain Guy06882f82009-06-10 13:36:04 -07003941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 public void setInTouchMode(boolean mode) {
3943 synchronized(mWindowMap) {
3944 mInTouchMode = mode;
3945 }
3946 }
3947
Romain Guy06882f82009-06-10 13:36:04 -07003948 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003949 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003951 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003952 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
3954
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003955 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 }
Romain Guy06882f82009-06-10 13:36:04 -07003957
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003958 public void setRotationUnchecked(int rotation,
3959 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 if(DEBUG_ORIENTATION) Log.v(TAG,
3961 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 long origId = Binder.clearCallingIdentity();
3964 boolean changed;
3965 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003966 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
Romain Guy06882f82009-06-10 13:36:04 -07003968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 if (changed) {
3970 sendNewConfiguration();
3971 synchronized(mWindowMap) {
3972 mLayoutNeeded = true;
3973 performLayoutAndPlaceSurfacesLocked();
3974 }
3975 } else if (alwaysSendConfiguration) {
3976 //update configuration ignoring orientation change
3977 sendNewConfiguration();
3978 }
Romain Guy06882f82009-06-10 13:36:04 -07003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 Binder.restoreCallingIdentity(origId);
3981 }
Romain Guy06882f82009-06-10 13:36:04 -07003982
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003983 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 boolean changed;
3985 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3986 rotation = mRequestedRotation;
3987 } else {
3988 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003989 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003992 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 mRotation, mDisplayEnabled);
3994 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3995 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003998 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 "Rotation changed to " + rotation
4000 + " from " + mRotation
4001 + " (forceApp=" + mForcedAppOrientation
4002 + ", req=" + mRequestedRotation + ")");
4003 mRotation = rotation;
4004 mWindowsFreezingScreen = true;
4005 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4006 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4007 2000);
4008 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004009 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 mQueue.setOrientation(rotation);
4011 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004012 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 }
4014 for (int i=mWindows.size()-1; i>=0; i--) {
4015 WindowState w = (WindowState)mWindows.get(i);
4016 if (w.mSurface != null) {
4017 w.mOrientationChanging = true;
4018 }
4019 }
4020 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4021 try {
4022 mRotationWatchers.get(i).onRotationChanged(rotation);
4023 } catch (RemoteException e) {
4024 }
4025 }
4026 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 return changed;
4029 }
Romain Guy06882f82009-06-10 13:36:04 -07004030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 public int getRotation() {
4032 return mRotation;
4033 }
4034
4035 public int watchRotation(IRotationWatcher watcher) {
4036 final IBinder watcherBinder = watcher.asBinder();
4037 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4038 public void binderDied() {
4039 synchronized (mWindowMap) {
4040 for (int i=0; i<mRotationWatchers.size(); i++) {
4041 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004042 IRotationWatcher removed = mRotationWatchers.remove(i);
4043 if (removed != null) {
4044 removed.asBinder().unlinkToDeath(this, 0);
4045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 i--;
4047 }
4048 }
4049 }
4050 }
4051 };
Romain Guy06882f82009-06-10 13:36:04 -07004052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 synchronized (mWindowMap) {
4054 try {
4055 watcher.asBinder().linkToDeath(dr, 0);
4056 mRotationWatchers.add(watcher);
4057 } catch (RemoteException e) {
4058 // Client died, no cleanup needed.
4059 }
Romain Guy06882f82009-06-10 13:36:04 -07004060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 return mRotation;
4062 }
4063 }
4064
4065 /**
4066 * Starts the view server on the specified port.
4067 *
4068 * @param port The port to listener to.
4069 *
4070 * @return True if the server was successfully started, false otherwise.
4071 *
4072 * @see com.android.server.ViewServer
4073 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4074 */
4075 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004076 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 return false;
4078 }
4079
4080 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4081 return false;
4082 }
4083
4084 if (port < 1024) {
4085 return false;
4086 }
4087
4088 if (mViewServer != null) {
4089 if (!mViewServer.isRunning()) {
4090 try {
4091 return mViewServer.start();
4092 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004093 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
4095 }
4096 return false;
4097 }
4098
4099 try {
4100 mViewServer = new ViewServer(this, port);
4101 return mViewServer.start();
4102 } catch (IOException e) {
4103 Log.w(TAG, "View server did not start");
4104 }
4105 return false;
4106 }
4107
Romain Guy06882f82009-06-10 13:36:04 -07004108 private boolean isSystemSecure() {
4109 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4110 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4111 }
4112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 /**
4114 * Stops the view server if it exists.
4115 *
4116 * @return True if the server stopped, false if it wasn't started or
4117 * couldn't be stopped.
4118 *
4119 * @see com.android.server.ViewServer
4120 */
4121 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004122 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 return false;
4124 }
4125
4126 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4127 return false;
4128 }
4129
4130 if (mViewServer != null) {
4131 return mViewServer.stop();
4132 }
4133 return false;
4134 }
4135
4136 /**
4137 * Indicates whether the view server is running.
4138 *
4139 * @return True if the server is running, false otherwise.
4140 *
4141 * @see com.android.server.ViewServer
4142 */
4143 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004144 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 return false;
4146 }
4147
4148 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4149 return false;
4150 }
4151
4152 return mViewServer != null && mViewServer.isRunning();
4153 }
4154
4155 /**
4156 * Lists all availble windows in the system. The listing is written in the
4157 * specified Socket's output stream with the following syntax:
4158 * windowHashCodeInHexadecimal windowName
4159 * Each line of the ouput represents a different window.
4160 *
4161 * @param client The remote client to send the listing to.
4162 * @return False if an error occured, true otherwise.
4163 */
4164 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004165 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 return false;
4167 }
4168
4169 boolean result = true;
4170
4171 Object[] windows;
4172 synchronized (mWindowMap) {
4173 windows = new Object[mWindows.size()];
4174 //noinspection unchecked
4175 windows = mWindows.toArray(windows);
4176 }
4177
4178 BufferedWriter out = null;
4179
4180 // Any uncaught exception will crash the system process
4181 try {
4182 OutputStream clientStream = client.getOutputStream();
4183 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4184
4185 final int count = windows.length;
4186 for (int i = 0; i < count; i++) {
4187 final WindowState w = (WindowState) windows[i];
4188 out.write(Integer.toHexString(System.identityHashCode(w)));
4189 out.write(' ');
4190 out.append(w.mAttrs.getTitle());
4191 out.write('\n');
4192 }
4193
4194 out.write("DONE.\n");
4195 out.flush();
4196 } catch (Exception e) {
4197 result = false;
4198 } finally {
4199 if (out != null) {
4200 try {
4201 out.close();
4202 } catch (IOException e) {
4203 result = false;
4204 }
4205 }
4206 }
4207
4208 return result;
4209 }
4210
4211 /**
4212 * Sends a command to a target window. The result of the command, if any, will be
4213 * written in the output stream of the specified socket.
4214 *
4215 * The parameters must follow this syntax:
4216 * windowHashcode extra
4217 *
4218 * Where XX is the length in characeters of the windowTitle.
4219 *
4220 * The first parameter is the target window. The window with the specified hashcode
4221 * will be the target. If no target can be found, nothing happens. The extra parameters
4222 * will be delivered to the target window and as parameters to the command itself.
4223 *
4224 * @param client The remote client to sent the result, if any, to.
4225 * @param command The command to execute.
4226 * @param parameters The command parameters.
4227 *
4228 * @return True if the command was successfully delivered, false otherwise. This does
4229 * not indicate whether the command itself was successful.
4230 */
4231 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004232 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 return false;
4234 }
4235
4236 boolean success = true;
4237 Parcel data = null;
4238 Parcel reply = null;
4239
4240 // Any uncaught exception will crash the system process
4241 try {
4242 // Find the hashcode of the window
4243 int index = parameters.indexOf(' ');
4244 if (index == -1) {
4245 index = parameters.length();
4246 }
4247 final String code = parameters.substring(0, index);
4248 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4249
4250 // Extract the command's parameter after the window description
4251 if (index < parameters.length()) {
4252 parameters = parameters.substring(index + 1);
4253 } else {
4254 parameters = "";
4255 }
4256
4257 final WindowManagerService.WindowState window = findWindow(hashCode);
4258 if (window == null) {
4259 return false;
4260 }
4261
4262 data = Parcel.obtain();
4263 data.writeInterfaceToken("android.view.IWindow");
4264 data.writeString(command);
4265 data.writeString(parameters);
4266 data.writeInt(1);
4267 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4268
4269 reply = Parcel.obtain();
4270
4271 final IBinder binder = window.mClient.asBinder();
4272 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4273 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4274
4275 reply.readException();
4276
4277 } catch (Exception e) {
4278 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4279 success = false;
4280 } finally {
4281 if (data != null) {
4282 data.recycle();
4283 }
4284 if (reply != null) {
4285 reply.recycle();
4286 }
4287 }
4288
4289 return success;
4290 }
4291
4292 private WindowState findWindow(int hashCode) {
4293 if (hashCode == -1) {
4294 return getFocusedWindow();
4295 }
4296
4297 synchronized (mWindowMap) {
4298 final ArrayList windows = mWindows;
4299 final int count = windows.size();
4300
4301 for (int i = 0; i < count; i++) {
4302 WindowState w = (WindowState) windows.get(i);
4303 if (System.identityHashCode(w) == hashCode) {
4304 return w;
4305 }
4306 }
4307 }
4308
4309 return null;
4310 }
4311
4312 /*
4313 * Instruct the Activity Manager to fetch the current configuration and broadcast
4314 * that to config-changed listeners if appropriate.
4315 */
4316 void sendNewConfiguration() {
4317 try {
4318 mActivityManager.updateConfiguration(null);
4319 } catch (RemoteException e) {
4320 }
4321 }
Romain Guy06882f82009-06-10 13:36:04 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 public Configuration computeNewConfiguration() {
4324 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004325 return computeNewConfigurationLocked();
4326 }
4327 }
Romain Guy06882f82009-06-10 13:36:04 -07004328
Dianne Hackbornc485a602009-03-24 22:39:49 -07004329 Configuration computeNewConfigurationLocked() {
4330 Configuration config = new Configuration();
4331 if (!computeNewConfigurationLocked(config)) {
4332 return null;
4333 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004334 return config;
4335 }
Romain Guy06882f82009-06-10 13:36:04 -07004336
Dianne Hackbornc485a602009-03-24 22:39:49 -07004337 boolean computeNewConfigurationLocked(Configuration config) {
4338 if (mDisplay == null) {
4339 return false;
4340 }
4341 mQueue.getInputConfiguration(config);
4342 final int dw = mDisplay.getWidth();
4343 final int dh = mDisplay.getHeight();
4344 int orientation = Configuration.ORIENTATION_SQUARE;
4345 if (dw < dh) {
4346 orientation = Configuration.ORIENTATION_PORTRAIT;
4347 } else if (dw > dh) {
4348 orientation = Configuration.ORIENTATION_LANDSCAPE;
4349 }
4350 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004351
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004352 DisplayMetrics dm = new DisplayMetrics();
4353 mDisplay.getMetrics(dm);
4354 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4355
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004356 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004357 // Note we only do this once because at this point we don't
4358 // expect the screen to change in this way at runtime, and want
4359 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004360 int longSize = dw;
4361 int shortSize = dh;
4362 if (longSize < shortSize) {
4363 int tmp = longSize;
4364 longSize = shortSize;
4365 shortSize = tmp;
4366 }
4367 longSize = (int)(longSize/dm.density);
4368 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004369
Dianne Hackborn723738c2009-06-25 19:48:04 -07004370 // These semi-magic numbers define our compatibility modes for
4371 // applications with different screens. Don't change unless you
4372 // make sure to test lots and lots of apps!
4373 if (longSize < 470) {
4374 // This is shorter than an HVGA normal density screen (which
4375 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004376 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4377 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004378 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004379 // Is this a large screen?
4380 if (longSize > 640 && shortSize >= 480) {
4381 // VGA or larger screens at medium density are the point
4382 // at which we consider it to be a large screen.
4383 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4384 } else {
4385 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4386
4387 // If this screen is wider than normal HVGA, or taller
4388 // than FWVGA, then for old apps we want to run in size
4389 // compatibility mode.
4390 if (shortSize > 321 || longSize > 570) {
4391 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4392 }
4393 }
4394
4395 // Is this a long screen?
4396 if (((longSize*3)/5) >= (shortSize-1)) {
4397 // Anything wider than WVGA (5:3) is considering to be long.
4398 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4399 } else {
4400 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4401 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004402 }
4403 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004404 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004405
Dianne Hackbornc485a602009-03-24 22:39:49 -07004406 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4407 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4408 mPolicy.adjustConfigurationLw(config);
4409 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 }
Romain Guy06882f82009-06-10 13:36:04 -07004411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 // -------------------------------------------------------------
4413 // Input Events and Focus Management
4414 // -------------------------------------------------------------
4415
4416 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004417 long curTime = SystemClock.uptimeMillis();
4418
Michael Chane10de972009-05-18 11:24:50 -07004419 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004420 if (mLastTouchEventType == eventType &&
4421 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4422 return;
4423 }
4424 mLastUserActivityCallTime = curTime;
4425 mLastTouchEventType = eventType;
4426 }
4427
4428 if (targetWin == null
4429 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4430 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 }
4432 }
4433
4434 // tells if it's a cheek event or not -- this function is stateful
4435 private static final int EVENT_NONE = 0;
4436 private static final int EVENT_UNKNOWN = 0;
4437 private static final int EVENT_CHEEK = 0;
4438 private static final int EVENT_IGNORE_DURATION = 300; // ms
4439 private static final float CHEEK_THRESHOLD = 0.6f;
4440 private int mEventState = EVENT_NONE;
4441 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 private int eventType(MotionEvent ev) {
4444 float size = ev.getSize();
4445 switch (ev.getAction()) {
4446 case MotionEvent.ACTION_DOWN:
4447 mEventSize = size;
4448 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4449 case MotionEvent.ACTION_UP:
4450 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004451 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 case MotionEvent.ACTION_MOVE:
4453 final int N = ev.getHistorySize();
4454 if (size > mEventSize) mEventSize = size;
4455 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4456 for (int i=0; i<N; i++) {
4457 size = ev.getHistoricalSize(i);
4458 if (size > mEventSize) mEventSize = size;
4459 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4460 }
4461 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4462 return TOUCH_EVENT;
4463 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004464 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
4466 default:
4467 // not good
4468 return OTHER_EVENT;
4469 }
4470 }
4471
4472 /**
4473 * @return Returns true if event was dispatched, false if it was dropped for any reason
4474 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004475 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4477 "dispatchPointer " + ev);
4478
Michael Chan53071d62009-05-13 17:29:48 -07004479 if (MEASURE_LATENCY) {
4480 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4481 }
4482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004484 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485
Michael Chan53071d62009-05-13 17:29:48 -07004486 if (MEASURE_LATENCY) {
4487 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4488 }
4489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 if (action == MotionEvent.ACTION_UP) {
4493 // let go of our target
4494 mKeyWaiter.mMotionTarget = null;
4495 mPowerManager.logPointerUpEvent();
4496 } else if (action == MotionEvent.ACTION_DOWN) {
4497 mPowerManager.logPointerDownEvent();
4498 }
4499
4500 if (targetObj == null) {
4501 // In this case we are either dropping the event, or have received
4502 // a move or up without a down. It is common to receive move
4503 // events in such a way, since this means the user is moving the
4504 // pointer without actually pressing down. All other cases should
4505 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004506 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4508 }
4509 if (qev != null) {
4510 mQueue.recycleEvent(qev);
4511 }
4512 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004513 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 }
4515 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4516 if (qev != null) {
4517 mQueue.recycleEvent(qev);
4518 }
4519 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004520 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
Romain Guy06882f82009-06-10 13:36:04 -07004522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004526 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527
4528 //Log.i(TAG, "Sending " + ev + " to " + target);
4529
4530 if (uid != 0 && uid != target.mSession.mUid) {
4531 if (mContext.checkPermission(
4532 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4533 != PackageManager.PERMISSION_GRANTED) {
4534 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4535 + pid + " uid " + uid + " to window " + target
4536 + " owned by uid " + target.mSession.mUid);
4537 if (qev != null) {
4538 mQueue.recycleEvent(qev);
4539 }
4540 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004541 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 }
4543 }
4544
Michael Chan53071d62009-05-13 17:29:48 -07004545 if (MEASURE_LATENCY) {
4546 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4547 }
4548
Romain Guy06882f82009-06-10 13:36:04 -07004549 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4551 //target wants to ignore fat touch events
4552 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4553 //explicit flag to return without processing event further
4554 boolean returnFlag = false;
4555 if((action == MotionEvent.ACTION_DOWN)) {
4556 mFatTouch = false;
4557 if(cheekPress) {
4558 mFatTouch = true;
4559 returnFlag = true;
4560 }
4561 } else {
4562 if(action == MotionEvent.ACTION_UP) {
4563 if(mFatTouch) {
4564 //earlier even was invalid doesnt matter if current up is cheekpress or not
4565 mFatTouch = false;
4566 returnFlag = true;
4567 } else if(cheekPress) {
4568 //cancel the earlier event
4569 ev.setAction(MotionEvent.ACTION_CANCEL);
4570 action = MotionEvent.ACTION_CANCEL;
4571 }
4572 } else if(action == MotionEvent.ACTION_MOVE) {
4573 if(mFatTouch) {
4574 //two cases here
4575 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004576 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 returnFlag = true;
4578 } else if(cheekPress) {
4579 //valid down followed by invalid moves
4580 //an invalid move have to cancel earlier action
4581 ev.setAction(MotionEvent.ACTION_CANCEL);
4582 action = MotionEvent.ACTION_CANCEL;
4583 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4584 //note that the subsequent invalid moves will not get here
4585 mFatTouch = true;
4586 }
4587 }
4588 } //else if action
4589 if(returnFlag) {
4590 //recycle que, ev
4591 if (qev != null) {
4592 mQueue.recycleEvent(qev);
4593 }
4594 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004595 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 }
4597 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004598
Michael Chan9f028e62009-08-04 17:37:46 -07004599 // Enable this for testing the "right" value
4600 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004601 int max_events_per_sec = 35;
4602 try {
4603 max_events_per_sec = Integer.parseInt(SystemProperties
4604 .get("windowsmgr.max_events_per_sec"));
4605 if (max_events_per_sec < 1) {
4606 max_events_per_sec = 35;
4607 }
4608 } catch (NumberFormatException e) {
4609 }
4610 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4611 }
4612
4613 /*
4614 * Throttle events to minimize CPU usage when there's a flood of events
4615 * e.g. constant contact with the screen
4616 */
4617 if (action == MotionEvent.ACTION_MOVE) {
4618 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4619 long now = SystemClock.uptimeMillis();
4620 if (now < nextEventTime) {
4621 try {
4622 Thread.sleep(nextEventTime - now);
4623 } catch (InterruptedException e) {
4624 }
4625 mLastTouchEventTime = nextEventTime;
4626 } else {
4627 mLastTouchEventTime = now;
4628 }
4629 }
4630
Michael Chan53071d62009-05-13 17:29:48 -07004631 if (MEASURE_LATENCY) {
4632 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4633 }
4634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 synchronized(mWindowMap) {
4636 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4637 mKeyWaiter.bindTargetWindowLocked(target,
4638 KeyWaiter.RETURN_PENDING_POINTER, qev);
4639 ev = null;
4640 } else {
4641 if (action == MotionEvent.ACTION_DOWN) {
4642 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4643 if (out != null) {
4644 MotionEvent oev = MotionEvent.obtain(ev);
4645 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4646 do {
4647 final Rect frame = out.mFrame;
4648 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4649 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004650 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 } catch (android.os.RemoteException e) {
4652 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4653 }
4654 oev.offsetLocation((float)frame.left, (float)frame.top);
4655 out = out.mNextOutsideTouch;
4656 } while (out != null);
4657 mKeyWaiter.mOutsideTouchTargets = null;
4658 }
4659 }
4660 final Rect frame = target.mFrame;
4661 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4662 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004663
4664 // If we are on top of the wallpaper, then the wallpaper also
4665 // gets to see this movement.
4666 if (mWallpaperTarget == target) {
4667 sendPointerToWallpaperLocked(target, ev, eventTime);
4668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 }
4670 }
Romain Guy06882f82009-06-10 13:36:04 -07004671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 // finally offset the event to the target's coordinate system and
4673 // dispatch the event.
4674 try {
4675 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4676 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4677 }
Michael Chan53071d62009-05-13 17:29:48 -07004678
4679 if (MEASURE_LATENCY) {
4680 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4681 }
4682
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004683 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004684
4685 if (MEASURE_LATENCY) {
4686 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4687 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004688 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 } catch (android.os.RemoteException e) {
4690 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4691 mKeyWaiter.mMotionTarget = null;
4692 try {
4693 removeWindow(target.mSession, target.mClient);
4694 } catch (java.util.NoSuchElementException ex) {
4695 // This will happen if the window has already been
4696 // removed.
4697 }
4698 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004699 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 }
Romain Guy06882f82009-06-10 13:36:04 -07004701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 /**
4703 * @return Returns true if event was dispatched, false if it was dropped for any reason
4704 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004705 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 if (DEBUG_INPUT) Log.v(
4707 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004710 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 if (focusObj == null) {
4712 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4713 if (qev != null) {
4714 mQueue.recycleEvent(qev);
4715 }
4716 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004717 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
4719 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4720 if (qev != null) {
4721 mQueue.recycleEvent(qev);
4722 }
4723 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004724 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
Romain Guy06882f82009-06-10 13:36:04 -07004726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 if (uid != 0 && uid != focus.mSession.mUid) {
4730 if (mContext.checkPermission(
4731 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4732 != PackageManager.PERMISSION_GRANTED) {
4733 Log.w(TAG, "Permission denied: injecting key event from pid "
4734 + pid + " uid " + uid + " to window " + focus
4735 + " owned by uid " + focus.mSession.mUid);
4736 if (qev != null) {
4737 mQueue.recycleEvent(qev);
4738 }
4739 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004740 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 }
4742 }
Romain Guy06882f82009-06-10 13:36:04 -07004743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 synchronized(mWindowMap) {
4747 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4748 mKeyWaiter.bindTargetWindowLocked(focus,
4749 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4750 // We don't deliver movement events to the client, we hold
4751 // them and wait for them to call back.
4752 ev = null;
4753 } else {
4754 mKeyWaiter.bindTargetWindowLocked(focus);
4755 }
4756 }
Romain Guy06882f82009-06-10 13:36:04 -07004757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004759 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004760 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 } catch (android.os.RemoteException e) {
4762 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4763 try {
4764 removeWindow(focus.mSession, focus.mClient);
4765 } catch (java.util.NoSuchElementException ex) {
4766 // This will happen if the window has already been
4767 // removed.
4768 }
4769 }
Romain Guy06882f82009-06-10 13:36:04 -07004770
Dianne Hackborncfaef692009-06-15 14:24:44 -07004771 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 }
Romain Guy06882f82009-06-10 13:36:04 -07004773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 /**
4775 * @return Returns true if event was dispatched, false if it was dropped for any reason
4776 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004777 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4779
4780 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004781 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 if (focusObj == null) {
4783 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004784 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 }
4786 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004787 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 }
Romain Guy06882f82009-06-10 13:36:04 -07004789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 if (DEBUG_INPUT) Log.v(
4793 TAG, "Dispatching to " + focus + ": " + event);
4794
4795 if (uid != 0 && uid != focus.mSession.mUid) {
4796 if (mContext.checkPermission(
4797 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4798 != PackageManager.PERMISSION_GRANTED) {
4799 Log.w(TAG, "Permission denied: injecting key event from pid "
4800 + pid + " uid " + uid + " to window " + focus
4801 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004802 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004803 }
4804 }
Romain Guy06882f82009-06-10 13:36:04 -07004805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 synchronized(mWindowMap) {
4807 mKeyWaiter.bindTargetWindowLocked(focus);
4808 }
4809
4810 // NOSHIP extra state logging
4811 mKeyWaiter.recordDispatchState(event, focus);
4812 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 try {
4815 if (DEBUG_INPUT || DEBUG_FOCUS) {
4816 Log.v(TAG, "Delivering key " + event.getKeyCode()
4817 + " to " + focus);
4818 }
4819 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004820 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 } catch (android.os.RemoteException e) {
4822 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4823 try {
4824 removeWindow(focus.mSession, focus.mClient);
4825 } catch (java.util.NoSuchElementException ex) {
4826 // This will happen if the window has already been
4827 // removed.
4828 }
4829 }
Romain Guy06882f82009-06-10 13:36:04 -07004830
Dianne Hackborncfaef692009-06-15 14:24:44 -07004831 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 }
Romain Guy06882f82009-06-10 13:36:04 -07004833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 public void pauseKeyDispatching(IBinder _token) {
4835 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4836 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004837 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 }
4839
4840 synchronized (mWindowMap) {
4841 WindowToken token = mTokenMap.get(_token);
4842 if (token != null) {
4843 mKeyWaiter.pauseDispatchingLocked(token);
4844 }
4845 }
4846 }
4847
4848 public void resumeKeyDispatching(IBinder _token) {
4849 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4850 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004851 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
4853
4854 synchronized (mWindowMap) {
4855 WindowToken token = mTokenMap.get(_token);
4856 if (token != null) {
4857 mKeyWaiter.resumeDispatchingLocked(token);
4858 }
4859 }
4860 }
4861
4862 public void setEventDispatching(boolean enabled) {
4863 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4864 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004865 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 }
4867
4868 synchronized (mWindowMap) {
4869 mKeyWaiter.setEventDispatchingLocked(enabled);
4870 }
4871 }
Romain Guy06882f82009-06-10 13:36:04 -07004872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 /**
4874 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004875 *
4876 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 * {@link SystemClock#uptimeMillis()} as the timebase.)
4878 * @param sync If true, wait for the event to be completed before returning to the caller.
4879 * @return Returns true if event was dispatched, false if it was dropped for any reason
4880 */
4881 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4882 long downTime = ev.getDownTime();
4883 long eventTime = ev.getEventTime();
4884
4885 int action = ev.getAction();
4886 int code = ev.getKeyCode();
4887 int repeatCount = ev.getRepeatCount();
4888 int metaState = ev.getMetaState();
4889 int deviceId = ev.getDeviceId();
4890 int scancode = ev.getScanCode();
4891
4892 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4893 if (downTime == 0) downTime = eventTime;
4894
4895 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004896 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004898 final int pid = Binder.getCallingPid();
4899 final int uid = Binder.getCallingUid();
4900 final long ident = Binder.clearCallingIdentity();
4901 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004903 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004905 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004906 switch (result) {
4907 case INJECT_NO_PERMISSION:
4908 throw new SecurityException(
4909 "Injecting to another application requires INJECT_EVENT permission");
4910 case INJECT_SUCCEEDED:
4911 return true;
4912 }
4913 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
4915
4916 /**
4917 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004918 *
4919 * @param ev A motion event describing the pointer (touch) action. (As noted in
4920 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 * {@link SystemClock#uptimeMillis()} as the timebase.)
4922 * @param sync If true, wait for the event to be completed before returning to the caller.
4923 * @return Returns true if event was dispatched, false if it was dropped for any reason
4924 */
4925 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004926 final int pid = Binder.getCallingPid();
4927 final int uid = Binder.getCallingUid();
4928 final long ident = Binder.clearCallingIdentity();
4929 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004931 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004933 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004934 switch (result) {
4935 case INJECT_NO_PERMISSION:
4936 throw new SecurityException(
4937 "Injecting to another application requires INJECT_EVENT permission");
4938 case INJECT_SUCCEEDED:
4939 return true;
4940 }
4941 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 }
Romain Guy06882f82009-06-10 13:36:04 -07004943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 /**
4945 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004946 *
4947 * @param ev A motion event describing the trackball action. (As noted in
4948 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 * {@link SystemClock#uptimeMillis()} as the timebase.)
4950 * @param sync If true, wait for the event to be completed before returning to the caller.
4951 * @return Returns true if event was dispatched, false if it was dropped for any reason
4952 */
4953 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004954 final int pid = Binder.getCallingPid();
4955 final int uid = Binder.getCallingUid();
4956 final long ident = Binder.clearCallingIdentity();
4957 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004959 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004961 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004962 switch (result) {
4963 case INJECT_NO_PERMISSION:
4964 throw new SecurityException(
4965 "Injecting to another application requires INJECT_EVENT permission");
4966 case INJECT_SUCCEEDED:
4967 return true;
4968 }
4969 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
Romain Guy06882f82009-06-10 13:36:04 -07004971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 private WindowState getFocusedWindow() {
4973 synchronized (mWindowMap) {
4974 return getFocusedWindowLocked();
4975 }
4976 }
4977
4978 private WindowState getFocusedWindowLocked() {
4979 return mCurrentFocus;
4980 }
Romain Guy06882f82009-06-10 13:36:04 -07004981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 /**
4983 * This class holds the state for dispatching key events. This state
4984 * is protected by the KeyWaiter instance, NOT by the window lock. You
4985 * can be holding the main window lock while acquire the KeyWaiter lock,
4986 * but not the other way around.
4987 */
4988 final class KeyWaiter {
4989 // NOSHIP debugging
4990 public class DispatchState {
4991 private KeyEvent event;
4992 private WindowState focus;
4993 private long time;
4994 private WindowState lastWin;
4995 private IBinder lastBinder;
4996 private boolean finished;
4997 private boolean gotFirstWindow;
4998 private boolean eventDispatching;
4999 private long timeToSwitch;
5000 private boolean wasFrozen;
5001 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005002 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5005 focus = theFocus;
5006 event = theEvent;
5007 time = System.currentTimeMillis();
5008 // snapshot KeyWaiter state
5009 lastWin = mLastWin;
5010 lastBinder = mLastBinder;
5011 finished = mFinished;
5012 gotFirstWindow = mGotFirstWindow;
5013 eventDispatching = mEventDispatching;
5014 timeToSwitch = mTimeToSwitch;
5015 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005016 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 // cache the paused state at ctor time as well
5018 if (theFocus == null || theFocus.mToken == null) {
5019 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5020 focusPaused = false;
5021 } else {
5022 focusPaused = theFocus.mToken.paused;
5023 }
5024 }
Romain Guy06882f82009-06-10 13:36:04 -07005025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 public String toString() {
5027 return "{{" + event + " to " + focus + " @ " + time
5028 + " lw=" + lastWin + " lb=" + lastBinder
5029 + " fin=" + finished + " gfw=" + gotFirstWindow
5030 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005031 + " wf=" + wasFrozen + " fp=" + focusPaused
5032 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 }
5034 };
5035 private DispatchState mDispatchState = null;
5036 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5037 mDispatchState = new DispatchState(theEvent, theFocus);
5038 }
5039 // END NOSHIP
5040
5041 public static final int RETURN_NOTHING = 0;
5042 public static final int RETURN_PENDING_POINTER = 1;
5043 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 final Object SKIP_TARGET_TOKEN = new Object();
5046 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 private WindowState mLastWin = null;
5049 private IBinder mLastBinder = null;
5050 private boolean mFinished = true;
5051 private boolean mGotFirstWindow = false;
5052 private boolean mEventDispatching = true;
5053 private long mTimeToSwitch = 0;
5054 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 // Target of Motion events
5057 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 // Windows above the target who would like to receive an "outside"
5060 // touch event for any down events outside of them.
5061 WindowState mOutsideTouchTargets;
5062
5063 /**
5064 * Wait for the last event dispatch to complete, then find the next
5065 * target that should receive the given event and wait for that one
5066 * to be ready to receive it.
5067 */
5068 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5069 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005070 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 long startTime = SystemClock.uptimeMillis();
5072 long keyDispatchingTimeout = 5 * 1000;
5073 long waitedFor = 0;
5074
5075 while (true) {
5076 // Figure out which window we care about. It is either the
5077 // last window we are waiting to have process the event or,
5078 // if none, then the next window we think the event should go
5079 // to. Note: we retrieve mLastWin outside of the lock, so
5080 // it may change before we lock. Thus we must check it again.
5081 WindowState targetWin = mLastWin;
5082 boolean targetIsNew = targetWin == null;
5083 if (DEBUG_INPUT) Log.v(
5084 TAG, "waitForLastKey: mFinished=" + mFinished +
5085 ", mLastWin=" + mLastWin);
5086 if (targetIsNew) {
5087 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005088 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 if (target == SKIP_TARGET_TOKEN) {
5090 // The user has pressed a special key, and we are
5091 // dropping all pending events before it.
5092 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5093 + " " + nextMotion);
5094 return null;
5095 }
5096 if (target == CONSUMED_EVENT_TOKEN) {
5097 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5098 + " " + nextMotion);
5099 return target;
5100 }
5101 targetWin = (WindowState)target;
5102 }
Romain Guy06882f82009-06-10 13:36:04 -07005103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 // Now: is it okay to send the next event to this window?
5107 synchronized (this) {
5108 // First: did we come here based on the last window not
5109 // being null, but it changed by the time we got here?
5110 // If so, try again.
5111 if (!targetIsNew && mLastWin == null) {
5112 continue;
5113 }
Romain Guy06882f82009-06-10 13:36:04 -07005114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 // We never dispatch events if not finished with the
5116 // last one, or the display is frozen.
5117 if (mFinished && !mDisplayFrozen) {
5118 // If event dispatching is disabled, then we
5119 // just consume the events.
5120 if (!mEventDispatching) {
5121 if (DEBUG_INPUT) Log.v(TAG,
5122 "Skipping event; dispatching disabled: "
5123 + nextKey + " " + nextMotion);
5124 return null;
5125 }
5126 if (targetWin != null) {
5127 // If this is a new target, and that target is not
5128 // paused or unresponsive, then all looks good to
5129 // handle the event.
5130 if (targetIsNew && !targetWin.mToken.paused) {
5131 return targetWin;
5132 }
Romain Guy06882f82009-06-10 13:36:04 -07005133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 // If we didn't find a target window, and there is no
5135 // focused app window, then just eat the events.
5136 } else if (mFocusedApp == null) {
5137 if (DEBUG_INPUT) Log.v(TAG,
5138 "Skipping event; no focused app: "
5139 + nextKey + " " + nextMotion);
5140 return null;
5141 }
5142 }
Romain Guy06882f82009-06-10 13:36:04 -07005143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 if (DEBUG_INPUT) Log.v(
5145 TAG, "Waiting for last key in " + mLastBinder
5146 + " target=" + targetWin
5147 + " mFinished=" + mFinished
5148 + " mDisplayFrozen=" + mDisplayFrozen
5149 + " targetIsNew=" + targetIsNew
5150 + " paused="
5151 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005152 + " mFocusedApp=" + mFocusedApp
5153 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 targetApp = targetWin != null
5156 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005158 long curTimeout = keyDispatchingTimeout;
5159 if (mTimeToSwitch != 0) {
5160 long now = SystemClock.uptimeMillis();
5161 if (mTimeToSwitch <= now) {
5162 // If an app switch key has been pressed, and we have
5163 // waited too long for the current app to finish
5164 // processing keys, then wait no more!
5165 doFinishedKeyLocked(true);
5166 continue;
5167 }
5168 long switchTimeout = mTimeToSwitch - now;
5169 if (curTimeout > switchTimeout) {
5170 curTimeout = switchTimeout;
5171 }
5172 }
Romain Guy06882f82009-06-10 13:36:04 -07005173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 try {
5175 // after that continue
5176 // processing keys, so we don't get stuck.
5177 if (DEBUG_INPUT) Log.v(
5178 TAG, "Waiting for key dispatch: " + curTimeout);
5179 wait(curTimeout);
5180 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5181 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005182 + startTime + " switchTime=" + mTimeToSwitch
5183 + " target=" + targetWin + " mLW=" + mLastWin
5184 + " mLB=" + mLastBinder + " fin=" + mFinished
5185 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 } catch (InterruptedException e) {
5187 }
5188 }
5189
5190 // If we were frozen during configuration change, restart the
5191 // timeout checks from now; otherwise look at whether we timed
5192 // out before awakening.
5193 if (mWasFrozen) {
5194 waitedFor = 0;
5195 mWasFrozen = false;
5196 } else {
5197 waitedFor = SystemClock.uptimeMillis() - startTime;
5198 }
5199
5200 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5201 IApplicationToken at = null;
5202 synchronized (this) {
5203 Log.w(TAG, "Key dispatching timed out sending to " +
5204 (targetWin != null ? targetWin.mAttrs.getTitle()
5205 : "<null>"));
5206 // NOSHIP debugging
5207 Log.w(TAG, "Dispatch state: " + mDispatchState);
5208 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5209 // END NOSHIP
5210 //dump();
5211 if (targetWin != null) {
5212 at = targetWin.getAppToken();
5213 } else if (targetApp != null) {
5214 at = targetApp.appToken;
5215 }
5216 }
5217
5218 boolean abort = true;
5219 if (at != null) {
5220 try {
5221 long timeout = at.getKeyDispatchingTimeout();
5222 if (timeout > waitedFor) {
5223 // we did not wait the proper amount of time for this application.
5224 // set the timeout to be the real timeout and wait again.
5225 keyDispatchingTimeout = timeout - waitedFor;
5226 continue;
5227 } else {
5228 abort = at.keyDispatchingTimedOut();
5229 }
5230 } catch (RemoteException ex) {
5231 }
5232 }
5233
5234 synchronized (this) {
5235 if (abort && (mLastWin == targetWin || targetWin == null)) {
5236 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005237 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 if (DEBUG_INPUT) Log.v(TAG,
5239 "Window " + mLastWin +
5240 " timed out on key input");
5241 if (mLastWin.mToken.paused) {
5242 Log.w(TAG, "Un-pausing dispatching to this window");
5243 mLastWin.mToken.paused = false;
5244 }
5245 }
5246 if (mMotionTarget == targetWin) {
5247 mMotionTarget = null;
5248 }
5249 mLastWin = null;
5250 mLastBinder = null;
5251 if (failIfTimeout || targetWin == null) {
5252 return null;
5253 }
5254 } else {
5255 Log.w(TAG, "Continuing to wait for key to be dispatched");
5256 startTime = SystemClock.uptimeMillis();
5257 }
5258 }
5259 }
5260 }
5261 }
Romain Guy06882f82009-06-10 13:36:04 -07005262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005264 MotionEvent nextMotion, boolean isPointerEvent,
5265 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 if (nextKey != null) {
5269 // Find the target window for a normal key event.
5270 final int keycode = nextKey.getKeyCode();
5271 final int repeatCount = nextKey.getRepeatCount();
5272 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5273 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005276 if (callingUid == 0 ||
5277 mContext.checkPermission(
5278 android.Manifest.permission.INJECT_EVENTS,
5279 callingPid, callingUid)
5280 == PackageManager.PERMISSION_GRANTED) {
5281 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005282 nextKey.getMetaState(), down, repeatCount,
5283 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 Log.w(TAG, "Event timeout during app switch: dropping "
5286 + nextKey);
5287 return SKIP_TARGET_TOKEN;
5288 }
Romain Guy06882f82009-06-10 13:36:04 -07005289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 WindowState focus = null;
5293 synchronized(mWindowMap) {
5294 focus = getFocusedWindowLocked();
5295 }
Romain Guy06882f82009-06-10 13:36:04 -07005296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005298
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005299 if (callingUid == 0 ||
5300 (focus != null && callingUid == focus.mSession.mUid) ||
5301 mContext.checkPermission(
5302 android.Manifest.permission.INJECT_EVENTS,
5303 callingPid, callingUid)
5304 == PackageManager.PERMISSION_GRANTED) {
5305 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005306 keycode, nextKey.getMetaState(), down, repeatCount,
5307 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005308 return CONSUMED_EVENT_TOKEN;
5309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 }
Romain Guy06882f82009-06-10 13:36:04 -07005311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 } else if (!isPointerEvent) {
5315 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5316 if (!dispatch) {
5317 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5318 + nextMotion);
5319 return SKIP_TARGET_TOKEN;
5320 }
Romain Guy06882f82009-06-10 13:36:04 -07005321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 WindowState focus = null;
5323 synchronized(mWindowMap) {
5324 focus = getFocusedWindowLocked();
5325 }
Romain Guy06882f82009-06-10 13:36:04 -07005326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5328 return focus;
5329 }
Romain Guy06882f82009-06-10 13:36:04 -07005330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 if (nextMotion == null) {
5332 return SKIP_TARGET_TOKEN;
5333 }
Romain Guy06882f82009-06-10 13:36:04 -07005334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5336 KeyEvent.KEYCODE_UNKNOWN);
5337 if (!dispatch) {
5338 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5339 + nextMotion);
5340 return SKIP_TARGET_TOKEN;
5341 }
Romain Guy06882f82009-06-10 13:36:04 -07005342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 // Find the target window for a pointer event.
5344 int action = nextMotion.getAction();
5345 final float xf = nextMotion.getX();
5346 final float yf = nextMotion.getY();
5347 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 final boolean screenWasOff = qev != null
5350 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 synchronized(mWindowMap) {
5355 synchronized (this) {
5356 if (action == MotionEvent.ACTION_DOWN) {
5357 if (mMotionTarget != null) {
5358 // this is weird, we got a pen down, but we thought it was
5359 // already down!
5360 // XXX: We should probably send an ACTION_UP to the current
5361 // target.
5362 Log.w(TAG, "Pointer down received while already down in: "
5363 + mMotionTarget);
5364 mMotionTarget = null;
5365 }
Romain Guy06882f82009-06-10 13:36:04 -07005366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 // ACTION_DOWN is special, because we need to lock next events to
5368 // the window we'll land onto.
5369 final int x = (int)xf;
5370 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 final ArrayList windows = mWindows;
5373 final int N = windows.size();
5374 WindowState topErrWindow = null;
5375 final Rect tmpRect = mTempRect;
5376 for (int i=N-1; i>=0; i--) {
5377 WindowState child = (WindowState)windows.get(i);
5378 //Log.i(TAG, "Checking dispatch to: " + child);
5379 final int flags = child.mAttrs.flags;
5380 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5381 if (topErrWindow == null) {
5382 topErrWindow = child;
5383 }
5384 }
5385 if (!child.isVisibleLw()) {
5386 //Log.i(TAG, "Not visible!");
5387 continue;
5388 }
5389 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5390 //Log.i(TAG, "Not touchable!");
5391 if ((flags & WindowManager.LayoutParams
5392 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5393 child.mNextOutsideTouch = mOutsideTouchTargets;
5394 mOutsideTouchTargets = child;
5395 }
5396 continue;
5397 }
5398 tmpRect.set(child.mFrame);
5399 if (child.mTouchableInsets == ViewTreeObserver
5400 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5401 // The touch is inside of the window if it is
5402 // inside the frame, AND the content part of that
5403 // frame that was given by the application.
5404 tmpRect.left += child.mGivenContentInsets.left;
5405 tmpRect.top += child.mGivenContentInsets.top;
5406 tmpRect.right -= child.mGivenContentInsets.right;
5407 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5408 } else if (child.mTouchableInsets == ViewTreeObserver
5409 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5410 // The touch is inside of the window if it is
5411 // inside the frame, AND the visible part of that
5412 // frame that was given by the application.
5413 tmpRect.left += child.mGivenVisibleInsets.left;
5414 tmpRect.top += child.mGivenVisibleInsets.top;
5415 tmpRect.right -= child.mGivenVisibleInsets.right;
5416 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5417 }
5418 final int touchFlags = flags &
5419 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5420 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5421 if (tmpRect.contains(x, y) || touchFlags == 0) {
5422 //Log.i(TAG, "Using this target!");
5423 if (!screenWasOff || (flags &
5424 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5425 mMotionTarget = child;
5426 } else {
5427 //Log.i(TAG, "Waking, skip!");
5428 mMotionTarget = null;
5429 }
5430 break;
5431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 if ((flags & WindowManager.LayoutParams
5434 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5435 child.mNextOutsideTouch = mOutsideTouchTargets;
5436 mOutsideTouchTargets = child;
5437 //Log.i(TAG, "Adding to outside target list: " + child);
5438 }
5439 }
5440
5441 // if there's an error window but it's not accepting
5442 // focus (typically because it is not yet visible) just
5443 // wait for it -- any other focused window may in fact
5444 // be in ANR state.
5445 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5446 mMotionTarget = null;
5447 }
5448 }
Romain Guy06882f82009-06-10 13:36:04 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 target = mMotionTarget;
5451 }
5452 }
Romain Guy06882f82009-06-10 13:36:04 -07005453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 // Pointer events are a little different -- if there isn't a
5457 // target found for any event, then just drop it.
5458 return target != null ? target : SKIP_TARGET_TOKEN;
5459 }
Romain Guy06882f82009-06-10 13:36:04 -07005460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 boolean checkShouldDispatchKey(int keycode) {
5462 synchronized (this) {
5463 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5464 mTimeToSwitch = 0;
5465 return true;
5466 }
5467 if (mTimeToSwitch != 0
5468 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5469 return false;
5470 }
5471 return true;
5472 }
5473 }
Romain Guy06882f82009-06-10 13:36:04 -07005474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 void bindTargetWindowLocked(WindowState win,
5476 int pendingWhat, QueuedEvent pendingMotion) {
5477 synchronized (this) {
5478 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5479 }
5480 }
Romain Guy06882f82009-06-10 13:36:04 -07005481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 void bindTargetWindowLocked(WindowState win) {
5483 synchronized (this) {
5484 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5485 }
5486 }
5487
5488 void bindTargetWindowLockedLocked(WindowState win,
5489 int pendingWhat, QueuedEvent pendingMotion) {
5490 mLastWin = win;
5491 mLastBinder = win.mClient.asBinder();
5492 mFinished = false;
5493 if (pendingMotion != null) {
5494 final Session s = win.mSession;
5495 if (pendingWhat == RETURN_PENDING_POINTER) {
5496 releasePendingPointerLocked(s);
5497 s.mPendingPointerMove = pendingMotion;
5498 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005499 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 "bindTargetToWindow " + s.mPendingPointerMove);
5501 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5502 releasePendingTrackballLocked(s);
5503 s.mPendingTrackballMove = pendingMotion;
5504 s.mPendingTrackballWindow = win;
5505 }
5506 }
5507 }
Romain Guy06882f82009-06-10 13:36:04 -07005508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 void releasePendingPointerLocked(Session s) {
5510 if (DEBUG_INPUT) Log.v(TAG,
5511 "releasePendingPointer " + s.mPendingPointerMove);
5512 if (s.mPendingPointerMove != null) {
5513 mQueue.recycleEvent(s.mPendingPointerMove);
5514 s.mPendingPointerMove = null;
5515 }
5516 }
Romain Guy06882f82009-06-10 13:36:04 -07005517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 void releasePendingTrackballLocked(Session s) {
5519 if (s.mPendingTrackballMove != null) {
5520 mQueue.recycleEvent(s.mPendingTrackballMove);
5521 s.mPendingTrackballMove = null;
5522 }
5523 }
Romain Guy06882f82009-06-10 13:36:04 -07005524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005525 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5526 int returnWhat) {
5527 if (DEBUG_INPUT) Log.v(
5528 TAG, "finishedKey: client=" + client + ", force=" + force);
5529
5530 if (client == null) {
5531 return null;
5532 }
5533
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005534 MotionEvent res = null;
5535 QueuedEvent qev = null;
5536 WindowState win = null;
5537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 synchronized (this) {
5539 if (DEBUG_INPUT) Log.v(
5540 TAG, "finishedKey: client=" + client.asBinder()
5541 + ", force=" + force + ", last=" + mLastBinder
5542 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 if (returnWhat == RETURN_PENDING_POINTER) {
5545 qev = session.mPendingPointerMove;
5546 win = session.mPendingPointerWindow;
5547 session.mPendingPointerMove = null;
5548 session.mPendingPointerWindow = null;
5549 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5550 qev = session.mPendingTrackballMove;
5551 win = session.mPendingTrackballWindow;
5552 session.mPendingTrackballMove = null;
5553 session.mPendingTrackballWindow = null;
5554 }
Romain Guy06882f82009-06-10 13:36:04 -07005555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 if (mLastBinder == client.asBinder()) {
5557 if (DEBUG_INPUT) Log.v(
5558 TAG, "finishedKey: last paused="
5559 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5560 if (mLastWin != null && (!mLastWin.mToken.paused || force
5561 || !mEventDispatching)) {
5562 doFinishedKeyLocked(false);
5563 } else {
5564 // Make sure to wake up anyone currently waiting to
5565 // dispatch a key, so they can re-evaluate their
5566 // current situation.
5567 mFinished = true;
5568 notifyAll();
5569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 }
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005573 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 if (DEBUG_INPUT) Log.v(TAG,
5575 "Returning pending motion: " + res);
5576 mQueue.recycleEvent(qev);
5577 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5578 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005582
5583 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5584 synchronized (mWindowMap) {
5585 if (mWallpaperTarget == win) {
5586 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5587 }
5588 }
5589 }
5590
5591 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 }
5593
5594 void tickle() {
5595 synchronized (this) {
5596 notifyAll();
5597 }
5598 }
Romain Guy06882f82009-06-10 13:36:04 -07005599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 void handleNewWindowLocked(WindowState newWindow) {
5601 if (!newWindow.canReceiveKeys()) {
5602 return;
5603 }
5604 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005605 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 TAG, "New key dispatch window: win="
5607 + newWindow.mClient.asBinder()
5608 + ", last=" + mLastBinder
5609 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5610 + "), finished=" + mFinished + ", paused="
5611 + newWindow.mToken.paused);
5612
5613 // Displaying a window implicitly causes dispatching to
5614 // be unpaused. (This is to protect against bugs if someone
5615 // pauses dispatching but forgets to resume.)
5616 newWindow.mToken.paused = false;
5617
5618 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619
5620 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5621 if (DEBUG_INPUT) Log.v(TAG,
5622 "New SYSTEM_ERROR window; resetting state");
5623 mLastWin = null;
5624 mLastBinder = null;
5625 mMotionTarget = null;
5626 mFinished = true;
5627 } else if (mLastWin != null) {
5628 // If the new window is above the window we are
5629 // waiting on, then stop waiting and let key dispatching
5630 // start on the new guy.
5631 if (DEBUG_INPUT) Log.v(
5632 TAG, "Last win layer=" + mLastWin.mLayer
5633 + ", new win layer=" + newWindow.mLayer);
5634 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005635 // The new window is above the old; finish pending input to the last
5636 // window and start directing it to the new one.
5637 mLastWin.mToken.paused = false;
5638 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005640 // Either the new window is lower, so there is no need to wake key waiters,
5641 // or we just finished key input to the previous window, which implicitly
5642 // notified the key waiters. In both cases, we don't need to issue the
5643 // notification here.
5644 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 }
5646
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005647 // Now that we've put a new window state in place, make the event waiter
5648 // take notice and retarget its attentions.
5649 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 }
5651 }
5652
5653 void pauseDispatchingLocked(WindowToken token) {
5654 synchronized (this)
5655 {
5656 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5657 token.paused = true;
5658
5659 /*
5660 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5661 mPaused = true;
5662 } else {
5663 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005664 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005666 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005668 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 }
5670 }
5671 */
5672 }
5673 }
5674
5675 void resumeDispatchingLocked(WindowToken token) {
5676 synchronized (this) {
5677 if (token.paused) {
5678 if (DEBUG_INPUT) Log.v(
5679 TAG, "Resuming WindowToken " + token
5680 + ", last=" + mLastBinder
5681 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5682 + "), finished=" + mFinished + ", paused="
5683 + token.paused);
5684 token.paused = false;
5685 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5686 doFinishedKeyLocked(true);
5687 } else {
5688 notifyAll();
5689 }
5690 }
5691 }
5692 }
5693
5694 void setEventDispatchingLocked(boolean enabled) {
5695 synchronized (this) {
5696 mEventDispatching = enabled;
5697 notifyAll();
5698 }
5699 }
Romain Guy06882f82009-06-10 13:36:04 -07005700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 void appSwitchComing() {
5702 synchronized (this) {
5703 // Don't wait for more than .5 seconds for app to finish
5704 // processing the pending events.
5705 long now = SystemClock.uptimeMillis() + 500;
5706 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5707 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5708 mTimeToSwitch = now;
5709 }
5710 notifyAll();
5711 }
5712 }
Romain Guy06882f82009-06-10 13:36:04 -07005713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 private final void doFinishedKeyLocked(boolean doRecycle) {
5715 if (mLastWin != null) {
5716 releasePendingPointerLocked(mLastWin.mSession);
5717 releasePendingTrackballLocked(mLastWin.mSession);
5718 }
Romain Guy06882f82009-06-10 13:36:04 -07005719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 if (mLastWin == null || !mLastWin.mToken.paused
5721 || !mLastWin.isVisibleLw()) {
5722 // If the current window has been paused, we aren't -really-
5723 // finished... so let the waiters still wait.
5724 mLastWin = null;
5725 mLastBinder = null;
5726 }
5727 mFinished = true;
5728 notifyAll();
5729 }
5730 }
5731
5732 private class KeyQ extends KeyInputQueue
5733 implements KeyInputQueue.FilterCallback {
5734 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005737 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5739 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5740 "KEEP_SCREEN_ON_FLAG");
5741 mHoldingScreen.setReferenceCounted(false);
5742 }
5743
5744 @Override
5745 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5746 if (mPolicy.preprocessInputEventTq(event)) {
5747 return true;
5748 }
Romain Guy06882f82009-06-10 13:36:04 -07005749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 switch (event.type) {
5751 case RawInputEvent.EV_KEY: {
5752 // XXX begin hack
5753 if (DEBUG) {
5754 if (event.keycode == KeyEvent.KEYCODE_G) {
5755 if (event.value != 0) {
5756 // G down
5757 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5758 }
5759 return false;
5760 }
5761 if (event.keycode == KeyEvent.KEYCODE_D) {
5762 if (event.value != 0) {
5763 //dump();
5764 }
5765 return false;
5766 }
5767 }
5768 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 boolean screenIsOff = !mPowerManager.screenIsOn();
5771 boolean screenIsDim = !mPowerManager.screenIsBright();
5772 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5775 mPowerManager.goToSleep(event.when);
5776 }
5777
5778 if (screenIsOff) {
5779 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5780 }
5781 if (screenIsDim) {
5782 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5783 }
5784 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5785 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005786 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 }
Romain Guy06882f82009-06-10 13:36:04 -07005788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5790 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5791 filterQueue(this);
5792 mKeyWaiter.appSwitchComing();
5793 }
5794 return true;
5795 } else {
5796 return false;
5797 }
5798 }
Romain Guy06882f82009-06-10 13:36:04 -07005799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800 case RawInputEvent.EV_REL: {
5801 boolean screenIsOff = !mPowerManager.screenIsOn();
5802 boolean screenIsDim = !mPowerManager.screenIsBright();
5803 if (screenIsOff) {
5804 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5805 device.classes, event)) {
5806 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5807 return false;
5808 }
5809 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5810 }
5811 if (screenIsDim) {
5812 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5813 }
5814 return true;
5815 }
Romain Guy06882f82009-06-10 13:36:04 -07005816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 case RawInputEvent.EV_ABS: {
5818 boolean screenIsOff = !mPowerManager.screenIsOn();
5819 boolean screenIsDim = !mPowerManager.screenIsBright();
5820 if (screenIsOff) {
5821 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5822 device.classes, event)) {
5823 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5824 return false;
5825 }
5826 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5827 }
5828 if (screenIsDim) {
5829 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5830 }
5831 return true;
5832 }
Romain Guy06882f82009-06-10 13:36:04 -07005833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 default:
5835 return true;
5836 }
5837 }
5838
5839 public int filterEvent(QueuedEvent ev) {
5840 switch (ev.classType) {
5841 case RawInputEvent.CLASS_KEYBOARD:
5842 KeyEvent ke = (KeyEvent)ev.event;
5843 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5844 Log.w(TAG, "Dropping movement key during app switch: "
5845 + ke.getKeyCode() + ", action=" + ke.getAction());
5846 return FILTER_REMOVE;
5847 }
5848 return FILTER_ABORT;
5849 default:
5850 return FILTER_KEEP;
5851 }
5852 }
Romain Guy06882f82009-06-10 13:36:04 -07005853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 /**
5855 * Must be called with the main window manager lock held.
5856 */
5857 void setHoldScreenLocked(boolean holding) {
5858 boolean state = mHoldingScreen.isHeld();
5859 if (holding != state) {
5860 if (holding) {
5861 mHoldingScreen.acquire();
5862 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005863 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 mHoldingScreen.release();
5865 }
5866 }
5867 }
Michael Chan53071d62009-05-13 17:29:48 -07005868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869
5870 public boolean detectSafeMode() {
5871 mSafeMode = mPolicy.detectSafeMode();
5872 return mSafeMode;
5873 }
Romain Guy06882f82009-06-10 13:36:04 -07005874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 public void systemReady() {
5876 mPolicy.systemReady();
5877 }
Romain Guy06882f82009-06-10 13:36:04 -07005878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 private final class InputDispatcherThread extends Thread {
5880 // Time to wait when there is nothing to do: 9999 seconds.
5881 static final int LONG_WAIT=9999*1000;
5882
5883 public InputDispatcherThread() {
5884 super("InputDispatcher");
5885 }
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 @Override
5888 public void run() {
5889 while (true) {
5890 try {
5891 process();
5892 } catch (Exception e) {
5893 Log.e(TAG, "Exception in input dispatcher", e);
5894 }
5895 }
5896 }
Romain Guy06882f82009-06-10 13:36:04 -07005897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 private void process() {
5899 android.os.Process.setThreadPriority(
5900 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 // The last key event we saw
5903 KeyEvent lastKey = null;
5904
5905 // Last keydown time for auto-repeating keys
5906 long lastKeyTime = SystemClock.uptimeMillis();
5907 long nextKeyTime = lastKeyTime+LONG_WAIT;
5908
Romain Guy06882f82009-06-10 13:36:04 -07005909 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 int keyRepeatCount = 0;
5911
5912 // Need to report that configuration has changed?
5913 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 while (true) {
5916 long curTime = SystemClock.uptimeMillis();
5917
5918 if (DEBUG_INPUT) Log.v(
5919 TAG, "Waiting for next key: now=" + curTime
5920 + ", repeat @ " + nextKeyTime);
5921
5922 // Retrieve next event, waiting only as long as the next
5923 // repeat timeout. If the configuration has changed, then
5924 // don't wait at all -- we'll report the change as soon as
5925 // we have processed all events.
5926 QueuedEvent ev = mQueue.getEvent(
5927 (int)((!configChanged && curTime < nextKeyTime)
5928 ? (nextKeyTime-curTime) : 0));
5929
5930 if (DEBUG_INPUT && ev != null) Log.v(
5931 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5932
Michael Chan53071d62009-05-13 17:29:48 -07005933 if (MEASURE_LATENCY) {
5934 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5935 }
5936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 try {
5938 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005939 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 int eventType;
5941 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5942 eventType = eventType((MotionEvent)ev.event);
5943 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5944 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5945 eventType = LocalPowerManager.BUTTON_EVENT;
5946 } else {
5947 eventType = LocalPowerManager.OTHER_EVENT;
5948 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005949 try {
Michael Chan53071d62009-05-13 17:29:48 -07005950 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005951 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005952 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005953 mBatteryStats.noteInputEvent();
5954 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005955 } catch (RemoteException e) {
5956 // Ignore
5957 }
Michael Chane10de972009-05-18 11:24:50 -07005958
5959 if (eventType != TOUCH_EVENT
5960 && eventType != LONG_TOUCH_EVENT
5961 && eventType != CHEEK_EVENT) {
5962 mPowerManager.userActivity(curTime, false,
5963 eventType, false);
5964 } else if (mLastTouchEventType != eventType
5965 || (curTime - mLastUserActivityCallTime)
5966 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5967 mLastUserActivityCallTime = curTime;
5968 mLastTouchEventType = eventType;
5969 mPowerManager.userActivity(curTime, false,
5970 eventType, false);
5971 }
5972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 switch (ev.classType) {
5974 case RawInputEvent.CLASS_KEYBOARD:
5975 KeyEvent ke = (KeyEvent)ev.event;
5976 if (ke.isDown()) {
5977 lastKey = ke;
5978 keyRepeatCount = 0;
5979 lastKeyTime = curTime;
5980 nextKeyTime = lastKeyTime
5981 + KEY_REPEAT_FIRST_DELAY;
5982 if (DEBUG_INPUT) Log.v(
5983 TAG, "Received key down: first repeat @ "
5984 + nextKeyTime);
5985 } else {
5986 lastKey = null;
5987 // Arbitrary long timeout.
5988 lastKeyTime = curTime;
5989 nextKeyTime = curTime + LONG_WAIT;
5990 if (DEBUG_INPUT) Log.v(
5991 TAG, "Received key up: ignore repeat @ "
5992 + nextKeyTime);
5993 }
5994 dispatchKey((KeyEvent)ev.event, 0, 0);
5995 mQueue.recycleEvent(ev);
5996 break;
5997 case RawInputEvent.CLASS_TOUCHSCREEN:
5998 //Log.i(TAG, "Read next event " + ev);
5999 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6000 break;
6001 case RawInputEvent.CLASS_TRACKBALL:
6002 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6003 break;
6004 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6005 configChanged = true;
6006 break;
6007 default:
6008 mQueue.recycleEvent(ev);
6009 break;
6010 }
Romain Guy06882f82009-06-10 13:36:04 -07006011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 } else if (configChanged) {
6013 configChanged = false;
6014 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 } else if (lastKey != null) {
6017 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 // Timeout occurred while key was down. If it is at or
6020 // past the key repeat time, dispatch the repeat.
6021 if (DEBUG_INPUT) Log.v(
6022 TAG, "Key timeout: repeat=" + nextKeyTime
6023 + ", now=" + curTime);
6024 if (curTime < nextKeyTime) {
6025 continue;
6026 }
Romain Guy06882f82009-06-10 13:36:04 -07006027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 lastKeyTime = nextKeyTime;
6029 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6030 keyRepeatCount++;
6031 if (DEBUG_INPUT) Log.v(
6032 TAG, "Key repeat: count=" + keyRepeatCount
6033 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006034 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 } else {
6037 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 lastKeyTime = curTime;
6040 nextKeyTime = curTime + LONG_WAIT;
6041 }
Romain Guy06882f82009-06-10 13:36:04 -07006042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 } catch (Exception e) {
6044 Log.e(TAG,
6045 "Input thread received uncaught exception: " + e, e);
6046 }
6047 }
6048 }
6049 }
6050
6051 // -------------------------------------------------------------
6052 // Client Session State
6053 // -------------------------------------------------------------
6054
6055 private final class Session extends IWindowSession.Stub
6056 implements IBinder.DeathRecipient {
6057 final IInputMethodClient mClient;
6058 final IInputContext mInputContext;
6059 final int mUid;
6060 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006061 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006062 SurfaceSession mSurfaceSession;
6063 int mNumWindow = 0;
6064 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 /**
6067 * Current pointer move event being dispatched to client window... must
6068 * hold key lock to access.
6069 */
6070 QueuedEvent mPendingPointerMove;
6071 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 /**
6074 * Current trackball move event being dispatched to client window... must
6075 * hold key lock to access.
6076 */
6077 QueuedEvent mPendingTrackballMove;
6078 WindowState mPendingTrackballWindow;
6079
6080 public Session(IInputMethodClient client, IInputContext inputContext) {
6081 mClient = client;
6082 mInputContext = inputContext;
6083 mUid = Binder.getCallingUid();
6084 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006085 StringBuilder sb = new StringBuilder();
6086 sb.append("Session{");
6087 sb.append(Integer.toHexString(System.identityHashCode(this)));
6088 sb.append(" uid ");
6089 sb.append(mUid);
6090 sb.append("}");
6091 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 synchronized (mWindowMap) {
6094 if (mInputMethodManager == null && mHaveInputMethods) {
6095 IBinder b = ServiceManager.getService(
6096 Context.INPUT_METHOD_SERVICE);
6097 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6098 }
6099 }
6100 long ident = Binder.clearCallingIdentity();
6101 try {
6102 // Note: it is safe to call in to the input method manager
6103 // here because we are not holding our lock.
6104 if (mInputMethodManager != null) {
6105 mInputMethodManager.addClient(client, inputContext,
6106 mUid, mPid);
6107 } else {
6108 client.setUsingInputMethod(false);
6109 }
6110 client.asBinder().linkToDeath(this, 0);
6111 } catch (RemoteException e) {
6112 // The caller has died, so we can just forget about this.
6113 try {
6114 if (mInputMethodManager != null) {
6115 mInputMethodManager.removeClient(client);
6116 }
6117 } catch (RemoteException ee) {
6118 }
6119 } finally {
6120 Binder.restoreCallingIdentity(ident);
6121 }
6122 }
Romain Guy06882f82009-06-10 13:36:04 -07006123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 @Override
6125 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6126 throws RemoteException {
6127 try {
6128 return super.onTransact(code, data, reply, flags);
6129 } catch (RuntimeException e) {
6130 // Log all 'real' exceptions thrown to the caller
6131 if (!(e instanceof SecurityException)) {
6132 Log.e(TAG, "Window Session Crash", e);
6133 }
6134 throw e;
6135 }
6136 }
6137
6138 public void binderDied() {
6139 // Note: it is safe to call in to the input method manager
6140 // here because we are not holding our lock.
6141 try {
6142 if (mInputMethodManager != null) {
6143 mInputMethodManager.removeClient(mClient);
6144 }
6145 } catch (RemoteException e) {
6146 }
6147 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006148 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 mClientDead = true;
6150 killSessionLocked();
6151 }
6152 }
6153
6154 public int add(IWindow window, WindowManager.LayoutParams attrs,
6155 int viewVisibility, Rect outContentInsets) {
6156 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6157 }
Romain Guy06882f82009-06-10 13:36:04 -07006158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 public void remove(IWindow window) {
6160 removeWindow(this, window);
6161 }
Romain Guy06882f82009-06-10 13:36:04 -07006162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6164 int requestedWidth, int requestedHeight, int viewFlags,
6165 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6166 Rect outVisibleInsets, Surface outSurface) {
6167 return relayoutWindow(this, window, attrs,
6168 requestedWidth, requestedHeight, viewFlags, insetsPending,
6169 outFrame, outContentInsets, outVisibleInsets, outSurface);
6170 }
Romain Guy06882f82009-06-10 13:36:04 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 public void setTransparentRegion(IWindow window, Region region) {
6173 setTransparentRegionWindow(this, window, region);
6174 }
Romain Guy06882f82009-06-10 13:36:04 -07006175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 public void setInsets(IWindow window, int touchableInsets,
6177 Rect contentInsets, Rect visibleInsets) {
6178 setInsetsWindow(this, window, touchableInsets, contentInsets,
6179 visibleInsets);
6180 }
Romain Guy06882f82009-06-10 13:36:04 -07006181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6183 getWindowDisplayFrame(this, window, outDisplayFrame);
6184 }
Romain Guy06882f82009-06-10 13:36:04 -07006185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 public void finishDrawing(IWindow window) {
6187 if (localLOGV) Log.v(
6188 TAG, "IWindow finishDrawing called for " + window);
6189 finishDrawingWindow(this, window);
6190 }
6191
6192 public void finishKey(IWindow window) {
6193 if (localLOGV) Log.v(
6194 TAG, "IWindow finishKey called for " + window);
6195 mKeyWaiter.finishedKey(this, window, false,
6196 KeyWaiter.RETURN_NOTHING);
6197 }
6198
6199 public MotionEvent getPendingPointerMove(IWindow window) {
6200 if (localLOGV) Log.v(
6201 TAG, "IWindow getPendingMotionEvent called for " + window);
6202 return mKeyWaiter.finishedKey(this, window, false,
6203 KeyWaiter.RETURN_PENDING_POINTER);
6204 }
Romain Guy06882f82009-06-10 13:36:04 -07006205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 public MotionEvent getPendingTrackballMove(IWindow window) {
6207 if (localLOGV) Log.v(
6208 TAG, "IWindow getPendingMotionEvent called for " + window);
6209 return mKeyWaiter.finishedKey(this, window, false,
6210 KeyWaiter.RETURN_PENDING_TRACKBALL);
6211 }
6212
6213 public void setInTouchMode(boolean mode) {
6214 synchronized(mWindowMap) {
6215 mInTouchMode = mode;
6216 }
6217 }
6218
6219 public boolean getInTouchMode() {
6220 synchronized(mWindowMap) {
6221 return mInTouchMode;
6222 }
6223 }
6224
6225 public boolean performHapticFeedback(IWindow window, int effectId,
6226 boolean always) {
6227 synchronized(mWindowMap) {
6228 long ident = Binder.clearCallingIdentity();
6229 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006230 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 windowForClientLocked(this, window), effectId, always);
6232 } finally {
6233 Binder.restoreCallingIdentity(ident);
6234 }
6235 }
6236 }
Romain Guy06882f82009-06-10 13:36:04 -07006237
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006238 public void setWallpaperPosition(IBinder window, float x, float y) {
6239 synchronized(mWindowMap) {
6240 long ident = Binder.clearCallingIdentity();
6241 try {
6242 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6243 x, y);
6244 } finally {
6245 Binder.restoreCallingIdentity(ident);
6246 }
6247 }
6248 }
6249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 void windowAddedLocked() {
6251 if (mSurfaceSession == null) {
6252 if (localLOGV) Log.v(
6253 TAG, "First window added to " + this + ", creating SurfaceSession");
6254 mSurfaceSession = new SurfaceSession();
6255 mSessions.add(this);
6256 }
6257 mNumWindow++;
6258 }
6259
6260 void windowRemovedLocked() {
6261 mNumWindow--;
6262 killSessionLocked();
6263 }
Romain Guy06882f82009-06-10 13:36:04 -07006264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 void killSessionLocked() {
6266 if (mNumWindow <= 0 && mClientDead) {
6267 mSessions.remove(this);
6268 if (mSurfaceSession != null) {
6269 if (localLOGV) Log.v(
6270 TAG, "Last window removed from " + this
6271 + ", destroying " + mSurfaceSession);
6272 try {
6273 mSurfaceSession.kill();
6274 } catch (Exception e) {
6275 Log.w(TAG, "Exception thrown when killing surface session "
6276 + mSurfaceSession + " in session " + this
6277 + ": " + e.toString());
6278 }
6279 mSurfaceSession = null;
6280 }
6281 }
6282 }
Romain Guy06882f82009-06-10 13:36:04 -07006283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006285 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6286 pw.print(" mClientDead="); pw.print(mClientDead);
6287 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6288 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6289 pw.print(prefix);
6290 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6291 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6292 }
6293 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6294 pw.print(prefix);
6295 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6296 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 }
6299
6300 @Override
6301 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006302 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 }
6304 }
6305
6306 // -------------------------------------------------------------
6307 // Client Window State
6308 // -------------------------------------------------------------
6309
6310 private final class WindowState implements WindowManagerPolicy.WindowState {
6311 final Session mSession;
6312 final IWindow mClient;
6313 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006314 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 AppWindowToken mAppToken;
6316 AppWindowToken mTargetAppToken;
6317 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6318 final DeathRecipient mDeathRecipient;
6319 final WindowState mAttachedWindow;
6320 final ArrayList mChildWindows = new ArrayList();
6321 final int mBaseLayer;
6322 final int mSubLayer;
6323 final boolean mLayoutAttached;
6324 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006325 final boolean mIsWallpaper;
6326 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 int mViewVisibility;
6328 boolean mPolicyVisibility = true;
6329 boolean mPolicyVisibilityAfterAnim = true;
6330 boolean mAppFreezing;
6331 Surface mSurface;
6332 boolean mAttachedHidden; // is our parent window hidden?
6333 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006334 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 int mRequestedWidth;
6336 int mRequestedHeight;
6337 int mLastRequestedWidth;
6338 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 int mLayer;
6340 int mAnimLayer;
6341 int mLastLayer;
6342 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006343 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344
6345 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 // Actual frame shown on-screen (may be modified by animation)
6348 final Rect mShownFrame = new Rect();
6349 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 /**
6352 * Insets that determine the actually visible area
6353 */
6354 final Rect mVisibleInsets = new Rect();
6355 final Rect mLastVisibleInsets = new Rect();
6356 boolean mVisibleInsetsChanged;
6357
6358 /**
6359 * Insets that are covered by system windows
6360 */
6361 final Rect mContentInsets = new Rect();
6362 final Rect mLastContentInsets = new Rect();
6363 boolean mContentInsetsChanged;
6364
6365 /**
6366 * Set to true if we are waiting for this window to receive its
6367 * given internal insets before laying out other windows based on it.
6368 */
6369 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 /**
6372 * These are the content insets that were given during layout for
6373 * this window, to be applied to windows behind it.
6374 */
6375 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 /**
6378 * These are the visible insets that were given during layout for
6379 * this window, to be applied to windows behind it.
6380 */
6381 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 /**
6384 * Flag indicating whether the touchable region should be adjusted by
6385 * the visible insets; if false the area outside the visible insets is
6386 * NOT touchable, so we must use those to adjust the frame during hit
6387 * tests.
6388 */
6389 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 // Current transformation being applied.
6392 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6393 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6394 float mHScale=1, mVScale=1;
6395 float mLastHScale=1, mLastVScale=1;
6396 final Matrix mTmpMatrix = new Matrix();
6397
6398 // "Real" frame that the application sees.
6399 final Rect mFrame = new Rect();
6400 final Rect mLastFrame = new Rect();
6401
6402 final Rect mContainingFrame = new Rect();
6403 final Rect mDisplayFrame = new Rect();
6404 final Rect mContentFrame = new Rect();
6405 final Rect mVisibleFrame = new Rect();
6406
6407 float mShownAlpha = 1;
6408 float mAlpha = 1;
6409 float mLastAlpha = 1;
6410
6411 // Set to true if, when the window gets displayed, it should perform
6412 // an enter animation.
6413 boolean mEnterAnimationPending;
6414
6415 // Currently running animation.
6416 boolean mAnimating;
6417 boolean mLocalAnimating;
6418 Animation mAnimation;
6419 boolean mAnimationIsEntrance;
6420 boolean mHasTransformation;
6421 boolean mHasLocalTransformation;
6422 final Transformation mTransformation = new Transformation();
6423
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006424 // If a window showing a wallpaper: the requested offset for the
6425 // wallpaper; if a wallpaper window: the currently applied offset.
6426 float mWallpaperX = -1;
6427 float mWallpaperY = -1;
6428
6429 // Wallpaper windows: pixels offset based on above variables.
6430 int mXOffset;
6431 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 // This is set after IWindowSession.relayout() has been called at
6434 // least once for the window. It allows us to detect the situation
6435 // where we don't yet have a surface, but should have one soon, so
6436 // we can give the window focus before waiting for the relayout.
6437 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 // This is set after the Surface has been created but before the
6440 // window has been drawn. During this time the surface is hidden.
6441 boolean mDrawPending;
6442
6443 // This is set after the window has finished drawing for the first
6444 // time but before its surface is shown. The surface will be
6445 // displayed when the next layout is run.
6446 boolean mCommitDrawPending;
6447
6448 // This is set during the time after the window's drawing has been
6449 // committed, and before its surface is actually shown. It is used
6450 // to delay showing the surface until all windows in a token are ready
6451 // to be shown.
6452 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 // Set when the window has been shown in the screen the first time.
6455 boolean mHasDrawn;
6456
6457 // Currently running an exit animation?
6458 boolean mExiting;
6459
6460 // Currently on the mDestroySurface list?
6461 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 // Completely remove from window manager after exit animation?
6464 boolean mRemoveOnExit;
6465
6466 // Set when the orientation is changing and this window has not yet
6467 // been updated for the new orientation.
6468 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 // Is this window now (or just being) removed?
6471 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 WindowState(Session s, IWindow c, WindowToken token,
6474 WindowState attachedWindow, WindowManager.LayoutParams a,
6475 int viewVisibility) {
6476 mSession = s;
6477 mClient = c;
6478 mToken = token;
6479 mAttrs.copyFrom(a);
6480 mViewVisibility = viewVisibility;
6481 DeathRecipient deathRecipient = new DeathRecipient();
6482 mAlpha = a.alpha;
6483 if (localLOGV) Log.v(
6484 TAG, "Window " + this + " client=" + c.asBinder()
6485 + " token=" + token + " (" + mAttrs.token + ")");
6486 try {
6487 c.asBinder().linkToDeath(deathRecipient, 0);
6488 } catch (RemoteException e) {
6489 mDeathRecipient = null;
6490 mAttachedWindow = null;
6491 mLayoutAttached = false;
6492 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006493 mIsWallpaper = false;
6494 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 mBaseLayer = 0;
6496 mSubLayer = 0;
6497 return;
6498 }
6499 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6502 mAttrs.type <= LAST_SUB_WINDOW)) {
6503 // The multiplier here is to reserve space for multiple
6504 // windows in the same type layer.
6505 mBaseLayer = mPolicy.windowTypeToLayerLw(
6506 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6507 + TYPE_LAYER_OFFSET;
6508 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6509 mAttachedWindow = attachedWindow;
6510 mAttachedWindow.mChildWindows.add(this);
6511 mLayoutAttached = mAttrs.type !=
6512 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6513 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6514 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006515 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6516 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 } else {
6518 // The multiplier here is to reserve space for multiple
6519 // windows in the same type layer.
6520 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6521 * TYPE_LAYER_MULTIPLIER
6522 + TYPE_LAYER_OFFSET;
6523 mSubLayer = 0;
6524 mAttachedWindow = null;
6525 mLayoutAttached = false;
6526 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6527 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006528 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6529 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 }
6531
6532 WindowState appWin = this;
6533 while (appWin.mAttachedWindow != null) {
6534 appWin = mAttachedWindow;
6535 }
6536 WindowToken appToken = appWin.mToken;
6537 while (appToken.appWindowToken == null) {
6538 WindowToken parent = mTokenMap.get(appToken.token);
6539 if (parent == null || appToken == parent) {
6540 break;
6541 }
6542 appToken = parent;
6543 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006544 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 mAppToken = appToken.appWindowToken;
6546
6547 mSurface = null;
6548 mRequestedWidth = 0;
6549 mRequestedHeight = 0;
6550 mLastRequestedWidth = 0;
6551 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006552 mXOffset = 0;
6553 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 mLayer = 0;
6555 mAnimLayer = 0;
6556 mLastLayer = 0;
6557 }
6558
6559 void attach() {
6560 if (localLOGV) Log.v(
6561 TAG, "Attaching " + this + " token=" + mToken
6562 + ", list=" + mToken.windows);
6563 mSession.windowAddedLocked();
6564 }
6565
6566 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6567 mHaveFrame = true;
6568
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006569 final Rect container = mContainingFrame;
6570 container.set(pf);
6571
6572 final Rect display = mDisplayFrame;
6573 display.set(df);
6574
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006575 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006576 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006577 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6578 display.intersect(mCompatibleScreenFrame);
6579 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006580 }
6581
6582 final int pw = container.right - container.left;
6583 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584
6585 int w,h;
6586 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6587 w = mAttrs.width < 0 ? pw : mAttrs.width;
6588 h = mAttrs.height< 0 ? ph : mAttrs.height;
6589 } else {
6590 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6591 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6592 }
Romain Guy06882f82009-06-10 13:36:04 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 final Rect content = mContentFrame;
6595 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 final Rect visible = mVisibleFrame;
6598 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006600 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006601 final int fw = frame.width();
6602 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6605 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6606
6607 Gravity.apply(mAttrs.gravity, w, h, container,
6608 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6609 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6610
6611 //System.out.println("Out: " + mFrame);
6612
6613 // Now make sure the window fits in the overall display.
6614 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 // Make sure the content and visible frames are inside of the
6617 // final window frame.
6618 if (content.left < frame.left) content.left = frame.left;
6619 if (content.top < frame.top) content.top = frame.top;
6620 if (content.right > frame.right) content.right = frame.right;
6621 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6622 if (visible.left < frame.left) visible.left = frame.left;
6623 if (visible.top < frame.top) visible.top = frame.top;
6624 if (visible.right > frame.right) visible.right = frame.right;
6625 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 final Rect contentInsets = mContentInsets;
6628 contentInsets.left = content.left-frame.left;
6629 contentInsets.top = content.top-frame.top;
6630 contentInsets.right = frame.right-content.right;
6631 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 final Rect visibleInsets = mVisibleInsets;
6634 visibleInsets.left = visible.left-frame.left;
6635 visibleInsets.top = visible.top-frame.top;
6636 visibleInsets.right = frame.right-visible.right;
6637 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006638
Dianne Hackborn284ac932009-08-28 10:34:25 -07006639 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6640 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
6641 mDisplay.getHeight());
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006642 }
6643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 if (localLOGV) {
6645 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6646 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6647 Log.v(TAG, "Resolving (mRequestedWidth="
6648 + mRequestedWidth + ", mRequestedheight="
6649 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6650 + "): frame=" + mFrame.toShortString()
6651 + " ci=" + contentInsets.toShortString()
6652 + " vi=" + visibleInsets.toShortString());
6653 //}
6654 }
6655 }
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 public Rect getFrameLw() {
6658 return mFrame;
6659 }
6660
6661 public Rect getShownFrameLw() {
6662 return mShownFrame;
6663 }
6664
6665 public Rect getDisplayFrameLw() {
6666 return mDisplayFrame;
6667 }
6668
6669 public Rect getContentFrameLw() {
6670 return mContentFrame;
6671 }
6672
6673 public Rect getVisibleFrameLw() {
6674 return mVisibleFrame;
6675 }
6676
6677 public boolean getGivenInsetsPendingLw() {
6678 return mGivenInsetsPending;
6679 }
6680
6681 public Rect getGivenContentInsetsLw() {
6682 return mGivenContentInsets;
6683 }
Romain Guy06882f82009-06-10 13:36:04 -07006684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 public Rect getGivenVisibleInsetsLw() {
6686 return mGivenVisibleInsets;
6687 }
Romain Guy06882f82009-06-10 13:36:04 -07006688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 public WindowManager.LayoutParams getAttrs() {
6690 return mAttrs;
6691 }
6692
6693 public int getSurfaceLayer() {
6694 return mLayer;
6695 }
Romain Guy06882f82009-06-10 13:36:04 -07006696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 public IApplicationToken getAppToken() {
6698 return mAppToken != null ? mAppToken.appToken : null;
6699 }
6700
6701 public boolean hasAppShownWindows() {
6702 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6703 }
6704
6705 public boolean hasAppStartingIcon() {
6706 return mAppToken != null ? (mAppToken.startingData != null) : false;
6707 }
6708
6709 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6710 return mAppToken != null ? mAppToken.startingWindow : null;
6711 }
6712
6713 public void setAnimation(Animation anim) {
6714 if (localLOGV) Log.v(
6715 TAG, "Setting animation in " + this + ": " + anim);
6716 mAnimating = false;
6717 mLocalAnimating = false;
6718 mAnimation = anim;
6719 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6720 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6721 }
6722
6723 public void clearAnimation() {
6724 if (mAnimation != null) {
6725 mAnimating = true;
6726 mLocalAnimating = false;
6727 mAnimation = null;
6728 }
6729 }
Romain Guy06882f82009-06-10 13:36:04 -07006730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 Surface createSurfaceLocked() {
6732 if (mSurface == null) {
6733 mDrawPending = true;
6734 mCommitDrawPending = false;
6735 mReadyToShow = false;
6736 if (mAppToken != null) {
6737 mAppToken.allDrawn = false;
6738 }
6739
6740 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006741 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 flags |= Surface.PUSH_BUFFERS;
6743 }
6744
6745 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6746 flags |= Surface.SECURE;
6747 }
6748 if (DEBUG_VISIBILITY) Log.v(
6749 TAG, "Creating surface in session "
6750 + mSession.mSurfaceSession + " window " + this
6751 + " w=" + mFrame.width()
6752 + " h=" + mFrame.height() + " format="
6753 + mAttrs.format + " flags=" + flags);
6754
6755 int w = mFrame.width();
6756 int h = mFrame.height();
6757 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6758 // for a scaled surface, we always want the requested
6759 // size.
6760 w = mRequestedWidth;
6761 h = mRequestedHeight;
6762 }
6763
6764 try {
6765 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006766 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 0, w, h, mAttrs.format, flags);
6768 } catch (Surface.OutOfResourcesException e) {
6769 Log.w(TAG, "OutOfResourcesException creating surface");
6770 reclaimSomeSurfaceMemoryLocked(this, "create");
6771 return null;
6772 } catch (Exception e) {
6773 Log.e(TAG, "Exception creating surface", e);
6774 return null;
6775 }
Romain Guy06882f82009-06-10 13:36:04 -07006776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 if (localLOGV) Log.v(
6778 TAG, "Got surface: " + mSurface
6779 + ", set left=" + mFrame.left + " top=" + mFrame.top
6780 + ", animLayer=" + mAnimLayer);
6781 if (SHOW_TRANSACTIONS) {
6782 Log.i(TAG, ">>> OPEN TRANSACTION");
6783 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6784 + mAttrs.getTitle() + ") pos=(" +
6785 mFrame.left + "," + mFrame.top + ") (" +
6786 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6787 mAnimLayer + " HIDE");
6788 }
6789 Surface.openTransaction();
6790 try {
6791 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006792 mSurface.setPosition(mFrame.left + mXOffset,
6793 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 mSurface.setLayer(mAnimLayer);
6795 mSurface.hide();
6796 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6797 mSurface.setFlags(Surface.SURFACE_DITHER,
6798 Surface.SURFACE_DITHER);
6799 }
6800 } catch (RuntimeException e) {
6801 Log.w(TAG, "Error creating surface in " + w, e);
6802 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6803 }
6804 mLastHidden = true;
6805 } finally {
6806 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6807 Surface.closeTransaction();
6808 }
6809 if (localLOGV) Log.v(
6810 TAG, "Created surface " + this);
6811 }
6812 return mSurface;
6813 }
Romain Guy06882f82009-06-10 13:36:04 -07006814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 void destroySurfaceLocked() {
6816 // Window is no longer on-screen, so can no longer receive
6817 // key events... if we were waiting for it to finish
6818 // handling a key event, the wait is over!
6819 mKeyWaiter.finishedKey(mSession, mClient, true,
6820 KeyWaiter.RETURN_NOTHING);
6821 mKeyWaiter.releasePendingPointerLocked(mSession);
6822 mKeyWaiter.releasePendingTrackballLocked(mSession);
6823
6824 if (mAppToken != null && this == mAppToken.startingWindow) {
6825 mAppToken.startingDisplayed = false;
6826 }
Romain Guy06882f82009-06-10 13:36:04 -07006827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 if (mSurface != null) {
6829 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006830 if (DEBUG_VISIBILITY) {
6831 RuntimeException e = new RuntimeException();
6832 e.fillInStackTrace();
6833 Log.w(TAG, "Window " + this + " destroying surface "
6834 + mSurface + ", session " + mSession, e);
6835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 if (SHOW_TRANSACTIONS) {
6837 RuntimeException ex = new RuntimeException();
6838 ex.fillInStackTrace();
6839 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6840 + mAttrs.getTitle() + ")", ex);
6841 }
6842 mSurface.clear();
6843 } catch (RuntimeException e) {
6844 Log.w(TAG, "Exception thrown when destroying Window " + this
6845 + " surface " + mSurface + " session " + mSession
6846 + ": " + e.toString());
6847 }
6848 mSurface = null;
6849 mDrawPending = false;
6850 mCommitDrawPending = false;
6851 mReadyToShow = false;
6852
6853 int i = mChildWindows.size();
6854 while (i > 0) {
6855 i--;
6856 WindowState c = (WindowState)mChildWindows.get(i);
6857 c.mAttachedHidden = true;
6858 }
6859 }
6860 }
6861
6862 boolean finishDrawingLocked() {
6863 if (mDrawPending) {
6864 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6865 TAG, "finishDrawingLocked: " + mSurface);
6866 mCommitDrawPending = true;
6867 mDrawPending = false;
6868 return true;
6869 }
6870 return false;
6871 }
6872
6873 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006874 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6876 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006877 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 }
6879 mCommitDrawPending = false;
6880 mReadyToShow = true;
6881 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6882 final AppWindowToken atoken = mAppToken;
6883 if (atoken == null || atoken.allDrawn || starting) {
6884 performShowLocked();
6885 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006886 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 }
6888
6889 // This must be called while inside a transaction.
6890 boolean performShowLocked() {
6891 if (DEBUG_VISIBILITY) {
6892 RuntimeException e = new RuntimeException();
6893 e.fillInStackTrace();
6894 Log.v(TAG, "performShow on " + this
6895 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6896 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6897 }
6898 if (mReadyToShow && isReadyForDisplay()) {
6899 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6900 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6901 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6902 + " during animation: policyVis=" + mPolicyVisibility
6903 + " attHidden=" + mAttachedHidden
6904 + " tok.hiddenRequested="
6905 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6906 + " tok.idden="
6907 + (mAppToken != null ? mAppToken.hidden : false)
6908 + " animating=" + mAnimating
6909 + " tok animating="
6910 + (mAppToken != null ? mAppToken.animating : false));
6911 if (!showSurfaceRobustlyLocked(this)) {
6912 return false;
6913 }
6914 mLastAlpha = -1;
6915 mHasDrawn = true;
6916 mLastHidden = false;
6917 mReadyToShow = false;
6918 enableScreenIfNeededLocked();
6919
6920 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 int i = mChildWindows.size();
6923 while (i > 0) {
6924 i--;
6925 WindowState c = (WindowState)mChildWindows.get(i);
6926 if (c.mSurface != null && c.mAttachedHidden) {
6927 c.mAttachedHidden = false;
6928 c.performShowLocked();
6929 }
6930 }
Romain Guy06882f82009-06-10 13:36:04 -07006931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 if (mAttrs.type != TYPE_APPLICATION_STARTING
6933 && mAppToken != null) {
6934 mAppToken.firstWindowDrawn = true;
6935 if (mAnimation == null && mAppToken.startingData != null) {
6936 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6937 + mToken
6938 + ": first real window is shown, no animation");
6939 mFinishedStarting.add(mAppToken);
6940 mH.sendEmptyMessage(H.FINISHED_STARTING);
6941 }
6942 mAppToken.updateReportedVisibilityLocked();
6943 }
6944 }
6945 return true;
6946 }
Romain Guy06882f82009-06-10 13:36:04 -07006947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006948 // This must be called while inside a transaction. Returns true if
6949 // there is more animation to run.
6950 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6951 if (!mDisplayFrozen) {
6952 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6955 mHasTransformation = true;
6956 mHasLocalTransformation = true;
6957 if (!mLocalAnimating) {
6958 if (DEBUG_ANIM) Log.v(
6959 TAG, "Starting animation in " + this +
6960 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6961 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6962 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6963 mAnimation.setStartTime(currentTime);
6964 mLocalAnimating = true;
6965 mAnimating = true;
6966 }
6967 mTransformation.clear();
6968 final boolean more = mAnimation.getTransformation(
6969 currentTime, mTransformation);
6970 if (DEBUG_ANIM) Log.v(
6971 TAG, "Stepped animation in " + this +
6972 ": more=" + more + ", xform=" + mTransformation);
6973 if (more) {
6974 // we're not done!
6975 return true;
6976 }
6977 if (DEBUG_ANIM) Log.v(
6978 TAG, "Finished animation in " + this +
6979 " @ " + currentTime);
6980 mAnimation = null;
6981 //WindowManagerService.this.dump();
6982 }
6983 mHasLocalTransformation = false;
6984 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006985 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 // When our app token is animating, we kind-of pretend like
6987 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6988 // part of this check means that we will only do this if
6989 // our window is not currently exiting, or it is not
6990 // locally animating itself. The idea being that one that
6991 // is exiting and doing a local animation should be removed
6992 // once that animation is done.
6993 mAnimating = true;
6994 mHasTransformation = true;
6995 mTransformation.clear();
6996 return false;
6997 } else if (mHasTransformation) {
6998 // Little trick to get through the path below to act like
6999 // we have finished an animation.
7000 mAnimating = true;
7001 } else if (isAnimating()) {
7002 mAnimating = true;
7003 }
7004 } else if (mAnimation != null) {
7005 // If the display is frozen, and there is a pending animation,
7006 // clear it and make sure we run the cleanup code.
7007 mAnimating = true;
7008 mLocalAnimating = true;
7009 mAnimation = null;
7010 }
Romain Guy06882f82009-06-10 13:36:04 -07007011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 if (!mAnimating && !mLocalAnimating) {
7013 return false;
7014 }
7015
7016 if (DEBUG_ANIM) Log.v(
7017 TAG, "Animation done in " + this + ": exiting=" + mExiting
7018 + ", reportedVisible="
7019 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021 mAnimating = false;
7022 mLocalAnimating = false;
7023 mAnimation = null;
7024 mAnimLayer = mLayer;
7025 if (mIsImWindow) {
7026 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007027 } else if (mIsWallpaper) {
7028 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 }
7030 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7031 + " anim layer: " + mAnimLayer);
7032 mHasTransformation = false;
7033 mHasLocalTransformation = false;
7034 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7035 mTransformation.clear();
7036 if (mHasDrawn
7037 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7038 && mAppToken != null
7039 && mAppToken.firstWindowDrawn
7040 && mAppToken.startingData != null) {
7041 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7042 + mToken + ": first real window done animating");
7043 mFinishedStarting.add(mAppToken);
7044 mH.sendEmptyMessage(H.FINISHED_STARTING);
7045 }
Romain Guy06882f82009-06-10 13:36:04 -07007046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 finishExit();
7048
7049 if (mAppToken != null) {
7050 mAppToken.updateReportedVisibilityLocked();
7051 }
7052
7053 return false;
7054 }
7055
7056 void finishExit() {
7057 if (DEBUG_ANIM) Log.v(
7058 TAG, "finishExit in " + this
7059 + ": exiting=" + mExiting
7060 + " remove=" + mRemoveOnExit
7061 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063 final int N = mChildWindows.size();
7064 for (int i=0; i<N; i++) {
7065 ((WindowState)mChildWindows.get(i)).finishExit();
7066 }
Romain Guy06882f82009-06-10 13:36:04 -07007067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 if (!mExiting) {
7069 return;
7070 }
Romain Guy06882f82009-06-10 13:36:04 -07007071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 if (isWindowAnimating()) {
7073 return;
7074 }
7075
7076 if (localLOGV) Log.v(
7077 TAG, "Exit animation finished in " + this
7078 + ": remove=" + mRemoveOnExit);
7079 if (mSurface != null) {
7080 mDestroySurface.add(this);
7081 mDestroying = true;
7082 if (SHOW_TRANSACTIONS) Log.i(
7083 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7084 try {
7085 mSurface.hide();
7086 } catch (RuntimeException e) {
7087 Log.w(TAG, "Error hiding surface in " + this, e);
7088 }
7089 mLastHidden = true;
7090 mKeyWaiter.releasePendingPointerLocked(mSession);
7091 }
7092 mExiting = false;
7093 if (mRemoveOnExit) {
7094 mPendingRemove.add(this);
7095 mRemoveOnExit = false;
7096 }
7097 }
Romain Guy06882f82009-06-10 13:36:04 -07007098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7100 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7101 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7102 if (dtdx < -.000001f || dtdx > .000001f) return false;
7103 if (dsdy < -.000001f || dsdy > .000001f) return false;
7104 return true;
7105 }
Romain Guy06882f82009-06-10 13:36:04 -07007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 void computeShownFrameLocked() {
7108 final boolean selfTransformation = mHasLocalTransformation;
7109 Transformation attachedTransformation =
7110 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7111 ? mAttachedWindow.mTransformation : null;
7112 Transformation appTransformation =
7113 (mAppToken != null && mAppToken.hasTransformation)
7114 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007115
7116 // Wallpapers are animated based on the "real" window they
7117 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007118 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007119 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007120 if (mWallpaperTarget.mHasLocalTransformation) {
7121 attachedTransformation = mWallpaperTarget.mTransformation;
7122 }
7123 if (mWallpaperTarget.mAppToken != null &&
7124 mWallpaperTarget.mAppToken.hasTransformation) {
7125 appTransformation = mWallpaperTarget.mAppToken.transformation;
7126 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007127 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7128 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7129 }
7130 if (DEBUG_WALLPAPER && appTransformation != null) {
7131 Log.v(TAG, "WP target app xform: " + appTransformation);
7132 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007133 }
7134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 if (selfTransformation || attachedTransformation != null
7136 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007137 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 final Rect frame = mFrame;
7139 final float tmpFloats[] = mTmpFloats;
7140 final Matrix tmpMatrix = mTmpMatrix;
7141
7142 // Compute the desired transformation.
7143 tmpMatrix.setTranslate(frame.left, frame.top);
7144 if (selfTransformation) {
7145 tmpMatrix.preConcat(mTransformation.getMatrix());
7146 }
7147 if (attachedTransformation != null) {
7148 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7149 }
7150 if (appTransformation != null) {
7151 tmpMatrix.preConcat(appTransformation.getMatrix());
7152 }
7153
7154 // "convert" it into SurfaceFlinger's format
7155 // (a 2x2 matrix + an offset)
7156 // Here we must not transform the position of the surface
7157 // since it is already included in the transformation.
7158 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 tmpMatrix.getValues(tmpFloats);
7161 mDsDx = tmpFloats[Matrix.MSCALE_X];
7162 mDtDx = tmpFloats[Matrix.MSKEW_X];
7163 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7164 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007165 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7166 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 int w = frame.width();
7168 int h = frame.height();
7169 mShownFrame.set(x, y, x+w, y+h);
7170
7171 // Now set the alpha... but because our current hardware
7172 // can't do alpha transformation on a non-opaque surface,
7173 // turn it off if we are running an animation that is also
7174 // transforming since it is more important to have that
7175 // animation be smooth.
7176 mShownAlpha = mAlpha;
7177 if (!mLimitedAlphaCompositing
7178 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7179 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7180 && x == frame.left && y == frame.top))) {
7181 //Log.i(TAG, "Applying alpha transform");
7182 if (selfTransformation) {
7183 mShownAlpha *= mTransformation.getAlpha();
7184 }
7185 if (attachedTransformation != null) {
7186 mShownAlpha *= attachedTransformation.getAlpha();
7187 }
7188 if (appTransformation != null) {
7189 mShownAlpha *= appTransformation.getAlpha();
7190 }
7191 } else {
7192 //Log.i(TAG, "Not applying alpha transform");
7193 }
Romain Guy06882f82009-06-10 13:36:04 -07007194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 if (localLOGV) Log.v(
7196 TAG, "Continuing animation in " + this +
7197 ": " + mShownFrame +
7198 ", alpha=" + mTransformation.getAlpha());
7199 return;
7200 }
Romain Guy06882f82009-06-10 13:36:04 -07007201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007203 if (mXOffset != 0 || mYOffset != 0) {
7204 mShownFrame.offset(mXOffset, mYOffset);
7205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 mShownAlpha = mAlpha;
7207 mDsDx = 1;
7208 mDtDx = 0;
7209 mDsDy = 0;
7210 mDtDy = 1;
7211 }
Romain Guy06882f82009-06-10 13:36:04 -07007212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 /**
7214 * Is this window visible? It is not visible if there is no
7215 * surface, or we are in the process of running an exit animation
7216 * that will remove the surface, or its app token has been hidden.
7217 */
7218 public boolean isVisibleLw() {
7219 final AppWindowToken atoken = mAppToken;
7220 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7221 && (atoken == null || !atoken.hiddenRequested)
7222 && !mExiting && !mDestroying;
7223 }
7224
7225 /**
7226 * Is this window visible, ignoring its app token? It is not visible
7227 * if there is no surface, or we are in the process of running an exit animation
7228 * that will remove the surface.
7229 */
7230 public boolean isWinVisibleLw() {
7231 final AppWindowToken atoken = mAppToken;
7232 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7233 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7234 && !mExiting && !mDestroying;
7235 }
7236
7237 /**
7238 * The same as isVisible(), but follows the current hidden state of
7239 * the associated app token, not the pending requested hidden state.
7240 */
7241 boolean isVisibleNow() {
7242 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007243 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 }
7245
7246 /**
7247 * Same as isVisible(), but we also count it as visible between the
7248 * call to IWindowSession.add() and the first relayout().
7249 */
7250 boolean isVisibleOrAdding() {
7251 final AppWindowToken atoken = mAppToken;
7252 return (mSurface != null
7253 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7254 && mPolicyVisibility && !mAttachedHidden
7255 && (atoken == null || !atoken.hiddenRequested)
7256 && !mExiting && !mDestroying;
7257 }
7258
7259 /**
7260 * Is this window currently on-screen? It is on-screen either if it
7261 * is visible or it is currently running an animation before no longer
7262 * being visible.
7263 */
7264 boolean isOnScreen() {
7265 final AppWindowToken atoken = mAppToken;
7266 if (atoken != null) {
7267 return mSurface != null && mPolicyVisibility && !mDestroying
7268 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007269 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 } else {
7271 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007272 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 }
7274 }
Romain Guy06882f82009-06-10 13:36:04 -07007275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 /**
7277 * Like isOnScreen(), but we don't return true if the window is part
7278 * of a transition that has not yet been started.
7279 */
7280 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007281 if (mRootToken.waitingToShow &&
7282 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7283 return false;
7284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007286 final boolean animating = atoken != null
7287 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007289 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7290 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007291 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 }
7293
7294 /** Is the window or its container currently animating? */
7295 boolean isAnimating() {
7296 final WindowState attached = mAttachedWindow;
7297 final AppWindowToken atoken = mAppToken;
7298 return mAnimation != null
7299 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007300 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 (atoken.animation != null
7302 || atoken.inPendingTransaction));
7303 }
7304
7305 /** Is this window currently animating? */
7306 boolean isWindowAnimating() {
7307 return mAnimation != null;
7308 }
7309
7310 /**
7311 * Like isOnScreen, but returns false if the surface hasn't yet
7312 * been drawn.
7313 */
7314 public boolean isDisplayedLw() {
7315 final AppWindowToken atoken = mAppToken;
7316 return mSurface != null && mPolicyVisibility && !mDestroying
7317 && !mDrawPending && !mCommitDrawPending
7318 && ((!mAttachedHidden &&
7319 (atoken == null || !atoken.hiddenRequested))
7320 || mAnimating);
7321 }
7322
7323 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7324 boolean shownFrame, boolean onlyOpaque) {
7325 if (mSurface == null) {
7326 return false;
7327 }
7328 if (mAppToken != null && !mAppToken.appFullscreen) {
7329 return false;
7330 }
7331 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7332 return false;
7333 }
7334 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007335
7336 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7337 return frame.left <= mCompatibleScreenFrame.left &&
7338 frame.top <= mCompatibleScreenFrame.top &&
7339 frame.right >= mCompatibleScreenFrame.right &&
7340 frame.bottom >= mCompatibleScreenFrame.bottom;
7341 } else {
7342 return frame.left <= 0 && frame.top <= 0
7343 && frame.right >= screenWidth
7344 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 }
Romain Guy06882f82009-06-10 13:36:04 -07007347
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007348 /**
7349 * Return true if the window is opaque and fully drawn.
7350 */
7351 boolean isOpaqueDrawn() {
7352 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7353 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7354 }
7355
7356 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7357 return
7358 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007359 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7360 // only if it's visible
7361 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007362 // and only if the application fills the compatible screen
7363 mFrame.left <= mCompatibleScreenFrame.left &&
7364 mFrame.top <= mCompatibleScreenFrame.top &&
7365 mFrame.right >= mCompatibleScreenFrame.right &&
7366 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007367 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007368 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007369 }
7370
7371 boolean isFullscreen(int screenWidth, int screenHeight) {
7372 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007373 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 }
7375
7376 void removeLocked() {
7377 if (mAttachedWindow != null) {
7378 mAttachedWindow.mChildWindows.remove(this);
7379 }
7380 destroySurfaceLocked();
7381 mSession.windowRemovedLocked();
7382 try {
7383 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7384 } catch (RuntimeException e) {
7385 // Ignore if it has already been removed (usually because
7386 // we are doing this as part of processing a death note.)
7387 }
7388 }
7389
7390 private class DeathRecipient implements IBinder.DeathRecipient {
7391 public void binderDied() {
7392 try {
7393 synchronized(mWindowMap) {
7394 WindowState win = windowForClientLocked(mSession, mClient);
7395 Log.i(TAG, "WIN DEATH: " + win);
7396 if (win != null) {
7397 removeWindowLocked(mSession, win);
7398 }
7399 }
7400 } catch (IllegalArgumentException ex) {
7401 // This will happen if the window has already been
7402 // removed.
7403 }
7404 }
7405 }
7406
7407 /** Returns true if this window desires key events. */
7408 public final boolean canReceiveKeys() {
7409 return isVisibleOrAdding()
7410 && (mViewVisibility == View.VISIBLE)
7411 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7412 }
7413
7414 public boolean hasDrawnLw() {
7415 return mHasDrawn;
7416 }
7417
7418 public boolean showLw(boolean doAnimation) {
7419 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7420 mPolicyVisibility = true;
7421 mPolicyVisibilityAfterAnim = true;
7422 if (doAnimation) {
7423 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7424 }
7425 requestAnimationLocked(0);
7426 return true;
7427 }
7428 return false;
7429 }
7430
7431 public boolean hideLw(boolean doAnimation) {
7432 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7433 : mPolicyVisibility;
7434 if (current) {
7435 if (doAnimation) {
7436 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7437 if (mAnimation == null) {
7438 doAnimation = false;
7439 }
7440 }
7441 if (doAnimation) {
7442 mPolicyVisibilityAfterAnim = false;
7443 } else {
7444 mPolicyVisibilityAfterAnim = false;
7445 mPolicyVisibility = false;
7446 }
7447 requestAnimationLocked(0);
7448 return true;
7449 }
7450 return false;
7451 }
7452
7453 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007454 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007455
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007456 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7457 pw.print(" mClient="); pw.println(mClient.asBinder());
7458 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7459 if (mAttachedWindow != null || mLayoutAttached) {
7460 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7461 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7462 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007463 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7464 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7465 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007466 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7467 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007468 }
7469 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7470 pw.print(" mSubLayer="); pw.print(mSubLayer);
7471 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7472 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7473 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7474 pw.print("="); pw.print(mAnimLayer);
7475 pw.print(" mLastLayer="); pw.println(mLastLayer);
7476 if (mSurface != null) {
7477 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7478 }
7479 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7480 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7481 if (mAppToken != null) {
7482 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7483 }
7484 if (mTargetAppToken != null) {
7485 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7486 }
7487 pw.print(prefix); pw.print("mViewVisibility=0x");
7488 pw.print(Integer.toHexString(mViewVisibility));
7489 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007490 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7491 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007492 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7493 pw.print(prefix); pw.print("mPolicyVisibility=");
7494 pw.print(mPolicyVisibility);
7495 pw.print(" mPolicyVisibilityAfterAnim=");
7496 pw.print(mPolicyVisibilityAfterAnim);
7497 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7498 }
7499 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007500 pw.print(" h="); pw.println(mRequestedHeight);
7501 if (mXOffset != 0 || mYOffset != 0) {
7502 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7503 pw.print(" y="); pw.println(mYOffset);
7504 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007505 pw.print(prefix); pw.print("mGivenContentInsets=");
7506 mGivenContentInsets.printShortString(pw);
7507 pw.print(" mGivenVisibleInsets=");
7508 mGivenVisibleInsets.printShortString(pw);
7509 pw.println();
7510 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7511 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7512 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7513 }
7514 pw.print(prefix); pw.print("mShownFrame=");
7515 mShownFrame.printShortString(pw);
7516 pw.print(" last="); mLastShownFrame.printShortString(pw);
7517 pw.println();
7518 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7519 pw.print(" last="); mLastFrame.printShortString(pw);
7520 pw.println();
7521 pw.print(prefix); pw.print("mContainingFrame=");
7522 mContainingFrame.printShortString(pw);
7523 pw.print(" mDisplayFrame=");
7524 mDisplayFrame.printShortString(pw);
7525 pw.println();
7526 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7527 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7528 pw.println();
7529 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7530 pw.print(" last="); mLastContentInsets.printShortString(pw);
7531 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7532 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7533 pw.println();
7534 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7535 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7536 pw.print(" mAlpha="); pw.print(mAlpha);
7537 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7538 }
7539 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7540 || mAnimation != null) {
7541 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7542 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7543 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7544 pw.print(" mAnimation="); pw.println(mAnimation);
7545 }
7546 if (mHasTransformation || mHasLocalTransformation) {
7547 pw.print(prefix); pw.print("XForm: has=");
7548 pw.print(mHasTransformation);
7549 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7550 pw.print(" "); mTransformation.printShortString(pw);
7551 pw.println();
7552 }
7553 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7554 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7555 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7556 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7557 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7558 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7559 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7560 pw.print(" mDestroying="); pw.print(mDestroying);
7561 pw.print(" mRemoved="); pw.println(mRemoved);
7562 }
7563 if (mOrientationChanging || mAppFreezing) {
7564 pw.print(prefix); pw.print("mOrientationChanging=");
7565 pw.print(mOrientationChanging);
7566 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7567 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007568 if (mHScale != 1 || mVScale != 1) {
7569 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7570 pw.print(" mVScale="); pw.println(mVScale);
7571 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007572 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007573 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7574 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007576 }
7577
7578 @Override
7579 public String toString() {
7580 return "Window{"
7581 + Integer.toHexString(System.identityHashCode(this))
7582 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7583 }
7584 }
Romain Guy06882f82009-06-10 13:36:04 -07007585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 // -------------------------------------------------------------
7587 // Window Token State
7588 // -------------------------------------------------------------
7589
7590 class WindowToken {
7591 // The actual token.
7592 final IBinder token;
7593
7594 // The type of window this token is for, as per WindowManager.LayoutParams.
7595 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 // Set if this token was explicitly added by a client, so should
7598 // not be removed when all windows are removed.
7599 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007600
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007601 // For printing.
7602 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 // If this is an AppWindowToken, this is non-null.
7605 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 // All of the windows associated with this token.
7608 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7609
7610 // Is key dispatching paused for this token?
7611 boolean paused = false;
7612
7613 // Should this token's windows be hidden?
7614 boolean hidden;
7615
7616 // Temporary for finding which tokens no longer have visible windows.
7617 boolean hasVisible;
7618
Dianne Hackborna8f60182009-09-01 19:01:50 -07007619 // Set to true when this token is in a pending transaction where it
7620 // will be shown.
7621 boolean waitingToShow;
7622
7623 // Set to true when this token is in a pending transaction where it
7624 // will be hidden.
7625 boolean waitingToHide;
7626
7627 // Set to true when this token is in a pending transaction where its
7628 // windows will be put to the bottom of the list.
7629 boolean sendingToBottom;
7630
7631 // Set to true when this token is in a pending transaction where its
7632 // windows will be put to the top of the list.
7633 boolean sendingToTop;
7634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 WindowToken(IBinder _token, int type, boolean _explicit) {
7636 token = _token;
7637 windowType = type;
7638 explicit = _explicit;
7639 }
7640
7641 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007642 pw.print(prefix); pw.print("token="); pw.println(token);
7643 pw.print(prefix); pw.print("windows="); pw.println(windows);
7644 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7645 pw.print(" hidden="); pw.print(hidden);
7646 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007647 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7648 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7649 pw.print(" waitingToHide="); pw.print(waitingToHide);
7650 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7651 pw.print(" sendingToTop="); pw.println(sendingToTop);
7652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 }
7654
7655 @Override
7656 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007657 if (stringName == null) {
7658 StringBuilder sb = new StringBuilder();
7659 sb.append("WindowToken{");
7660 sb.append(Integer.toHexString(System.identityHashCode(this)));
7661 sb.append(" token="); sb.append(token); sb.append('}');
7662 stringName = sb.toString();
7663 }
7664 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
7666 };
7667
7668 class AppWindowToken extends WindowToken {
7669 // Non-null only for application tokens.
7670 final IApplicationToken appToken;
7671
7672 // All of the windows and child windows that are included in this
7673 // application token. Note this list is NOT sorted!
7674 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7675
7676 int groupId = -1;
7677 boolean appFullscreen;
7678 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 // These are used for determining when all windows associated with
7681 // an activity have been drawn, so they can be made visible together
7682 // at the same time.
7683 int lastTransactionSequence = mTransactionSequence-1;
7684 int numInterestingWindows;
7685 int numDrawnWindows;
7686 boolean inPendingTransaction;
7687 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689 // Is this token going to be hidden in a little while? If so, it
7690 // won't be taken into account for setting the screen orientation.
7691 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 // Is this window's surface needed? This is almost like hidden, except
7694 // it will sometimes be true a little earlier: when the token has
7695 // been shown, but is still waiting for its app transition to execute
7696 // before making its windows shown.
7697 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 // Have we told the window clients to hide themselves?
7700 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007702 // Last visibility state we reported to the app token.
7703 boolean reportedVisible;
7704
7705 // Set to true when the token has been removed from the window mgr.
7706 boolean removed;
7707
7708 // Have we been asked to have this token keep the screen frozen?
7709 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 boolean animating;
7712 Animation animation;
7713 boolean hasTransformation;
7714 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 // Offset to the window of all layers in the token, for use by
7717 // AppWindowToken animations.
7718 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 // Information about an application starting window if displayed.
7721 StartingData startingData;
7722 WindowState startingWindow;
7723 View startingView;
7724 boolean startingDisplayed;
7725 boolean startingMoved;
7726 boolean firstWindowDrawn;
7727
7728 AppWindowToken(IApplicationToken _token) {
7729 super(_token.asBinder(),
7730 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7731 appWindowToken = this;
7732 appToken = _token;
7733 }
Romain Guy06882f82009-06-10 13:36:04 -07007734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007735 public void setAnimation(Animation anim) {
7736 if (localLOGV) Log.v(
7737 TAG, "Setting animation in " + this + ": " + anim);
7738 animation = anim;
7739 animating = false;
7740 anim.restrictDuration(MAX_ANIMATION_DURATION);
7741 anim.scaleCurrentDuration(mTransitionAnimationScale);
7742 int zorder = anim.getZAdjustment();
7743 int adj = 0;
7744 if (zorder == Animation.ZORDER_TOP) {
7745 adj = TYPE_LAYER_OFFSET;
7746 } else if (zorder == Animation.ZORDER_BOTTOM) {
7747 adj = -TYPE_LAYER_OFFSET;
7748 }
Romain Guy06882f82009-06-10 13:36:04 -07007749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 if (animLayerAdjustment != adj) {
7751 animLayerAdjustment = adj;
7752 updateLayers();
7753 }
7754 }
Romain Guy06882f82009-06-10 13:36:04 -07007755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 public void setDummyAnimation() {
7757 if (animation == null) {
7758 if (localLOGV) Log.v(
7759 TAG, "Setting dummy animation in " + this);
7760 animation = sDummyAnimation;
7761 }
7762 }
7763
7764 public void clearAnimation() {
7765 if (animation != null) {
7766 animation = null;
7767 animating = true;
7768 }
7769 }
Romain Guy06882f82009-06-10 13:36:04 -07007770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 void updateLayers() {
7772 final int N = allAppWindows.size();
7773 final int adj = animLayerAdjustment;
7774 for (int i=0; i<N; i++) {
7775 WindowState w = allAppWindows.get(i);
7776 w.mAnimLayer = w.mLayer + adj;
7777 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7778 + w.mAnimLayer);
7779 if (w == mInputMethodTarget) {
7780 setInputMethodAnimLayerAdjustment(adj);
7781 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007782 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007783 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 }
7786 }
Romain Guy06882f82009-06-10 13:36:04 -07007787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 void sendAppVisibilityToClients() {
7789 final int N = allAppWindows.size();
7790 for (int i=0; i<N; i++) {
7791 WindowState win = allAppWindows.get(i);
7792 if (win == startingWindow && clientHidden) {
7793 // Don't hide the starting window.
7794 continue;
7795 }
7796 try {
7797 if (DEBUG_VISIBILITY) Log.v(TAG,
7798 "Setting visibility of " + win + ": " + (!clientHidden));
7799 win.mClient.dispatchAppVisibility(!clientHidden);
7800 } catch (RemoteException e) {
7801 }
7802 }
7803 }
Romain Guy06882f82009-06-10 13:36:04 -07007804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805 void showAllWindowsLocked() {
7806 final int NW = allAppWindows.size();
7807 for (int i=0; i<NW; i++) {
7808 WindowState w = allAppWindows.get(i);
7809 if (DEBUG_VISIBILITY) Log.v(TAG,
7810 "performing show on: " + w);
7811 w.performShowLocked();
7812 }
7813 }
Romain Guy06882f82009-06-10 13:36:04 -07007814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 // This must be called while inside a transaction.
7816 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7817 if (!mDisplayFrozen) {
7818 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 if (animation == sDummyAnimation) {
7821 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007822 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 // when it is really time to animate, this will be set to
7824 // a real animation and the next call will execute normally.
7825 return false;
7826 }
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7829 if (!animating) {
7830 if (DEBUG_ANIM) Log.v(
7831 TAG, "Starting animation in " + this +
7832 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7833 + " scale=" + mTransitionAnimationScale
7834 + " allDrawn=" + allDrawn + " animating=" + animating);
7835 animation.initialize(dw, dh, dw, dh);
7836 animation.setStartTime(currentTime);
7837 animating = true;
7838 }
7839 transformation.clear();
7840 final boolean more = animation.getTransformation(
7841 currentTime, transformation);
7842 if (DEBUG_ANIM) Log.v(
7843 TAG, "Stepped animation in " + this +
7844 ": more=" + more + ", xform=" + transformation);
7845 if (more) {
7846 // we're done!
7847 hasTransformation = true;
7848 return true;
7849 }
7850 if (DEBUG_ANIM) Log.v(
7851 TAG, "Finished animation in " + this +
7852 " @ " + currentTime);
7853 animation = null;
7854 }
7855 } else if (animation != null) {
7856 // If the display is frozen, and there is a pending animation,
7857 // clear it and make sure we run the cleanup code.
7858 animating = true;
7859 animation = null;
7860 }
7861
7862 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 if (!animating) {
7865 return false;
7866 }
7867
7868 clearAnimation();
7869 animating = false;
7870 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7871 moveInputMethodWindowsIfNeededLocked(true);
7872 }
Romain Guy06882f82009-06-10 13:36:04 -07007873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 if (DEBUG_ANIM) Log.v(
7875 TAG, "Animation done in " + this
7876 + ": reportedVisible=" + reportedVisible);
7877
7878 transformation.clear();
7879 if (animLayerAdjustment != 0) {
7880 animLayerAdjustment = 0;
7881 updateLayers();
7882 }
Romain Guy06882f82009-06-10 13:36:04 -07007883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 final int N = windows.size();
7885 for (int i=0; i<N; i++) {
7886 ((WindowState)windows.get(i)).finishExit();
7887 }
7888 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 return false;
7891 }
7892
7893 void updateReportedVisibilityLocked() {
7894 if (appToken == null) {
7895 return;
7896 }
Romain Guy06882f82009-06-10 13:36:04 -07007897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 int numInteresting = 0;
7899 int numVisible = 0;
7900 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7903 final int N = allAppWindows.size();
7904 for (int i=0; i<N; i++) {
7905 WindowState win = allAppWindows.get(i);
7906 if (win == startingWindow || win.mAppFreezing) {
7907 continue;
7908 }
7909 if (DEBUG_VISIBILITY) {
7910 Log.v(TAG, "Win " + win + ": isDisplayed="
7911 + win.isDisplayedLw()
7912 + ", isAnimating=" + win.isAnimating());
7913 if (!win.isDisplayedLw()) {
7914 Log.v(TAG, "Not displayed: s=" + win.mSurface
7915 + " pv=" + win.mPolicyVisibility
7916 + " dp=" + win.mDrawPending
7917 + " cdp=" + win.mCommitDrawPending
7918 + " ah=" + win.mAttachedHidden
7919 + " th="
7920 + (win.mAppToken != null
7921 ? win.mAppToken.hiddenRequested : false)
7922 + " a=" + win.mAnimating);
7923 }
7924 }
7925 numInteresting++;
7926 if (win.isDisplayedLw()) {
7927 if (!win.isAnimating()) {
7928 numVisible++;
7929 }
7930 nowGone = false;
7931 } else if (win.isAnimating()) {
7932 nowGone = false;
7933 }
7934 }
Romain Guy06882f82009-06-10 13:36:04 -07007935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7937 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7938 + numInteresting + " visible=" + numVisible);
7939 if (nowVisible != reportedVisible) {
7940 if (DEBUG_VISIBILITY) Log.v(
7941 TAG, "Visibility changed in " + this
7942 + ": vis=" + nowVisible);
7943 reportedVisible = nowVisible;
7944 Message m = mH.obtainMessage(
7945 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7946 nowVisible ? 1 : 0,
7947 nowGone ? 1 : 0,
7948 this);
7949 mH.sendMessage(m);
7950 }
7951 }
Romain Guy06882f82009-06-10 13:36:04 -07007952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 void dump(PrintWriter pw, String prefix) {
7954 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007955 if (appToken != null) {
7956 pw.print(prefix); pw.println("app=true");
7957 }
7958 if (allAppWindows.size() > 0) {
7959 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7960 }
7961 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007962 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007963 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7964 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7965 pw.print(" clientHidden="); pw.print(clientHidden);
7966 pw.print(" willBeHidden="); pw.print(willBeHidden);
7967 pw.print(" reportedVisible="); pw.println(reportedVisible);
7968 if (paused || freezingScreen) {
7969 pw.print(prefix); pw.print("paused="); pw.print(paused);
7970 pw.print(" freezingScreen="); pw.println(freezingScreen);
7971 }
7972 if (numInterestingWindows != 0 || numDrawnWindows != 0
7973 || inPendingTransaction || allDrawn) {
7974 pw.print(prefix); pw.print("numInterestingWindows=");
7975 pw.print(numInterestingWindows);
7976 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7977 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7978 pw.print(" allDrawn="); pw.println(allDrawn);
7979 }
7980 if (animating || animation != null) {
7981 pw.print(prefix); pw.print("animating="); pw.print(animating);
7982 pw.print(" animation="); pw.println(animation);
7983 }
7984 if (animLayerAdjustment != 0) {
7985 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7986 }
7987 if (hasTransformation) {
7988 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7989 pw.print(" transformation="); transformation.printShortString(pw);
7990 pw.println();
7991 }
7992 if (startingData != null || removed || firstWindowDrawn) {
7993 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7994 pw.print(" removed="); pw.print(removed);
7995 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7996 }
7997 if (startingWindow != null || startingView != null
7998 || startingDisplayed || startingMoved) {
7999 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8000 pw.print(" startingView="); pw.print(startingView);
8001 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8002 pw.print(" startingMoved"); pw.println(startingMoved);
8003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 }
8005
8006 @Override
8007 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008008 if (stringName == null) {
8009 StringBuilder sb = new StringBuilder();
8010 sb.append("AppWindowToken{");
8011 sb.append(Integer.toHexString(System.identityHashCode(this)));
8012 sb.append(" token="); sb.append(token); sb.append('}');
8013 stringName = sb.toString();
8014 }
8015 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 }
8017 }
Romain Guy06882f82009-06-10 13:36:04 -07008018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 public static WindowManager.LayoutParams findAnimations(
8020 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008021 ArrayList<AppWindowToken> openingTokenList1,
8022 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008024
8025 // First, check if there is a compatible window in opening/closing
8026 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 WindowManager.LayoutParams animParams = null;
8028 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008029 animParams = findCompatibleWindowParams(openingTokenList1);
8030 if (animParams == null) {
8031 animParams = findCompatibleWindowParams(closingTokenList2);
8032 }
8033 if (animParams != null) {
8034 return animParams;
8035 }
8036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 //Log.i(TAG, "Looking for animations...");
8038 for (int i=order.size()-1; i>=0; i--) {
8039 AppWindowToken wtoken = order.get(i);
8040 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008041 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 int j = wtoken.windows.size();
8043 while (j > 0) {
8044 j--;
8045 WindowState win = wtoken.windows.get(j);
8046 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
8047 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8048 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8049 //Log.i(TAG, "Found base or application window, done!");
8050 if (wtoken.appFullscreen) {
8051 return win.mAttrs;
8052 }
8053 if (animSrc < 2) {
8054 animParams = win.mAttrs;
8055 animSrc = 2;
8056 }
8057 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
8058 //Log.i(TAG, "Found normal window, we may use this...");
8059 animParams = win.mAttrs;
8060 animSrc = 1;
8061 }
8062 }
8063 }
8064 }
Romain Guy06882f82009-06-10 13:36:04 -07008065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 return animParams;
8067 }
Romain Guy06882f82009-06-10 13:36:04 -07008068
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07008069 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
8070 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
8071 AppWindowToken wtoken = tokenList.get(appCount);
8072 // Just checking one window is sufficient as all windows have the compatible flag
8073 // if the application is in compatibility mode.
8074 if (wtoken.windows.size() > 0) {
8075 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
8076 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8077 return params;
8078 }
8079 }
8080 }
8081 return null;
8082 }
8083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 // -------------------------------------------------------------
8085 // DummyAnimation
8086 // -------------------------------------------------------------
8087
8088 // This is an animation that does nothing: it just immediately finishes
8089 // itself every time it is called. It is used as a stub animation in cases
8090 // where we want to synchronize multiple things that may be animating.
8091 static final class DummyAnimation extends Animation {
8092 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8093 return false;
8094 }
8095 }
8096 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 // -------------------------------------------------------------
8099 // Async Handler
8100 // -------------------------------------------------------------
8101
8102 static final class StartingData {
8103 final String pkg;
8104 final int theme;
8105 final CharSequence nonLocalizedLabel;
8106 final int labelRes;
8107 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8110 int _labelRes, int _icon) {
8111 pkg = _pkg;
8112 theme = _theme;
8113 nonLocalizedLabel = _nonLocalizedLabel;
8114 labelRes = _labelRes;
8115 icon = _icon;
8116 }
8117 }
8118
8119 private final class H extends Handler {
8120 public static final int REPORT_FOCUS_CHANGE = 2;
8121 public static final int REPORT_LOSING_FOCUS = 3;
8122 public static final int ANIMATE = 4;
8123 public static final int ADD_STARTING = 5;
8124 public static final int REMOVE_STARTING = 6;
8125 public static final int FINISHED_STARTING = 7;
8126 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8128 public static final int HOLD_SCREEN_CHANGED = 12;
8129 public static final int APP_TRANSITION_TIMEOUT = 13;
8130 public static final int PERSIST_ANIMATION_SCALE = 14;
8131 public static final int FORCE_GC = 15;
8132 public static final int ENABLE_SCREEN = 16;
8133 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008134 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008136 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 public H() {
8139 }
Romain Guy06882f82009-06-10 13:36:04 -07008140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 @Override
8142 public void handleMessage(Message msg) {
8143 switch (msg.what) {
8144 case REPORT_FOCUS_CHANGE: {
8145 WindowState lastFocus;
8146 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 synchronized(mWindowMap) {
8149 lastFocus = mLastFocus;
8150 newFocus = mCurrentFocus;
8151 if (lastFocus == newFocus) {
8152 // Focus is not changing, so nothing to do.
8153 return;
8154 }
8155 mLastFocus = newFocus;
8156 //Log.i(TAG, "Focus moving from " + lastFocus
8157 // + " to " + newFocus);
8158 if (newFocus != null && lastFocus != null
8159 && !newFocus.isDisplayedLw()) {
8160 //Log.i(TAG, "Delaying loss of focus...");
8161 mLosingFocus.add(lastFocus);
8162 lastFocus = null;
8163 }
8164 }
8165
8166 if (lastFocus != newFocus) {
8167 //System.out.println("Changing focus from " + lastFocus
8168 // + " to " + newFocus);
8169 if (newFocus != null) {
8170 try {
8171 //Log.i(TAG, "Gaining focus: " + newFocus);
8172 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8173 } catch (RemoteException e) {
8174 // Ignore if process has died.
8175 }
8176 }
8177
8178 if (lastFocus != null) {
8179 try {
8180 //Log.i(TAG, "Losing focus: " + lastFocus);
8181 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8182 } catch (RemoteException e) {
8183 // Ignore if process has died.
8184 }
8185 }
8186 }
8187 } break;
8188
8189 case REPORT_LOSING_FOCUS: {
8190 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 synchronized(mWindowMap) {
8193 losers = mLosingFocus;
8194 mLosingFocus = new ArrayList<WindowState>();
8195 }
8196
8197 final int N = losers.size();
8198 for (int i=0; i<N; i++) {
8199 try {
8200 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8201 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8202 } catch (RemoteException e) {
8203 // Ignore if process has died.
8204 }
8205 }
8206 } break;
8207
8208 case ANIMATE: {
8209 synchronized(mWindowMap) {
8210 mAnimationPending = false;
8211 performLayoutAndPlaceSurfacesLocked();
8212 }
8213 } break;
8214
8215 case ADD_STARTING: {
8216 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8217 final StartingData sd = wtoken.startingData;
8218
8219 if (sd == null) {
8220 // Animation has been canceled... do nothing.
8221 return;
8222 }
Romain Guy06882f82009-06-10 13:36:04 -07008223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8225 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 View view = null;
8228 try {
8229 view = mPolicy.addStartingWindow(
8230 wtoken.token, sd.pkg,
8231 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8232 sd.icon);
8233 } catch (Exception e) {
8234 Log.w(TAG, "Exception when adding starting window", e);
8235 }
8236
8237 if (view != null) {
8238 boolean abort = false;
8239
8240 synchronized(mWindowMap) {
8241 if (wtoken.removed || wtoken.startingData == null) {
8242 // If the window was successfully added, then
8243 // we need to remove it.
8244 if (wtoken.startingWindow != null) {
8245 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8246 "Aborted starting " + wtoken
8247 + ": removed=" + wtoken.removed
8248 + " startingData=" + wtoken.startingData);
8249 wtoken.startingWindow = null;
8250 wtoken.startingData = null;
8251 abort = true;
8252 }
8253 } else {
8254 wtoken.startingView = view;
8255 }
8256 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8257 "Added starting " + wtoken
8258 + ": startingWindow="
8259 + wtoken.startingWindow + " startingView="
8260 + wtoken.startingView);
8261 }
8262
8263 if (abort) {
8264 try {
8265 mPolicy.removeStartingWindow(wtoken.token, view);
8266 } catch (Exception e) {
8267 Log.w(TAG, "Exception when removing starting window", e);
8268 }
8269 }
8270 }
8271 } break;
8272
8273 case REMOVE_STARTING: {
8274 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8275 IBinder token = null;
8276 View view = null;
8277 synchronized (mWindowMap) {
8278 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8279 + wtoken + ": startingWindow="
8280 + wtoken.startingWindow + " startingView="
8281 + wtoken.startingView);
8282 if (wtoken.startingWindow != null) {
8283 view = wtoken.startingView;
8284 token = wtoken.token;
8285 wtoken.startingData = null;
8286 wtoken.startingView = null;
8287 wtoken.startingWindow = null;
8288 }
8289 }
8290 if (view != null) {
8291 try {
8292 mPolicy.removeStartingWindow(token, view);
8293 } catch (Exception e) {
8294 Log.w(TAG, "Exception when removing starting window", e);
8295 }
8296 }
8297 } break;
8298
8299 case FINISHED_STARTING: {
8300 IBinder token = null;
8301 View view = null;
8302 while (true) {
8303 synchronized (mWindowMap) {
8304 final int N = mFinishedStarting.size();
8305 if (N <= 0) {
8306 break;
8307 }
8308 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8309
8310 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8311 "Finished starting " + wtoken
8312 + ": startingWindow=" + wtoken.startingWindow
8313 + " startingView=" + wtoken.startingView);
8314
8315 if (wtoken.startingWindow == null) {
8316 continue;
8317 }
8318
8319 view = wtoken.startingView;
8320 token = wtoken.token;
8321 wtoken.startingData = null;
8322 wtoken.startingView = null;
8323 wtoken.startingWindow = null;
8324 }
8325
8326 try {
8327 mPolicy.removeStartingWindow(token, view);
8328 } catch (Exception e) {
8329 Log.w(TAG, "Exception when removing starting window", e);
8330 }
8331 }
8332 } break;
8333
8334 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8335 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8336
8337 boolean nowVisible = msg.arg1 != 0;
8338 boolean nowGone = msg.arg2 != 0;
8339
8340 try {
8341 if (DEBUG_VISIBILITY) Log.v(
8342 TAG, "Reporting visible in " + wtoken
8343 + " visible=" + nowVisible
8344 + " gone=" + nowGone);
8345 if (nowVisible) {
8346 wtoken.appToken.windowsVisible();
8347 } else {
8348 wtoken.appToken.windowsGone();
8349 }
8350 } catch (RemoteException ex) {
8351 }
8352 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 case WINDOW_FREEZE_TIMEOUT: {
8355 synchronized (mWindowMap) {
8356 Log.w(TAG, "Window freeze timeout expired.");
8357 int i = mWindows.size();
8358 while (i > 0) {
8359 i--;
8360 WindowState w = (WindowState)mWindows.get(i);
8361 if (w.mOrientationChanging) {
8362 w.mOrientationChanging = false;
8363 Log.w(TAG, "Force clearing orientation change: " + w);
8364 }
8365 }
8366 performLayoutAndPlaceSurfacesLocked();
8367 }
8368 break;
8369 }
Romain Guy06882f82009-06-10 13:36:04 -07008370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 case HOLD_SCREEN_CHANGED: {
8372 Session oldHold;
8373 Session newHold;
8374 synchronized (mWindowMap) {
8375 oldHold = mLastReportedHold;
8376 newHold = (Session)msg.obj;
8377 mLastReportedHold = newHold;
8378 }
Romain Guy06882f82009-06-10 13:36:04 -07008379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 if (oldHold != newHold) {
8381 try {
8382 if (oldHold != null) {
8383 mBatteryStats.noteStopWakelock(oldHold.mUid,
8384 "window",
8385 BatteryStats.WAKE_TYPE_WINDOW);
8386 }
8387 if (newHold != null) {
8388 mBatteryStats.noteStartWakelock(newHold.mUid,
8389 "window",
8390 BatteryStats.WAKE_TYPE_WINDOW);
8391 }
8392 } catch (RemoteException e) {
8393 }
8394 }
8395 break;
8396 }
Romain Guy06882f82009-06-10 13:36:04 -07008397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 case APP_TRANSITION_TIMEOUT: {
8399 synchronized (mWindowMap) {
8400 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8401 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8402 "*** APP TRANSITION TIMEOUT");
8403 mAppTransitionReady = true;
8404 mAppTransitionTimeout = true;
8405 performLayoutAndPlaceSurfacesLocked();
8406 }
8407 }
8408 break;
8409 }
Romain Guy06882f82009-06-10 13:36:04 -07008410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 case PERSIST_ANIMATION_SCALE: {
8412 Settings.System.putFloat(mContext.getContentResolver(),
8413 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8414 Settings.System.putFloat(mContext.getContentResolver(),
8415 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8416 break;
8417 }
Romain Guy06882f82009-06-10 13:36:04 -07008418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 case FORCE_GC: {
8420 synchronized(mWindowMap) {
8421 if (mAnimationPending) {
8422 // If we are animating, don't do the gc now but
8423 // delay a bit so we don't interrupt the animation.
8424 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8425 2000);
8426 return;
8427 }
8428 // If we are currently rotating the display, it will
8429 // schedule a new message when done.
8430 if (mDisplayFrozen) {
8431 return;
8432 }
8433 mFreezeGcPending = 0;
8434 }
8435 Runtime.getRuntime().gc();
8436 break;
8437 }
Romain Guy06882f82009-06-10 13:36:04 -07008438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 case ENABLE_SCREEN: {
8440 performEnableScreen();
8441 break;
8442 }
Romain Guy06882f82009-06-10 13:36:04 -07008443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 case APP_FREEZE_TIMEOUT: {
8445 synchronized (mWindowMap) {
8446 Log.w(TAG, "App freeze timeout expired.");
8447 int i = mAppTokens.size();
8448 while (i > 0) {
8449 i--;
8450 AppWindowToken tok = mAppTokens.get(i);
8451 if (tok.freezingScreen) {
8452 Log.w(TAG, "Force clearing freeze: " + tok);
8453 unsetAppFreezingScreenLocked(tok, true, true);
8454 }
8455 }
8456 }
8457 break;
8458 }
Romain Guy06882f82009-06-10 13:36:04 -07008459
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008460 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008461 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008462 sendNewConfiguration();
8463 }
8464 break;
8465 }
Romain Guy06882f82009-06-10 13:36:04 -07008466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008467 }
8468 }
8469 }
8470
8471 // -------------------------------------------------------------
8472 // IWindowManager API
8473 // -------------------------------------------------------------
8474
8475 public IWindowSession openSession(IInputMethodClient client,
8476 IInputContext inputContext) {
8477 if (client == null) throw new IllegalArgumentException("null client");
8478 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8479 return new Session(client, inputContext);
8480 }
8481
8482 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8483 synchronized (mWindowMap) {
8484 // The focus for the client is the window immediately below
8485 // where we would place the input method window.
8486 int idx = findDesiredInputMethodWindowIndexLocked(false);
8487 WindowState imFocus;
8488 if (idx > 0) {
8489 imFocus = (WindowState)mWindows.get(idx-1);
8490 if (imFocus != null) {
8491 if (imFocus.mSession.mClient != null &&
8492 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8493 return true;
8494 }
8495 }
8496 }
8497 }
8498 return false;
8499 }
Romain Guy06882f82009-06-10 13:36:04 -07008500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 // -------------------------------------------------------------
8502 // Internals
8503 // -------------------------------------------------------------
8504
8505 final WindowState windowForClientLocked(Session session, IWindow client) {
8506 return windowForClientLocked(session, client.asBinder());
8507 }
Romain Guy06882f82009-06-10 13:36:04 -07008508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 final WindowState windowForClientLocked(Session session, IBinder client) {
8510 WindowState win = mWindowMap.get(client);
8511 if (localLOGV) Log.v(
8512 TAG, "Looking up client " + client + ": " + win);
8513 if (win == null) {
8514 RuntimeException ex = new RuntimeException();
8515 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8516 return null;
8517 }
8518 if (session != null && win.mSession != session) {
8519 RuntimeException ex = new RuntimeException();
8520 Log.w(TAG, "Requested window " + client + " is in session " +
8521 win.mSession + ", not " + session, ex);
8522 return null;
8523 }
8524
8525 return win;
8526 }
8527
Dianne Hackborna8f60182009-09-01 19:01:50 -07008528 final void rebuildAppWindowListLocked() {
8529 int NW = mWindows.size();
8530 int i;
8531
8532 // First remove all existing app windows.
8533 i=0;
8534 while (i < NW) {
8535 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008536 WindowState win = (WindowState)mWindows.remove(i);
8537 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8538 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008539 NW--;
8540 continue;
8541 }
8542 i++;
8543 }
8544
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008545 // First add all of the exiting app tokens... these are no longer
8546 // in the main app list, but still have windows shown. We put them
8547 // in the back because now that the animation is over we no longer
8548 // will care about them.
8549 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008550 i = 0;
8551 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008552 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8553 }
8554
8555 // And add in the still active app tokens in Z order.
8556 NT = mAppTokens.size();
8557 for (int j=0; j<NT; j++) {
8558 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008559 }
8560 }
8561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 private final void assignLayersLocked() {
8563 int N = mWindows.size();
8564 int curBaseLayer = 0;
8565 int curLayer = 0;
8566 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 for (i=0; i<N; i++) {
8569 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008570 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8571 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 curLayer += WINDOW_LAYER_MULTIPLIER;
8573 w.mLayer = curLayer;
8574 } else {
8575 curBaseLayer = curLayer = w.mBaseLayer;
8576 w.mLayer = curLayer;
8577 }
8578 if (w.mTargetAppToken != null) {
8579 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8580 } else if (w.mAppToken != null) {
8581 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8582 } else {
8583 w.mAnimLayer = w.mLayer;
8584 }
8585 if (w.mIsImWindow) {
8586 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008587 } else if (w.mIsWallpaper) {
8588 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 }
8590 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8591 + w.mAnimLayer);
8592 //System.out.println(
8593 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8594 }
8595 }
8596
8597 private boolean mInLayout = false;
8598 private final void performLayoutAndPlaceSurfacesLocked() {
8599 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008600 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 throw new RuntimeException("Recursive call!");
8602 }
8603 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8604 return;
8605 }
8606
8607 boolean recoveringMemory = false;
8608 if (mForceRemoves != null) {
8609 recoveringMemory = true;
8610 // Wait a little it for things to settle down, and off we go.
8611 for (int i=0; i<mForceRemoves.size(); i++) {
8612 WindowState ws = mForceRemoves.get(i);
8613 Log.i(TAG, "Force removing: " + ws);
8614 removeWindowInnerLocked(ws.mSession, ws);
8615 }
8616 mForceRemoves = null;
8617 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8618 Object tmp = new Object();
8619 synchronized (tmp) {
8620 try {
8621 tmp.wait(250);
8622 } catch (InterruptedException e) {
8623 }
8624 }
8625 }
Romain Guy06882f82009-06-10 13:36:04 -07008626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 mInLayout = true;
8628 try {
8629 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 int i = mPendingRemove.size()-1;
8632 if (i >= 0) {
8633 while (i >= 0) {
8634 WindowState w = mPendingRemove.get(i);
8635 removeWindowInnerLocked(w.mSession, w);
8636 i--;
8637 }
8638 mPendingRemove.clear();
8639
8640 mInLayout = false;
8641 assignLayersLocked();
8642 mLayoutNeeded = true;
8643 performLayoutAndPlaceSurfacesLocked();
8644
8645 } else {
8646 mInLayout = false;
8647 if (mLayoutNeeded) {
8648 requestAnimationLocked(0);
8649 }
8650 }
8651 } catch (RuntimeException e) {
8652 mInLayout = false;
8653 Log.e(TAG, "Unhandled exception while layout out windows", e);
8654 }
8655 }
8656
8657 private final void performLayoutLockedInner() {
8658 final int dw = mDisplay.getWidth();
8659 final int dh = mDisplay.getHeight();
8660
8661 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008662 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 int i;
8664
8665 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008666
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008667 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 mPolicy.beginLayoutLw(dw, dh);
8669
8670 // First perform layout of any root windows (not attached
8671 // to another window).
8672 int topAttached = -1;
8673 for (i = N-1; i >= 0; i--) {
8674 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008675
8676 // Don't do layout of a window if it is not visible, or
8677 // soon won't be visible, to avoid wasting time and funky
8678 // changes while a window is animating away.
8679 final AppWindowToken atoken = win.mAppToken;
8680 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008682 || win.mRootToken.hidden
8683 || (atoken != null && atoken.hiddenRequested)
8684 || !win.mPolicyVisibility
8685 || win.mAttachedHidden
8686 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687
8688 // If this view is GONE, then skip it -- keep the current
8689 // frame, and let the caller know so they can ignore it
8690 // if they want. (We do the normal layout for INVISIBLE
8691 // windows, since that means "perform layout as normal,
8692 // just don't display").
8693 if (!gone || !win.mHaveFrame) {
8694 if (!win.mLayoutAttached) {
8695 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8696 } else {
8697 if (topAttached < 0) topAttached = i;
8698 }
8699 }
8700 }
Romain Guy06882f82009-06-10 13:36:04 -07008701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 // Now perform layout of attached windows, which usually
8703 // depend on the position of the window they are attached to.
8704 // XXX does not deal with windows that are attached to windows
8705 // that are themselves attached.
8706 for (i = topAttached; i >= 0; i--) {
8707 WindowState win = (WindowState) mWindows.get(i);
8708
8709 // If this view is GONE, then skip it -- keep the current
8710 // frame, and let the caller know so they can ignore it
8711 // if they want. (We do the normal layout for INVISIBLE
8712 // windows, since that means "perform layout as normal,
8713 // just don't display").
8714 if (win.mLayoutAttached) {
8715 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8716 || !win.mHaveFrame) {
8717 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8718 }
8719 }
8720 }
8721
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008722 if (!mPolicy.finishLayoutLw()) {
8723 mLayoutNeeded = false;
8724 } else if (repeats > 2) {
8725 Log.w(TAG, "Layout repeat aborted after too many iterations");
8726 mLayoutNeeded = false;
8727 } else {
8728 repeats++;
8729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 }
8731 }
Romain Guy06882f82009-06-10 13:36:04 -07008732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 private final void performLayoutAndPlaceSurfacesLockedInner(
8734 boolean recoveringMemory) {
8735 final long currentTime = SystemClock.uptimeMillis();
8736 final int dw = mDisplay.getWidth();
8737 final int dh = mDisplay.getHeight();
8738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 int i;
8740
8741 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 if (mFxSession == null) {
8745 mFxSession = new SurfaceSession();
8746 }
Romain Guy06882f82009-06-10 13:36:04 -07008747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8749
8750 // Initialize state of exiting tokens.
8751 for (i=mExitingTokens.size()-1; i>=0; i--) {
8752 mExitingTokens.get(i).hasVisible = false;
8753 }
8754
8755 // Initialize state of exiting applications.
8756 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8757 mExitingAppTokens.get(i).hasVisible = false;
8758 }
8759
8760 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008761 boolean orientationChangeComplete = true;
8762 Session holdScreen = null;
8763 float screenBrightness = -1;
8764 boolean focusDisplayed = false;
8765 boolean animating = false;
8766
8767 Surface.openTransaction();
8768 try {
8769 boolean restart;
8770
8771 do {
8772 final int transactionSequence = ++mTransactionSequence;
8773
8774 // Update animations of all applications, including those
8775 // associated with exiting/removed apps
8776 boolean tokensAnimating = false;
8777 final int NAT = mAppTokens.size();
8778 for (i=0; i<NAT; i++) {
8779 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8780 tokensAnimating = true;
8781 }
8782 }
8783 final int NEAT = mExitingAppTokens.size();
8784 for (i=0; i<NEAT; i++) {
8785 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8786 tokensAnimating = true;
8787 }
8788 }
8789
8790 animating = tokensAnimating;
8791 restart = false;
8792
8793 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008794 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795
8796 mPolicy.beginAnimationLw(dw, dh);
8797
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008798 final int N = mWindows.size();
8799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 for (i=N-1; i>=0; i--) {
8801 WindowState w = (WindowState)mWindows.get(i);
8802
8803 final WindowManager.LayoutParams attrs = w.mAttrs;
8804
8805 if (w.mSurface != null) {
8806 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008807 if (w.commitFinishDrawingLocked(currentTime)) {
8808 if ((w.mAttrs.flags
8809 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
8810 wallpaperMayChange = true;
8811 }
8812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8814 animating = true;
8815 //w.dump(" ");
8816 }
8817
8818 mPolicy.animatingWindowLw(w, attrs);
8819 }
8820
8821 final AppWindowToken atoken = w.mAppToken;
8822 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8823 if (atoken.lastTransactionSequence != transactionSequence) {
8824 atoken.lastTransactionSequence = transactionSequence;
8825 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8826 atoken.startingDisplayed = false;
8827 }
8828 if ((w.isOnScreen() || w.mAttrs.type
8829 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8830 && !w.mExiting && !w.mDestroying) {
8831 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8832 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8833 + w.isDisplayedLw()
8834 + ", isAnimating=" + w.isAnimating());
8835 if (!w.isDisplayedLw()) {
8836 Log.v(TAG, "Not displayed: s=" + w.mSurface
8837 + " pv=" + w.mPolicyVisibility
8838 + " dp=" + w.mDrawPending
8839 + " cdp=" + w.mCommitDrawPending
8840 + " ah=" + w.mAttachedHidden
8841 + " th=" + atoken.hiddenRequested
8842 + " a=" + w.mAnimating);
8843 }
8844 }
8845 if (w != atoken.startingWindow) {
8846 if (!atoken.freezingScreen || !w.mAppFreezing) {
8847 atoken.numInterestingWindows++;
8848 if (w.isDisplayedLw()) {
8849 atoken.numDrawnWindows++;
8850 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8851 "tokenMayBeDrawn: " + atoken
8852 + " freezingScreen=" + atoken.freezingScreen
8853 + " mAppFreezing=" + w.mAppFreezing);
8854 tokenMayBeDrawn = true;
8855 }
8856 }
8857 } else if (w.isDisplayedLw()) {
8858 atoken.startingDisplayed = true;
8859 }
8860 }
8861 } else if (w.mReadyToShow) {
8862 w.performShowLocked();
8863 }
8864 }
8865
8866 if (mPolicy.finishAnimationLw()) {
8867 restart = true;
8868 }
8869
8870 if (tokenMayBeDrawn) {
8871 // See if any windows have been drawn, so they (and others
8872 // associated with them) can now be shown.
8873 final int NT = mTokenList.size();
8874 for (i=0; i<NT; i++) {
8875 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8876 if (wtoken == null) {
8877 continue;
8878 }
8879 if (wtoken.freezingScreen) {
8880 int numInteresting = wtoken.numInterestingWindows;
8881 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8882 if (DEBUG_VISIBILITY) Log.v(TAG,
8883 "allDrawn: " + wtoken
8884 + " interesting=" + numInteresting
8885 + " drawn=" + wtoken.numDrawnWindows);
8886 wtoken.showAllWindowsLocked();
8887 unsetAppFreezingScreenLocked(wtoken, false, true);
8888 orientationChangeComplete = true;
8889 }
8890 } else if (!wtoken.allDrawn) {
8891 int numInteresting = wtoken.numInterestingWindows;
8892 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8893 if (DEBUG_VISIBILITY) Log.v(TAG,
8894 "allDrawn: " + wtoken
8895 + " interesting=" + numInteresting
8896 + " drawn=" + wtoken.numDrawnWindows);
8897 wtoken.allDrawn = true;
8898 restart = true;
8899
8900 // We can now show all of the drawn windows!
8901 if (!mOpeningApps.contains(wtoken)) {
8902 wtoken.showAllWindowsLocked();
8903 }
8904 }
8905 }
8906 }
8907 }
8908
8909 // If we are ready to perform an app transition, check through
8910 // all of the app tokens to be shown and see if they are ready
8911 // to go.
8912 if (mAppTransitionReady) {
8913 int NN = mOpeningApps.size();
8914 boolean goodToGo = true;
8915 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8916 "Checking " + NN + " opening apps (frozen="
8917 + mDisplayFrozen + " timeout="
8918 + mAppTransitionTimeout + ")...");
8919 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8920 // If the display isn't frozen, wait to do anything until
8921 // all of the apps are ready. Otherwise just go because
8922 // we'll unfreeze the display when everyone is ready.
8923 for (i=0; i<NN && goodToGo; i++) {
8924 AppWindowToken wtoken = mOpeningApps.get(i);
8925 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8926 "Check opening app" + wtoken + ": allDrawn="
8927 + wtoken.allDrawn + " startingDisplayed="
8928 + wtoken.startingDisplayed);
8929 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8930 && !wtoken.startingMoved) {
8931 goodToGo = false;
8932 }
8933 }
8934 }
8935 if (goodToGo) {
8936 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8937 int transit = mNextAppTransition;
8938 if (mSkipAppTransitionAnimation) {
8939 transit = WindowManagerPolicy.TRANSIT_NONE;
8940 }
8941 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8942 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008943 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008944 mAppTransitionTimeout = false;
8945 mStartingIconInTransition = false;
8946 mSkipAppTransitionAnimation = false;
8947
8948 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8949
Dianne Hackborna8f60182009-09-01 19:01:50 -07008950 // If there are applications waiting to come to the
8951 // top of the stack, now is the time to move their windows.
8952 // (Note that we don't do apps going to the bottom
8953 // here -- we want to keep their windows in the old
8954 // Z-order until the animation completes.)
8955 if (mToTopApps.size() > 0) {
8956 NN = mAppTokens.size();
8957 for (i=0; i<NN; i++) {
8958 AppWindowToken wtoken = mAppTokens.get(i);
8959 if (wtoken.sendingToTop) {
8960 wtoken.sendingToTop = false;
8961 moveAppWindowsLocked(wtoken, NN, false);
8962 }
8963 }
8964 mToTopApps.clear();
8965 }
8966
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008967 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008968 wallpaperMayChange = false;
8969
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008970 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008971 "New wallpaper target=" + mWallpaperTarget
8972 + ", lower target=" + mLowerWallpaperTarget
8973 + ", upper target=" + mUpperWallpaperTarget);
8974 if (mLowerWallpaperTarget != null) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008975 // Need to determine if both the closing and
8976 // opening app token sets are wallpaper targets,
8977 // in which case special animations are needed
8978 // (since the wallpaper needs to stay static
8979 // behind them).
8980 int found = 0;
8981 NN = mOpeningApps.size();
8982 for (i=0; i<NN; i++) {
8983 AppWindowToken wtoken = mOpeningApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008984 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008985 found |= 1;
8986 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008987 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008988 found |= 1;
8989 }
8990 }
8991 NN = mClosingApps.size();
8992 for (i=0; i<NN; i++) {
8993 AppWindowToken wtoken = mClosingApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008994 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008995 found |= 2;
8996 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008997 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008998 found |= 2;
8999 }
9000 }
9001
9002 if (found == 3) {
9003 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9004 "Wallpaper animation!");
9005 switch (transit) {
9006 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9007 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9008 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9009 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN;
9010 break;
9011 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9012 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9013 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9014 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE;
9015 break;
9016 }
9017 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9018 "New transit: " + transit);
9019 }
9020 }
9021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 // We need to figure out which animation to use...
9023 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
9024 mOpeningApps, mClosingApps);
9025
9026 NN = mOpeningApps.size();
9027 for (i=0; i<NN; i++) {
9028 AppWindowToken wtoken = mOpeningApps.get(i);
9029 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9030 "Now opening app" + wtoken);
9031 wtoken.reportedVisible = false;
9032 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009033 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
9035 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009036 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 wtoken.showAllWindowsLocked();
9038 }
9039 NN = mClosingApps.size();
9040 for (i=0; i<NN; i++) {
9041 AppWindowToken wtoken = mClosingApps.get(i);
9042 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9043 "Now closing app" + wtoken);
9044 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009045 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
9047 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009048 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009049 // Force the allDrawn flag, because we want to start
9050 // this guy's animations regardless of whether it's
9051 // gotten drawn.
9052 wtoken.allDrawn = true;
9053 }
9054
9055 mOpeningApps.clear();
9056 mClosingApps.clear();
9057
9058 // This has changed the visibility of windows, so perform
9059 // a new layout to get them all up-to-date.
9060 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009061 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9062 assignLayersLocked();
9063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 performLayoutLockedInner();
9065 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9066
9067 restart = true;
9068 }
9069 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009070
Dianne Hackborna8f60182009-09-01 19:01:50 -07009071 if (!animating && mAppTransitionRunning) {
9072 // We have finished the animation of an app transition. To do
9073 // this, we have delayed a lot of operations like showing and
9074 // hiding apps, moving apps in Z-order, etc. The app token list
9075 // reflects the correct Z-order, but the window list may now
9076 // be out of sync with it. So here we will just rebuild the
9077 // entire app window list. Fun!
9078 mAppTransitionRunning = false;
9079 // Clear information about apps that were moving.
9080 mToBottomApps.clear();
9081
9082 rebuildAppWindowListLocked();
9083 restart = true;
9084 moveInputMethodWindowsIfNeededLocked(false);
9085 wallpaperMayChange = true;
9086 mLayoutNeeded = true;
9087 }
9088
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009089 if (wallpaperMayChange) {
9090 if (adjustWallpaperWindowsLocked()) {
9091 assignLayersLocked();
9092 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009093 if (mLayoutNeeded) {
9094 performLayoutLockedInner();
9095 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009096 }
9097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 } while (restart);
9099
9100 // THIRD LOOP: Update the surfaces of all windows.
9101
9102 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9103
9104 boolean obscured = false;
9105 boolean blurring = false;
9106 boolean dimming = false;
9107 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009108 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009109 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009111 final int N = mWindows.size();
9112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 for (i=N-1; i>=0; i--) {
9114 WindowState w = (WindowState)mWindows.get(i);
9115
9116 boolean displayed = false;
9117 final WindowManager.LayoutParams attrs = w.mAttrs;
9118 final int attrFlags = attrs.flags;
9119
9120 if (w.mSurface != null) {
9121 w.computeShownFrameLocked();
9122 if (localLOGV) Log.v(
9123 TAG, "Placing surface #" + i + " " + w.mSurface
9124 + ": new=" + w.mShownFrame + ", old="
9125 + w.mLastShownFrame);
9126
9127 boolean resize;
9128 int width, height;
9129 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9130 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9131 w.mLastRequestedHeight != w.mRequestedHeight;
9132 // for a scaled surface, we just want to use
9133 // the requested size.
9134 width = w.mRequestedWidth;
9135 height = w.mRequestedHeight;
9136 w.mLastRequestedWidth = width;
9137 w.mLastRequestedHeight = height;
9138 w.mLastShownFrame.set(w.mShownFrame);
9139 try {
9140 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9141 } catch (RuntimeException e) {
9142 Log.w(TAG, "Error positioning surface in " + w, e);
9143 if (!recoveringMemory) {
9144 reclaimSomeSurfaceMemoryLocked(w, "position");
9145 }
9146 }
9147 } else {
9148 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9149 width = w.mShownFrame.width();
9150 height = w.mShownFrame.height();
9151 w.mLastShownFrame.set(w.mShownFrame);
9152 if (resize) {
9153 if (SHOW_TRANSACTIONS) Log.i(
9154 TAG, " SURFACE " + w.mSurface + ": ("
9155 + w.mShownFrame.left + ","
9156 + w.mShownFrame.top + ") ("
9157 + w.mShownFrame.width() + "x"
9158 + w.mShownFrame.height() + ")");
9159 }
9160 }
9161
9162 if (resize) {
9163 if (width < 1) width = 1;
9164 if (height < 1) height = 1;
9165 if (w.mSurface != null) {
9166 try {
9167 w.mSurface.setSize(width, height);
9168 w.mSurface.setPosition(w.mShownFrame.left,
9169 w.mShownFrame.top);
9170 } catch (RuntimeException e) {
9171 // If something goes wrong with the surface (such
9172 // as running out of memory), don't take down the
9173 // entire system.
9174 Log.e(TAG, "Failure updating surface of " + w
9175 + "size=(" + width + "x" + height
9176 + "), pos=(" + w.mShownFrame.left
9177 + "," + w.mShownFrame.top + ")", e);
9178 if (!recoveringMemory) {
9179 reclaimSomeSurfaceMemoryLocked(w, "size");
9180 }
9181 }
9182 }
9183 }
9184 if (!w.mAppFreezing) {
9185 w.mContentInsetsChanged =
9186 !w.mLastContentInsets.equals(w.mContentInsets);
9187 w.mVisibleInsetsChanged =
9188 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009189 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 || w.mContentInsetsChanged
9191 || w.mVisibleInsetsChanged) {
9192 w.mLastFrame.set(w.mFrame);
9193 w.mLastContentInsets.set(w.mContentInsets);
9194 w.mLastVisibleInsets.set(w.mVisibleInsets);
9195 // If the orientation is changing, then we need to
9196 // hold off on unfreezing the display until this
9197 // window has been redrawn; to do that, we need
9198 // to go through the process of getting informed
9199 // by the application when it has finished drawing.
9200 if (w.mOrientationChanging) {
9201 if (DEBUG_ORIENTATION) Log.v(TAG,
9202 "Orientation start waiting for draw in "
9203 + w + ", surface " + w.mSurface);
9204 w.mDrawPending = true;
9205 w.mCommitDrawPending = false;
9206 w.mReadyToShow = false;
9207 if (w.mAppToken != null) {
9208 w.mAppToken.allDrawn = false;
9209 }
9210 }
Romain Guy06882f82009-06-10 13:36:04 -07009211 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 "Resizing window " + w + " to " + w.mFrame);
9213 mResizingWindows.add(w);
9214 } else if (w.mOrientationChanging) {
9215 if (!w.mDrawPending && !w.mCommitDrawPending) {
9216 if (DEBUG_ORIENTATION) Log.v(TAG,
9217 "Orientation not waiting for draw in "
9218 + w + ", surface " + w.mSurface);
9219 w.mOrientationChanging = false;
9220 }
9221 }
9222 }
9223
9224 if (w.mAttachedHidden) {
9225 if (!w.mLastHidden) {
9226 //dump();
9227 w.mLastHidden = true;
9228 if (SHOW_TRANSACTIONS) Log.i(
9229 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9230 if (w.mSurface != null) {
9231 try {
9232 w.mSurface.hide();
9233 } catch (RuntimeException e) {
9234 Log.w(TAG, "Exception hiding surface in " + w);
9235 }
9236 }
9237 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9238 }
9239 // If we are waiting for this window to handle an
9240 // orientation change, well, it is hidden, so
9241 // doesn't really matter. Note that this does
9242 // introduce a potential glitch if the window
9243 // becomes unhidden before it has drawn for the
9244 // new orientation.
9245 if (w.mOrientationChanging) {
9246 w.mOrientationChanging = false;
9247 if (DEBUG_ORIENTATION) Log.v(TAG,
9248 "Orientation change skips hidden " + w);
9249 }
9250 } else if (!w.isReadyForDisplay()) {
9251 if (!w.mLastHidden) {
9252 //dump();
9253 w.mLastHidden = true;
9254 if (SHOW_TRANSACTIONS) Log.i(
9255 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9256 if (w.mSurface != null) {
9257 try {
9258 w.mSurface.hide();
9259 } catch (RuntimeException e) {
9260 Log.w(TAG, "Exception exception hiding surface in " + w);
9261 }
9262 }
9263 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9264 }
9265 // If we are waiting for this window to handle an
9266 // orientation change, well, it is hidden, so
9267 // doesn't really matter. Note that this does
9268 // introduce a potential glitch if the window
9269 // becomes unhidden before it has drawn for the
9270 // new orientation.
9271 if (w.mOrientationChanging) {
9272 w.mOrientationChanging = false;
9273 if (DEBUG_ORIENTATION) Log.v(TAG,
9274 "Orientation change skips hidden " + w);
9275 }
9276 } else if (w.mLastLayer != w.mAnimLayer
9277 || w.mLastAlpha != w.mShownAlpha
9278 || w.mLastDsDx != w.mDsDx
9279 || w.mLastDtDx != w.mDtDx
9280 || w.mLastDsDy != w.mDsDy
9281 || w.mLastDtDy != w.mDtDy
9282 || w.mLastHScale != w.mHScale
9283 || w.mLastVScale != w.mVScale
9284 || w.mLastHidden) {
9285 displayed = true;
9286 w.mLastAlpha = w.mShownAlpha;
9287 w.mLastLayer = w.mAnimLayer;
9288 w.mLastDsDx = w.mDsDx;
9289 w.mLastDtDx = w.mDtDx;
9290 w.mLastDsDy = w.mDsDy;
9291 w.mLastDtDy = w.mDtDy;
9292 w.mLastHScale = w.mHScale;
9293 w.mLastVScale = w.mVScale;
9294 if (SHOW_TRANSACTIONS) Log.i(
9295 TAG, " SURFACE " + w.mSurface + ": alpha="
9296 + w.mShownAlpha + " layer=" + w.mAnimLayer);
9297 if (w.mSurface != null) {
9298 try {
9299 w.mSurface.setAlpha(w.mShownAlpha);
9300 w.mSurface.setLayer(w.mAnimLayer);
9301 w.mSurface.setMatrix(
9302 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9303 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9304 } catch (RuntimeException e) {
9305 Log.w(TAG, "Error updating surface in " + w, e);
9306 if (!recoveringMemory) {
9307 reclaimSomeSurfaceMemoryLocked(w, "update");
9308 }
9309 }
9310 }
9311
9312 if (w.mLastHidden && !w.mDrawPending
9313 && !w.mCommitDrawPending
9314 && !w.mReadyToShow) {
9315 if (SHOW_TRANSACTIONS) Log.i(
9316 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9317 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9318 + " during relayout");
9319 if (showSurfaceRobustlyLocked(w)) {
9320 w.mHasDrawn = true;
9321 w.mLastHidden = false;
9322 } else {
9323 w.mOrientationChanging = false;
9324 }
9325 }
9326 if (w.mSurface != null) {
9327 w.mToken.hasVisible = true;
9328 }
9329 } else {
9330 displayed = true;
9331 }
9332
9333 if (displayed) {
9334 if (!covered) {
9335 if (attrs.width == LayoutParams.FILL_PARENT
9336 && attrs.height == LayoutParams.FILL_PARENT) {
9337 covered = true;
9338 }
9339 }
9340 if (w.mOrientationChanging) {
9341 if (w.mDrawPending || w.mCommitDrawPending) {
9342 orientationChangeComplete = false;
9343 if (DEBUG_ORIENTATION) Log.v(TAG,
9344 "Orientation continue waiting for draw in " + w);
9345 } else {
9346 w.mOrientationChanging = false;
9347 if (DEBUG_ORIENTATION) Log.v(TAG,
9348 "Orientation change complete in " + w);
9349 }
9350 }
9351 w.mToken.hasVisible = true;
9352 }
9353 } else if (w.mOrientationChanging) {
9354 if (DEBUG_ORIENTATION) Log.v(TAG,
9355 "Orientation change skips hidden " + w);
9356 w.mOrientationChanging = false;
9357 }
9358
9359 final boolean canBeSeen = w.isDisplayedLw();
9360
9361 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9362 focusDisplayed = true;
9363 }
9364
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009365 final boolean obscuredChanged = w.mObscured != obscured;
9366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009368 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009369 if (w.mSurface != null) {
9370 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9371 holdScreen = w.mSession;
9372 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009373 if (!syswin && w.mAttrs.screenBrightness >= 0
9374 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 screenBrightness = w.mAttrs.screenBrightness;
9376 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009377 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9378 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9379 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9380 syswin = true;
9381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009383
9384 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009385 if ((opaqueDrawn && w.isFullscreen(dw, dh))
9386 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009387 // This window completely covers everything behind it,
9388 // so we want to leave all of them as unblurred (for
9389 // performance reasons).
9390 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009391 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9392 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009393 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009394 obscured = true;
9395 if (mBackgroundFillerSurface == null) {
9396 try {
9397 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9398 0, dw, dh,
9399 PixelFormat.OPAQUE,
9400 Surface.FX_SURFACE_NORMAL);
9401 } catch (Exception e) {
9402 Log.e(TAG, "Exception creating filler surface", e);
9403 }
9404 }
9405 try {
9406 mBackgroundFillerSurface.setPosition(0, 0);
9407 mBackgroundFillerSurface.setSize(dw, dh);
9408 // Using the same layer as Dim because they will never be shown at the
9409 // same time.
9410 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9411 mBackgroundFillerSurface.show();
9412 } catch (RuntimeException e) {
9413 Log.e(TAG, "Exception showing filler surface");
9414 }
9415 backgroundFillerShown = true;
9416 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 } else if (canBeSeen && !obscured &&
9418 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9419 if (localLOGV) Log.v(TAG, "Win " + w
9420 + ": blurring=" + blurring
9421 + " obscured=" + obscured
9422 + " displayed=" + displayed);
9423 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9424 if (!dimming) {
9425 //Log.i(TAG, "DIM BEHIND: " + w);
9426 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009427 if (mDimAnimator == null) {
9428 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009429 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009430 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009432 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009433 }
9434 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9435 if (!blurring) {
9436 //Log.i(TAG, "BLUR BEHIND: " + w);
9437 blurring = true;
9438 mBlurShown = true;
9439 if (mBlurSurface == null) {
9440 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9441 + mBlurSurface + ": CREATE");
9442 try {
Romain Guy06882f82009-06-10 13:36:04 -07009443 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444 -1, 16, 16,
9445 PixelFormat.OPAQUE,
9446 Surface.FX_SURFACE_BLUR);
9447 } catch (Exception e) {
9448 Log.e(TAG, "Exception creating Blur surface", e);
9449 }
9450 }
9451 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9452 + mBlurSurface + ": SHOW pos=(0,0) (" +
9453 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9454 if (mBlurSurface != null) {
9455 mBlurSurface.setPosition(0, 0);
9456 mBlurSurface.setSize(dw, dh);
9457 try {
9458 mBlurSurface.show();
9459 } catch (RuntimeException e) {
9460 Log.w(TAG, "Failure showing blur surface", e);
9461 }
9462 }
9463 }
9464 mBlurSurface.setLayer(w.mAnimLayer-2);
9465 }
9466 }
9467 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009468
9469 if (obscuredChanged && mWallpaperTarget == w) {
9470 // This is the wallpaper target and its obscured state
9471 // changed... make sure the current wallaper's visibility
9472 // has been updated accordingly.
9473 updateWallpaperVisibilityLocked();
9474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009475 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009476
9477 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9478 mBackgroundFillerShown = false;
9479 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9480 try {
9481 mBackgroundFillerSurface.hide();
9482 } catch (RuntimeException e) {
9483 Log.e(TAG, "Exception hiding filler surface", e);
9484 }
9485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009487 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9488 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 }
Romain Guy06882f82009-06-10 13:36:04 -07009490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 if (!blurring && mBlurShown) {
9492 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9493 + ": HIDE");
9494 try {
9495 mBlurSurface.hide();
9496 } catch (IllegalArgumentException e) {
9497 Log.w(TAG, "Illegal argument exception hiding blur surface");
9498 }
9499 mBlurShown = false;
9500 }
9501
9502 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9503 } catch (RuntimeException e) {
9504 Log.e(TAG, "Unhandled exception in Window Manager", e);
9505 }
9506
9507 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9510 "With display frozen, orientationChangeComplete="
9511 + orientationChangeComplete);
9512 if (orientationChangeComplete) {
9513 if (mWindowsFreezingScreen) {
9514 mWindowsFreezingScreen = false;
9515 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9516 }
9517 if (mAppsFreezingScreen == 0) {
9518 stopFreezingDisplayLocked();
9519 }
9520 }
Romain Guy06882f82009-06-10 13:36:04 -07009521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 i = mResizingWindows.size();
9523 if (i > 0) {
9524 do {
9525 i--;
9526 WindowState win = mResizingWindows.get(i);
9527 try {
9528 win.mClient.resized(win.mFrame.width(),
9529 win.mFrame.height(), win.mLastContentInsets,
9530 win.mLastVisibleInsets, win.mDrawPending);
9531 win.mContentInsetsChanged = false;
9532 win.mVisibleInsetsChanged = false;
9533 } catch (RemoteException e) {
9534 win.mOrientationChanging = false;
9535 }
9536 } while (i > 0);
9537 mResizingWindows.clear();
9538 }
Romain Guy06882f82009-06-10 13:36:04 -07009539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009541 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 i = mDestroySurface.size();
9543 if (i > 0) {
9544 do {
9545 i--;
9546 WindowState win = mDestroySurface.get(i);
9547 win.mDestroying = false;
9548 if (mInputMethodWindow == win) {
9549 mInputMethodWindow = null;
9550 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009551 if (win == mWallpaperTarget) {
9552 wallpaperDestroyed = true;
9553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009554 win.destroySurfaceLocked();
9555 } while (i > 0);
9556 mDestroySurface.clear();
9557 }
9558
9559 // Time to remove any exiting tokens?
9560 for (i=mExitingTokens.size()-1; i>=0; i--) {
9561 WindowToken token = mExitingTokens.get(i);
9562 if (!token.hasVisible) {
9563 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009564 if (token.windowType == TYPE_WALLPAPER) {
9565 mWallpaperTokens.remove(token);
9566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 }
9568 }
9569
9570 // Time to remove any exiting applications?
9571 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9572 AppWindowToken token = mExitingAppTokens.get(i);
9573 if (!token.hasVisible && !mClosingApps.contains(token)) {
9574 mAppTokens.remove(token);
9575 mExitingAppTokens.remove(i);
9576 }
9577 }
9578
Dianne Hackborna8f60182009-09-01 19:01:50 -07009579 boolean needRelayout = false;
9580
9581 if (!animating && mAppTransitionRunning) {
9582 // We have finished the animation of an app transition. To do
9583 // this, we have delayed a lot of operations like showing and
9584 // hiding apps, moving apps in Z-order, etc. The app token list
9585 // reflects the correct Z-order, but the window list may now
9586 // be out of sync with it. So here we will just rebuild the
9587 // entire app window list. Fun!
9588 mAppTransitionRunning = false;
9589 needRelayout = true;
9590 rebuildAppWindowListLocked();
9591 // Clear information about apps that were moving.
9592 mToBottomApps.clear();
9593 }
9594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 if (focusDisplayed) {
9596 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9597 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009598 if (wallpaperDestroyed) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07009599 needRelayout = adjustWallpaperWindowsLocked();
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009600 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009601 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009602 requestAnimationLocked(0);
9603 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009604 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9605 }
9606 mQueue.setHoldScreenLocked(holdScreen != null);
9607 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9608 mPowerManager.setScreenBrightnessOverride(-1);
9609 } else {
9610 mPowerManager.setScreenBrightnessOverride((int)
9611 (screenBrightness * Power.BRIGHTNESS_ON));
9612 }
9613 if (holdScreen != mHoldingScreenOn) {
9614 mHoldingScreenOn = holdScreen;
9615 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9616 mH.sendMessage(m);
9617 }
9618 }
9619
9620 void requestAnimationLocked(long delay) {
9621 if (!mAnimationPending) {
9622 mAnimationPending = true;
9623 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9624 }
9625 }
Romain Guy06882f82009-06-10 13:36:04 -07009626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 /**
9628 * Have the surface flinger show a surface, robustly dealing with
9629 * error conditions. In particular, if there is not enough memory
9630 * to show the surface, then we will try to get rid of other surfaces
9631 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009632 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 * @return Returns true if the surface was successfully shown.
9634 */
9635 boolean showSurfaceRobustlyLocked(WindowState win) {
9636 try {
9637 if (win.mSurface != null) {
9638 win.mSurface.show();
9639 }
9640 return true;
9641 } catch (RuntimeException e) {
9642 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9643 }
Romain Guy06882f82009-06-10 13:36:04 -07009644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 return false;
9648 }
Romain Guy06882f82009-06-10 13:36:04 -07009649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9651 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9654 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009656 if (mForceRemoves == null) {
9657 mForceRemoves = new ArrayList<WindowState>();
9658 }
Romain Guy06882f82009-06-10 13:36:04 -07009659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 long callingIdentity = Binder.clearCallingIdentity();
9661 try {
9662 // There was some problem... first, do a sanity check of the
9663 // window list to make sure we haven't left any dangling surfaces
9664 // around.
9665 int N = mWindows.size();
9666 boolean leakedSurface = false;
9667 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9668 for (int i=0; i<N; i++) {
9669 WindowState ws = (WindowState)mWindows.get(i);
9670 if (ws.mSurface != null) {
9671 if (!mSessions.contains(ws.mSession)) {
9672 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9673 + ws + " surface=" + ws.mSurface
9674 + " token=" + win.mToken
9675 + " pid=" + ws.mSession.mPid
9676 + " uid=" + ws.mSession.mUid);
9677 ws.mSurface.clear();
9678 ws.mSurface = null;
9679 mForceRemoves.add(ws);
9680 i--;
9681 N--;
9682 leakedSurface = true;
9683 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9684 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9685 + ws + " surface=" + ws.mSurface
9686 + " token=" + win.mAppToken);
9687 ws.mSurface.clear();
9688 ws.mSurface = null;
9689 leakedSurface = true;
9690 }
9691 }
9692 }
Romain Guy06882f82009-06-10 13:36:04 -07009693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 boolean killedApps = false;
9695 if (!leakedSurface) {
9696 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9697 SparseIntArray pidCandidates = new SparseIntArray();
9698 for (int i=0; i<N; i++) {
9699 WindowState ws = (WindowState)mWindows.get(i);
9700 if (ws.mSurface != null) {
9701 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9702 }
9703 }
9704 if (pidCandidates.size() > 0) {
9705 int[] pids = new int[pidCandidates.size()];
9706 for (int i=0; i<pids.length; i++) {
9707 pids[i] = pidCandidates.keyAt(i);
9708 }
9709 try {
9710 if (mActivityManager.killPidsForMemory(pids)) {
9711 killedApps = true;
9712 }
9713 } catch (RemoteException e) {
9714 }
9715 }
9716 }
Romain Guy06882f82009-06-10 13:36:04 -07009717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 if (leakedSurface || killedApps) {
9719 // We managed to reclaim some memory, so get rid of the trouble
9720 // surface and ask the app to request another one.
9721 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9722 if (surface != null) {
9723 surface.clear();
9724 win.mSurface = null;
9725 }
Romain Guy06882f82009-06-10 13:36:04 -07009726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009727 try {
9728 win.mClient.dispatchGetNewSurface();
9729 } catch (RemoteException e) {
9730 }
9731 }
9732 } finally {
9733 Binder.restoreCallingIdentity(callingIdentity);
9734 }
9735 }
Romain Guy06882f82009-06-10 13:36:04 -07009736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009737 private boolean updateFocusedWindowLocked(int mode) {
9738 WindowState newFocus = computeFocusedWindowLocked();
9739 if (mCurrentFocus != newFocus) {
9740 // This check makes sure that we don't already have the focus
9741 // change message pending.
9742 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9743 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9744 if (localLOGV) Log.v(
9745 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9746 final WindowState oldFocus = mCurrentFocus;
9747 mCurrentFocus = newFocus;
9748 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 final WindowState imWindow = mInputMethodWindow;
9751 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009752 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009753 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009754 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9755 mLayoutNeeded = true;
9756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009757 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9758 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009759 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9760 // Client will do the layout, but we need to assign layers
9761 // for handleNewWindowLocked() below.
9762 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763 }
9764 }
Romain Guy06882f82009-06-10 13:36:04 -07009765
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009766 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9767 mKeyWaiter.handleNewWindowLocked(newFocus);
9768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 return true;
9770 }
9771 return false;
9772 }
9773
9774 private WindowState computeFocusedWindowLocked() {
9775 WindowState result = null;
9776 WindowState win;
9777
9778 int i = mWindows.size() - 1;
9779 int nextAppIndex = mAppTokens.size()-1;
9780 WindowToken nextApp = nextAppIndex >= 0
9781 ? mAppTokens.get(nextAppIndex) : null;
9782
9783 while (i >= 0) {
9784 win = (WindowState)mWindows.get(i);
9785
9786 if (localLOGV || DEBUG_FOCUS) Log.v(
9787 TAG, "Looking for focus: " + i
9788 + " = " + win
9789 + ", flags=" + win.mAttrs.flags
9790 + ", canReceive=" + win.canReceiveKeys());
9791
9792 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 // If this window's application has been removed, just skip it.
9795 if (thisApp != null && thisApp.removed) {
9796 i--;
9797 continue;
9798 }
Romain Guy06882f82009-06-10 13:36:04 -07009799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 // If there is a focused app, don't allow focus to go to any
9801 // windows below it. If this is an application window, step
9802 // through the app tokens until we find its app.
9803 if (thisApp != null && nextApp != null && thisApp != nextApp
9804 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9805 int origAppIndex = nextAppIndex;
9806 while (nextAppIndex > 0) {
9807 if (nextApp == mFocusedApp) {
9808 // Whoops, we are below the focused app... no focus
9809 // for you!
9810 if (localLOGV || DEBUG_FOCUS) Log.v(
9811 TAG, "Reached focused app: " + mFocusedApp);
9812 return null;
9813 }
9814 nextAppIndex--;
9815 nextApp = mAppTokens.get(nextAppIndex);
9816 if (nextApp == thisApp) {
9817 break;
9818 }
9819 }
9820 if (thisApp != nextApp) {
9821 // Uh oh, the app token doesn't exist! This shouldn't
9822 // happen, but if it does we can get totally hosed...
9823 // so restart at the original app.
9824 nextAppIndex = origAppIndex;
9825 nextApp = mAppTokens.get(nextAppIndex);
9826 }
9827 }
9828
9829 // Dispatch to this window if it is wants key events.
9830 if (win.canReceiveKeys()) {
9831 if (DEBUG_FOCUS) Log.v(
9832 TAG, "Found focus @ " + i + " = " + win);
9833 result = win;
9834 break;
9835 }
9836
9837 i--;
9838 }
9839
9840 return result;
9841 }
9842
9843 private void startFreezingDisplayLocked() {
9844 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009845 // Freezing the display also suspends key event delivery, to
9846 // keep events from going astray while the display is reconfigured.
9847 // If someone has changed orientation again while the screen is
9848 // still frozen, the events will continue to be blocked while the
9849 // successive orientation change is processed. To prevent spurious
9850 // ANRs, we reset the event dispatch timeout in this case.
9851 synchronized (mKeyWaiter) {
9852 mKeyWaiter.mWasFrozen = true;
9853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009854 return;
9855 }
Romain Guy06882f82009-06-10 13:36:04 -07009856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 long now = SystemClock.uptimeMillis();
9860 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9861 if (mFreezeGcPending != 0) {
9862 if (now > (mFreezeGcPending+1000)) {
9863 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9864 mH.removeMessages(H.FORCE_GC);
9865 Runtime.getRuntime().gc();
9866 mFreezeGcPending = now;
9867 }
9868 } else {
9869 mFreezeGcPending = now;
9870 }
Romain Guy06882f82009-06-10 13:36:04 -07009871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 mDisplayFrozen = true;
9873 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9874 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9875 mAppTransitionReady = true;
9876 }
Romain Guy06882f82009-06-10 13:36:04 -07009877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 if (PROFILE_ORIENTATION) {
9879 File file = new File("/data/system/frozen");
9880 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9881 }
9882 Surface.freezeDisplay(0);
9883 }
Romain Guy06882f82009-06-10 13:36:04 -07009884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885 private void stopFreezingDisplayLocked() {
9886 if (!mDisplayFrozen) {
9887 return;
9888 }
Romain Guy06882f82009-06-10 13:36:04 -07009889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 mDisplayFrozen = false;
9891 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9892 if (PROFILE_ORIENTATION) {
9893 Debug.stopMethodTracing();
9894 }
9895 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009896
Chris Tate2ad63a92009-03-25 17:36:48 -07009897 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9898 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 synchronized (mKeyWaiter) {
9900 mKeyWaiter.mWasFrozen = true;
9901 mKeyWaiter.notifyAll();
9902 }
9903
9904 // A little kludge: a lot could have happened while the
9905 // display was frozen, so now that we are coming back we
9906 // do a gc so that any remote references the system
9907 // processes holds on others can be released if they are
9908 // no longer needed.
9909 mH.removeMessages(H.FORCE_GC);
9910 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9911 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 mScreenFrozenLock.release();
9914 }
Romain Guy06882f82009-06-10 13:36:04 -07009915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 @Override
9917 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9918 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9919 != PackageManager.PERMISSION_GRANTED) {
9920 pw.println("Permission Denial: can't dump WindowManager from from pid="
9921 + Binder.getCallingPid()
9922 + ", uid=" + Binder.getCallingUid());
9923 return;
9924 }
Romain Guy06882f82009-06-10 13:36:04 -07009925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 synchronized(mWindowMap) {
9927 pw.println("Current Window Manager state:");
9928 for (int i=mWindows.size()-1; i>=0; i--) {
9929 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009930 pw.print(" Window #"); pw.print(i); pw.print(' ');
9931 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009932 w.dump(pw, " ");
9933 }
9934 if (mInputMethodDialogs.size() > 0) {
9935 pw.println(" ");
9936 pw.println(" Input method dialogs:");
9937 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9938 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009939 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 }
9941 }
9942 if (mPendingRemove.size() > 0) {
9943 pw.println(" ");
9944 pw.println(" Remove pending for:");
9945 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9946 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009947 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9948 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 w.dump(pw, " ");
9950 }
9951 }
9952 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9953 pw.println(" ");
9954 pw.println(" Windows force removing:");
9955 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9956 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009957 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9958 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 w.dump(pw, " ");
9960 }
9961 }
9962 if (mDestroySurface.size() > 0) {
9963 pw.println(" ");
9964 pw.println(" Windows waiting to destroy their surface:");
9965 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9966 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009967 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9968 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 w.dump(pw, " ");
9970 }
9971 }
9972 if (mLosingFocus.size() > 0) {
9973 pw.println(" ");
9974 pw.println(" Windows losing focus:");
9975 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9976 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009977 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9978 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 w.dump(pw, " ");
9980 }
9981 }
9982 if (mSessions.size() > 0) {
9983 pw.println(" ");
9984 pw.println(" All active sessions:");
9985 Iterator<Session> it = mSessions.iterator();
9986 while (it.hasNext()) {
9987 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009988 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 s.dump(pw, " ");
9990 }
9991 }
9992 if (mTokenMap.size() > 0) {
9993 pw.println(" ");
9994 pw.println(" All tokens:");
9995 Iterator<WindowToken> it = mTokenMap.values().iterator();
9996 while (it.hasNext()) {
9997 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009998 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 token.dump(pw, " ");
10000 }
10001 }
10002 if (mTokenList.size() > 0) {
10003 pw.println(" ");
10004 pw.println(" Window token list:");
10005 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010006 pw.print(" #"); pw.print(i); pw.print(": ");
10007 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 }
10009 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010010 if (mWallpaperTokens.size() > 0) {
10011 pw.println(" ");
10012 pw.println(" Wallpaper tokens:");
10013 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10014 WindowToken token = mWallpaperTokens.get(i);
10015 pw.print(" Wallpaper #"); pw.print(i);
10016 pw.print(' '); pw.print(token); pw.println(':');
10017 token.dump(pw, " ");
10018 }
10019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 if (mAppTokens.size() > 0) {
10021 pw.println(" ");
10022 pw.println(" Application tokens in Z order:");
10023 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010024 pw.print(" App #"); pw.print(i); pw.print(": ");
10025 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 }
10027 }
10028 if (mFinishedStarting.size() > 0) {
10029 pw.println(" ");
10030 pw.println(" Finishing start of application tokens:");
10031 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10032 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010033 pw.print(" Finished Starting #"); pw.print(i);
10034 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010035 token.dump(pw, " ");
10036 }
10037 }
10038 if (mExitingTokens.size() > 0) {
10039 pw.println(" ");
10040 pw.println(" Exiting tokens:");
10041 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10042 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010043 pw.print(" Exiting #"); pw.print(i);
10044 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 token.dump(pw, " ");
10046 }
10047 }
10048 if (mExitingAppTokens.size() > 0) {
10049 pw.println(" ");
10050 pw.println(" Exiting application tokens:");
10051 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10052 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010053 pw.print(" Exiting App #"); pw.print(i);
10054 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010055 token.dump(pw, " ");
10056 }
10057 }
10058 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010059 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10060 pw.print(" mLastFocus="); pw.println(mLastFocus);
10061 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10062 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10063 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010064 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010065 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10066 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10067 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10068 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010069 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10070 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10071 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10072 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10073 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010074 if (mDimAnimator != null) {
10075 mDimAnimator.printTo(pw);
10076 } else {
10077 pw.print( " no DimAnimator ");
10078 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010079 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010080 pw.print(mInputMethodAnimLayerAdjustment);
10081 pw.print(" mWallpaperAnimLayerAdjustment=");
10082 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010083 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10084 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010085 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10086 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10087 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10088 pw.print(" mRotation="); pw.print(mRotation);
10089 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10090 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10091 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10092 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10093 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10094 pw.print(" mNextAppTransition=0x");
10095 pw.print(Integer.toHexString(mNextAppTransition));
10096 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010097 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010098 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10099 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10100 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10101 if (mOpeningApps.size() > 0) {
10102 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10103 }
10104 if (mClosingApps.size() > 0) {
10105 pw.print(" mClosingApps="); pw.println(mClosingApps);
10106 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010107 if (mToTopApps.size() > 0) {
10108 pw.print(" mToTopApps="); pw.println(mToTopApps);
10109 }
10110 if (mToBottomApps.size() > 0) {
10111 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10112 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010113 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10114 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010116 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10117 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10118 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10119 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10120 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10121 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 }
10123 }
10124
10125 public void monitor() {
10126 synchronized (mWindowMap) { }
10127 synchronized (mKeyguardDisabled) { }
10128 synchronized (mKeyWaiter) { }
10129 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010130
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010131 public void virtualKeyFeedback(KeyEvent event) {
10132 mPolicy.keyFeedbackFromInput(event);
10133 }
10134
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010135 /**
10136 * DimAnimator class that controls the dim animation. This holds the surface and
10137 * all state used for dim animation.
10138 */
10139 private static class DimAnimator {
10140 Surface mDimSurface;
10141 boolean mDimShown = false;
10142 float mDimCurrentAlpha;
10143 float mDimTargetAlpha;
10144 float mDimDeltaPerMs;
10145 long mLastDimAnimTime;
10146
10147 DimAnimator (SurfaceSession session) {
10148 if (mDimSurface == null) {
10149 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10150 + mDimSurface + ": CREATE");
10151 try {
10152 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10153 Surface.FX_SURFACE_DIM);
10154 } catch (Exception e) {
10155 Log.e(TAG, "Exception creating Dim surface", e);
10156 }
10157 }
10158 }
10159
10160 /**
10161 * Show the dim surface.
10162 */
10163 void show(int dw, int dh) {
10164 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10165 dw + "x" + dh + ")");
10166 mDimShown = true;
10167 try {
10168 mDimSurface.setPosition(0, 0);
10169 mDimSurface.setSize(dw, dh);
10170 mDimSurface.show();
10171 } catch (RuntimeException e) {
10172 Log.w(TAG, "Failure showing dim surface", e);
10173 }
10174 }
10175
10176 /**
10177 * Set's the dim surface's layer and update dim parameters that will be used in
10178 * {@link updateSurface} after all windows are examined.
10179 */
10180 void updateParameters(WindowState w, long currentTime) {
10181 mDimSurface.setLayer(w.mAnimLayer-1);
10182
10183 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
10184 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
10185 if (mDimTargetAlpha != target) {
10186 // If the desired dim level has changed, then
10187 // start an animation to it.
10188 mLastDimAnimTime = currentTime;
10189 long duration = (w.mAnimating && w.mAnimation != null)
10190 ? w.mAnimation.computeDurationHint()
10191 : DEFAULT_DIM_DURATION;
10192 if (target > mDimTargetAlpha) {
10193 // This is happening behind the activity UI,
10194 // so we can make it run a little longer to
10195 // give a stronger impression without disrupting
10196 // the user.
10197 duration *= DIM_DURATION_MULTIPLIER;
10198 }
10199 if (duration < 1) {
10200 // Don't divide by zero
10201 duration = 1;
10202 }
10203 mDimTargetAlpha = target;
10204 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10205 }
10206 }
10207
10208 /**
10209 * Updating the surface's alpha. Returns true if the animation continues, or returns
10210 * false when the animation is finished and the dim surface is hidden.
10211 */
10212 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10213 if (!dimming) {
10214 if (mDimTargetAlpha != 0) {
10215 mLastDimAnimTime = currentTime;
10216 mDimTargetAlpha = 0;
10217 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10218 }
10219 }
10220
10221 boolean animating = false;
10222 if (mLastDimAnimTime != 0) {
10223 mDimCurrentAlpha += mDimDeltaPerMs
10224 * (currentTime-mLastDimAnimTime);
10225 boolean more = true;
10226 if (displayFrozen) {
10227 // If the display is frozen, there is no reason to animate.
10228 more = false;
10229 } else if (mDimDeltaPerMs > 0) {
10230 if (mDimCurrentAlpha > mDimTargetAlpha) {
10231 more = false;
10232 }
10233 } else if (mDimDeltaPerMs < 0) {
10234 if (mDimCurrentAlpha < mDimTargetAlpha) {
10235 more = false;
10236 }
10237 } else {
10238 more = false;
10239 }
10240
10241 // Do we need to continue animating?
10242 if (more) {
10243 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10244 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10245 mLastDimAnimTime = currentTime;
10246 mDimSurface.setAlpha(mDimCurrentAlpha);
10247 animating = true;
10248 } else {
10249 mDimCurrentAlpha = mDimTargetAlpha;
10250 mLastDimAnimTime = 0;
10251 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10252 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10253 mDimSurface.setAlpha(mDimCurrentAlpha);
10254 if (!dimming) {
10255 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10256 + ": HIDE");
10257 try {
10258 mDimSurface.hide();
10259 } catch (RuntimeException e) {
10260 Log.w(TAG, "Illegal argument exception hiding dim surface");
10261 }
10262 mDimShown = false;
10263 }
10264 }
10265 }
10266 return animating;
10267 }
10268
10269 public void printTo(PrintWriter pw) {
10270 pw.print(" mDimShown="); pw.print(mDimShown);
10271 pw.print(" current="); pw.print(mDimCurrentAlpha);
10272 pw.print(" target="); pw.print(mDimTargetAlpha);
10273 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10274 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10275 }
10276 }
10277
10278 /**
10279 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10280 * This is used for opening/closing transition for apps in compatible mode.
10281 */
10282 private static class FadeInOutAnimation extends Animation {
10283 int mWidth;
10284 boolean mFadeIn;
10285
10286 public FadeInOutAnimation(boolean fadeIn) {
10287 setInterpolator(new AccelerateInterpolator());
10288 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10289 mFadeIn = fadeIn;
10290 }
10291
10292 @Override
10293 protected void applyTransformation(float interpolatedTime, Transformation t) {
10294 float x = interpolatedTime;
10295 if (!mFadeIn) {
10296 x = 1.0f - x; // reverse the interpolation for fade out
10297 }
10298 if (x < 0.5) {
10299 // move the window out of the screen.
10300 t.getMatrix().setTranslate(mWidth, 0);
10301 } else {
10302 t.getMatrix().setTranslate(0, 0);// show
10303 t.setAlpha((x - 0.5f) * 2);
10304 }
10305 }
10306
10307 @Override
10308 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10309 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10310 mWidth = width;
10311 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010312
10313 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010314 public int getZAdjustment() {
10315 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010316 }
10317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318}