blob: 7ca8b5268192e6f695f4465e6260aee7b9a7f3b1 [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;
138 static final boolean DEBUG_ORIENTATION = false;
139 static final boolean DEBUG_APP_TRANSITIONS = false;
140 static final boolean DEBUG_STARTING_WINDOW = false;
141 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700142 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700144 static final boolean MEASURE_LATENCY = false;
145 static private LatencyTimer lt;
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean PROFILE_ORIENTATION = false;
148 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700149 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /** How long to wait for first key repeat, in milliseconds */
154 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /** How long to wait for subsequent key repeats, in milliseconds */
157 static final int KEY_REPEAT_DELAY = 50;
158
159 /** How much to multiply the policy's type layer, to reserve room
160 * for multiple windows of the same type and Z-ordering adjustment
161 * with TYPE_LAYER_OFFSET. */
162 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
165 * or below others in the same layer. */
166 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** How much to increment the layer for each window, to reserve room
169 * for effect surfaces between them.
170 */
171 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** The maximum length we will accept for a loaded animation duration:
174 * this is 10 seconds.
175 */
176 static final int MAX_ANIMATION_DURATION = 10*1000;
177
178 /** Amount of time (in milliseconds) to animate the dim surface from one
179 * value to another, when no window animation is driving it.
180 */
181 static final int DEFAULT_DIM_DURATION = 200;
182
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700183 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
184 * compatible windows.
185 */
186 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** Adjustment to time to perform a dim, to make it more dramatic.
189 */
190 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700191
Dianne Hackborncfaef692009-06-15 14:24:44 -0700192 static final int INJECT_FAILED = 0;
193 static final int INJECT_SUCCEEDED = 1;
194 static final int INJECT_NO_PERMISSION = -1;
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 static final int UPDATE_FOCUS_NORMAL = 0;
197 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
198 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
199 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700200
Michael Chane96440f2009-05-06 10:27:36 -0700201 /** The minimum time between dispatching touch events. */
202 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
203
204 // Last touch event time
205 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Last touch event type
208 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700209
Michael Chane96440f2009-05-06 10:27:36 -0700210 // Time to wait before calling useractivity again. This saves CPU usage
211 // when we get a flood of touch events.
212 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
213
214 // Last time we call user activity
215 long mLastUserActivityCallTime = 0;
216
Romain Guy06882f82009-06-10 13:36:04 -0700217 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700218 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700221 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 /**
224 * Condition waited on by {@link #reenableKeyguard} to know the call to
225 * the window policy has finished.
226 */
227 private boolean mWaitingUntilKeyguardReenabled = false;
228
229
230 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
231 new Handler(), "WindowManagerService.mKeyguardDisabled") {
232 public void acquired() {
233 mPolicy.enableKeyguard(false);
234 }
235 public void released() {
236 synchronized (mKeyguardDisabled) {
237 mPolicy.enableKeyguard(true);
238 mWaitingUntilKeyguardReenabled = false;
239 mKeyguardDisabled.notifyAll();
240 }
241 }
242 };
243
244 final Context mContext;
245
246 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
251
252 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * All currently active sessions with clients.
258 */
259 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * Mapping from an IWindow IBinder to the server's Window object.
263 * This is also used as the lock for all of our state.
264 */
265 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
266
267 /**
268 * Mapping from a token IBinder to a WindowToken object.
269 */
270 final HashMap<IBinder, WindowToken> mTokenMap =
271 new HashMap<IBinder, WindowToken>();
272
273 /**
274 * The same tokens as mTokenMap, stored in a list for efficient iteration
275 * over them.
276 */
277 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Window tokens that are in the process of exiting, but still
281 * on screen for animations.
282 */
283 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
284
285 /**
286 * Z-ordered (bottom-most first) list of all application tokens, for
287 * controlling the ordering of windows in different applications. This
288 * contains WindowToken objects.
289 */
290 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
291
292 /**
293 * Application tokens that are in the process of exiting, but still
294 * on screen for animations.
295 */
296 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
297
298 /**
299 * List of window tokens that have finished starting their application,
300 * and now need to have the policy remove their windows.
301 */
302 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
303
304 /**
305 * Z-ordered (bottom-most first) list of all Window objects.
306 */
307 final ArrayList mWindows = new ArrayList();
308
309 /**
310 * Windows that are being resized. Used so we can tell the client about
311 * the resize after closing the transaction in which we resized the
312 * underlying surface.
313 */
314 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
315
316 /**
317 * Windows whose animations have ended and now must be removed.
318 */
319 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
320
321 /**
322 * Windows whose surface should be destroyed.
323 */
324 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
325
326 /**
327 * Windows that have lost input focus and are waiting for the new
328 * focus window to be displayed before they are told about this.
329 */
330 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
331
332 /**
333 * This is set when we have run out of memory, and will either be an empty
334 * list or contain windows that need to be force removed.
335 */
336 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700341 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 Surface mBlurSurface;
343 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 final float[] mTmpFloats = new float[9];
348
349 boolean mSafeMode;
350 boolean mDisplayEnabled = false;
351 boolean mSystemBooted = false;
352 int mRotation = 0;
353 int mRequestedRotation = 0;
354 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700355 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 ArrayList<IRotationWatcher> mRotationWatchers
357 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 boolean mLayoutNeeded = true;
360 boolean mAnimationPending = false;
361 boolean mDisplayFrozen = false;
362 boolean mWindowsFreezingScreen = false;
363 long mFreezeGcPending = 0;
364 int mAppsFreezingScreen = 0;
365
366 // This is held as long as we have the screen frozen, to give us time to
367 // perform a rotation animation when turning off shows the lock screen which
368 // changes the orientation.
369 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 // State management of app transitions. When we are preparing for a
372 // transition, mNextAppTransition will be the kind of transition to
373 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
374 // mOpeningApps and mClosingApps are the lists of tokens that will be
375 // made visible or hidden at the next transition.
376 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
377 boolean mAppTransitionReady = false;
378 boolean mAppTransitionTimeout = false;
379 boolean mStartingIconInTransition = false;
380 boolean mSkipAppTransitionAnimation = false;
381 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
382 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 //flag to detect fat touch events
385 boolean mFatTouch = false;
386 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 H mH = new H();
389
390 WindowState mCurrentFocus = null;
391 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 // This just indicates the window the input method is on top of, not
394 // necessarily the window its input is going to.
395 WindowState mInputMethodTarget = null;
396 WindowState mUpcomingInputMethodTarget = null;
397 boolean mInputMethodTargetWaitingAnim;
398 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 WindowState mInputMethodWindow = null;
401 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
402
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700403 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
404
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700405 // If non-null, this is the currently visible window that is associated
406 // with the wallpaper.
407 WindowState mWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -0700408 WindowState mUpcomingWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700409 int mWallpaperAnimLayerAdjustment;
410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 AppWindowToken mFocusedApp = null;
412
413 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 float mWindowAnimationScale = 1.0f;
416 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 final KeyWaiter mKeyWaiter = new KeyWaiter();
419 final KeyQ mQueue;
420 final InputDispatcherThread mInputThread;
421
422 // Who is holding the screen on.
423 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Whether the UI is currently running in touch mode (not showing
427 * navigational focus because the user is directly pressing the screen).
428 */
429 boolean mInTouchMode = false;
430
431 private ViewServer mViewServer;
432
433 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700434
Dianne Hackbornc485a602009-03-24 22:39:49 -0700435 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700436 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700437
438 // The frame use to limit the size of the app running in compatibility mode.
439 Rect mCompatibleScreenFrame = new Rect();
440 // The surface used to fill the outer rim of the app running in compatibility mode.
441 Surface mBackgroundFillerSurface = null;
442 boolean mBackgroundFillerShown = false;
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public static WindowManagerService main(Context context,
445 PowerManagerService pm, boolean haveInputMethods) {
446 WMThread thr = new WMThread(context, pm, haveInputMethods);
447 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 synchronized (thr) {
450 while (thr.mService == null) {
451 try {
452 thr.wait();
453 } catch (InterruptedException e) {
454 }
455 }
456 }
Romain Guy06882f82009-06-10 13:36:04 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 return thr.mService;
459 }
Romain Guy06882f82009-06-10 13:36:04 -0700460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 static class WMThread extends Thread {
462 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 private final Context mContext;
465 private final PowerManagerService mPM;
466 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 public WMThread(Context context, PowerManagerService pm,
469 boolean haveInputMethods) {
470 super("WindowManager");
471 mContext = context;
472 mPM = pm;
473 mHaveInputMethods = haveInputMethods;
474 }
Romain Guy06882f82009-06-10 13:36:04 -0700475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 public void run() {
477 Looper.prepare();
478 WindowManagerService s = new WindowManagerService(mContext, mPM,
479 mHaveInputMethods);
480 android.os.Process.setThreadPriority(
481 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 synchronized (this) {
484 mService = s;
485 notifyAll();
486 }
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 Looper.loop();
489 }
490 }
491
492 static class PolicyThread extends Thread {
493 private final WindowManagerPolicy mPolicy;
494 private final WindowManagerService mService;
495 private final Context mContext;
496 private final PowerManagerService mPM;
497 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public PolicyThread(WindowManagerPolicy policy,
500 WindowManagerService service, Context context,
501 PowerManagerService pm) {
502 super("WindowManagerPolicy");
503 mPolicy = policy;
504 mService = service;
505 mContext = context;
506 mPM = pm;
507 }
Romain Guy06882f82009-06-10 13:36:04 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public void run() {
510 Looper.prepare();
511 //Looper.myLooper().setMessageLogging(new LogPrinter(
512 // Log.VERBOSE, "WindowManagerPolicy"));
513 android.os.Process.setThreadPriority(
514 android.os.Process.THREAD_PRIORITY_FOREGROUND);
515 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 synchronized (this) {
518 mRunning = true;
519 notifyAll();
520 }
Romain Guy06882f82009-06-10 13:36:04 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 Looper.loop();
523 }
524 }
525
526 private WindowManagerService(Context context, PowerManagerService pm,
527 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700528 if (MEASURE_LATENCY) {
529 lt = new LatencyTimer(100, 1000);
530 }
531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mContext = context;
533 mHaveInputMethods = haveInputMethods;
534 mLimitedAlphaCompositing = context.getResources().getBoolean(
535 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 mPowerManager = pm;
538 mPowerManager.setPolicy(mPolicy);
539 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
540 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
541 "SCREEN_FROZEN");
542 mScreenFrozenLock.setReferenceCounted(false);
543
544 mActivityManager = ActivityManagerNative.getDefault();
545 mBatteryStats = BatteryStatsService.getService();
546
547 // Get persisted window scale setting
548 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
549 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
550 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
551 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700552
Michael Chan9f028e62009-08-04 17:37:46 -0700553 int max_events_per_sec = 35;
554 try {
555 max_events_per_sec = Integer.parseInt(SystemProperties
556 .get("windowsmgr.max_events_per_sec"));
557 if (max_events_per_sec < 1) {
558 max_events_per_sec = 35;
559 }
560 } catch (NumberFormatException e) {
561 }
562 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 mQueue = new KeyQ();
565
566 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
569 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 synchronized (thr) {
572 while (!thr.mRunning) {
573 try {
574 thr.wait();
575 } catch (InterruptedException e) {
576 }
577 }
578 }
Romain Guy06882f82009-06-10 13:36:04 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 // Add ourself to the Watchdog monitors.
583 Watchdog.getInstance().addMonitor(this);
584 }
585
586 @Override
587 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
588 throws RemoteException {
589 try {
590 return super.onTransact(code, data, reply, flags);
591 } catch (RuntimeException e) {
592 // The window manager only throws security exceptions, so let's
593 // log all others.
594 if (!(e instanceof SecurityException)) {
595 Log.e(TAG, "Window Manager Crash", e);
596 }
597 throw e;
598 }
599 }
600
601 private void placeWindowAfter(Object pos, WindowState window) {
602 final int i = mWindows.indexOf(pos);
603 if (localLOGV || DEBUG_FOCUS) Log.v(
604 TAG, "Adding window " + window + " at "
605 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
606 mWindows.add(i+1, window);
607 }
608
609 private void placeWindowBefore(Object pos, WindowState window) {
610 final int i = mWindows.indexOf(pos);
611 if (localLOGV || DEBUG_FOCUS) Log.v(
612 TAG, "Adding window " + window + " at "
613 + i + " of " + mWindows.size() + " (before " + pos + ")");
614 mWindows.add(i, window);
615 }
616
617 //This method finds out the index of a window that has the same app token as
618 //win. used for z ordering the windows in mWindows
619 private int findIdxBasedOnAppTokens(WindowState win) {
620 //use a local variable to cache mWindows
621 ArrayList localmWindows = mWindows;
622 int jmax = localmWindows.size();
623 if(jmax == 0) {
624 return -1;
625 }
626 for(int j = (jmax-1); j >= 0; j--) {
627 WindowState wentry = (WindowState)localmWindows.get(j);
628 if(wentry.mAppToken == win.mAppToken) {
629 return j;
630 }
631 }
632 return -1;
633 }
Romain Guy06882f82009-06-10 13:36:04 -0700634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
636 final IWindow client = win.mClient;
637 final WindowToken token = win.mToken;
638 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 final int N = localmWindows.size();
641 final WindowState attached = win.mAttachedWindow;
642 int i;
643 if (attached == null) {
644 int tokenWindowsPos = token.windows.size();
645 if (token.appWindowToken != null) {
646 int index = tokenWindowsPos-1;
647 if (index >= 0) {
648 // If this application has existing windows, we
649 // simply place the new window on top of them... but
650 // keep the starting window on top.
651 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
652 // Base windows go behind everything else.
653 placeWindowBefore(token.windows.get(0), win);
654 tokenWindowsPos = 0;
655 } else {
656 AppWindowToken atoken = win.mAppToken;
657 if (atoken != null &&
658 token.windows.get(index) == atoken.startingWindow) {
659 placeWindowBefore(token.windows.get(index), win);
660 tokenWindowsPos--;
661 } else {
662 int newIdx = findIdxBasedOnAppTokens(win);
663 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700664 //there is a window above this one associated with the same
665 //apptoken note that the window could be a floating window
666 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 //windows associated with this token.
668 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
672 } else {
673 if (localLOGV) Log.v(
674 TAG, "Figuring out where to add app window "
675 + client.asBinder() + " (token=" + token + ")");
676 // Figure out where the window should go, based on the
677 // order of applications.
678 final int NA = mAppTokens.size();
679 Object pos = null;
680 for (i=NA-1; i>=0; i--) {
681 AppWindowToken t = mAppTokens.get(i);
682 if (t == token) {
683 i--;
684 break;
685 }
686 if (t.windows.size() > 0) {
687 pos = t.windows.get(0);
688 }
689 }
690 // We now know the index into the apps. If we found
691 // an app window above, that gives us the position; else
692 // we need to look some more.
693 if (pos != null) {
694 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700695 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 mTokenMap.get(((WindowState)pos).mClient.asBinder());
697 if (atoken != null) {
698 final int NC = atoken.windows.size();
699 if (NC > 0) {
700 WindowState bottom = atoken.windows.get(0);
701 if (bottom.mSubLayer < 0) {
702 pos = bottom;
703 }
704 }
705 }
706 placeWindowBefore(pos, win);
707 } else {
708 while (i >= 0) {
709 AppWindowToken t = mAppTokens.get(i);
710 final int NW = t.windows.size();
711 if (NW > 0) {
712 pos = t.windows.get(NW-1);
713 break;
714 }
715 i--;
716 }
717 if (pos != null) {
718 // Move in front of any windows attached to this
719 // one.
720 WindowToken atoken =
721 mTokenMap.get(((WindowState)pos).mClient.asBinder());
722 if (atoken != null) {
723 final int NC = atoken.windows.size();
724 if (NC > 0) {
725 WindowState top = atoken.windows.get(NC-1);
726 if (top.mSubLayer >= 0) {
727 pos = top;
728 }
729 }
730 }
731 placeWindowAfter(pos, win);
732 } else {
733 // Just search for the start of this layer.
734 final int myLayer = win.mBaseLayer;
735 for (i=0; i<N; i++) {
736 WindowState w = (WindowState)localmWindows.get(i);
737 if (w.mBaseLayer > myLayer) {
738 break;
739 }
740 }
741 if (localLOGV || DEBUG_FOCUS) Log.v(
742 TAG, "Adding window " + win + " at "
743 + i + " of " + N);
744 localmWindows.add(i, win);
745 }
746 }
747 }
748 } else {
749 // Figure out where window should go, based on layer.
750 final int myLayer = win.mBaseLayer;
751 for (i=N-1; i>=0; i--) {
752 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
753 i++;
754 break;
755 }
756 }
757 if (i < 0) i = 0;
758 if (localLOGV || DEBUG_FOCUS) Log.v(
759 TAG, "Adding window " + win + " at "
760 + i + " of " + N);
761 localmWindows.add(i, win);
762 }
763 if (addToToken) {
764 token.windows.add(tokenWindowsPos, win);
765 }
766
767 } else {
768 // Figure out this window's ordering relative to the window
769 // it is attached to.
770 final int NA = token.windows.size();
771 final int sublayer = win.mSubLayer;
772 int largestSublayer = Integer.MIN_VALUE;
773 WindowState windowWithLargestSublayer = null;
774 for (i=0; i<NA; i++) {
775 WindowState w = token.windows.get(i);
776 final int wSublayer = w.mSubLayer;
777 if (wSublayer >= largestSublayer) {
778 largestSublayer = wSublayer;
779 windowWithLargestSublayer = w;
780 }
781 if (sublayer < 0) {
782 // For negative sublayers, we go below all windows
783 // in the same sublayer.
784 if (wSublayer >= sublayer) {
785 if (addToToken) {
786 token.windows.add(i, win);
787 }
788 placeWindowBefore(
789 wSublayer >= 0 ? attached : w, win);
790 break;
791 }
792 } else {
793 // For positive sublayers, we go above all windows
794 // in the same sublayer.
795 if (wSublayer > sublayer) {
796 if (addToToken) {
797 token.windows.add(i, win);
798 }
799 placeWindowBefore(w, win);
800 break;
801 }
802 }
803 }
804 if (i >= NA) {
805 if (addToToken) {
806 token.windows.add(win);
807 }
808 if (sublayer < 0) {
809 placeWindowBefore(attached, win);
810 } else {
811 placeWindowAfter(largestSublayer >= 0
812 ? windowWithLargestSublayer
813 : attached,
814 win);
815 }
816 }
817 }
Romain Guy06882f82009-06-10 13:36:04 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 if (win.mAppToken != null && addToToken) {
820 win.mAppToken.allAppWindows.add(win);
821 }
822 }
Romain Guy06882f82009-06-10 13:36:04 -0700823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 static boolean canBeImeTarget(WindowState w) {
825 final int fl = w.mAttrs.flags
826 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
827 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
828 return w.isVisibleOrAdding();
829 }
830 return false;
831 }
Romain Guy06882f82009-06-10 13:36:04 -0700832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
834 final ArrayList localmWindows = mWindows;
835 final int N = localmWindows.size();
836 WindowState w = null;
837 int i = N;
838 while (i > 0) {
839 i--;
840 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
843 // + Integer.toHexString(w.mAttrs.flags));
844 if (canBeImeTarget(w)) {
845 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // Yet more tricksyness! If this window is a "starting"
848 // window, we do actually want to be on top of it, but
849 // it is not -really- where input will go. So if the caller
850 // is not actually looking to move the IME, look down below
851 // for a real window to target...
852 if (!willMove
853 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
854 && i > 0) {
855 WindowState wb = (WindowState)localmWindows.get(i-1);
856 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
857 i--;
858 w = wb;
859 }
860 }
861 break;
862 }
863 }
Romain Guy06882f82009-06-10 13:36:04 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
868 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (willMove && w != null) {
871 final WindowState curTarget = mInputMethodTarget;
872 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // Now some fun for dealing with window animations that
875 // modify the Z order. We need to look at all windows below
876 // the current target that are in this app, finding the highest
877 // visible one in layering.
878 AppWindowToken token = curTarget.mAppToken;
879 WindowState highestTarget = null;
880 int highestPos = 0;
881 if (token.animating || token.animation != null) {
882 int pos = 0;
883 pos = localmWindows.indexOf(curTarget);
884 while (pos >= 0) {
885 WindowState win = (WindowState)localmWindows.get(pos);
886 if (win.mAppToken != token) {
887 break;
888 }
889 if (!win.mRemoved) {
890 if (highestTarget == null || win.mAnimLayer >
891 highestTarget.mAnimLayer) {
892 highestTarget = win;
893 highestPos = pos;
894 }
895 }
896 pos--;
897 }
898 }
Romain Guy06882f82009-06-10 13:36:04 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700901 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 + mNextAppTransition + " " + highestTarget
903 + " animating=" + highestTarget.isAnimating()
904 + " layer=" + highestTarget.mAnimLayer
905 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
908 // If we are currently setting up for an animation,
909 // hold everything until we can find out what will happen.
910 mInputMethodTargetWaitingAnim = true;
911 mInputMethodTarget = highestTarget;
912 return highestPos + 1;
913 } else if (highestTarget.isAnimating() &&
914 highestTarget.mAnimLayer > w.mAnimLayer) {
915 // If the window we are currently targeting is involved
916 // with an animation, and it is on top of the next target
917 // we will be over, then hold off on moving until
918 // that is done.
919 mInputMethodTarget = highestTarget;
920 return highestPos + 1;
921 }
922 }
923 }
924 }
Romain Guy06882f82009-06-10 13:36:04 -0700925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 //Log.i(TAG, "Placing input method @" + (i+1));
927 if (w != null) {
928 if (willMove) {
929 RuntimeException e = new RuntimeException();
930 e.fillInStackTrace();
931 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
932 + mInputMethodTarget + " to " + w, e);
933 mInputMethodTarget = w;
934 if (w.mAppToken != null) {
935 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
936 } else {
937 setInputMethodAnimLayerAdjustment(0);
938 }
939 }
940 return i+1;
941 }
942 if (willMove) {
943 RuntimeException e = new RuntimeException();
944 e.fillInStackTrace();
945 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
946 + mInputMethodTarget + " to null", e);
947 mInputMethodTarget = null;
948 setInputMethodAnimLayerAdjustment(0);
949 }
950 return -1;
951 }
Romain Guy06882f82009-06-10 13:36:04 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 void addInputMethodWindowToListLocked(WindowState win) {
954 int pos = findDesiredInputMethodWindowIndexLocked(true);
955 if (pos >= 0) {
956 win.mTargetAppToken = mInputMethodTarget.mAppToken;
957 mWindows.add(pos, win);
958 moveInputMethodDialogsLocked(pos+1);
959 return;
960 }
961 win.mTargetAppToken = null;
962 addWindowToListInOrderLocked(win, true);
963 moveInputMethodDialogsLocked(pos);
964 }
Romain Guy06882f82009-06-10 13:36:04 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 void setInputMethodAnimLayerAdjustment(int adj) {
967 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
968 mInputMethodAnimLayerAdjustment = adj;
969 WindowState imw = mInputMethodWindow;
970 if (imw != null) {
971 imw.mAnimLayer = imw.mLayer + adj;
972 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
973 + " anim layer: " + imw.mAnimLayer);
974 int wi = imw.mChildWindows.size();
975 while (wi > 0) {
976 wi--;
977 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
978 cw.mAnimLayer = cw.mLayer + adj;
979 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
980 + " anim layer: " + cw.mAnimLayer);
981 }
982 }
983 int di = mInputMethodDialogs.size();
984 while (di > 0) {
985 di --;
986 imw = mInputMethodDialogs.get(di);
987 imw.mAnimLayer = imw.mLayer + adj;
988 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
989 + " anim layer: " + imw.mAnimLayer);
990 }
991 }
Romain Guy06882f82009-06-10 13:36:04 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
994 int wpos = mWindows.indexOf(win);
995 if (wpos >= 0) {
996 if (wpos < interestingPos) interestingPos--;
997 mWindows.remove(wpos);
998 int NC = win.mChildWindows.size();
999 while (NC > 0) {
1000 NC--;
1001 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1002 int cpos = mWindows.indexOf(cw);
1003 if (cpos >= 0) {
1004 if (cpos < interestingPos) interestingPos--;
1005 mWindows.remove(cpos);
1006 }
1007 }
1008 }
1009 return interestingPos;
1010 }
Romain Guy06882f82009-06-10 13:36:04 -07001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 private void reAddWindowToListInOrderLocked(WindowState win) {
1013 addWindowToListInOrderLocked(win, false);
1014 // This is a hack to get all of the child windows added as well
1015 // at the right position. Child windows should be rare and
1016 // this case should be rare, so it shouldn't be that big a deal.
1017 int wpos = mWindows.indexOf(win);
1018 if (wpos >= 0) {
1019 mWindows.remove(wpos);
1020 reAddWindowLocked(wpos, win);
1021 }
1022 }
Romain Guy06882f82009-06-10 13:36:04 -07001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 void logWindowList(String prefix) {
1025 int N = mWindows.size();
1026 while (N > 0) {
1027 N--;
1028 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1029 }
1030 }
Romain Guy06882f82009-06-10 13:36:04 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 void moveInputMethodDialogsLocked(int pos) {
1033 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 final int N = dialogs.size();
1036 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1037 for (int i=0; i<N; i++) {
1038 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1039 }
1040 if (DEBUG_INPUT_METHOD) {
1041 Log.v(TAG, "Window list w/pos=" + pos);
1042 logWindowList(" ");
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (pos >= 0) {
1046 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1047 if (pos < mWindows.size()) {
1048 WindowState wp = (WindowState)mWindows.get(pos);
1049 if (wp == mInputMethodWindow) {
1050 pos++;
1051 }
1052 }
1053 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1054 for (int i=0; i<N; i++) {
1055 WindowState win = dialogs.get(i);
1056 win.mTargetAppToken = targetAppToken;
1057 pos = reAddWindowLocked(pos, win);
1058 }
1059 if (DEBUG_INPUT_METHOD) {
1060 Log.v(TAG, "Final window list:");
1061 logWindowList(" ");
1062 }
1063 return;
1064 }
1065 for (int i=0; i<N; i++) {
1066 WindowState win = dialogs.get(i);
1067 win.mTargetAppToken = null;
1068 reAddWindowToListInOrderLocked(win);
1069 if (DEBUG_INPUT_METHOD) {
1070 Log.v(TAG, "No IM target, final list:");
1071 logWindowList(" ");
1072 }
1073 }
1074 }
Romain Guy06882f82009-06-10 13:36:04 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1077 final WindowState imWin = mInputMethodWindow;
1078 final int DN = mInputMethodDialogs.size();
1079 if (imWin == null && DN == 0) {
1080 return false;
1081 }
Romain Guy06882f82009-06-10 13:36:04 -07001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1084 if (imPos >= 0) {
1085 // In this case, the input method windows are to be placed
1086 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 // First check to see if the input method windows are already
1089 // located here, and contiguous.
1090 final int N = mWindows.size();
1091 WindowState firstImWin = imPos < N
1092 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 // Figure out the actual input method window that should be
1095 // at the bottom of their stack.
1096 WindowState baseImWin = imWin != null
1097 ? imWin : mInputMethodDialogs.get(0);
1098 if (baseImWin.mChildWindows.size() > 0) {
1099 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1100 if (cw.mSubLayer < 0) baseImWin = cw;
1101 }
Romain Guy06882f82009-06-10 13:36:04 -07001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (firstImWin == baseImWin) {
1104 // The windows haven't moved... but are they still contiguous?
1105 // First find the top IM window.
1106 int pos = imPos+1;
1107 while (pos < N) {
1108 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1109 break;
1110 }
1111 pos++;
1112 }
1113 pos++;
1114 // Now there should be no more input method windows above.
1115 while (pos < N) {
1116 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1117 break;
1118 }
1119 pos++;
1120 }
1121 if (pos >= N) {
1122 // All is good!
1123 return false;
1124 }
1125 }
Romain Guy06882f82009-06-10 13:36:04 -07001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 if (imWin != null) {
1128 if (DEBUG_INPUT_METHOD) {
1129 Log.v(TAG, "Moving IM from " + imPos);
1130 logWindowList(" ");
1131 }
1132 imPos = tmpRemoveWindowLocked(imPos, imWin);
1133 if (DEBUG_INPUT_METHOD) {
1134 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1135 logWindowList(" ");
1136 }
1137 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1138 reAddWindowLocked(imPos, imWin);
1139 if (DEBUG_INPUT_METHOD) {
1140 Log.v(TAG, "List after moving IM to " + imPos + ":");
1141 logWindowList(" ");
1142 }
1143 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1144 } else {
1145 moveInputMethodDialogsLocked(imPos);
1146 }
Romain Guy06882f82009-06-10 13:36:04 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 } else {
1149 // In this case, the input method windows go in a fixed layer,
1150 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if (imWin != null) {
1153 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1154 tmpRemoveWindowLocked(0, imWin);
1155 imWin.mTargetAppToken = null;
1156 reAddWindowToListInOrderLocked(imWin);
1157 if (DEBUG_INPUT_METHOD) {
1158 Log.v(TAG, "List with no IM target:");
1159 logWindowList(" ");
1160 }
1161 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1162 } else {
1163 moveInputMethodDialogsLocked(-1);;
1164 }
Romain Guy06882f82009-06-10 13:36:04 -07001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
Romain Guy06882f82009-06-10 13:36:04 -07001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 if (needAssignLayers) {
1169 assignLayersLocked();
1170 }
Romain Guy06882f82009-06-10 13:36:04 -07001171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 return true;
1173 }
Romain Guy06882f82009-06-10 13:36:04 -07001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 void adjustInputMethodDialogsLocked() {
1176 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1177 }
Romain Guy06882f82009-06-10 13:36:04 -07001178
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001179 boolean adjustWallpaperWindowsLocked() {
1180 boolean changed = false;
1181
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001182 mUpcomingWallpaperTarget = null;
1183
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001184 // First find top-most window that has asked to be on top of the
1185 // wallpaper; all wallpapers go behind it.
1186 final ArrayList localmWindows = mWindows;
1187 int N = localmWindows.size();
1188 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001189 WindowState foundW = null;
1190 int foundI = 0;
1191 AppWindowToken topToken = null;
1192 AppWindowToken behindToken = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001193 int i = N;
1194 while (i > 0) {
1195 i--;
1196 w = (WindowState)localmWindows.get(i);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001197 if (topToken != null) {
1198 if (w.mAppToken == topToken) {
1199 continue;
1200 }
1201 if (w.mAppToken != null) {
1202 if (behindToken == null) {
1203 // We need to look through for what is behind the
1204 // potential new wallpaper target... skip all tokens
1205 // that are hidden and not animating, since they can't
1206 // be involved with the transition.
1207 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1208 continue;
1209 }
1210 behindToken = w.mAppToken;
1211 }
1212 if (w.mAppToken != behindToken) {
1213 break;
1214 }
1215 }
1216 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001217 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
1218 && !w.mDrawPending && !w.mCommitDrawPending) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001219 if (behindToken != null && w.mAppToken == behindToken) {
1220 // We had previously found a wallpaper window that was
1221 // animating, and now we found one behind it. We could
1222 // be doing an animation between two windows on top of
1223 // the wallpaper!
1224 if (mWallpaperTarget == w || mWallpaperTarget == foundW) {
1225 // Retain the current wallpaper target (don't move
1226 // the wallpaper yet), but note the window that is
1227 // going to become the wallpaper target so that
1228 // others know about this special state.
1229 if (DEBUG_WALLPAPER) Log.v(TAG,
1230 "Switching wallpaper activities: cur#" + i + "="
1231 + w + " upcoming#" + foundI + "=" + foundW);
1232 mUpcomingWallpaperTarget = foundW;
1233 foundW = w;
1234 foundI = i;
1235 break;
1236 }
1237 }
1238 foundW = w;
1239 foundI = i;
1240 if (w.mAppToken != null && w.mAppToken.animation != null) {
1241 // If this app token is animating, we want to keep the
1242 // wallpaper below it if it is animating on top of another
1243 // app with a wallpaper.
1244 topToken = w.mAppToken;
1245 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) {
1261 return false;
1262 }
1263 if (foundW != null && foundW.mAppToken != null) {
1264 return false;
1265 }
1266 if (mUpcomingWallpaperTarget != null && mUpcomingWallpaperTarget.mAppToken != null) {
1267 return false;
1268 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001269 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001270
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001271 if (mWallpaperTarget != foundW) {
1272 mWallpaperTarget = foundW;
1273 if (DEBUG_WALLPAPER) {
1274 Log.v(TAG, "New wallpaper target: " + foundW);
1275 }
1276 }
1277
1278 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001279 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001280 // The window is visible to the compositor... but is it visible
1281 // to the user? That is what the wallpaper cares about.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001282 visible = !foundW.mObscured;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001283 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001284
1285 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 // its layer adjustment. Only do this if we are not transfering
1287 // between two wallpaper targets.
1288 mWallpaperAnimLayerAdjustment =
1289 (mUpcomingWallpaperTarget == null && foundW.mAppToken != null)
1290 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001291
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001292 // Now w is the window we are supposed to be behind... but we
1293 // need to be sure to also be behind any of its attached windows,
1294 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001295 while (foundI > 0) {
1296 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1297 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001298 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001299 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001300 // This window is not related to the previous one in any
1301 // interesting way, so stop here.
1302 break;
1303 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001304 foundW = wb;
1305 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001306 }
1307 }
1308
1309 // Okay i is the position immediately above the wallpaper. Look at
1310 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001311 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001312
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001313 final int dw = mDisplay.getWidth();
1314 final int dh = mDisplay.getHeight();
1315
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001316 // Start stepping backwards from here, ensuring that our wallpaper windows
1317 // are correctly placed.
1318 int curTokenIndex = mWallpaperTokens.size();
1319 while (curTokenIndex > 0) {
1320 curTokenIndex--;
1321 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1322 int curWallpaperIndex = token.windows.size();
1323 while (curWallpaperIndex > 0) {
1324 curWallpaperIndex--;
1325 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001326
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001327 if (visible) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001328 updateWallpaperOffsetLocked(mWallpaperTarget,
1329 wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001330 }
1331
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001332 // First, make sure the client has the current visibility
1333 // state.
1334 if (wallpaper.mWallpaperVisible != visible) {
1335 wallpaper.mWallpaperVisible = visible;
1336 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001337 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001338 "Setting visibility of wallpaper " + wallpaper
1339 + ": " + visible);
1340 wallpaper.mClient.dispatchAppVisibility(visible);
1341 } catch (RemoteException e) {
1342 }
1343 }
1344
1345 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001346 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1347 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001348
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001349 // First, if this window is at the current index, then all
1350 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001351 if (wallpaper == foundW) {
1352 foundI--;
1353 foundW = foundI > 0
1354 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001355 continue;
1356 }
1357
1358 // The window didn't match... the current wallpaper window,
1359 // wherever it is, is in the wrong place, so make sure it is
1360 // not in the list.
1361 int oldIndex = localmWindows.indexOf(wallpaper);
1362 if (oldIndex >= 0) {
1363 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001364 if (oldIndex < foundI) {
1365 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001366 }
1367 }
1368
1369 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001370 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001371 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001372
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001373 localmWindows.add(foundI, wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001374 changed = true;
1375 }
1376 }
1377
1378 return changed;
1379 }
1380
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001381 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001382 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1383 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001384 mWallpaperAnimLayerAdjustment = adj;
1385 int curTokenIndex = mWallpaperTokens.size();
1386 while (curTokenIndex > 0) {
1387 curTokenIndex--;
1388 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1389 int curWallpaperIndex = token.windows.size();
1390 while (curWallpaperIndex > 0) {
1391 curWallpaperIndex--;
1392 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1393 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001394 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1395 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001396 }
1397 }
1398 }
1399
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001400 boolean updateWallpaperOffsetLocked(WindowState target,
1401 WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001402 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001403 boolean rawChanged = false;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001404 if (target.mWallpaperX >= 0) {
1405 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1406 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1407 changed = wallpaperWin.mXOffset != offset;
1408 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001409 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1410 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001411 wallpaperWin.mXOffset = offset;
1412 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001413 if (wallpaperWin.mWallpaperX != target.mWallpaperX) {
1414 wallpaperWin.mWallpaperX = target.mWallpaperX;
1415 rawChanged = true;
1416 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001417 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001418
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001419 if (target.mWallpaperY >= 0) {
1420 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1421 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1422 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001423 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1424 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001425 changed = true;
1426 wallpaperWin.mYOffset = offset;
1427 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001428 if (wallpaperWin.mWallpaperY != target.mWallpaperY) {
1429 wallpaperWin.mWallpaperY = target.mWallpaperY;
1430 rawChanged = true;
1431 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001432 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001433
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001434 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001435 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001436 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1437 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1438 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001439 wallpaperWin.mClient.dispatchWallpaperOffsets(
1440 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1441 } catch (RemoteException e) {
1442 }
1443 }
1444
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001445 return changed;
1446 }
1447
1448 boolean updateWallpaperOffsetLocked() {
1449 final int dw = mDisplay.getWidth();
1450 final int dh = mDisplay.getHeight();
1451
1452 boolean changed = false;
1453
1454 WindowState target = mWallpaperTarget;
1455 if (target != null) {
1456 int curTokenIndex = mWallpaperTokens.size();
1457 while (curTokenIndex > 0) {
1458 curTokenIndex--;
1459 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1460 int curWallpaperIndex = token.windows.size();
1461 while (curWallpaperIndex > 0) {
1462 curWallpaperIndex--;
1463 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1464 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1465 wallpaper.computeShownFrameLocked();
1466 changed = true;
1467 }
1468 }
1469 }
1470 }
1471
1472 return changed;
1473 }
1474
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001475 void updateWallpaperVisibilityLocked() {
1476 final boolean visible = mWallpaperTarget != null
1477 && !mWallpaperTarget.mObscured;
1478 final int dw = mDisplay.getWidth();
1479 final int dh = mDisplay.getHeight();
1480
1481 int curTokenIndex = mWallpaperTokens.size();
1482 while (curTokenIndex > 0) {
1483 curTokenIndex--;
1484 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1485 int curWallpaperIndex = token.windows.size();
1486 while (curWallpaperIndex > 0) {
1487 curWallpaperIndex--;
1488 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1489 if (visible) {
1490 updateWallpaperOffsetLocked(mWallpaperTarget,
1491 wallpaper, dw, dh);
1492 }
1493
1494 if (wallpaper.mWallpaperVisible != visible) {
1495 wallpaper.mWallpaperVisible = visible;
1496 try {
1497 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
1498 "Setting visibility of wallpaper " + wallpaper
1499 + ": " + visible);
1500 wallpaper.mClient.dispatchAppVisibility(visible);
1501 } catch (RemoteException e) {
1502 }
1503 }
1504 }
1505 }
1506 }
1507
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001508 void sendPointerToWallpaperLocked(WindowState srcWin,
1509 MotionEvent pointer, long eventTime) {
1510 int curTokenIndex = mWallpaperTokens.size();
1511 while (curTokenIndex > 0) {
1512 curTokenIndex--;
1513 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1514 int curWallpaperIndex = token.windows.size();
1515 while (curWallpaperIndex > 0) {
1516 curWallpaperIndex--;
1517 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1518 if ((wallpaper.mAttrs.flags &
1519 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1520 continue;
1521 }
1522 try {
1523 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1524 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1525 srcWin.mFrame.top-wallpaper.mFrame.top);
1526 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1527 } catch (RemoteException e) {
1528 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1529 }
1530 }
1531 }
1532 }
1533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 public int addWindow(Session session, IWindow client,
1535 WindowManager.LayoutParams attrs, int viewVisibility,
1536 Rect outContentInsets) {
1537 int res = mPolicy.checkAddPermission(attrs);
1538 if (res != WindowManagerImpl.ADD_OKAY) {
1539 return res;
1540 }
Romain Guy06882f82009-06-10 13:36:04 -07001541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 boolean reportNewConfig = false;
1543 WindowState attachedWindow = null;
1544 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 synchronized(mWindowMap) {
1547 // Instantiating a Display requires talking with the simulator,
1548 // so don't do it until we know the system is mostly up and
1549 // running.
1550 if (mDisplay == null) {
1551 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1552 mDisplay = wm.getDefaultDisplay();
1553 mQueue.setDisplay(mDisplay);
1554 reportNewConfig = true;
1555 }
Romain Guy06882f82009-06-10 13:36:04 -07001556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 if (mWindowMap.containsKey(client.asBinder())) {
1558 Log.w(TAG, "Window " + client + " is already added");
1559 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1560 }
1561
1562 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001563 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 if (attachedWindow == null) {
1565 Log.w(TAG, "Attempted to add window with token that is not a window: "
1566 + attrs.token + ". Aborting.");
1567 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1568 }
1569 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1570 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1571 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1572 + attrs.token + ". Aborting.");
1573 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1574 }
1575 }
1576
1577 boolean addToken = false;
1578 WindowToken token = mTokenMap.get(attrs.token);
1579 if (token == null) {
1580 if (attrs.type >= FIRST_APPLICATION_WINDOW
1581 && attrs.type <= LAST_APPLICATION_WINDOW) {
1582 Log.w(TAG, "Attempted to add application window with unknown token "
1583 + attrs.token + ". Aborting.");
1584 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1585 }
1586 if (attrs.type == TYPE_INPUT_METHOD) {
1587 Log.w(TAG, "Attempted to add input method window with unknown token "
1588 + attrs.token + ". Aborting.");
1589 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1590 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001591 if (attrs.type == TYPE_WALLPAPER) {
1592 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1593 + attrs.token + ". Aborting.");
1594 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 token = new WindowToken(attrs.token, -1, false);
1597 addToken = true;
1598 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1599 && attrs.type <= LAST_APPLICATION_WINDOW) {
1600 AppWindowToken atoken = token.appWindowToken;
1601 if (atoken == null) {
1602 Log.w(TAG, "Attempted to add window with non-application token "
1603 + token + ". Aborting.");
1604 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1605 } else if (atoken.removed) {
1606 Log.w(TAG, "Attempted to add window with exiting application token "
1607 + token + ". Aborting.");
1608 return WindowManagerImpl.ADD_APP_EXITING;
1609 }
1610 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1611 // No need for this guy!
1612 if (localLOGV) Log.v(
1613 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1614 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1615 }
1616 } else if (attrs.type == TYPE_INPUT_METHOD) {
1617 if (token.windowType != TYPE_INPUT_METHOD) {
1618 Log.w(TAG, "Attempted to add input method window with bad token "
1619 + attrs.token + ". Aborting.");
1620 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1621 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001622 } else if (attrs.type == TYPE_WALLPAPER) {
1623 if (token.windowType != TYPE_WALLPAPER) {
1624 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1625 + attrs.token + ". Aborting.");
1626 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
1629
1630 win = new WindowState(session, client, token,
1631 attachedWindow, attrs, viewVisibility);
1632 if (win.mDeathRecipient == null) {
1633 // Client has apparently died, so there is no reason to
1634 // continue.
1635 Log.w(TAG, "Adding window client " + client.asBinder()
1636 + " that is dead, aborting.");
1637 return WindowManagerImpl.ADD_APP_EXITING;
1638 }
1639
1640 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 res = mPolicy.prepareAddWindowLw(win, attrs);
1643 if (res != WindowManagerImpl.ADD_OKAY) {
1644 return res;
1645 }
1646
1647 // From now on, no exceptions or errors allowed!
1648
1649 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (addToken) {
1654 mTokenMap.put(attrs.token, token);
1655 mTokenList.add(token);
1656 }
1657 win.attach();
1658 mWindowMap.put(client.asBinder(), win);
1659
1660 if (attrs.type == TYPE_APPLICATION_STARTING &&
1661 token.appWindowToken != null) {
1662 token.appWindowToken.startingWindow = win;
1663 }
1664
1665 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (attrs.type == TYPE_INPUT_METHOD) {
1668 mInputMethodWindow = win;
1669 addInputMethodWindowToListLocked(win);
1670 imMayMove = false;
1671 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1672 mInputMethodDialogs.add(win);
1673 addWindowToListInOrderLocked(win, true);
1674 adjustInputMethodDialogsLocked();
1675 imMayMove = false;
1676 } else {
1677 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001678 if (attrs.type == TYPE_WALLPAPER ||
1679 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1680 adjustWallpaperWindowsLocked();
1681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
Romain Guy06882f82009-06-10 13:36:04 -07001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if (mInTouchMode) {
1689 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1690 }
1691 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1692 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1693 }
Romain Guy06882f82009-06-10 13:36:04 -07001694
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001695 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001697 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1698 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 imMayMove = false;
1700 }
1701 }
Romain Guy06882f82009-06-10 13:36:04 -07001702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001704 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
Romain Guy06882f82009-06-10 13:36:04 -07001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 assignLayersLocked();
1708 // Don't do layout here, the window must call
1709 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 //dump();
1712
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001713 if (focusChanged) {
1714 if (mCurrentFocus != null) {
1715 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1716 }
1717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 if (localLOGV) Log.v(
1719 TAG, "New client " + client.asBinder()
1720 + ": window=" + win);
1721 }
1722
1723 // sendNewConfiguration() checks caller permissions so we must call it with
1724 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1725 // identity anyway, so it's safe to just clear & restore around this whole
1726 // block.
1727 final long origId = Binder.clearCallingIdentity();
1728 if (reportNewConfig) {
1729 sendNewConfiguration();
1730 } else {
1731 // Update Orientation after adding a window, only if the window needs to be
1732 // displayed right away
1733 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001734 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 sendNewConfiguration();
1736 }
1737 }
1738 }
1739 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 return res;
1742 }
Romain Guy06882f82009-06-10 13:36:04 -07001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 public void removeWindow(Session session, IWindow client) {
1745 synchronized(mWindowMap) {
1746 WindowState win = windowForClientLocked(session, client);
1747 if (win == null) {
1748 return;
1749 }
1750 removeWindowLocked(session, win);
1751 }
1752 }
Romain Guy06882f82009-06-10 13:36:04 -07001753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 public void removeWindowLocked(Session session, WindowState win) {
1755
1756 if (localLOGV || DEBUG_FOCUS) Log.v(
1757 TAG, "Remove " + win + " client="
1758 + Integer.toHexString(System.identityHashCode(
1759 win.mClient.asBinder()))
1760 + ", surface=" + win.mSurface);
1761
1762 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (DEBUG_APP_TRANSITIONS) Log.v(
1765 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1766 + " mExiting=" + win.mExiting
1767 + " isAnimating=" + win.isAnimating()
1768 + " app-animation="
1769 + (win.mAppToken != null ? win.mAppToken.animation : null)
1770 + " inPendingTransaction="
1771 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1772 + " mDisplayFrozen=" + mDisplayFrozen);
1773 // Visibility of the removed window. Will be used later to update orientation later on.
1774 boolean wasVisible = false;
1775 // First, see if we need to run an animation. If we do, we have
1776 // to hold off on removing the window until the animation is done.
1777 // If the display is frozen, just remove immediately, since the
1778 // animation wouldn't be seen.
1779 if (win.mSurface != null && !mDisplayFrozen) {
1780 // If we are not currently running the exit animation, we
1781 // need to see about starting one.
1782 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1785 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1786 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1787 }
1788 // Try starting an animation.
1789 if (applyAnimationLocked(win, transit, false)) {
1790 win.mExiting = true;
1791 }
1792 }
1793 if (win.mExiting || win.isAnimating()) {
1794 // The exit animation is running... wait for it!
1795 //Log.i(TAG, "*** Running exit animation...");
1796 win.mExiting = true;
1797 win.mRemoveOnExit = true;
1798 mLayoutNeeded = true;
1799 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1800 performLayoutAndPlaceSurfacesLocked();
1801 if (win.mAppToken != null) {
1802 win.mAppToken.updateReportedVisibilityLocked();
1803 }
1804 //dump();
1805 Binder.restoreCallingIdentity(origId);
1806 return;
1807 }
1808 }
1809
1810 removeWindowInnerLocked(session, win);
1811 // Removing a visible window will effect the computed orientation
1812 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001813 if (wasVisible && computeForcedAppOrientationLocked()
1814 != mForcedAppOrientation) {
1815 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1818 Binder.restoreCallingIdentity(origId);
1819 }
Romain Guy06882f82009-06-10 13:36:04 -07001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001822 mKeyWaiter.finishedKey(session, win.mClient, true,
1823 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1825 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if (mInputMethodTarget == win) {
1830 moveInputMethodWindowsIfNeededLocked(false);
1831 }
Romain Guy06882f82009-06-10 13:36:04 -07001832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 mPolicy.removeWindowLw(win);
1834 win.removeLocked();
1835
1836 mWindowMap.remove(win.mClient.asBinder());
1837 mWindows.remove(win);
1838
1839 if (mInputMethodWindow == win) {
1840 mInputMethodWindow = null;
1841 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1842 mInputMethodDialogs.remove(win);
1843 }
Romain Guy06882f82009-06-10 13:36:04 -07001844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 final WindowToken token = win.mToken;
1846 final AppWindowToken atoken = win.mAppToken;
1847 token.windows.remove(win);
1848 if (atoken != null) {
1849 atoken.allAppWindows.remove(win);
1850 }
1851 if (localLOGV) Log.v(
1852 TAG, "**** Removing window " + win + ": count="
1853 + token.windows.size());
1854 if (token.windows.size() == 0) {
1855 if (!token.explicit) {
1856 mTokenMap.remove(token.token);
1857 mTokenList.remove(token);
1858 } else if (atoken != null) {
1859 atoken.firstWindowDrawn = false;
1860 }
1861 }
1862
1863 if (atoken != null) {
1864 if (atoken.startingWindow == win) {
1865 atoken.startingWindow = null;
1866 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1867 // If this is the last window and we had requested a starting
1868 // transition window, well there is no point now.
1869 atoken.startingData = null;
1870 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1871 // If this is the last window except for a starting transition
1872 // window, we need to get rid of the starting transition.
1873 if (DEBUG_STARTING_WINDOW) {
1874 Log.v(TAG, "Schedule remove starting " + token
1875 + ": no more real windows");
1876 }
1877 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1878 mH.sendMessage(m);
1879 }
1880 }
Romain Guy06882f82009-06-10 13:36:04 -07001881
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001882 if (win.mAttrs.type == TYPE_WALLPAPER ||
1883 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1884 adjustWallpaperWindowsLocked();
1885 }
1886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (!mInLayout) {
1888 assignLayersLocked();
1889 mLayoutNeeded = true;
1890 performLayoutAndPlaceSurfacesLocked();
1891 if (win.mAppToken != null) {
1892 win.mAppToken.updateReportedVisibilityLocked();
1893 }
1894 }
1895 }
1896
1897 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1898 long origId = Binder.clearCallingIdentity();
1899 try {
1900 synchronized (mWindowMap) {
1901 WindowState w = windowForClientLocked(session, client);
1902 if ((w != null) && (w.mSurface != null)) {
1903 Surface.openTransaction();
1904 try {
1905 w.mSurface.setTransparentRegionHint(region);
1906 } finally {
1907 Surface.closeTransaction();
1908 }
1909 }
1910 }
1911 } finally {
1912 Binder.restoreCallingIdentity(origId);
1913 }
1914 }
1915
1916 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001917 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 Rect visibleInsets) {
1919 long origId = Binder.clearCallingIdentity();
1920 try {
1921 synchronized (mWindowMap) {
1922 WindowState w = windowForClientLocked(session, client);
1923 if (w != null) {
1924 w.mGivenInsetsPending = false;
1925 w.mGivenContentInsets.set(contentInsets);
1926 w.mGivenVisibleInsets.set(visibleInsets);
1927 w.mTouchableInsets = touchableInsets;
1928 mLayoutNeeded = true;
1929 performLayoutAndPlaceSurfacesLocked();
1930 }
1931 }
1932 } finally {
1933 Binder.restoreCallingIdentity(origId);
1934 }
1935 }
Romain Guy06882f82009-06-10 13:36:04 -07001936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 public void getWindowDisplayFrame(Session session, IWindow client,
1938 Rect outDisplayFrame) {
1939 synchronized(mWindowMap) {
1940 WindowState win = windowForClientLocked(session, client);
1941 if (win == null) {
1942 outDisplayFrame.setEmpty();
1943 return;
1944 }
1945 outDisplayFrame.set(win.mDisplayFrame);
1946 }
1947 }
1948
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001949 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
1950 if (window.mWallpaperX != x || window.mWallpaperY != y) {
1951 window.mWallpaperX = x;
1952 window.mWallpaperY = y;
1953
1954 if (mWallpaperTarget == window) {
1955 if (updateWallpaperOffsetLocked()) {
1956 performLayoutAndPlaceSurfacesLocked();
1957 }
1958 }
1959 }
1960 }
1961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 public int relayoutWindow(Session session, IWindow client,
1963 WindowManager.LayoutParams attrs, int requestedWidth,
1964 int requestedHeight, int viewVisibility, boolean insetsPending,
1965 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
1966 Surface outSurface) {
1967 boolean displayed = false;
1968 boolean inTouchMode;
1969 Configuration newConfig = null;
1970 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 synchronized(mWindowMap) {
1973 WindowState win = windowForClientLocked(session, client);
1974 if (win == null) {
1975 return 0;
1976 }
1977 win.mRequestedWidth = requestedWidth;
1978 win.mRequestedHeight = requestedHeight;
1979
1980 if (attrs != null) {
1981 mPolicy.adjustWindowParamsLw(attrs);
1982 }
Romain Guy06882f82009-06-10 13:36:04 -07001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 int attrChanges = 0;
1985 int flagChanges = 0;
1986 if (attrs != null) {
1987 flagChanges = win.mAttrs.flags ^= attrs.flags;
1988 attrChanges = win.mAttrs.copyFrom(attrs);
1989 }
1990
1991 if (localLOGV) Log.v(
1992 TAG, "Relayout given client " + client.asBinder()
1993 + " (" + win.mAttrs.getTitle() + ")");
1994
1995
1996 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
1997 win.mAlpha = attrs.alpha;
1998 }
1999
2000 final boolean scaledWindow =
2001 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2002
2003 if (scaledWindow) {
2004 // requested{Width|Height} Surface's physical size
2005 // attrs.{width|height} Size on screen
2006 win.mHScale = (attrs.width != requestedWidth) ?
2007 (attrs.width / (float)requestedWidth) : 1.0f;
2008 win.mVScale = (attrs.height != requestedHeight) ?
2009 (attrs.height / (float)requestedHeight) : 1.0f;
2010 }
2011
2012 boolean imMayMove = (flagChanges&(
2013 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2014 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 boolean focusMayChange = win.mViewVisibility != viewVisibility
2017 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2018 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002019
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002020 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2021 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 win.mRelayoutCalled = true;
2024 final int oldVisibility = win.mViewVisibility;
2025 win.mViewVisibility = viewVisibility;
2026 if (viewVisibility == View.VISIBLE &&
2027 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2028 displayed = !win.isVisibleLw();
2029 if (win.mExiting) {
2030 win.mExiting = false;
2031 win.mAnimation = null;
2032 }
2033 if (win.mDestroying) {
2034 win.mDestroying = false;
2035 mDestroySurface.remove(win);
2036 }
2037 if (oldVisibility == View.GONE) {
2038 win.mEnterAnimationPending = true;
2039 }
2040 if (displayed && win.mSurface != null && !win.mDrawPending
2041 && !win.mCommitDrawPending && !mDisplayFrozen) {
2042 applyEnterAnimationLocked(win);
2043 }
2044 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2045 // To change the format, we need to re-build the surface.
2046 win.destroySurfaceLocked();
2047 displayed = true;
2048 }
2049 try {
2050 Surface surface = win.createSurfaceLocked();
2051 if (surface != null) {
2052 outSurface.copyFrom(surface);
2053 } else {
2054 outSurface.clear();
2055 }
2056 } catch (Exception e) {
2057 Log.w(TAG, "Exception thrown when creating surface for client "
2058 + client + " (" + win.mAttrs.getTitle() + ")",
2059 e);
2060 Binder.restoreCallingIdentity(origId);
2061 return 0;
2062 }
2063 if (displayed) {
2064 focusMayChange = true;
2065 }
2066 if (win.mAttrs.type == TYPE_INPUT_METHOD
2067 && mInputMethodWindow == null) {
2068 mInputMethodWindow = win;
2069 imMayMove = true;
2070 }
2071 } else {
2072 win.mEnterAnimationPending = false;
2073 if (win.mSurface != null) {
2074 // If we are not currently running the exit animation, we
2075 // need to see about starting one.
2076 if (!win.mExiting) {
2077 // Try starting an animation; if there isn't one, we
2078 // can destroy the surface right away.
2079 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2080 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2081 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2082 }
2083 if (win.isWinVisibleLw() &&
2084 applyAnimationLocked(win, transit, false)) {
2085 win.mExiting = true;
2086 mKeyWaiter.finishedKey(session, client, true,
2087 KeyWaiter.RETURN_NOTHING);
2088 } else if (win.isAnimating()) {
2089 // Currently in a hide animation... turn this into
2090 // an exit.
2091 win.mExiting = true;
2092 } else {
2093 if (mInputMethodWindow == win) {
2094 mInputMethodWindow = null;
2095 }
2096 win.destroySurfaceLocked();
2097 }
2098 }
2099 }
2100 outSurface.clear();
2101 }
2102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 if (focusMayChange) {
2104 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2105 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 imMayMove = false;
2107 }
2108 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2109 }
Romain Guy06882f82009-06-10 13:36:04 -07002110
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002111 // updateFocusedWindowLocked() already assigned layers so we only need to
2112 // reassign them at this point if the IM window state gets shuffled
2113 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if (imMayMove) {
2116 if (moveInputMethodWindowsIfNeededLocked(false)) {
2117 assignLayers = true;
2118 }
2119 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002120 if (wallpaperMayMove) {
2121 if (adjustWallpaperWindowsLocked()) {
2122 assignLayers = true;
2123 }
2124 }
Romain Guy06882f82009-06-10 13:36:04 -07002125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 mLayoutNeeded = true;
2127 win.mGivenInsetsPending = insetsPending;
2128 if (assignLayers) {
2129 assignLayersLocked();
2130 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002131 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 performLayoutAndPlaceSurfacesLocked();
2133 if (win.mAppToken != null) {
2134 win.mAppToken.updateReportedVisibilityLocked();
2135 }
2136 outFrame.set(win.mFrame);
2137 outContentInsets.set(win.mContentInsets);
2138 outVisibleInsets.set(win.mVisibleInsets);
2139 if (localLOGV) Log.v(
2140 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002141 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 + ", requestedHeight=" + requestedHeight
2143 + ", viewVisibility=" + viewVisibility
2144 + "\nRelayout returning frame=" + outFrame
2145 + ", surface=" + outSurface);
2146
2147 if (localLOGV || DEBUG_FOCUS) Log.v(
2148 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2149
2150 inTouchMode = mInTouchMode;
2151 }
2152
2153 if (newConfig != null) {
2154 sendNewConfiguration();
2155 }
Romain Guy06882f82009-06-10 13:36:04 -07002156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2160 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2161 }
2162
2163 public void finishDrawingWindow(Session session, IWindow client) {
2164 final long origId = Binder.clearCallingIdentity();
2165 synchronized(mWindowMap) {
2166 WindowState win = windowForClientLocked(session, client);
2167 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002168 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2169 adjustWallpaperWindowsLocked();
2170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 mLayoutNeeded = true;
2172 performLayoutAndPlaceSurfacesLocked();
2173 }
2174 }
2175 Binder.restoreCallingIdentity(origId);
2176 }
2177
2178 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2179 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2180 + (lp != null ? lp.packageName : null)
2181 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2182 if (lp != null && lp.windowAnimations != 0) {
2183 // If this is a system resource, don't try to load it from the
2184 // application resources. It is nice to avoid loading application
2185 // resources if we can.
2186 String packageName = lp.packageName != null ? lp.packageName : "android";
2187 int resId = lp.windowAnimations;
2188 if ((resId&0xFF000000) == 0x01000000) {
2189 packageName = "android";
2190 }
2191 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2192 + packageName);
2193 return AttributeCache.instance().get(packageName, resId,
2194 com.android.internal.R.styleable.WindowAnimation);
2195 }
2196 return null;
2197 }
Romain Guy06882f82009-06-10 13:36:04 -07002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 private void applyEnterAnimationLocked(WindowState win) {
2200 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2201 if (win.mEnterAnimationPending) {
2202 win.mEnterAnimationPending = false;
2203 transit = WindowManagerPolicy.TRANSIT_ENTER;
2204 }
2205
2206 applyAnimationLocked(win, transit, true);
2207 }
2208
2209 private boolean applyAnimationLocked(WindowState win,
2210 int transit, boolean isEntrance) {
2211 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2212 // If we are trying to apply an animation, but already running
2213 // an animation of the same type, then just leave that one alone.
2214 return true;
2215 }
Romain Guy06882f82009-06-10 13:36:04 -07002216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 // Only apply an animation if the display isn't frozen. If it is
2218 // frozen, there is no reason to animate and it can cause strange
2219 // artifacts when we unfreeze the display if some different animation
2220 // is running.
2221 if (!mDisplayFrozen) {
2222 int anim = mPolicy.selectAnimationLw(win, transit);
2223 int attr = -1;
2224 Animation a = null;
2225 if (anim != 0) {
2226 a = AnimationUtils.loadAnimation(mContext, anim);
2227 } else {
2228 switch (transit) {
2229 case WindowManagerPolicy.TRANSIT_ENTER:
2230 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2231 break;
2232 case WindowManagerPolicy.TRANSIT_EXIT:
2233 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2234 break;
2235 case WindowManagerPolicy.TRANSIT_SHOW:
2236 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2237 break;
2238 case WindowManagerPolicy.TRANSIT_HIDE:
2239 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2240 break;
2241 }
2242 if (attr >= 0) {
2243 a = loadAnimation(win.mAttrs, attr);
2244 }
2245 }
2246 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2247 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2248 + " mAnimation=" + win.mAnimation
2249 + " isEntrance=" + isEntrance);
2250 if (a != null) {
2251 if (DEBUG_ANIM) {
2252 RuntimeException e = new RuntimeException();
2253 e.fillInStackTrace();
2254 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2255 }
2256 win.setAnimation(a);
2257 win.mAnimationIsEntrance = isEntrance;
2258 }
2259 } else {
2260 win.clearAnimation();
2261 }
2262
2263 return win.mAnimation != null;
2264 }
2265
2266 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2267 int anim = 0;
2268 Context context = mContext;
2269 if (animAttr >= 0) {
2270 AttributeCache.Entry ent = getCachedAnimations(lp);
2271 if (ent != null) {
2272 context = ent.context;
2273 anim = ent.array.getResourceId(animAttr, 0);
2274 }
2275 }
2276 if (anim != 0) {
2277 return AnimationUtils.loadAnimation(context, anim);
2278 }
2279 return null;
2280 }
Romain Guy06882f82009-06-10 13:36:04 -07002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 private boolean applyAnimationLocked(AppWindowToken wtoken,
2283 WindowManager.LayoutParams lp, int transit, boolean enter) {
2284 // Only apply an animation if the display isn't frozen. If it is
2285 // frozen, there is no reason to animate and it can cause strange
2286 // artifacts when we unfreeze the display if some different animation
2287 // is running.
2288 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002289 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002290 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002291 a = new FadeInOutAnimation(enter);
2292 if (DEBUG_ANIM) Log.v(TAG,
2293 "applying FadeInOutAnimation for a window in compatibility mode");
2294 } else {
2295 int animAttr = 0;
2296 switch (transit) {
2297 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2298 animAttr = enter
2299 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2300 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2301 break;
2302 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2303 animAttr = enter
2304 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2305 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2306 break;
2307 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2308 animAttr = enter
2309 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2310 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2311 break;
2312 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2313 animAttr = enter
2314 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2315 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2316 break;
2317 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2318 animAttr = enter
2319 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2320 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2321 break;
2322 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2323 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002324 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002325 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2326 break;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002327 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN:
2328 animAttr = enter
2329 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenEnterAnimation
2330 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenExitAnimation;
2331 break;
2332 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE:
2333 animAttr = enter
2334 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseEnterAnimation
2335 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseExitAnimation;
2336 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002337 }
2338 a = loadAnimation(lp, animAttr);
2339 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2340 + " anim=" + a
2341 + " animAttr=0x" + Integer.toHexString(animAttr)
2342 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 if (a != null) {
2345 if (DEBUG_ANIM) {
2346 RuntimeException e = new RuntimeException();
2347 e.fillInStackTrace();
2348 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2349 }
2350 wtoken.setAnimation(a);
2351 }
2352 } else {
2353 wtoken.clearAnimation();
2354 }
2355
2356 return wtoken.animation != null;
2357 }
2358
2359 // -------------------------------------------------------------
2360 // Application Window Tokens
2361 // -------------------------------------------------------------
2362
2363 public void validateAppTokens(List tokens) {
2364 int v = tokens.size()-1;
2365 int m = mAppTokens.size()-1;
2366 while (v >= 0 && m >= 0) {
2367 AppWindowToken wtoken = mAppTokens.get(m);
2368 if (wtoken.removed) {
2369 m--;
2370 continue;
2371 }
2372 if (tokens.get(v) != wtoken.token) {
2373 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2374 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2375 }
2376 v--;
2377 m--;
2378 }
2379 while (v >= 0) {
2380 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2381 v--;
2382 }
2383 while (m >= 0) {
2384 AppWindowToken wtoken = mAppTokens.get(m);
2385 if (!wtoken.removed) {
2386 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2387 }
2388 m--;
2389 }
2390 }
2391
2392 boolean checkCallingPermission(String permission, String func) {
2393 // Quick check: if the calling permission is me, it's all okay.
2394 if (Binder.getCallingPid() == Process.myPid()) {
2395 return true;
2396 }
Romain Guy06882f82009-06-10 13:36:04 -07002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 if (mContext.checkCallingPermission(permission)
2399 == PackageManager.PERMISSION_GRANTED) {
2400 return true;
2401 }
2402 String msg = "Permission Denial: " + func + " from pid="
2403 + Binder.getCallingPid()
2404 + ", uid=" + Binder.getCallingUid()
2405 + " requires " + permission;
2406 Log.w(TAG, msg);
2407 return false;
2408 }
Romain Guy06882f82009-06-10 13:36:04 -07002409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 AppWindowToken findAppWindowToken(IBinder token) {
2411 WindowToken wtoken = mTokenMap.get(token);
2412 if (wtoken == null) {
2413 return null;
2414 }
2415 return wtoken.appWindowToken;
2416 }
Romain Guy06882f82009-06-10 13:36:04 -07002417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 public void addWindowToken(IBinder token, int type) {
2419 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2420 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002421 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 }
Romain Guy06882f82009-06-10 13:36:04 -07002423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 synchronized(mWindowMap) {
2425 WindowToken wtoken = mTokenMap.get(token);
2426 if (wtoken != null) {
2427 Log.w(TAG, "Attempted to add existing input method token: " + token);
2428 return;
2429 }
2430 wtoken = new WindowToken(token, type, true);
2431 mTokenMap.put(token, wtoken);
2432 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002433 if (type == TYPE_WALLPAPER) {
2434 mWallpaperTokens.add(wtoken);
2435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437 }
Romain Guy06882f82009-06-10 13:36:04 -07002438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 public void removeWindowToken(IBinder token) {
2440 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2441 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002442 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
2445 final long origId = Binder.clearCallingIdentity();
2446 synchronized(mWindowMap) {
2447 WindowToken wtoken = mTokenMap.remove(token);
2448 mTokenList.remove(wtoken);
2449 if (wtoken != null) {
2450 boolean delayed = false;
2451 if (!wtoken.hidden) {
2452 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 final int N = wtoken.windows.size();
2455 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 for (int i=0; i<N; i++) {
2458 WindowState win = wtoken.windows.get(i);
2459
2460 if (win.isAnimating()) {
2461 delayed = true;
2462 }
Romain Guy06882f82009-06-10 13:36:04 -07002463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (win.isVisibleNow()) {
2465 applyAnimationLocked(win,
2466 WindowManagerPolicy.TRANSIT_EXIT, false);
2467 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2468 KeyWaiter.RETURN_NOTHING);
2469 changed = true;
2470 }
2471 }
2472
2473 if (changed) {
2474 mLayoutNeeded = true;
2475 performLayoutAndPlaceSurfacesLocked();
2476 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2477 }
Romain Guy06882f82009-06-10 13:36:04 -07002478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 if (delayed) {
2480 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002481 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2482 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
2484 }
Romain Guy06882f82009-06-10 13:36:04 -07002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 } else {
2487 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2488 }
2489 }
2490 Binder.restoreCallingIdentity(origId);
2491 }
2492
2493 public void addAppToken(int addPos, IApplicationToken token,
2494 int groupId, int requestedOrientation, boolean fullscreen) {
2495 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2496 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002497 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
Romain Guy06882f82009-06-10 13:36:04 -07002499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 synchronized(mWindowMap) {
2501 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2502 if (wtoken != null) {
2503 Log.w(TAG, "Attempted to add existing app token: " + token);
2504 return;
2505 }
2506 wtoken = new AppWindowToken(token);
2507 wtoken.groupId = groupId;
2508 wtoken.appFullscreen = fullscreen;
2509 wtoken.requestedOrientation = requestedOrientation;
2510 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002511 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 mTokenMap.put(token.asBinder(), wtoken);
2513 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 // Application tokens start out hidden.
2516 wtoken.hidden = true;
2517 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 //dump();
2520 }
2521 }
Romain Guy06882f82009-06-10 13:36:04 -07002522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 public void setAppGroupId(IBinder token, int groupId) {
2524 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2525 "setAppStartingIcon()")) {
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 synchronized(mWindowMap) {
2530 AppWindowToken wtoken = findAppWindowToken(token);
2531 if (wtoken == null) {
2532 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2533 return;
2534 }
2535 wtoken.groupId = groupId;
2536 }
2537 }
Romain Guy06882f82009-06-10 13:36:04 -07002538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 public int getOrientationFromWindowsLocked() {
2540 int pos = mWindows.size() - 1;
2541 while (pos >= 0) {
2542 WindowState wtoken = (WindowState) mWindows.get(pos);
2543 pos--;
2544 if (wtoken.mAppToken != null) {
2545 // We hit an application window. so the orientation will be determined by the
2546 // app window. No point in continuing further.
2547 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2548 }
2549 if (!wtoken.isVisibleLw()) {
2550 continue;
2551 }
2552 int req = wtoken.mAttrs.screenOrientation;
2553 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2554 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2555 continue;
2556 } else {
2557 return req;
2558 }
2559 }
2560 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2561 }
Romain Guy06882f82009-06-10 13:36:04 -07002562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 public int getOrientationFromAppTokensLocked() {
2564 int pos = mAppTokens.size() - 1;
2565 int curGroup = 0;
2566 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002567 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002569 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 while (pos >= 0) {
2571 AppWindowToken wtoken = mAppTokens.get(pos);
2572 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002573 // if we're about to tear down this window and not seek for
2574 // the behind activity, don't use it for orientation
2575 if (!findingBehind
2576 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002577 continue;
2578 }
2579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 if (!haveGroup) {
2581 // We ignore any hidden applications on the top.
2582 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2583 continue;
2584 }
2585 haveGroup = true;
2586 curGroup = wtoken.groupId;
2587 lastOrientation = wtoken.requestedOrientation;
2588 } else if (curGroup != wtoken.groupId) {
2589 // If we have hit a new application group, and the bottom
2590 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002591 // the orientation behind it, and the last app was
2592 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002594 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2595 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 return lastOrientation;
2597 }
2598 }
2599 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002600 // If this application is fullscreen, and didn't explicitly say
2601 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002603 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002604 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002605 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 return or;
2607 }
2608 // If this application has requested an explicit orientation,
2609 // then use it.
2610 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2611 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2612 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2613 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2614 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2615 return or;
2616 }
Owen Lin3413b892009-05-01 17:12:32 -07002617 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
2619 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2620 }
Romain Guy06882f82009-06-10 13:36:04 -07002621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002623 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002624 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2625 "updateOrientationFromAppTokens()")) {
2626 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2627 }
2628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 Configuration config;
2630 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002631 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2632 freezeThisOneIfNeeded);
2633 Binder.restoreCallingIdentity(ident);
2634 return config;
2635 }
2636
2637 Configuration updateOrientationFromAppTokensUnchecked(
2638 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2639 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002641 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
2643 if (config != null) {
2644 mLayoutNeeded = true;
2645 performLayoutAndPlaceSurfacesLocked();
2646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 return config;
2648 }
Romain Guy06882f82009-06-10 13:36:04 -07002649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 /*
2651 * The orientation is computed from non-application windows first. If none of
2652 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002653 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2655 * android.os.IBinder)
2656 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002657 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002658 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 long ident = Binder.clearCallingIdentity();
2661 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002662 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (req != mForcedAppOrientation) {
2665 changed = true;
2666 mForcedAppOrientation = req;
2667 //send a message to Policy indicating orientation change to take
2668 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002669 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 }
Romain Guy06882f82009-06-10 13:36:04 -07002671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 if (changed) {
2673 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002674 WindowManagerPolicy.USE_LAST_ROTATION,
2675 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 if (changed) {
2677 if (freezeThisOneIfNeeded != null) {
2678 AppWindowToken wtoken = findAppWindowToken(
2679 freezeThisOneIfNeeded);
2680 if (wtoken != null) {
2681 startAppFreezingScreenLocked(wtoken,
2682 ActivityInfo.CONFIG_ORIENTATION);
2683 }
2684 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002685 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 }
2687 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002688
2689 // No obvious action we need to take, but if our current
2690 // state mismatches the activity maanager's, update it
2691 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002692 mTempConfiguration.setToDefaults();
2693 if (computeNewConfigurationLocked(mTempConfiguration)) {
2694 if (appConfig.diff(mTempConfiguration) != 0) {
2695 Log.i(TAG, "Config changed: " + mTempConfiguration);
2696 return new Configuration(mTempConfiguration);
2697 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002698 }
2699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 } finally {
2701 Binder.restoreCallingIdentity(ident);
2702 }
Romain Guy06882f82009-06-10 13:36:04 -07002703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 return null;
2705 }
Romain Guy06882f82009-06-10 13:36:04 -07002706
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002707 int computeForcedAppOrientationLocked() {
2708 int req = getOrientationFromWindowsLocked();
2709 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2710 req = getOrientationFromAppTokensLocked();
2711 }
2712 return req;
2713 }
Romain Guy06882f82009-06-10 13:36:04 -07002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2716 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2717 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002718 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
Romain Guy06882f82009-06-10 13:36:04 -07002720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 synchronized(mWindowMap) {
2722 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2723 if (wtoken == null) {
2724 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2725 return;
2726 }
Romain Guy06882f82009-06-10 13:36:04 -07002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 wtoken.requestedOrientation = requestedOrientation;
2729 }
2730 }
Romain Guy06882f82009-06-10 13:36:04 -07002731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 public int getAppOrientation(IApplicationToken token) {
2733 synchronized(mWindowMap) {
2734 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2735 if (wtoken == null) {
2736 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2737 }
Romain Guy06882f82009-06-10 13:36:04 -07002738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 return wtoken.requestedOrientation;
2740 }
2741 }
Romain Guy06882f82009-06-10 13:36:04 -07002742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2744 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2745 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002746 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 }
2748
2749 synchronized(mWindowMap) {
2750 boolean changed = false;
2751 if (token == null) {
2752 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2753 changed = mFocusedApp != null;
2754 mFocusedApp = null;
2755 mKeyWaiter.tickle();
2756 } else {
2757 AppWindowToken newFocus = findAppWindowToken(token);
2758 if (newFocus == null) {
2759 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2760 return;
2761 }
2762 changed = mFocusedApp != newFocus;
2763 mFocusedApp = newFocus;
2764 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2765 mKeyWaiter.tickle();
2766 }
2767
2768 if (moveFocusNow && changed) {
2769 final long origId = Binder.clearCallingIdentity();
2770 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2771 Binder.restoreCallingIdentity(origId);
2772 }
2773 }
2774 }
2775
2776 public void prepareAppTransition(int transit) {
2777 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2778 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002779 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
Romain Guy06882f82009-06-10 13:36:04 -07002781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 synchronized(mWindowMap) {
2783 if (DEBUG_APP_TRANSITIONS) Log.v(
2784 TAG, "Prepare app transition: transit=" + transit
2785 + " mNextAppTransition=" + mNextAppTransition);
2786 if (!mDisplayFrozen) {
2787 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2788 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002789 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2790 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2791 // Opening a new task always supersedes a close for the anim.
2792 mNextAppTransition = transit;
2793 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2794 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2795 // Opening a new activity always supersedes a close for the anim.
2796 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 mAppTransitionReady = false;
2799 mAppTransitionTimeout = false;
2800 mStartingIconInTransition = false;
2801 mSkipAppTransitionAnimation = false;
2802 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2803 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2804 5000);
2805 }
2806 }
2807 }
2808
2809 public int getPendingAppTransition() {
2810 return mNextAppTransition;
2811 }
Romain Guy06882f82009-06-10 13:36:04 -07002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 public void executeAppTransition() {
2814 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2815 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002816 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 }
Romain Guy06882f82009-06-10 13:36:04 -07002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 synchronized(mWindowMap) {
2820 if (DEBUG_APP_TRANSITIONS) Log.v(
2821 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2822 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2823 mAppTransitionReady = true;
2824 final long origId = Binder.clearCallingIdentity();
2825 performLayoutAndPlaceSurfacesLocked();
2826 Binder.restoreCallingIdentity(origId);
2827 }
2828 }
2829 }
2830
2831 public void setAppStartingWindow(IBinder token, String pkg,
2832 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2833 IBinder transferFrom, boolean createIfNeeded) {
2834 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2835 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002836 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
2838
2839 synchronized(mWindowMap) {
2840 if (DEBUG_STARTING_WINDOW) Log.v(
2841 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2842 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 AppWindowToken wtoken = findAppWindowToken(token);
2845 if (wtoken == null) {
2846 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2847 return;
2848 }
2849
2850 // If the display is frozen, we won't do anything until the
2851 // actual window is displayed so there is no reason to put in
2852 // the starting window.
2853 if (mDisplayFrozen) {
2854 return;
2855 }
Romain Guy06882f82009-06-10 13:36:04 -07002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 if (wtoken.startingData != null) {
2858 return;
2859 }
Romain Guy06882f82009-06-10 13:36:04 -07002860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (transferFrom != null) {
2862 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2863 if (ttoken != null) {
2864 WindowState startingWindow = ttoken.startingWindow;
2865 if (startingWindow != null) {
2866 if (mStartingIconInTransition) {
2867 // In this case, the starting icon has already
2868 // been displayed, so start letting windows get
2869 // shown immediately without any more transitions.
2870 mSkipAppTransitionAnimation = true;
2871 }
2872 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2873 "Moving existing starting from " + ttoken
2874 + " to " + wtoken);
2875 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 // Transfer the starting window over to the new
2878 // token.
2879 wtoken.startingData = ttoken.startingData;
2880 wtoken.startingView = ttoken.startingView;
2881 wtoken.startingWindow = startingWindow;
2882 ttoken.startingData = null;
2883 ttoken.startingView = null;
2884 ttoken.startingWindow = null;
2885 ttoken.startingMoved = true;
2886 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002887 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 startingWindow.mAppToken = wtoken;
2889 mWindows.remove(startingWindow);
2890 ttoken.windows.remove(startingWindow);
2891 ttoken.allAppWindows.remove(startingWindow);
2892 addWindowToListInOrderLocked(startingWindow, true);
2893 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 // Propagate other interesting state between the
2896 // tokens. If the old token is displayed, we should
2897 // immediately force the new one to be displayed. If
2898 // it is animating, we need to move that animation to
2899 // the new one.
2900 if (ttoken.allDrawn) {
2901 wtoken.allDrawn = true;
2902 }
2903 if (ttoken.firstWindowDrawn) {
2904 wtoken.firstWindowDrawn = true;
2905 }
2906 if (!ttoken.hidden) {
2907 wtoken.hidden = false;
2908 wtoken.hiddenRequested = false;
2909 wtoken.willBeHidden = false;
2910 }
2911 if (wtoken.clientHidden != ttoken.clientHidden) {
2912 wtoken.clientHidden = ttoken.clientHidden;
2913 wtoken.sendAppVisibilityToClients();
2914 }
2915 if (ttoken.animation != null) {
2916 wtoken.animation = ttoken.animation;
2917 wtoken.animating = ttoken.animating;
2918 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2919 ttoken.animation = null;
2920 ttoken.animLayerAdjustment = 0;
2921 wtoken.updateLayers();
2922 ttoken.updateLayers();
2923 }
Romain Guy06882f82009-06-10 13:36:04 -07002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 mLayoutNeeded = true;
2927 performLayoutAndPlaceSurfacesLocked();
2928 Binder.restoreCallingIdentity(origId);
2929 return;
2930 } else if (ttoken.startingData != null) {
2931 // The previous app was getting ready to show a
2932 // starting window, but hasn't yet done so. Steal it!
2933 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2934 "Moving pending starting from " + ttoken
2935 + " to " + wtoken);
2936 wtoken.startingData = ttoken.startingData;
2937 ttoken.startingData = null;
2938 ttoken.startingMoved = true;
2939 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2940 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2941 // want to process the message ASAP, before any other queued
2942 // messages.
2943 mH.sendMessageAtFrontOfQueue(m);
2944 return;
2945 }
2946 }
2947 }
2948
2949 // There is no existing starting window, and the caller doesn't
2950 // want us to create one, so that's it!
2951 if (!createIfNeeded) {
2952 return;
2953 }
Romain Guy06882f82009-06-10 13:36:04 -07002954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 mStartingIconInTransition = true;
2956 wtoken.startingData = new StartingData(
2957 pkg, theme, nonLocalizedLabel,
2958 labelRes, icon);
2959 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2960 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2961 // want to process the message ASAP, before any other queued
2962 // messages.
2963 mH.sendMessageAtFrontOfQueue(m);
2964 }
2965 }
2966
2967 public void setAppWillBeHidden(IBinder token) {
2968 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2969 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002970 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 }
2972
2973 AppWindowToken wtoken;
2974
2975 synchronized(mWindowMap) {
2976 wtoken = findAppWindowToken(token);
2977 if (wtoken == null) {
2978 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
2979 return;
2980 }
2981 wtoken.willBeHidden = true;
2982 }
2983 }
Romain Guy06882f82009-06-10 13:36:04 -07002984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
2986 boolean visible, int transit, boolean performLayout) {
2987 boolean delayed = false;
2988
2989 if (wtoken.clientHidden == visible) {
2990 wtoken.clientHidden = !visible;
2991 wtoken.sendAppVisibilityToClients();
2992 }
Romain Guy06882f82009-06-10 13:36:04 -07002993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 wtoken.willBeHidden = false;
2995 if (wtoken.hidden == visible) {
2996 final int N = wtoken.allAppWindows.size();
2997 boolean changed = false;
2998 if (DEBUG_APP_TRANSITIONS) Log.v(
2999 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3000 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3005 if (wtoken.animation == sDummyAnimation) {
3006 wtoken.animation = null;
3007 }
3008 applyAnimationLocked(wtoken, lp, transit, visible);
3009 changed = true;
3010 if (wtoken.animation != null) {
3011 delayed = runningAppAnimation = true;
3012 }
3013 }
Romain Guy06882f82009-06-10 13:36:04 -07003014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 for (int i=0; i<N; i++) {
3016 WindowState win = wtoken.allAppWindows.get(i);
3017 if (win == wtoken.startingWindow) {
3018 continue;
3019 }
3020
3021 if (win.isAnimating()) {
3022 delayed = true;
3023 }
Romain Guy06882f82009-06-10 13:36:04 -07003024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3026 //win.dump(" ");
3027 if (visible) {
3028 if (!win.isVisibleNow()) {
3029 if (!runningAppAnimation) {
3030 applyAnimationLocked(win,
3031 WindowManagerPolicy.TRANSIT_ENTER, true);
3032 }
3033 changed = true;
3034 }
3035 } else if (win.isVisibleNow()) {
3036 if (!runningAppAnimation) {
3037 applyAnimationLocked(win,
3038 WindowManagerPolicy.TRANSIT_EXIT, false);
3039 }
3040 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3041 KeyWaiter.RETURN_NOTHING);
3042 changed = true;
3043 }
3044 }
3045
3046 wtoken.hidden = wtoken.hiddenRequested = !visible;
3047 if (!visible) {
3048 unsetAppFreezingScreenLocked(wtoken, true, true);
3049 } else {
3050 // If we are being set visible, and the starting window is
3051 // not yet displayed, then make sure it doesn't get displayed.
3052 WindowState swin = wtoken.startingWindow;
3053 if (swin != null && (swin.mDrawPending
3054 || swin.mCommitDrawPending)) {
3055 swin.mPolicyVisibility = false;
3056 swin.mPolicyVisibilityAfterAnim = false;
3057 }
3058 }
Romain Guy06882f82009-06-10 13:36:04 -07003059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3061 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3062 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 if (changed && performLayout) {
3065 mLayoutNeeded = true;
3066 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 performLayoutAndPlaceSurfacesLocked();
3068 }
3069 }
3070
3071 if (wtoken.animation != null) {
3072 delayed = true;
3073 }
Romain Guy06882f82009-06-10 13:36:04 -07003074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 return delayed;
3076 }
3077
3078 public void setAppVisibility(IBinder token, boolean visible) {
3079 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3080 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003081 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 }
3083
3084 AppWindowToken wtoken;
3085
3086 synchronized(mWindowMap) {
3087 wtoken = findAppWindowToken(token);
3088 if (wtoken == null) {
3089 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3090 return;
3091 }
3092
3093 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3094 RuntimeException e = new RuntimeException();
3095 e.fillInStackTrace();
3096 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3097 + "): mNextAppTransition=" + mNextAppTransition
3098 + " hidden=" + wtoken.hidden
3099 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3100 }
Romain Guy06882f82009-06-10 13:36:04 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 // If we are preparing an app transition, then delay changing
3103 // the visibility of this token until we execute that transition.
3104 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3105 // Already in requested state, don't do anything more.
3106 if (wtoken.hiddenRequested != visible) {
3107 return;
3108 }
3109 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 if (DEBUG_APP_TRANSITIONS) Log.v(
3112 TAG, "Setting dummy animation on: " + wtoken);
3113 wtoken.setDummyAnimation();
3114 mOpeningApps.remove(wtoken);
3115 mClosingApps.remove(wtoken);
3116 wtoken.inPendingTransaction = true;
3117 if (visible) {
3118 mOpeningApps.add(wtoken);
3119 wtoken.allDrawn = false;
3120 wtoken.startingDisplayed = false;
3121 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07003122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 if (wtoken.clientHidden) {
3124 // In the case where we are making an app visible
3125 // but holding off for a transition, we still need
3126 // to tell the client to make its windows visible so
3127 // they get drawn. Otherwise, we will wait on
3128 // performing the transition until all windows have
3129 // been drawn, they never will be, and we are sad.
3130 wtoken.clientHidden = false;
3131 wtoken.sendAppVisibilityToClients();
3132 }
3133 } else {
3134 mClosingApps.add(wtoken);
3135 }
3136 return;
3137 }
Romain Guy06882f82009-06-10 13:36:04 -07003138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 final long origId = Binder.clearCallingIdentity();
3140 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3141 wtoken.updateReportedVisibilityLocked();
3142 Binder.restoreCallingIdentity(origId);
3143 }
3144 }
3145
3146 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3147 boolean unfreezeSurfaceNow, boolean force) {
3148 if (wtoken.freezingScreen) {
3149 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3150 + " force=" + force);
3151 final int N = wtoken.allAppWindows.size();
3152 boolean unfrozeWindows = false;
3153 for (int i=0; i<N; i++) {
3154 WindowState w = wtoken.allAppWindows.get(i);
3155 if (w.mAppFreezing) {
3156 w.mAppFreezing = false;
3157 if (w.mSurface != null && !w.mOrientationChanging) {
3158 w.mOrientationChanging = true;
3159 }
3160 unfrozeWindows = true;
3161 }
3162 }
3163 if (force || unfrozeWindows) {
3164 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3165 wtoken.freezingScreen = false;
3166 mAppsFreezingScreen--;
3167 }
3168 if (unfreezeSurfaceNow) {
3169 if (unfrozeWindows) {
3170 mLayoutNeeded = true;
3171 performLayoutAndPlaceSurfacesLocked();
3172 }
3173 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3174 stopFreezingDisplayLocked();
3175 }
3176 }
3177 }
3178 }
Romain Guy06882f82009-06-10 13:36:04 -07003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3181 int configChanges) {
3182 if (DEBUG_ORIENTATION) {
3183 RuntimeException e = new RuntimeException();
3184 e.fillInStackTrace();
3185 Log.i(TAG, "Set freezing of " + wtoken.appToken
3186 + ": hidden=" + wtoken.hidden + " freezing="
3187 + wtoken.freezingScreen, e);
3188 }
3189 if (!wtoken.hiddenRequested) {
3190 if (!wtoken.freezingScreen) {
3191 wtoken.freezingScreen = true;
3192 mAppsFreezingScreen++;
3193 if (mAppsFreezingScreen == 1) {
3194 startFreezingDisplayLocked();
3195 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3196 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3197 5000);
3198 }
3199 }
3200 final int N = wtoken.allAppWindows.size();
3201 for (int i=0; i<N; i++) {
3202 WindowState w = wtoken.allAppWindows.get(i);
3203 w.mAppFreezing = true;
3204 }
3205 }
3206 }
Romain Guy06882f82009-06-10 13:36:04 -07003207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 public void startAppFreezingScreen(IBinder token, int configChanges) {
3209 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3210 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003211 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 }
3213
3214 synchronized(mWindowMap) {
3215 if (configChanges == 0 && !mDisplayFrozen) {
3216 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3217 return;
3218 }
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 AppWindowToken wtoken = findAppWindowToken(token);
3221 if (wtoken == null || wtoken.appToken == null) {
3222 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3223 return;
3224 }
3225 final long origId = Binder.clearCallingIdentity();
3226 startAppFreezingScreenLocked(wtoken, configChanges);
3227 Binder.restoreCallingIdentity(origId);
3228 }
3229 }
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 public void stopAppFreezingScreen(IBinder token, boolean force) {
3232 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3233 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003234 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
3236
3237 synchronized(mWindowMap) {
3238 AppWindowToken wtoken = findAppWindowToken(token);
3239 if (wtoken == null || wtoken.appToken == null) {
3240 return;
3241 }
3242 final long origId = Binder.clearCallingIdentity();
3243 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3244 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3245 unsetAppFreezingScreenLocked(wtoken, true, force);
3246 Binder.restoreCallingIdentity(origId);
3247 }
3248 }
Romain Guy06882f82009-06-10 13:36:04 -07003249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 public void removeAppToken(IBinder token) {
3251 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3252 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003253 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 }
3255
3256 AppWindowToken wtoken = null;
3257 AppWindowToken startingToken = null;
3258 boolean delayed = false;
3259
3260 final long origId = Binder.clearCallingIdentity();
3261 synchronized(mWindowMap) {
3262 WindowToken basewtoken = mTokenMap.remove(token);
3263 mTokenList.remove(basewtoken);
3264 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3265 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3266 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3267 wtoken.inPendingTransaction = false;
3268 mOpeningApps.remove(wtoken);
3269 if (mClosingApps.contains(wtoken)) {
3270 delayed = true;
3271 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3272 mClosingApps.add(wtoken);
3273 delayed = true;
3274 }
3275 if (DEBUG_APP_TRANSITIONS) Log.v(
3276 TAG, "Removing app " + wtoken + " delayed=" + delayed
3277 + " animation=" + wtoken.animation
3278 + " animating=" + wtoken.animating);
3279 if (delayed) {
3280 // set the token aside because it has an active animation to be finished
3281 mExitingAppTokens.add(wtoken);
3282 }
3283 mAppTokens.remove(wtoken);
3284 wtoken.removed = true;
3285 if (wtoken.startingData != null) {
3286 startingToken = wtoken;
3287 }
3288 unsetAppFreezingScreenLocked(wtoken, true, true);
3289 if (mFocusedApp == wtoken) {
3290 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3291 mFocusedApp = null;
3292 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3293 mKeyWaiter.tickle();
3294 }
3295 } else {
3296 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3297 }
Romain Guy06882f82009-06-10 13:36:04 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 if (!delayed && wtoken != null) {
3300 wtoken.updateReportedVisibilityLocked();
3301 }
3302 }
3303 Binder.restoreCallingIdentity(origId);
3304
3305 if (startingToken != null) {
3306 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3307 + startingToken + ": app token removed");
3308 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3309 mH.sendMessage(m);
3310 }
3311 }
3312
3313 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3314 final int NW = token.windows.size();
3315 for (int i=0; i<NW; i++) {
3316 WindowState win = token.windows.get(i);
3317 mWindows.remove(win);
3318 int j = win.mChildWindows.size();
3319 while (j > 0) {
3320 j--;
3321 mWindows.remove(win.mChildWindows.get(j));
3322 }
3323 }
3324 return NW > 0;
3325 }
3326
3327 void dumpAppTokensLocked() {
3328 for (int i=mAppTokens.size()-1; i>=0; i--) {
3329 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3330 }
3331 }
Romain Guy06882f82009-06-10 13:36:04 -07003332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 void dumpWindowsLocked() {
3334 for (int i=mWindows.size()-1; i>=0; i--) {
3335 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3336 }
3337 }
Romain Guy06882f82009-06-10 13:36:04 -07003338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 private int findWindowOffsetLocked(int tokenPos) {
3340 final int NW = mWindows.size();
3341
3342 if (tokenPos >= mAppTokens.size()) {
3343 int i = NW;
3344 while (i > 0) {
3345 i--;
3346 WindowState win = (WindowState)mWindows.get(i);
3347 if (win.getAppToken() != null) {
3348 return i+1;
3349 }
3350 }
3351 }
3352
3353 while (tokenPos > 0) {
3354 // Find the first app token below the new position that has
3355 // a window displayed.
3356 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3357 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3358 + tokenPos + " -- " + wtoken.token);
3359 int i = wtoken.windows.size();
3360 while (i > 0) {
3361 i--;
3362 WindowState win = wtoken.windows.get(i);
3363 int j = win.mChildWindows.size();
3364 while (j > 0) {
3365 j--;
3366 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3367 if (cwin.mSubLayer >= 0 ) {
3368 for (int pos=NW-1; pos>=0; pos--) {
3369 if (mWindows.get(pos) == cwin) {
3370 if (DEBUG_REORDER) Log.v(TAG,
3371 "Found child win @" + (pos+1));
3372 return pos+1;
3373 }
3374 }
3375 }
3376 }
3377 for (int pos=NW-1; pos>=0; pos--) {
3378 if (mWindows.get(pos) == win) {
3379 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3380 return pos+1;
3381 }
3382 }
3383 }
3384 tokenPos--;
3385 }
3386
3387 return 0;
3388 }
3389
3390 private final int reAddWindowLocked(int index, WindowState win) {
3391 final int NCW = win.mChildWindows.size();
3392 boolean added = false;
3393 for (int j=0; j<NCW; j++) {
3394 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3395 if (!added && cwin.mSubLayer >= 0) {
3396 mWindows.add(index, win);
3397 index++;
3398 added = true;
3399 }
3400 mWindows.add(index, cwin);
3401 index++;
3402 }
3403 if (!added) {
3404 mWindows.add(index, win);
3405 index++;
3406 }
3407 return index;
3408 }
Romain Guy06882f82009-06-10 13:36:04 -07003409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3411 final int NW = token.windows.size();
3412 for (int i=0; i<NW; i++) {
3413 index = reAddWindowLocked(index, token.windows.get(i));
3414 }
3415 return index;
3416 }
3417
3418 public void moveAppToken(int index, IBinder token) {
3419 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3420 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003421 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 }
3423
3424 synchronized(mWindowMap) {
3425 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3426 if (DEBUG_REORDER) dumpAppTokensLocked();
3427 final AppWindowToken wtoken = findAppWindowToken(token);
3428 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3429 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3430 + token + " (" + wtoken + ")");
3431 return;
3432 }
3433 mAppTokens.add(index, wtoken);
3434 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3435 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 final long origId = Binder.clearCallingIdentity();
3438 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3439 if (DEBUG_REORDER) dumpWindowsLocked();
3440 if (tmpRemoveAppWindowsLocked(wtoken)) {
3441 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3442 if (DEBUG_REORDER) dumpWindowsLocked();
3443 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3444 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3445 if (DEBUG_REORDER) dumpWindowsLocked();
3446 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 mLayoutNeeded = true;
3448 performLayoutAndPlaceSurfacesLocked();
3449 }
3450 Binder.restoreCallingIdentity(origId);
3451 }
3452 }
3453
3454 private void removeAppTokensLocked(List<IBinder> tokens) {
3455 // XXX This should be done more efficiently!
3456 // (take advantage of the fact that both lists should be
3457 // ordered in the same way.)
3458 int N = tokens.size();
3459 for (int i=0; i<N; i++) {
3460 IBinder token = tokens.get(i);
3461 final AppWindowToken wtoken = findAppWindowToken(token);
3462 if (!mAppTokens.remove(wtoken)) {
3463 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3464 + token + " (" + wtoken + ")");
3465 i--;
3466 N--;
3467 }
3468 }
3469 }
3470
3471 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3472 // First remove all of the windows from the list.
3473 final int N = tokens.size();
3474 int i;
3475 for (i=0; i<N; i++) {
3476 WindowToken token = mTokenMap.get(tokens.get(i));
3477 if (token != null) {
3478 tmpRemoveAppWindowsLocked(token);
3479 }
3480 }
3481
3482 // Where to start adding?
3483 int pos = findWindowOffsetLocked(tokenPos);
3484
3485 // And now add them back at the correct place.
3486 for (i=0; i<N; i++) {
3487 WindowToken token = mTokenMap.get(tokens.get(i));
3488 if (token != null) {
3489 pos = reAddAppWindowsLocked(pos, token);
3490 }
3491 }
3492
3493 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 mLayoutNeeded = true;
3495 performLayoutAndPlaceSurfacesLocked();
3496
3497 //dump();
3498 }
3499
3500 public void moveAppTokensToTop(List<IBinder> tokens) {
3501 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3502 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003503 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
3505
3506 final long origId = Binder.clearCallingIdentity();
3507 synchronized(mWindowMap) {
3508 removeAppTokensLocked(tokens);
3509 final int N = tokens.size();
3510 for (int i=0; i<N; i++) {
3511 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3512 if (wt != null) {
3513 mAppTokens.add(wt);
3514 }
3515 }
3516 moveAppWindowsLocked(tokens, mAppTokens.size());
3517 }
3518 Binder.restoreCallingIdentity(origId);
3519 }
3520
3521 public void moveAppTokensToBottom(List<IBinder> tokens) {
3522 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3523 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003524 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
3526
3527 final long origId = Binder.clearCallingIdentity();
3528 synchronized(mWindowMap) {
3529 removeAppTokensLocked(tokens);
3530 final int N = tokens.size();
3531 int pos = 0;
3532 for (int i=0; i<N; i++) {
3533 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3534 if (wt != null) {
3535 mAppTokens.add(pos, wt);
3536 pos++;
3537 }
3538 }
3539 moveAppWindowsLocked(tokens, 0);
3540 }
3541 Binder.restoreCallingIdentity(origId);
3542 }
3543
3544 // -------------------------------------------------------------
3545 // Misc IWindowSession methods
3546 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 public void disableKeyguard(IBinder token, String tag) {
3549 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3550 != PackageManager.PERMISSION_GRANTED) {
3551 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3552 }
3553 mKeyguardDisabled.acquire(token, tag);
3554 }
3555
3556 public void reenableKeyguard(IBinder token) {
3557 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3558 != PackageManager.PERMISSION_GRANTED) {
3559 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3560 }
3561 synchronized (mKeyguardDisabled) {
3562 mKeyguardDisabled.release(token);
3563
3564 if (!mKeyguardDisabled.isAcquired()) {
3565 // if we are the last one to reenable the keyguard wait until
3566 // we have actaully finished reenabling until returning
3567 mWaitingUntilKeyguardReenabled = true;
3568 while (mWaitingUntilKeyguardReenabled) {
3569 try {
3570 mKeyguardDisabled.wait();
3571 } catch (InterruptedException e) {
3572 Thread.currentThread().interrupt();
3573 }
3574 }
3575 }
3576 }
3577 }
3578
3579 /**
3580 * @see android.app.KeyguardManager#exitKeyguardSecurely
3581 */
3582 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3583 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3584 != PackageManager.PERMISSION_GRANTED) {
3585 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3586 }
3587 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3588 public void onKeyguardExitResult(boolean success) {
3589 try {
3590 callback.onKeyguardExitResult(success);
3591 } catch (RemoteException e) {
3592 // Client has died, we don't care.
3593 }
3594 }
3595 });
3596 }
3597
3598 public boolean inKeyguardRestrictedInputMode() {
3599 return mPolicy.inKeyguardRestrictedKeyInputMode();
3600 }
Romain Guy06882f82009-06-10 13:36:04 -07003601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 static float fixScale(float scale) {
3603 if (scale < 0) scale = 0;
3604 else if (scale > 20) scale = 20;
3605 return Math.abs(scale);
3606 }
Romain Guy06882f82009-06-10 13:36:04 -07003607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 public void setAnimationScale(int which, float scale) {
3609 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3610 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003611 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613
3614 if (scale < 0) scale = 0;
3615 else if (scale > 20) scale = 20;
3616 scale = Math.abs(scale);
3617 switch (which) {
3618 case 0: mWindowAnimationScale = fixScale(scale); break;
3619 case 1: mTransitionAnimationScale = fixScale(scale); break;
3620 }
Romain Guy06882f82009-06-10 13:36:04 -07003621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 // Persist setting
3623 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3624 }
Romain Guy06882f82009-06-10 13:36:04 -07003625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 public void setAnimationScales(float[] scales) {
3627 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3628 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003629 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 }
3631
3632 if (scales != null) {
3633 if (scales.length >= 1) {
3634 mWindowAnimationScale = fixScale(scales[0]);
3635 }
3636 if (scales.length >= 2) {
3637 mTransitionAnimationScale = fixScale(scales[1]);
3638 }
3639 }
Romain Guy06882f82009-06-10 13:36:04 -07003640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 // Persist setting
3642 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3643 }
Romain Guy06882f82009-06-10 13:36:04 -07003644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 public float getAnimationScale(int which) {
3646 switch (which) {
3647 case 0: return mWindowAnimationScale;
3648 case 1: return mTransitionAnimationScale;
3649 }
3650 return 0;
3651 }
Romain Guy06882f82009-06-10 13:36:04 -07003652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 public float[] getAnimationScales() {
3654 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3655 }
Romain Guy06882f82009-06-10 13:36:04 -07003656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 public int getSwitchState(int sw) {
3658 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3659 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003660 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
3662 return KeyInputQueue.getSwitchState(sw);
3663 }
Romain Guy06882f82009-06-10 13:36:04 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 public int getSwitchStateForDevice(int devid, int sw) {
3666 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3667 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003668 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670 return KeyInputQueue.getSwitchState(devid, sw);
3671 }
Romain Guy06882f82009-06-10 13:36:04 -07003672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 public int getScancodeState(int sw) {
3674 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3675 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003676 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 }
3678 return KeyInputQueue.getScancodeState(sw);
3679 }
Romain Guy06882f82009-06-10 13:36:04 -07003680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 public int getScancodeStateForDevice(int devid, int sw) {
3682 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3683 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003684 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686 return KeyInputQueue.getScancodeState(devid, sw);
3687 }
Romain Guy06882f82009-06-10 13:36:04 -07003688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 public int getKeycodeState(int sw) {
3690 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3691 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003692 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
3694 return KeyInputQueue.getKeycodeState(sw);
3695 }
Romain Guy06882f82009-06-10 13:36:04 -07003696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 public int getKeycodeStateForDevice(int devid, int sw) {
3698 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3699 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003700 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 }
3702 return KeyInputQueue.getKeycodeState(devid, sw);
3703 }
Romain Guy06882f82009-06-10 13:36:04 -07003704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3706 return KeyInputQueue.hasKeys(keycodes, keyExists);
3707 }
Romain Guy06882f82009-06-10 13:36:04 -07003708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 public void enableScreenAfterBoot() {
3710 synchronized(mWindowMap) {
3711 if (mSystemBooted) {
3712 return;
3713 }
3714 mSystemBooted = true;
3715 }
Romain Guy06882f82009-06-10 13:36:04 -07003716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 performEnableScreen();
3718 }
Romain Guy06882f82009-06-10 13:36:04 -07003719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 public void enableScreenIfNeededLocked() {
3721 if (mDisplayEnabled) {
3722 return;
3723 }
3724 if (!mSystemBooted) {
3725 return;
3726 }
3727 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3728 }
Romain Guy06882f82009-06-10 13:36:04 -07003729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 public void performEnableScreen() {
3731 synchronized(mWindowMap) {
3732 if (mDisplayEnabled) {
3733 return;
3734 }
3735 if (!mSystemBooted) {
3736 return;
3737 }
Romain Guy06882f82009-06-10 13:36:04 -07003738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 // Don't enable the screen until all existing windows
3740 // have been drawn.
3741 final int N = mWindows.size();
3742 for (int i=0; i<N; i++) {
3743 WindowState w = (WindowState)mWindows.get(i);
3744 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3745 return;
3746 }
3747 }
Romain Guy06882f82009-06-10 13:36:04 -07003748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 mDisplayEnabled = true;
3750 if (false) {
3751 Log.i(TAG, "ENABLING SCREEN!");
3752 StringWriter sw = new StringWriter();
3753 PrintWriter pw = new PrintWriter(sw);
3754 this.dump(null, pw, null);
3755 Log.i(TAG, sw.toString());
3756 }
3757 try {
3758 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3759 if (surfaceFlinger != null) {
3760 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3761 Parcel data = Parcel.obtain();
3762 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3763 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3764 data, null, 0);
3765 data.recycle();
3766 }
3767 } catch (RemoteException ex) {
3768 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3769 }
3770 }
Romain Guy06882f82009-06-10 13:36:04 -07003771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003775 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3776 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
Romain Guy06882f82009-06-10 13:36:04 -07003778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 public void setInTouchMode(boolean mode) {
3780 synchronized(mWindowMap) {
3781 mInTouchMode = mode;
3782 }
3783 }
3784
Romain Guy06882f82009-06-10 13:36:04 -07003785 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003786 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003788 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003789 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 }
3791
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003792 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 }
Romain Guy06882f82009-06-10 13:36:04 -07003794
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003795 public void setRotationUnchecked(int rotation,
3796 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 if(DEBUG_ORIENTATION) Log.v(TAG,
3798 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 long origId = Binder.clearCallingIdentity();
3801 boolean changed;
3802 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003803 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 }
Romain Guy06882f82009-06-10 13:36:04 -07003805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 if (changed) {
3807 sendNewConfiguration();
3808 synchronized(mWindowMap) {
3809 mLayoutNeeded = true;
3810 performLayoutAndPlaceSurfacesLocked();
3811 }
3812 } else if (alwaysSendConfiguration) {
3813 //update configuration ignoring orientation change
3814 sendNewConfiguration();
3815 }
Romain Guy06882f82009-06-10 13:36:04 -07003816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 Binder.restoreCallingIdentity(origId);
3818 }
Romain Guy06882f82009-06-10 13:36:04 -07003819
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003820 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 boolean changed;
3822 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3823 rotation = mRequestedRotation;
3824 } else {
3825 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003826 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 }
3828 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003829 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 mRotation, mDisplayEnabled);
3831 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3832 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003835 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 "Rotation changed to " + rotation
3837 + " from " + mRotation
3838 + " (forceApp=" + mForcedAppOrientation
3839 + ", req=" + mRequestedRotation + ")");
3840 mRotation = rotation;
3841 mWindowsFreezingScreen = true;
3842 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3843 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3844 2000);
3845 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003846 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 mQueue.setOrientation(rotation);
3848 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003849 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 }
3851 for (int i=mWindows.size()-1; i>=0; i--) {
3852 WindowState w = (WindowState)mWindows.get(i);
3853 if (w.mSurface != null) {
3854 w.mOrientationChanging = true;
3855 }
3856 }
3857 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3858 try {
3859 mRotationWatchers.get(i).onRotationChanged(rotation);
3860 } catch (RemoteException e) {
3861 }
3862 }
3863 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 return changed;
3866 }
Romain Guy06882f82009-06-10 13:36:04 -07003867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 public int getRotation() {
3869 return mRotation;
3870 }
3871
3872 public int watchRotation(IRotationWatcher watcher) {
3873 final IBinder watcherBinder = watcher.asBinder();
3874 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3875 public void binderDied() {
3876 synchronized (mWindowMap) {
3877 for (int i=0; i<mRotationWatchers.size(); i++) {
3878 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003879 IRotationWatcher removed = mRotationWatchers.remove(i);
3880 if (removed != null) {
3881 removed.asBinder().unlinkToDeath(this, 0);
3882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 i--;
3884 }
3885 }
3886 }
3887 }
3888 };
Romain Guy06882f82009-06-10 13:36:04 -07003889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 synchronized (mWindowMap) {
3891 try {
3892 watcher.asBinder().linkToDeath(dr, 0);
3893 mRotationWatchers.add(watcher);
3894 } catch (RemoteException e) {
3895 // Client died, no cleanup needed.
3896 }
Romain Guy06882f82009-06-10 13:36:04 -07003897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 return mRotation;
3899 }
3900 }
3901
3902 /**
3903 * Starts the view server on the specified port.
3904 *
3905 * @param port The port to listener to.
3906 *
3907 * @return True if the server was successfully started, false otherwise.
3908 *
3909 * @see com.android.server.ViewServer
3910 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3911 */
3912 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003913 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 return false;
3915 }
3916
3917 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3918 return false;
3919 }
3920
3921 if (port < 1024) {
3922 return false;
3923 }
3924
3925 if (mViewServer != null) {
3926 if (!mViewServer.isRunning()) {
3927 try {
3928 return mViewServer.start();
3929 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003930 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 }
3932 }
3933 return false;
3934 }
3935
3936 try {
3937 mViewServer = new ViewServer(this, port);
3938 return mViewServer.start();
3939 } catch (IOException e) {
3940 Log.w(TAG, "View server did not start");
3941 }
3942 return false;
3943 }
3944
Romain Guy06882f82009-06-10 13:36:04 -07003945 private boolean isSystemSecure() {
3946 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
3947 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
3948 }
3949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 /**
3951 * Stops the view server if it exists.
3952 *
3953 * @return True if the server stopped, false if it wasn't started or
3954 * couldn't be stopped.
3955 *
3956 * @see com.android.server.ViewServer
3957 */
3958 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07003959 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 return false;
3961 }
3962
3963 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
3964 return false;
3965 }
3966
3967 if (mViewServer != null) {
3968 return mViewServer.stop();
3969 }
3970 return false;
3971 }
3972
3973 /**
3974 * Indicates whether the view server is running.
3975 *
3976 * @return True if the server is running, false otherwise.
3977 *
3978 * @see com.android.server.ViewServer
3979 */
3980 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07003981 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 return false;
3983 }
3984
3985 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
3986 return false;
3987 }
3988
3989 return mViewServer != null && mViewServer.isRunning();
3990 }
3991
3992 /**
3993 * Lists all availble windows in the system. The listing is written in the
3994 * specified Socket's output stream with the following syntax:
3995 * windowHashCodeInHexadecimal windowName
3996 * Each line of the ouput represents a different window.
3997 *
3998 * @param client The remote client to send the listing to.
3999 * @return False if an error occured, true otherwise.
4000 */
4001 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004002 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 return false;
4004 }
4005
4006 boolean result = true;
4007
4008 Object[] windows;
4009 synchronized (mWindowMap) {
4010 windows = new Object[mWindows.size()];
4011 //noinspection unchecked
4012 windows = mWindows.toArray(windows);
4013 }
4014
4015 BufferedWriter out = null;
4016
4017 // Any uncaught exception will crash the system process
4018 try {
4019 OutputStream clientStream = client.getOutputStream();
4020 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4021
4022 final int count = windows.length;
4023 for (int i = 0; i < count; i++) {
4024 final WindowState w = (WindowState) windows[i];
4025 out.write(Integer.toHexString(System.identityHashCode(w)));
4026 out.write(' ');
4027 out.append(w.mAttrs.getTitle());
4028 out.write('\n');
4029 }
4030
4031 out.write("DONE.\n");
4032 out.flush();
4033 } catch (Exception e) {
4034 result = false;
4035 } finally {
4036 if (out != null) {
4037 try {
4038 out.close();
4039 } catch (IOException e) {
4040 result = false;
4041 }
4042 }
4043 }
4044
4045 return result;
4046 }
4047
4048 /**
4049 * Sends a command to a target window. The result of the command, if any, will be
4050 * written in the output stream of the specified socket.
4051 *
4052 * The parameters must follow this syntax:
4053 * windowHashcode extra
4054 *
4055 * Where XX is the length in characeters of the windowTitle.
4056 *
4057 * The first parameter is the target window. The window with the specified hashcode
4058 * will be the target. If no target can be found, nothing happens. The extra parameters
4059 * will be delivered to the target window and as parameters to the command itself.
4060 *
4061 * @param client The remote client to sent the result, if any, to.
4062 * @param command The command to execute.
4063 * @param parameters The command parameters.
4064 *
4065 * @return True if the command was successfully delivered, false otherwise. This does
4066 * not indicate whether the command itself was successful.
4067 */
4068 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004069 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 return false;
4071 }
4072
4073 boolean success = true;
4074 Parcel data = null;
4075 Parcel reply = null;
4076
4077 // Any uncaught exception will crash the system process
4078 try {
4079 // Find the hashcode of the window
4080 int index = parameters.indexOf(' ');
4081 if (index == -1) {
4082 index = parameters.length();
4083 }
4084 final String code = parameters.substring(0, index);
4085 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4086
4087 // Extract the command's parameter after the window description
4088 if (index < parameters.length()) {
4089 parameters = parameters.substring(index + 1);
4090 } else {
4091 parameters = "";
4092 }
4093
4094 final WindowManagerService.WindowState window = findWindow(hashCode);
4095 if (window == null) {
4096 return false;
4097 }
4098
4099 data = Parcel.obtain();
4100 data.writeInterfaceToken("android.view.IWindow");
4101 data.writeString(command);
4102 data.writeString(parameters);
4103 data.writeInt(1);
4104 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4105
4106 reply = Parcel.obtain();
4107
4108 final IBinder binder = window.mClient.asBinder();
4109 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4110 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4111
4112 reply.readException();
4113
4114 } catch (Exception e) {
4115 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4116 success = false;
4117 } finally {
4118 if (data != null) {
4119 data.recycle();
4120 }
4121 if (reply != null) {
4122 reply.recycle();
4123 }
4124 }
4125
4126 return success;
4127 }
4128
4129 private WindowState findWindow(int hashCode) {
4130 if (hashCode == -1) {
4131 return getFocusedWindow();
4132 }
4133
4134 synchronized (mWindowMap) {
4135 final ArrayList windows = mWindows;
4136 final int count = windows.size();
4137
4138 for (int i = 0; i < count; i++) {
4139 WindowState w = (WindowState) windows.get(i);
4140 if (System.identityHashCode(w) == hashCode) {
4141 return w;
4142 }
4143 }
4144 }
4145
4146 return null;
4147 }
4148
4149 /*
4150 * Instruct the Activity Manager to fetch the current configuration and broadcast
4151 * that to config-changed listeners if appropriate.
4152 */
4153 void sendNewConfiguration() {
4154 try {
4155 mActivityManager.updateConfiguration(null);
4156 } catch (RemoteException e) {
4157 }
4158 }
Romain Guy06882f82009-06-10 13:36:04 -07004159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 public Configuration computeNewConfiguration() {
4161 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004162 return computeNewConfigurationLocked();
4163 }
4164 }
Romain Guy06882f82009-06-10 13:36:04 -07004165
Dianne Hackbornc485a602009-03-24 22:39:49 -07004166 Configuration computeNewConfigurationLocked() {
4167 Configuration config = new Configuration();
4168 if (!computeNewConfigurationLocked(config)) {
4169 return null;
4170 }
4171 Log.i(TAG, "Config changed: " + config);
4172 long now = SystemClock.uptimeMillis();
4173 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
4174 if (mFreezeGcPending != 0) {
4175 if (now > (mFreezeGcPending+1000)) {
4176 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
4177 mH.removeMessages(H.FORCE_GC);
4178 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 mFreezeGcPending = now;
4180 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004181 } else {
4182 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004184 return config;
4185 }
Romain Guy06882f82009-06-10 13:36:04 -07004186
Dianne Hackbornc485a602009-03-24 22:39:49 -07004187 boolean computeNewConfigurationLocked(Configuration config) {
4188 if (mDisplay == null) {
4189 return false;
4190 }
4191 mQueue.getInputConfiguration(config);
4192 final int dw = mDisplay.getWidth();
4193 final int dh = mDisplay.getHeight();
4194 int orientation = Configuration.ORIENTATION_SQUARE;
4195 if (dw < dh) {
4196 orientation = Configuration.ORIENTATION_PORTRAIT;
4197 } else if (dw > dh) {
4198 orientation = Configuration.ORIENTATION_LANDSCAPE;
4199 }
4200 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004201
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004202 DisplayMetrics dm = new DisplayMetrics();
4203 mDisplay.getMetrics(dm);
4204 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4205
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004206 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004207 // Note we only do this once because at this point we don't
4208 // expect the screen to change in this way at runtime, and want
4209 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004210 int longSize = dw;
4211 int shortSize = dh;
4212 if (longSize < shortSize) {
4213 int tmp = longSize;
4214 longSize = shortSize;
4215 shortSize = tmp;
4216 }
4217 longSize = (int)(longSize/dm.density);
4218 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004219
Dianne Hackborn723738c2009-06-25 19:48:04 -07004220 // These semi-magic numbers define our compatibility modes for
4221 // applications with different screens. Don't change unless you
4222 // make sure to test lots and lots of apps!
4223 if (longSize < 470) {
4224 // This is shorter than an HVGA normal density screen (which
4225 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004226 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4227 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004228 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004229 // Is this a large screen?
4230 if (longSize > 640 && shortSize >= 480) {
4231 // VGA or larger screens at medium density are the point
4232 // at which we consider it to be a large screen.
4233 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4234 } else {
4235 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4236
4237 // If this screen is wider than normal HVGA, or taller
4238 // than FWVGA, then for old apps we want to run in size
4239 // compatibility mode.
4240 if (shortSize > 321 || longSize > 570) {
4241 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4242 }
4243 }
4244
4245 // Is this a long screen?
4246 if (((longSize*3)/5) >= (shortSize-1)) {
4247 // Anything wider than WVGA (5:3) is considering to be long.
4248 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4249 } else {
4250 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4251 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004252 }
4253 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004254 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004255
Dianne Hackbornc485a602009-03-24 22:39:49 -07004256 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4257 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4258 mPolicy.adjustConfigurationLw(config);
4259 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
Romain Guy06882f82009-06-10 13:36:04 -07004261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 // -------------------------------------------------------------
4263 // Input Events and Focus Management
4264 // -------------------------------------------------------------
4265
4266 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004267 long curTime = SystemClock.uptimeMillis();
4268
Michael Chane10de972009-05-18 11:24:50 -07004269 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004270 if (mLastTouchEventType == eventType &&
4271 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4272 return;
4273 }
4274 mLastUserActivityCallTime = curTime;
4275 mLastTouchEventType = eventType;
4276 }
4277
4278 if (targetWin == null
4279 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4280 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 }
4282 }
4283
4284 // tells if it's a cheek event or not -- this function is stateful
4285 private static final int EVENT_NONE = 0;
4286 private static final int EVENT_UNKNOWN = 0;
4287 private static final int EVENT_CHEEK = 0;
4288 private static final int EVENT_IGNORE_DURATION = 300; // ms
4289 private static final float CHEEK_THRESHOLD = 0.6f;
4290 private int mEventState = EVENT_NONE;
4291 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 private int eventType(MotionEvent ev) {
4294 float size = ev.getSize();
4295 switch (ev.getAction()) {
4296 case MotionEvent.ACTION_DOWN:
4297 mEventSize = size;
4298 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4299 case MotionEvent.ACTION_UP:
4300 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004301 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 case MotionEvent.ACTION_MOVE:
4303 final int N = ev.getHistorySize();
4304 if (size > mEventSize) mEventSize = size;
4305 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4306 for (int i=0; i<N; i++) {
4307 size = ev.getHistoricalSize(i);
4308 if (size > mEventSize) mEventSize = size;
4309 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4310 }
4311 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4312 return TOUCH_EVENT;
4313 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004314 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
4316 default:
4317 // not good
4318 return OTHER_EVENT;
4319 }
4320 }
4321
4322 /**
4323 * @return Returns true if event was dispatched, false if it was dropped for any reason
4324 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004325 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4327 "dispatchPointer " + ev);
4328
Michael Chan53071d62009-05-13 17:29:48 -07004329 if (MEASURE_LATENCY) {
4330 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4331 }
4332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004334 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335
Michael Chan53071d62009-05-13 17:29:48 -07004336 if (MEASURE_LATENCY) {
4337 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4338 }
4339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 if (action == MotionEvent.ACTION_UP) {
4343 // let go of our target
4344 mKeyWaiter.mMotionTarget = null;
4345 mPowerManager.logPointerUpEvent();
4346 } else if (action == MotionEvent.ACTION_DOWN) {
4347 mPowerManager.logPointerDownEvent();
4348 }
4349
4350 if (targetObj == null) {
4351 // In this case we are either dropping the event, or have received
4352 // a move or up without a down. It is common to receive move
4353 // events in such a way, since this means the user is moving the
4354 // pointer without actually pressing down. All other cases should
4355 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004356 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4358 }
4359 if (qev != null) {
4360 mQueue.recycleEvent(qev);
4361 }
4362 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004363 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4366 if (qev != null) {
4367 mQueue.recycleEvent(qev);
4368 }
4369 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004370 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004376 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377
4378 //Log.i(TAG, "Sending " + ev + " to " + target);
4379
4380 if (uid != 0 && uid != target.mSession.mUid) {
4381 if (mContext.checkPermission(
4382 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4383 != PackageManager.PERMISSION_GRANTED) {
4384 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4385 + pid + " uid " + uid + " to window " + target
4386 + " owned by uid " + target.mSession.mUid);
4387 if (qev != null) {
4388 mQueue.recycleEvent(qev);
4389 }
4390 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004391 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 }
4393 }
4394
Michael Chan53071d62009-05-13 17:29:48 -07004395 if (MEASURE_LATENCY) {
4396 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4397 }
4398
Romain Guy06882f82009-06-10 13:36:04 -07004399 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4401 //target wants to ignore fat touch events
4402 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4403 //explicit flag to return without processing event further
4404 boolean returnFlag = false;
4405 if((action == MotionEvent.ACTION_DOWN)) {
4406 mFatTouch = false;
4407 if(cheekPress) {
4408 mFatTouch = true;
4409 returnFlag = true;
4410 }
4411 } else {
4412 if(action == MotionEvent.ACTION_UP) {
4413 if(mFatTouch) {
4414 //earlier even was invalid doesnt matter if current up is cheekpress or not
4415 mFatTouch = false;
4416 returnFlag = true;
4417 } else if(cheekPress) {
4418 //cancel the earlier event
4419 ev.setAction(MotionEvent.ACTION_CANCEL);
4420 action = MotionEvent.ACTION_CANCEL;
4421 }
4422 } else if(action == MotionEvent.ACTION_MOVE) {
4423 if(mFatTouch) {
4424 //two cases here
4425 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004426 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 returnFlag = true;
4428 } else if(cheekPress) {
4429 //valid down followed by invalid moves
4430 //an invalid move have to cancel earlier action
4431 ev.setAction(MotionEvent.ACTION_CANCEL);
4432 action = MotionEvent.ACTION_CANCEL;
4433 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4434 //note that the subsequent invalid moves will not get here
4435 mFatTouch = true;
4436 }
4437 }
4438 } //else if action
4439 if(returnFlag) {
4440 //recycle que, ev
4441 if (qev != null) {
4442 mQueue.recycleEvent(qev);
4443 }
4444 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004445 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 }
4447 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004448
Michael Chan9f028e62009-08-04 17:37:46 -07004449 // Enable this for testing the "right" value
4450 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004451 int max_events_per_sec = 35;
4452 try {
4453 max_events_per_sec = Integer.parseInt(SystemProperties
4454 .get("windowsmgr.max_events_per_sec"));
4455 if (max_events_per_sec < 1) {
4456 max_events_per_sec = 35;
4457 }
4458 } catch (NumberFormatException e) {
4459 }
4460 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4461 }
4462
4463 /*
4464 * Throttle events to minimize CPU usage when there's a flood of events
4465 * e.g. constant contact with the screen
4466 */
4467 if (action == MotionEvent.ACTION_MOVE) {
4468 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4469 long now = SystemClock.uptimeMillis();
4470 if (now < nextEventTime) {
4471 try {
4472 Thread.sleep(nextEventTime - now);
4473 } catch (InterruptedException e) {
4474 }
4475 mLastTouchEventTime = nextEventTime;
4476 } else {
4477 mLastTouchEventTime = now;
4478 }
4479 }
4480
Michael Chan53071d62009-05-13 17:29:48 -07004481 if (MEASURE_LATENCY) {
4482 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4483 }
4484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 synchronized(mWindowMap) {
4486 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4487 mKeyWaiter.bindTargetWindowLocked(target,
4488 KeyWaiter.RETURN_PENDING_POINTER, qev);
4489 ev = null;
4490 } else {
4491 if (action == MotionEvent.ACTION_DOWN) {
4492 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4493 if (out != null) {
4494 MotionEvent oev = MotionEvent.obtain(ev);
4495 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4496 do {
4497 final Rect frame = out.mFrame;
4498 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4499 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004500 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 } catch (android.os.RemoteException e) {
4502 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4503 }
4504 oev.offsetLocation((float)frame.left, (float)frame.top);
4505 out = out.mNextOutsideTouch;
4506 } while (out != null);
4507 mKeyWaiter.mOutsideTouchTargets = null;
4508 }
4509 }
4510 final Rect frame = target.mFrame;
4511 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4512 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004513
4514 // If we are on top of the wallpaper, then the wallpaper also
4515 // gets to see this movement.
4516 if (mWallpaperTarget == target) {
4517 sendPointerToWallpaperLocked(target, ev, eventTime);
4518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 }
4520 }
Romain Guy06882f82009-06-10 13:36:04 -07004521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 // finally offset the event to the target's coordinate system and
4523 // dispatch the event.
4524 try {
4525 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4526 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4527 }
Michael Chan53071d62009-05-13 17:29:48 -07004528
4529 if (MEASURE_LATENCY) {
4530 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4531 }
4532
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004533 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004534
4535 if (MEASURE_LATENCY) {
4536 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4537 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004538 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 } catch (android.os.RemoteException e) {
4540 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4541 mKeyWaiter.mMotionTarget = null;
4542 try {
4543 removeWindow(target.mSession, target.mClient);
4544 } catch (java.util.NoSuchElementException ex) {
4545 // This will happen if the window has already been
4546 // removed.
4547 }
4548 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004549 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
Romain Guy06882f82009-06-10 13:36:04 -07004551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 /**
4553 * @return Returns true if event was dispatched, false if it was dropped for any reason
4554 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004555 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 if (DEBUG_INPUT) Log.v(
4557 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004560 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 if (focusObj == null) {
4562 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4563 if (qev != null) {
4564 mQueue.recycleEvent(qev);
4565 }
4566 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004567 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 }
4569 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4570 if (qev != null) {
4571 mQueue.recycleEvent(qev);
4572 }
4573 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004574 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 }
Romain Guy06882f82009-06-10 13:36:04 -07004576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 if (uid != 0 && uid != focus.mSession.mUid) {
4580 if (mContext.checkPermission(
4581 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4582 != PackageManager.PERMISSION_GRANTED) {
4583 Log.w(TAG, "Permission denied: injecting key event from pid "
4584 + pid + " uid " + uid + " to window " + focus
4585 + " owned by uid " + focus.mSession.mUid);
4586 if (qev != null) {
4587 mQueue.recycleEvent(qev);
4588 }
4589 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004590 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 }
4592 }
Romain Guy06882f82009-06-10 13:36:04 -07004593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 synchronized(mWindowMap) {
4597 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4598 mKeyWaiter.bindTargetWindowLocked(focus,
4599 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4600 // We don't deliver movement events to the client, we hold
4601 // them and wait for them to call back.
4602 ev = null;
4603 } else {
4604 mKeyWaiter.bindTargetWindowLocked(focus);
4605 }
4606 }
Romain Guy06882f82009-06-10 13:36:04 -07004607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004609 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004610 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 } catch (android.os.RemoteException e) {
4612 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4613 try {
4614 removeWindow(focus.mSession, focus.mClient);
4615 } catch (java.util.NoSuchElementException ex) {
4616 // This will happen if the window has already been
4617 // removed.
4618 }
4619 }
Romain Guy06882f82009-06-10 13:36:04 -07004620
Dianne Hackborncfaef692009-06-15 14:24:44 -07004621 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 }
Romain Guy06882f82009-06-10 13:36:04 -07004623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 /**
4625 * @return Returns true if event was dispatched, false if it was dropped for any reason
4626 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004627 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4629
4630 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004631 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 if (focusObj == null) {
4633 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004634 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 }
4636 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004637 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 }
Romain Guy06882f82009-06-10 13:36:04 -07004639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 if (DEBUG_INPUT) Log.v(
4643 TAG, "Dispatching to " + focus + ": " + event);
4644
4645 if (uid != 0 && uid != focus.mSession.mUid) {
4646 if (mContext.checkPermission(
4647 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4648 != PackageManager.PERMISSION_GRANTED) {
4649 Log.w(TAG, "Permission denied: injecting key event from pid "
4650 + pid + " uid " + uid + " to window " + focus
4651 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004652 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 }
4654 }
Romain Guy06882f82009-06-10 13:36:04 -07004655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 synchronized(mWindowMap) {
4657 mKeyWaiter.bindTargetWindowLocked(focus);
4658 }
4659
4660 // NOSHIP extra state logging
4661 mKeyWaiter.recordDispatchState(event, focus);
4662 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 try {
4665 if (DEBUG_INPUT || DEBUG_FOCUS) {
4666 Log.v(TAG, "Delivering key " + event.getKeyCode()
4667 + " to " + focus);
4668 }
4669 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004670 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 } catch (android.os.RemoteException e) {
4672 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4673 try {
4674 removeWindow(focus.mSession, focus.mClient);
4675 } catch (java.util.NoSuchElementException ex) {
4676 // This will happen if the window has already been
4677 // removed.
4678 }
4679 }
Romain Guy06882f82009-06-10 13:36:04 -07004680
Dianne Hackborncfaef692009-06-15 14:24:44 -07004681 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 }
Romain Guy06882f82009-06-10 13:36:04 -07004683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 public void pauseKeyDispatching(IBinder _token) {
4685 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4686 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004687 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 }
4689
4690 synchronized (mWindowMap) {
4691 WindowToken token = mTokenMap.get(_token);
4692 if (token != null) {
4693 mKeyWaiter.pauseDispatchingLocked(token);
4694 }
4695 }
4696 }
4697
4698 public void resumeKeyDispatching(IBinder _token) {
4699 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4700 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004701 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004702 }
4703
4704 synchronized (mWindowMap) {
4705 WindowToken token = mTokenMap.get(_token);
4706 if (token != null) {
4707 mKeyWaiter.resumeDispatchingLocked(token);
4708 }
4709 }
4710 }
4711
4712 public void setEventDispatching(boolean enabled) {
4713 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4714 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004715 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 }
4717
4718 synchronized (mWindowMap) {
4719 mKeyWaiter.setEventDispatchingLocked(enabled);
4720 }
4721 }
Romain Guy06882f82009-06-10 13:36:04 -07004722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 /**
4724 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004725 *
4726 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 * {@link SystemClock#uptimeMillis()} as the timebase.)
4728 * @param sync If true, wait for the event to be completed before returning to the caller.
4729 * @return Returns true if event was dispatched, false if it was dropped for any reason
4730 */
4731 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4732 long downTime = ev.getDownTime();
4733 long eventTime = ev.getEventTime();
4734
4735 int action = ev.getAction();
4736 int code = ev.getKeyCode();
4737 int repeatCount = ev.getRepeatCount();
4738 int metaState = ev.getMetaState();
4739 int deviceId = ev.getDeviceId();
4740 int scancode = ev.getScanCode();
4741
4742 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4743 if (downTime == 0) downTime = eventTime;
4744
4745 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004746 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004748 final int pid = Binder.getCallingPid();
4749 final int uid = Binder.getCallingUid();
4750 final long ident = Binder.clearCallingIdentity();
4751 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004753 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004755 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004756 switch (result) {
4757 case INJECT_NO_PERMISSION:
4758 throw new SecurityException(
4759 "Injecting to another application requires INJECT_EVENT permission");
4760 case INJECT_SUCCEEDED:
4761 return true;
4762 }
4763 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 }
4765
4766 /**
4767 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004768 *
4769 * @param ev A motion event describing the pointer (touch) action. (As noted in
4770 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 * {@link SystemClock#uptimeMillis()} as the timebase.)
4772 * @param sync If true, wait for the event to be completed before returning to the caller.
4773 * @return Returns true if event was dispatched, false if it was dropped for any reason
4774 */
4775 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004776 final int pid = Binder.getCallingPid();
4777 final int uid = Binder.getCallingUid();
4778 final long ident = Binder.clearCallingIdentity();
4779 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004781 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004782 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004783 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004784 switch (result) {
4785 case INJECT_NO_PERMISSION:
4786 throw new SecurityException(
4787 "Injecting to another application requires INJECT_EVENT permission");
4788 case INJECT_SUCCEEDED:
4789 return true;
4790 }
4791 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 }
Romain Guy06882f82009-06-10 13:36:04 -07004793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004794 /**
4795 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004796 *
4797 * @param ev A motion event describing the trackball action. (As noted in
4798 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 * {@link SystemClock#uptimeMillis()} as the timebase.)
4800 * @param sync If true, wait for the event to be completed before returning to the caller.
4801 * @return Returns true if event was dispatched, false if it was dropped for any reason
4802 */
4803 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004804 final int pid = Binder.getCallingPid();
4805 final int uid = Binder.getCallingUid();
4806 final long ident = Binder.clearCallingIdentity();
4807 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004809 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004811 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004812 switch (result) {
4813 case INJECT_NO_PERMISSION:
4814 throw new SecurityException(
4815 "Injecting to another application requires INJECT_EVENT permission");
4816 case INJECT_SUCCEEDED:
4817 return true;
4818 }
4819 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 }
Romain Guy06882f82009-06-10 13:36:04 -07004821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 private WindowState getFocusedWindow() {
4823 synchronized (mWindowMap) {
4824 return getFocusedWindowLocked();
4825 }
4826 }
4827
4828 private WindowState getFocusedWindowLocked() {
4829 return mCurrentFocus;
4830 }
Romain Guy06882f82009-06-10 13:36:04 -07004831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 /**
4833 * This class holds the state for dispatching key events. This state
4834 * is protected by the KeyWaiter instance, NOT by the window lock. You
4835 * can be holding the main window lock while acquire the KeyWaiter lock,
4836 * but not the other way around.
4837 */
4838 final class KeyWaiter {
4839 // NOSHIP debugging
4840 public class DispatchState {
4841 private KeyEvent event;
4842 private WindowState focus;
4843 private long time;
4844 private WindowState lastWin;
4845 private IBinder lastBinder;
4846 private boolean finished;
4847 private boolean gotFirstWindow;
4848 private boolean eventDispatching;
4849 private long timeToSwitch;
4850 private boolean wasFrozen;
4851 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004852 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4855 focus = theFocus;
4856 event = theEvent;
4857 time = System.currentTimeMillis();
4858 // snapshot KeyWaiter state
4859 lastWin = mLastWin;
4860 lastBinder = mLastBinder;
4861 finished = mFinished;
4862 gotFirstWindow = mGotFirstWindow;
4863 eventDispatching = mEventDispatching;
4864 timeToSwitch = mTimeToSwitch;
4865 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004866 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 // cache the paused state at ctor time as well
4868 if (theFocus == null || theFocus.mToken == null) {
4869 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4870 focusPaused = false;
4871 } else {
4872 focusPaused = theFocus.mToken.paused;
4873 }
4874 }
Romain Guy06882f82009-06-10 13:36:04 -07004875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 public String toString() {
4877 return "{{" + event + " to " + focus + " @ " + time
4878 + " lw=" + lastWin + " lb=" + lastBinder
4879 + " fin=" + finished + " gfw=" + gotFirstWindow
4880 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004881 + " wf=" + wasFrozen + " fp=" + focusPaused
4882 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
4884 };
4885 private DispatchState mDispatchState = null;
4886 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4887 mDispatchState = new DispatchState(theEvent, theFocus);
4888 }
4889 // END NOSHIP
4890
4891 public static final int RETURN_NOTHING = 0;
4892 public static final int RETURN_PENDING_POINTER = 1;
4893 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 final Object SKIP_TARGET_TOKEN = new Object();
4896 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 private WindowState mLastWin = null;
4899 private IBinder mLastBinder = null;
4900 private boolean mFinished = true;
4901 private boolean mGotFirstWindow = false;
4902 private boolean mEventDispatching = true;
4903 private long mTimeToSwitch = 0;
4904 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004906 // Target of Motion events
4907 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 // Windows above the target who would like to receive an "outside"
4910 // touch event for any down events outside of them.
4911 WindowState mOutsideTouchTargets;
4912
4913 /**
4914 * Wait for the last event dispatch to complete, then find the next
4915 * target that should receive the given event and wait for that one
4916 * to be ready to receive it.
4917 */
4918 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4919 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004920 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 long startTime = SystemClock.uptimeMillis();
4922 long keyDispatchingTimeout = 5 * 1000;
4923 long waitedFor = 0;
4924
4925 while (true) {
4926 // Figure out which window we care about. It is either the
4927 // last window we are waiting to have process the event or,
4928 // if none, then the next window we think the event should go
4929 // to. Note: we retrieve mLastWin outside of the lock, so
4930 // it may change before we lock. Thus we must check it again.
4931 WindowState targetWin = mLastWin;
4932 boolean targetIsNew = targetWin == null;
4933 if (DEBUG_INPUT) Log.v(
4934 TAG, "waitForLastKey: mFinished=" + mFinished +
4935 ", mLastWin=" + mLastWin);
4936 if (targetIsNew) {
4937 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004938 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (target == SKIP_TARGET_TOKEN) {
4940 // The user has pressed a special key, and we are
4941 // dropping all pending events before it.
4942 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
4943 + " " + nextMotion);
4944 return null;
4945 }
4946 if (target == CONSUMED_EVENT_TOKEN) {
4947 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
4948 + " " + nextMotion);
4949 return target;
4950 }
4951 targetWin = (WindowState)target;
4952 }
Romain Guy06882f82009-06-10 13:36:04 -07004953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07004955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 // Now: is it okay to send the next event to this window?
4957 synchronized (this) {
4958 // First: did we come here based on the last window not
4959 // being null, but it changed by the time we got here?
4960 // If so, try again.
4961 if (!targetIsNew && mLastWin == null) {
4962 continue;
4963 }
Romain Guy06882f82009-06-10 13:36:04 -07004964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 // We never dispatch events if not finished with the
4966 // last one, or the display is frozen.
4967 if (mFinished && !mDisplayFrozen) {
4968 // If event dispatching is disabled, then we
4969 // just consume the events.
4970 if (!mEventDispatching) {
4971 if (DEBUG_INPUT) Log.v(TAG,
4972 "Skipping event; dispatching disabled: "
4973 + nextKey + " " + nextMotion);
4974 return null;
4975 }
4976 if (targetWin != null) {
4977 // If this is a new target, and that target is not
4978 // paused or unresponsive, then all looks good to
4979 // handle the event.
4980 if (targetIsNew && !targetWin.mToken.paused) {
4981 return targetWin;
4982 }
Romain Guy06882f82009-06-10 13:36:04 -07004983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 // If we didn't find a target window, and there is no
4985 // focused app window, then just eat the events.
4986 } else if (mFocusedApp == null) {
4987 if (DEBUG_INPUT) Log.v(TAG,
4988 "Skipping event; no focused app: "
4989 + nextKey + " " + nextMotion);
4990 return null;
4991 }
4992 }
Romain Guy06882f82009-06-10 13:36:04 -07004993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 if (DEBUG_INPUT) Log.v(
4995 TAG, "Waiting for last key in " + mLastBinder
4996 + " target=" + targetWin
4997 + " mFinished=" + mFinished
4998 + " mDisplayFrozen=" + mDisplayFrozen
4999 + " targetIsNew=" + targetIsNew
5000 + " paused="
5001 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005002 + " mFocusedApp=" + mFocusedApp
5003 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 targetApp = targetWin != null
5006 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 long curTimeout = keyDispatchingTimeout;
5009 if (mTimeToSwitch != 0) {
5010 long now = SystemClock.uptimeMillis();
5011 if (mTimeToSwitch <= now) {
5012 // If an app switch key has been pressed, and we have
5013 // waited too long for the current app to finish
5014 // processing keys, then wait no more!
5015 doFinishedKeyLocked(true);
5016 continue;
5017 }
5018 long switchTimeout = mTimeToSwitch - now;
5019 if (curTimeout > switchTimeout) {
5020 curTimeout = switchTimeout;
5021 }
5022 }
Romain Guy06882f82009-06-10 13:36:04 -07005023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024 try {
5025 // after that continue
5026 // processing keys, so we don't get stuck.
5027 if (DEBUG_INPUT) Log.v(
5028 TAG, "Waiting for key dispatch: " + curTimeout);
5029 wait(curTimeout);
5030 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5031 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005032 + startTime + " switchTime=" + mTimeToSwitch
5033 + " target=" + targetWin + " mLW=" + mLastWin
5034 + " mLB=" + mLastBinder + " fin=" + mFinished
5035 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 } catch (InterruptedException e) {
5037 }
5038 }
5039
5040 // If we were frozen during configuration change, restart the
5041 // timeout checks from now; otherwise look at whether we timed
5042 // out before awakening.
5043 if (mWasFrozen) {
5044 waitedFor = 0;
5045 mWasFrozen = false;
5046 } else {
5047 waitedFor = SystemClock.uptimeMillis() - startTime;
5048 }
5049
5050 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5051 IApplicationToken at = null;
5052 synchronized (this) {
5053 Log.w(TAG, "Key dispatching timed out sending to " +
5054 (targetWin != null ? targetWin.mAttrs.getTitle()
5055 : "<null>"));
5056 // NOSHIP debugging
5057 Log.w(TAG, "Dispatch state: " + mDispatchState);
5058 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5059 // END NOSHIP
5060 //dump();
5061 if (targetWin != null) {
5062 at = targetWin.getAppToken();
5063 } else if (targetApp != null) {
5064 at = targetApp.appToken;
5065 }
5066 }
5067
5068 boolean abort = true;
5069 if (at != null) {
5070 try {
5071 long timeout = at.getKeyDispatchingTimeout();
5072 if (timeout > waitedFor) {
5073 // we did not wait the proper amount of time for this application.
5074 // set the timeout to be the real timeout and wait again.
5075 keyDispatchingTimeout = timeout - waitedFor;
5076 continue;
5077 } else {
5078 abort = at.keyDispatchingTimedOut();
5079 }
5080 } catch (RemoteException ex) {
5081 }
5082 }
5083
5084 synchronized (this) {
5085 if (abort && (mLastWin == targetWin || targetWin == null)) {
5086 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005087 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 if (DEBUG_INPUT) Log.v(TAG,
5089 "Window " + mLastWin +
5090 " timed out on key input");
5091 if (mLastWin.mToken.paused) {
5092 Log.w(TAG, "Un-pausing dispatching to this window");
5093 mLastWin.mToken.paused = false;
5094 }
5095 }
5096 if (mMotionTarget == targetWin) {
5097 mMotionTarget = null;
5098 }
5099 mLastWin = null;
5100 mLastBinder = null;
5101 if (failIfTimeout || targetWin == null) {
5102 return null;
5103 }
5104 } else {
5105 Log.w(TAG, "Continuing to wait for key to be dispatched");
5106 startTime = SystemClock.uptimeMillis();
5107 }
5108 }
5109 }
5110 }
5111 }
Romain Guy06882f82009-06-10 13:36:04 -07005112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005114 MotionEvent nextMotion, boolean isPointerEvent,
5115 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 if (nextKey != null) {
5119 // Find the target window for a normal key event.
5120 final int keycode = nextKey.getKeyCode();
5121 final int repeatCount = nextKey.getRepeatCount();
5122 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5123 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005126 if (callingUid == 0 ||
5127 mContext.checkPermission(
5128 android.Manifest.permission.INJECT_EVENTS,
5129 callingPid, callingUid)
5130 == PackageManager.PERMISSION_GRANTED) {
5131 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005132 nextKey.getMetaState(), down, repeatCount,
5133 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 Log.w(TAG, "Event timeout during app switch: dropping "
5136 + nextKey);
5137 return SKIP_TARGET_TOKEN;
5138 }
Romain Guy06882f82009-06-10 13:36:04 -07005139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 WindowState focus = null;
5143 synchronized(mWindowMap) {
5144 focus = getFocusedWindowLocked();
5145 }
Romain Guy06882f82009-06-10 13:36:04 -07005146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005148
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005149 if (callingUid == 0 ||
5150 (focus != null && callingUid == focus.mSession.mUid) ||
5151 mContext.checkPermission(
5152 android.Manifest.permission.INJECT_EVENTS,
5153 callingPid, callingUid)
5154 == PackageManager.PERMISSION_GRANTED) {
5155 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005156 keycode, nextKey.getMetaState(), down, repeatCount,
5157 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005158 return CONSUMED_EVENT_TOKEN;
5159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
Romain Guy06882f82009-06-10 13:36:04 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 } else if (!isPointerEvent) {
5165 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5166 if (!dispatch) {
5167 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5168 + nextMotion);
5169 return SKIP_TARGET_TOKEN;
5170 }
Romain Guy06882f82009-06-10 13:36:04 -07005171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 WindowState focus = null;
5173 synchronized(mWindowMap) {
5174 focus = getFocusedWindowLocked();
5175 }
Romain Guy06882f82009-06-10 13:36:04 -07005176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5178 return focus;
5179 }
Romain Guy06882f82009-06-10 13:36:04 -07005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 if (nextMotion == null) {
5182 return SKIP_TARGET_TOKEN;
5183 }
Romain Guy06882f82009-06-10 13:36:04 -07005184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5186 KeyEvent.KEYCODE_UNKNOWN);
5187 if (!dispatch) {
5188 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5189 + nextMotion);
5190 return SKIP_TARGET_TOKEN;
5191 }
Romain Guy06882f82009-06-10 13:36:04 -07005192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 // Find the target window for a pointer event.
5194 int action = nextMotion.getAction();
5195 final float xf = nextMotion.getX();
5196 final float yf = nextMotion.getY();
5197 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 final boolean screenWasOff = qev != null
5200 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 synchronized(mWindowMap) {
5205 synchronized (this) {
5206 if (action == MotionEvent.ACTION_DOWN) {
5207 if (mMotionTarget != null) {
5208 // this is weird, we got a pen down, but we thought it was
5209 // already down!
5210 // XXX: We should probably send an ACTION_UP to the current
5211 // target.
5212 Log.w(TAG, "Pointer down received while already down in: "
5213 + mMotionTarget);
5214 mMotionTarget = null;
5215 }
Romain Guy06882f82009-06-10 13:36:04 -07005216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 // ACTION_DOWN is special, because we need to lock next events to
5218 // the window we'll land onto.
5219 final int x = (int)xf;
5220 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 final ArrayList windows = mWindows;
5223 final int N = windows.size();
5224 WindowState topErrWindow = null;
5225 final Rect tmpRect = mTempRect;
5226 for (int i=N-1; i>=0; i--) {
5227 WindowState child = (WindowState)windows.get(i);
5228 //Log.i(TAG, "Checking dispatch to: " + child);
5229 final int flags = child.mAttrs.flags;
5230 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5231 if (topErrWindow == null) {
5232 topErrWindow = child;
5233 }
5234 }
5235 if (!child.isVisibleLw()) {
5236 //Log.i(TAG, "Not visible!");
5237 continue;
5238 }
5239 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5240 //Log.i(TAG, "Not touchable!");
5241 if ((flags & WindowManager.LayoutParams
5242 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5243 child.mNextOutsideTouch = mOutsideTouchTargets;
5244 mOutsideTouchTargets = child;
5245 }
5246 continue;
5247 }
5248 tmpRect.set(child.mFrame);
5249 if (child.mTouchableInsets == ViewTreeObserver
5250 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5251 // The touch is inside of the window if it is
5252 // inside the frame, AND the content part of that
5253 // frame that was given by the application.
5254 tmpRect.left += child.mGivenContentInsets.left;
5255 tmpRect.top += child.mGivenContentInsets.top;
5256 tmpRect.right -= child.mGivenContentInsets.right;
5257 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5258 } else if (child.mTouchableInsets == ViewTreeObserver
5259 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5260 // The touch is inside of the window if it is
5261 // inside the frame, AND the visible part of that
5262 // frame that was given by the application.
5263 tmpRect.left += child.mGivenVisibleInsets.left;
5264 tmpRect.top += child.mGivenVisibleInsets.top;
5265 tmpRect.right -= child.mGivenVisibleInsets.right;
5266 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5267 }
5268 final int touchFlags = flags &
5269 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5270 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5271 if (tmpRect.contains(x, y) || touchFlags == 0) {
5272 //Log.i(TAG, "Using this target!");
5273 if (!screenWasOff || (flags &
5274 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5275 mMotionTarget = child;
5276 } else {
5277 //Log.i(TAG, "Waking, skip!");
5278 mMotionTarget = null;
5279 }
5280 break;
5281 }
Romain Guy06882f82009-06-10 13:36:04 -07005282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 if ((flags & WindowManager.LayoutParams
5284 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5285 child.mNextOutsideTouch = mOutsideTouchTargets;
5286 mOutsideTouchTargets = child;
5287 //Log.i(TAG, "Adding to outside target list: " + child);
5288 }
5289 }
5290
5291 // if there's an error window but it's not accepting
5292 // focus (typically because it is not yet visible) just
5293 // wait for it -- any other focused window may in fact
5294 // be in ANR state.
5295 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5296 mMotionTarget = null;
5297 }
5298 }
Romain Guy06882f82009-06-10 13:36:04 -07005299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 target = mMotionTarget;
5301 }
5302 }
Romain Guy06882f82009-06-10 13:36:04 -07005303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 // Pointer events are a little different -- if there isn't a
5307 // target found for any event, then just drop it.
5308 return target != null ? target : SKIP_TARGET_TOKEN;
5309 }
Romain Guy06882f82009-06-10 13:36:04 -07005310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 boolean checkShouldDispatchKey(int keycode) {
5312 synchronized (this) {
5313 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5314 mTimeToSwitch = 0;
5315 return true;
5316 }
5317 if (mTimeToSwitch != 0
5318 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5319 return false;
5320 }
5321 return true;
5322 }
5323 }
Romain Guy06882f82009-06-10 13:36:04 -07005324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 void bindTargetWindowLocked(WindowState win,
5326 int pendingWhat, QueuedEvent pendingMotion) {
5327 synchronized (this) {
5328 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5329 }
5330 }
Romain Guy06882f82009-06-10 13:36:04 -07005331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 void bindTargetWindowLocked(WindowState win) {
5333 synchronized (this) {
5334 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5335 }
5336 }
5337
5338 void bindTargetWindowLockedLocked(WindowState win,
5339 int pendingWhat, QueuedEvent pendingMotion) {
5340 mLastWin = win;
5341 mLastBinder = win.mClient.asBinder();
5342 mFinished = false;
5343 if (pendingMotion != null) {
5344 final Session s = win.mSession;
5345 if (pendingWhat == RETURN_PENDING_POINTER) {
5346 releasePendingPointerLocked(s);
5347 s.mPendingPointerMove = pendingMotion;
5348 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005349 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 "bindTargetToWindow " + s.mPendingPointerMove);
5351 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5352 releasePendingTrackballLocked(s);
5353 s.mPendingTrackballMove = pendingMotion;
5354 s.mPendingTrackballWindow = win;
5355 }
5356 }
5357 }
Romain Guy06882f82009-06-10 13:36:04 -07005358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 void releasePendingPointerLocked(Session s) {
5360 if (DEBUG_INPUT) Log.v(TAG,
5361 "releasePendingPointer " + s.mPendingPointerMove);
5362 if (s.mPendingPointerMove != null) {
5363 mQueue.recycleEvent(s.mPendingPointerMove);
5364 s.mPendingPointerMove = null;
5365 }
5366 }
Romain Guy06882f82009-06-10 13:36:04 -07005367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 void releasePendingTrackballLocked(Session s) {
5369 if (s.mPendingTrackballMove != null) {
5370 mQueue.recycleEvent(s.mPendingTrackballMove);
5371 s.mPendingTrackballMove = null;
5372 }
5373 }
Romain Guy06882f82009-06-10 13:36:04 -07005374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5376 int returnWhat) {
5377 if (DEBUG_INPUT) Log.v(
5378 TAG, "finishedKey: client=" + client + ", force=" + force);
5379
5380 if (client == null) {
5381 return null;
5382 }
5383
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005384 MotionEvent res = null;
5385 QueuedEvent qev = null;
5386 WindowState win = null;
5387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 synchronized (this) {
5389 if (DEBUG_INPUT) Log.v(
5390 TAG, "finishedKey: client=" + client.asBinder()
5391 + ", force=" + force + ", last=" + mLastBinder
5392 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 if (returnWhat == RETURN_PENDING_POINTER) {
5395 qev = session.mPendingPointerMove;
5396 win = session.mPendingPointerWindow;
5397 session.mPendingPointerMove = null;
5398 session.mPendingPointerWindow = null;
5399 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5400 qev = session.mPendingTrackballMove;
5401 win = session.mPendingTrackballWindow;
5402 session.mPendingTrackballMove = null;
5403 session.mPendingTrackballWindow = null;
5404 }
Romain Guy06882f82009-06-10 13:36:04 -07005405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 if (mLastBinder == client.asBinder()) {
5407 if (DEBUG_INPUT) Log.v(
5408 TAG, "finishedKey: last paused="
5409 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5410 if (mLastWin != null && (!mLastWin.mToken.paused || force
5411 || !mEventDispatching)) {
5412 doFinishedKeyLocked(false);
5413 } else {
5414 // Make sure to wake up anyone currently waiting to
5415 // dispatch a key, so they can re-evaluate their
5416 // current situation.
5417 mFinished = true;
5418 notifyAll();
5419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 }
Romain Guy06882f82009-06-10 13:36:04 -07005421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005423 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 if (DEBUG_INPUT) Log.v(TAG,
5425 "Returning pending motion: " + res);
5426 mQueue.recycleEvent(qev);
5427 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5428 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005432
5433 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5434 synchronized (mWindowMap) {
5435 if (mWallpaperTarget == win) {
5436 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5437 }
5438 }
5439 }
5440
5441 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 }
5443
5444 void tickle() {
5445 synchronized (this) {
5446 notifyAll();
5447 }
5448 }
Romain Guy06882f82009-06-10 13:36:04 -07005449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 void handleNewWindowLocked(WindowState newWindow) {
5451 if (!newWindow.canReceiveKeys()) {
5452 return;
5453 }
5454 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005455 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 TAG, "New key dispatch window: win="
5457 + newWindow.mClient.asBinder()
5458 + ", last=" + mLastBinder
5459 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5460 + "), finished=" + mFinished + ", paused="
5461 + newWindow.mToken.paused);
5462
5463 // Displaying a window implicitly causes dispatching to
5464 // be unpaused. (This is to protect against bugs if someone
5465 // pauses dispatching but forgets to resume.)
5466 newWindow.mToken.paused = false;
5467
5468 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469
5470 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5471 if (DEBUG_INPUT) Log.v(TAG,
5472 "New SYSTEM_ERROR window; resetting state");
5473 mLastWin = null;
5474 mLastBinder = null;
5475 mMotionTarget = null;
5476 mFinished = true;
5477 } else if (mLastWin != null) {
5478 // If the new window is above the window we are
5479 // waiting on, then stop waiting and let key dispatching
5480 // start on the new guy.
5481 if (DEBUG_INPUT) Log.v(
5482 TAG, "Last win layer=" + mLastWin.mLayer
5483 + ", new win layer=" + newWindow.mLayer);
5484 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005485 // The new window is above the old; finish pending input to the last
5486 // window and start directing it to the new one.
5487 mLastWin.mToken.paused = false;
5488 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005490 // Either the new window is lower, so there is no need to wake key waiters,
5491 // or we just finished key input to the previous window, which implicitly
5492 // notified the key waiters. In both cases, we don't need to issue the
5493 // notification here.
5494 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005495 }
5496
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005497 // Now that we've put a new window state in place, make the event waiter
5498 // take notice and retarget its attentions.
5499 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 }
5501 }
5502
5503 void pauseDispatchingLocked(WindowToken token) {
5504 synchronized (this)
5505 {
5506 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5507 token.paused = true;
5508
5509 /*
5510 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5511 mPaused = true;
5512 } else {
5513 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005514 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005516 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005518 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 }
5520 }
5521 */
5522 }
5523 }
5524
5525 void resumeDispatchingLocked(WindowToken token) {
5526 synchronized (this) {
5527 if (token.paused) {
5528 if (DEBUG_INPUT) Log.v(
5529 TAG, "Resuming WindowToken " + token
5530 + ", last=" + mLastBinder
5531 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5532 + "), finished=" + mFinished + ", paused="
5533 + token.paused);
5534 token.paused = false;
5535 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5536 doFinishedKeyLocked(true);
5537 } else {
5538 notifyAll();
5539 }
5540 }
5541 }
5542 }
5543
5544 void setEventDispatchingLocked(boolean enabled) {
5545 synchronized (this) {
5546 mEventDispatching = enabled;
5547 notifyAll();
5548 }
5549 }
Romain Guy06882f82009-06-10 13:36:04 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 void appSwitchComing() {
5552 synchronized (this) {
5553 // Don't wait for more than .5 seconds for app to finish
5554 // processing the pending events.
5555 long now = SystemClock.uptimeMillis() + 500;
5556 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5557 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5558 mTimeToSwitch = now;
5559 }
5560 notifyAll();
5561 }
5562 }
Romain Guy06882f82009-06-10 13:36:04 -07005563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 private final void doFinishedKeyLocked(boolean doRecycle) {
5565 if (mLastWin != null) {
5566 releasePendingPointerLocked(mLastWin.mSession);
5567 releasePendingTrackballLocked(mLastWin.mSession);
5568 }
Romain Guy06882f82009-06-10 13:36:04 -07005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 if (mLastWin == null || !mLastWin.mToken.paused
5571 || !mLastWin.isVisibleLw()) {
5572 // If the current window has been paused, we aren't -really-
5573 // finished... so let the waiters still wait.
5574 mLastWin = null;
5575 mLastBinder = null;
5576 }
5577 mFinished = true;
5578 notifyAll();
5579 }
5580 }
5581
5582 private class KeyQ extends KeyInputQueue
5583 implements KeyInputQueue.FilterCallback {
5584 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005587 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5589 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5590 "KEEP_SCREEN_ON_FLAG");
5591 mHoldingScreen.setReferenceCounted(false);
5592 }
5593
5594 @Override
5595 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5596 if (mPolicy.preprocessInputEventTq(event)) {
5597 return true;
5598 }
Romain Guy06882f82009-06-10 13:36:04 -07005599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 switch (event.type) {
5601 case RawInputEvent.EV_KEY: {
5602 // XXX begin hack
5603 if (DEBUG) {
5604 if (event.keycode == KeyEvent.KEYCODE_G) {
5605 if (event.value != 0) {
5606 // G down
5607 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5608 }
5609 return false;
5610 }
5611 if (event.keycode == KeyEvent.KEYCODE_D) {
5612 if (event.value != 0) {
5613 //dump();
5614 }
5615 return false;
5616 }
5617 }
5618 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 boolean screenIsOff = !mPowerManager.screenIsOn();
5621 boolean screenIsDim = !mPowerManager.screenIsBright();
5622 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5625 mPowerManager.goToSleep(event.when);
5626 }
5627
5628 if (screenIsOff) {
5629 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5630 }
5631 if (screenIsDim) {
5632 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5633 }
5634 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5635 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005636 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 }
Romain Guy06882f82009-06-10 13:36:04 -07005638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5640 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5641 filterQueue(this);
5642 mKeyWaiter.appSwitchComing();
5643 }
5644 return true;
5645 } else {
5646 return false;
5647 }
5648 }
Romain Guy06882f82009-06-10 13:36:04 -07005649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 case RawInputEvent.EV_REL: {
5651 boolean screenIsOff = !mPowerManager.screenIsOn();
5652 boolean screenIsDim = !mPowerManager.screenIsBright();
5653 if (screenIsOff) {
5654 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5655 device.classes, event)) {
5656 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5657 return false;
5658 }
5659 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5660 }
5661 if (screenIsDim) {
5662 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5663 }
5664 return true;
5665 }
Romain Guy06882f82009-06-10 13:36:04 -07005666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 case RawInputEvent.EV_ABS: {
5668 boolean screenIsOff = !mPowerManager.screenIsOn();
5669 boolean screenIsDim = !mPowerManager.screenIsBright();
5670 if (screenIsOff) {
5671 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5672 device.classes, event)) {
5673 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5674 return false;
5675 }
5676 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5677 }
5678 if (screenIsDim) {
5679 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5680 }
5681 return true;
5682 }
Romain Guy06882f82009-06-10 13:36:04 -07005683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 default:
5685 return true;
5686 }
5687 }
5688
5689 public int filterEvent(QueuedEvent ev) {
5690 switch (ev.classType) {
5691 case RawInputEvent.CLASS_KEYBOARD:
5692 KeyEvent ke = (KeyEvent)ev.event;
5693 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5694 Log.w(TAG, "Dropping movement key during app switch: "
5695 + ke.getKeyCode() + ", action=" + ke.getAction());
5696 return FILTER_REMOVE;
5697 }
5698 return FILTER_ABORT;
5699 default:
5700 return FILTER_KEEP;
5701 }
5702 }
Romain Guy06882f82009-06-10 13:36:04 -07005703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 /**
5705 * Must be called with the main window manager lock held.
5706 */
5707 void setHoldScreenLocked(boolean holding) {
5708 boolean state = mHoldingScreen.isHeld();
5709 if (holding != state) {
5710 if (holding) {
5711 mHoldingScreen.acquire();
5712 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005713 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 mHoldingScreen.release();
5715 }
5716 }
5717 }
Michael Chan53071d62009-05-13 17:29:48 -07005718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719
5720 public boolean detectSafeMode() {
5721 mSafeMode = mPolicy.detectSafeMode();
5722 return mSafeMode;
5723 }
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 public void systemReady() {
5726 mPolicy.systemReady();
5727 }
Romain Guy06882f82009-06-10 13:36:04 -07005728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 private final class InputDispatcherThread extends Thread {
5730 // Time to wait when there is nothing to do: 9999 seconds.
5731 static final int LONG_WAIT=9999*1000;
5732
5733 public InputDispatcherThread() {
5734 super("InputDispatcher");
5735 }
Romain Guy06882f82009-06-10 13:36:04 -07005736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 @Override
5738 public void run() {
5739 while (true) {
5740 try {
5741 process();
5742 } catch (Exception e) {
5743 Log.e(TAG, "Exception in input dispatcher", e);
5744 }
5745 }
5746 }
Romain Guy06882f82009-06-10 13:36:04 -07005747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 private void process() {
5749 android.os.Process.setThreadPriority(
5750 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005752 // The last key event we saw
5753 KeyEvent lastKey = null;
5754
5755 // Last keydown time for auto-repeating keys
5756 long lastKeyTime = SystemClock.uptimeMillis();
5757 long nextKeyTime = lastKeyTime+LONG_WAIT;
5758
Romain Guy06882f82009-06-10 13:36:04 -07005759 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 int keyRepeatCount = 0;
5761
5762 // Need to report that configuration has changed?
5763 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 while (true) {
5766 long curTime = SystemClock.uptimeMillis();
5767
5768 if (DEBUG_INPUT) Log.v(
5769 TAG, "Waiting for next key: now=" + curTime
5770 + ", repeat @ " + nextKeyTime);
5771
5772 // Retrieve next event, waiting only as long as the next
5773 // repeat timeout. If the configuration has changed, then
5774 // don't wait at all -- we'll report the change as soon as
5775 // we have processed all events.
5776 QueuedEvent ev = mQueue.getEvent(
5777 (int)((!configChanged && curTime < nextKeyTime)
5778 ? (nextKeyTime-curTime) : 0));
5779
5780 if (DEBUG_INPUT && ev != null) Log.v(
5781 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5782
Michael Chan53071d62009-05-13 17:29:48 -07005783 if (MEASURE_LATENCY) {
5784 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5785 }
5786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 try {
5788 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005789 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 int eventType;
5791 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5792 eventType = eventType((MotionEvent)ev.event);
5793 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5794 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5795 eventType = LocalPowerManager.BUTTON_EVENT;
5796 } else {
5797 eventType = LocalPowerManager.OTHER_EVENT;
5798 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005799 try {
Michael Chan53071d62009-05-13 17:29:48 -07005800 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005801 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005802 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005803 mBatteryStats.noteInputEvent();
5804 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005805 } catch (RemoteException e) {
5806 // Ignore
5807 }
Michael Chane10de972009-05-18 11:24:50 -07005808
5809 if (eventType != TOUCH_EVENT
5810 && eventType != LONG_TOUCH_EVENT
5811 && eventType != CHEEK_EVENT) {
5812 mPowerManager.userActivity(curTime, false,
5813 eventType, false);
5814 } else if (mLastTouchEventType != eventType
5815 || (curTime - mLastUserActivityCallTime)
5816 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5817 mLastUserActivityCallTime = curTime;
5818 mLastTouchEventType = eventType;
5819 mPowerManager.userActivity(curTime, false,
5820 eventType, false);
5821 }
5822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 switch (ev.classType) {
5824 case RawInputEvent.CLASS_KEYBOARD:
5825 KeyEvent ke = (KeyEvent)ev.event;
5826 if (ke.isDown()) {
5827 lastKey = ke;
5828 keyRepeatCount = 0;
5829 lastKeyTime = curTime;
5830 nextKeyTime = lastKeyTime
5831 + KEY_REPEAT_FIRST_DELAY;
5832 if (DEBUG_INPUT) Log.v(
5833 TAG, "Received key down: first repeat @ "
5834 + nextKeyTime);
5835 } else {
5836 lastKey = null;
5837 // Arbitrary long timeout.
5838 lastKeyTime = curTime;
5839 nextKeyTime = curTime + LONG_WAIT;
5840 if (DEBUG_INPUT) Log.v(
5841 TAG, "Received key up: ignore repeat @ "
5842 + nextKeyTime);
5843 }
5844 dispatchKey((KeyEvent)ev.event, 0, 0);
5845 mQueue.recycleEvent(ev);
5846 break;
5847 case RawInputEvent.CLASS_TOUCHSCREEN:
5848 //Log.i(TAG, "Read next event " + ev);
5849 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5850 break;
5851 case RawInputEvent.CLASS_TRACKBALL:
5852 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5853 break;
5854 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5855 configChanged = true;
5856 break;
5857 default:
5858 mQueue.recycleEvent(ev);
5859 break;
5860 }
Romain Guy06882f82009-06-10 13:36:04 -07005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 } else if (configChanged) {
5863 configChanged = false;
5864 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 } else if (lastKey != null) {
5867 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 // Timeout occurred while key was down. If it is at or
5870 // past the key repeat time, dispatch the repeat.
5871 if (DEBUG_INPUT) Log.v(
5872 TAG, "Key timeout: repeat=" + nextKeyTime
5873 + ", now=" + curTime);
5874 if (curTime < nextKeyTime) {
5875 continue;
5876 }
Romain Guy06882f82009-06-10 13:36:04 -07005877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 lastKeyTime = nextKeyTime;
5879 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5880 keyRepeatCount++;
5881 if (DEBUG_INPUT) Log.v(
5882 TAG, "Key repeat: count=" + keyRepeatCount
5883 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005884 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 } else {
5887 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 lastKeyTime = curTime;
5890 nextKeyTime = curTime + LONG_WAIT;
5891 }
Romain Guy06882f82009-06-10 13:36:04 -07005892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 } catch (Exception e) {
5894 Log.e(TAG,
5895 "Input thread received uncaught exception: " + e, e);
5896 }
5897 }
5898 }
5899 }
5900
5901 // -------------------------------------------------------------
5902 // Client Session State
5903 // -------------------------------------------------------------
5904
5905 private final class Session extends IWindowSession.Stub
5906 implements IBinder.DeathRecipient {
5907 final IInputMethodClient mClient;
5908 final IInputContext mInputContext;
5909 final int mUid;
5910 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005911 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 SurfaceSession mSurfaceSession;
5913 int mNumWindow = 0;
5914 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 /**
5917 * Current pointer move event being dispatched to client window... must
5918 * hold key lock to access.
5919 */
5920 QueuedEvent mPendingPointerMove;
5921 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 /**
5924 * Current trackball move event being dispatched to client window... must
5925 * hold key lock to access.
5926 */
5927 QueuedEvent mPendingTrackballMove;
5928 WindowState mPendingTrackballWindow;
5929
5930 public Session(IInputMethodClient client, IInputContext inputContext) {
5931 mClient = client;
5932 mInputContext = inputContext;
5933 mUid = Binder.getCallingUid();
5934 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005935 StringBuilder sb = new StringBuilder();
5936 sb.append("Session{");
5937 sb.append(Integer.toHexString(System.identityHashCode(this)));
5938 sb.append(" uid ");
5939 sb.append(mUid);
5940 sb.append("}");
5941 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 synchronized (mWindowMap) {
5944 if (mInputMethodManager == null && mHaveInputMethods) {
5945 IBinder b = ServiceManager.getService(
5946 Context.INPUT_METHOD_SERVICE);
5947 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
5948 }
5949 }
5950 long ident = Binder.clearCallingIdentity();
5951 try {
5952 // Note: it is safe to call in to the input method manager
5953 // here because we are not holding our lock.
5954 if (mInputMethodManager != null) {
5955 mInputMethodManager.addClient(client, inputContext,
5956 mUid, mPid);
5957 } else {
5958 client.setUsingInputMethod(false);
5959 }
5960 client.asBinder().linkToDeath(this, 0);
5961 } catch (RemoteException e) {
5962 // The caller has died, so we can just forget about this.
5963 try {
5964 if (mInputMethodManager != null) {
5965 mInputMethodManager.removeClient(client);
5966 }
5967 } catch (RemoteException ee) {
5968 }
5969 } finally {
5970 Binder.restoreCallingIdentity(ident);
5971 }
5972 }
Romain Guy06882f82009-06-10 13:36:04 -07005973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 @Override
5975 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
5976 throws RemoteException {
5977 try {
5978 return super.onTransact(code, data, reply, flags);
5979 } catch (RuntimeException e) {
5980 // Log all 'real' exceptions thrown to the caller
5981 if (!(e instanceof SecurityException)) {
5982 Log.e(TAG, "Window Session Crash", e);
5983 }
5984 throw e;
5985 }
5986 }
5987
5988 public void binderDied() {
5989 // Note: it is safe to call in to the input method manager
5990 // here because we are not holding our lock.
5991 try {
5992 if (mInputMethodManager != null) {
5993 mInputMethodManager.removeClient(mClient);
5994 }
5995 } catch (RemoteException e) {
5996 }
5997 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07005998 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 mClientDead = true;
6000 killSessionLocked();
6001 }
6002 }
6003
6004 public int add(IWindow window, WindowManager.LayoutParams attrs,
6005 int viewVisibility, Rect outContentInsets) {
6006 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6007 }
Romain Guy06882f82009-06-10 13:36:04 -07006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 public void remove(IWindow window) {
6010 removeWindow(this, window);
6011 }
Romain Guy06882f82009-06-10 13:36:04 -07006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6014 int requestedWidth, int requestedHeight, int viewFlags,
6015 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6016 Rect outVisibleInsets, Surface outSurface) {
6017 return relayoutWindow(this, window, attrs,
6018 requestedWidth, requestedHeight, viewFlags, insetsPending,
6019 outFrame, outContentInsets, outVisibleInsets, outSurface);
6020 }
Romain Guy06882f82009-06-10 13:36:04 -07006021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 public void setTransparentRegion(IWindow window, Region region) {
6023 setTransparentRegionWindow(this, window, region);
6024 }
Romain Guy06882f82009-06-10 13:36:04 -07006025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 public void setInsets(IWindow window, int touchableInsets,
6027 Rect contentInsets, Rect visibleInsets) {
6028 setInsetsWindow(this, window, touchableInsets, contentInsets,
6029 visibleInsets);
6030 }
Romain Guy06882f82009-06-10 13:36:04 -07006031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6033 getWindowDisplayFrame(this, window, outDisplayFrame);
6034 }
Romain Guy06882f82009-06-10 13:36:04 -07006035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 public void finishDrawing(IWindow window) {
6037 if (localLOGV) Log.v(
6038 TAG, "IWindow finishDrawing called for " + window);
6039 finishDrawingWindow(this, window);
6040 }
6041
6042 public void finishKey(IWindow window) {
6043 if (localLOGV) Log.v(
6044 TAG, "IWindow finishKey called for " + window);
6045 mKeyWaiter.finishedKey(this, window, false,
6046 KeyWaiter.RETURN_NOTHING);
6047 }
6048
6049 public MotionEvent getPendingPointerMove(IWindow window) {
6050 if (localLOGV) Log.v(
6051 TAG, "IWindow getPendingMotionEvent called for " + window);
6052 return mKeyWaiter.finishedKey(this, window, false,
6053 KeyWaiter.RETURN_PENDING_POINTER);
6054 }
Romain Guy06882f82009-06-10 13:36:04 -07006055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 public MotionEvent getPendingTrackballMove(IWindow window) {
6057 if (localLOGV) Log.v(
6058 TAG, "IWindow getPendingMotionEvent called for " + window);
6059 return mKeyWaiter.finishedKey(this, window, false,
6060 KeyWaiter.RETURN_PENDING_TRACKBALL);
6061 }
6062
6063 public void setInTouchMode(boolean mode) {
6064 synchronized(mWindowMap) {
6065 mInTouchMode = mode;
6066 }
6067 }
6068
6069 public boolean getInTouchMode() {
6070 synchronized(mWindowMap) {
6071 return mInTouchMode;
6072 }
6073 }
6074
6075 public boolean performHapticFeedback(IWindow window, int effectId,
6076 boolean always) {
6077 synchronized(mWindowMap) {
6078 long ident = Binder.clearCallingIdentity();
6079 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006080 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 windowForClientLocked(this, window), effectId, always);
6082 } finally {
6083 Binder.restoreCallingIdentity(ident);
6084 }
6085 }
6086 }
Romain Guy06882f82009-06-10 13:36:04 -07006087
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006088 public void setWallpaperPosition(IBinder window, float x, float y) {
6089 synchronized(mWindowMap) {
6090 long ident = Binder.clearCallingIdentity();
6091 try {
6092 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6093 x, y);
6094 } finally {
6095 Binder.restoreCallingIdentity(ident);
6096 }
6097 }
6098 }
6099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 void windowAddedLocked() {
6101 if (mSurfaceSession == null) {
6102 if (localLOGV) Log.v(
6103 TAG, "First window added to " + this + ", creating SurfaceSession");
6104 mSurfaceSession = new SurfaceSession();
6105 mSessions.add(this);
6106 }
6107 mNumWindow++;
6108 }
6109
6110 void windowRemovedLocked() {
6111 mNumWindow--;
6112 killSessionLocked();
6113 }
Romain Guy06882f82009-06-10 13:36:04 -07006114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 void killSessionLocked() {
6116 if (mNumWindow <= 0 && mClientDead) {
6117 mSessions.remove(this);
6118 if (mSurfaceSession != null) {
6119 if (localLOGV) Log.v(
6120 TAG, "Last window removed from " + this
6121 + ", destroying " + mSurfaceSession);
6122 try {
6123 mSurfaceSession.kill();
6124 } catch (Exception e) {
6125 Log.w(TAG, "Exception thrown when killing surface session "
6126 + mSurfaceSession + " in session " + this
6127 + ": " + e.toString());
6128 }
6129 mSurfaceSession = null;
6130 }
6131 }
6132 }
Romain Guy06882f82009-06-10 13:36:04 -07006133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006135 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6136 pw.print(" mClientDead="); pw.print(mClientDead);
6137 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6138 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6139 pw.print(prefix);
6140 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6141 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6142 }
6143 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6144 pw.print(prefix);
6145 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6146 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 }
6149
6150 @Override
6151 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006152 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 }
6154 }
6155
6156 // -------------------------------------------------------------
6157 // Client Window State
6158 // -------------------------------------------------------------
6159
6160 private final class WindowState implements WindowManagerPolicy.WindowState {
6161 final Session mSession;
6162 final IWindow mClient;
6163 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006164 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 AppWindowToken mAppToken;
6166 AppWindowToken mTargetAppToken;
6167 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6168 final DeathRecipient mDeathRecipient;
6169 final WindowState mAttachedWindow;
6170 final ArrayList mChildWindows = new ArrayList();
6171 final int mBaseLayer;
6172 final int mSubLayer;
6173 final boolean mLayoutAttached;
6174 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006175 final boolean mIsWallpaper;
6176 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 int mViewVisibility;
6178 boolean mPolicyVisibility = true;
6179 boolean mPolicyVisibilityAfterAnim = true;
6180 boolean mAppFreezing;
6181 Surface mSurface;
6182 boolean mAttachedHidden; // is our parent window hidden?
6183 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006184 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 int mRequestedWidth;
6186 int mRequestedHeight;
6187 int mLastRequestedWidth;
6188 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 int mLayer;
6190 int mAnimLayer;
6191 int mLastLayer;
6192 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006193 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194
6195 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 // Actual frame shown on-screen (may be modified by animation)
6198 final Rect mShownFrame = new Rect();
6199 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 /**
6202 * Insets that determine the actually visible area
6203 */
6204 final Rect mVisibleInsets = new Rect();
6205 final Rect mLastVisibleInsets = new Rect();
6206 boolean mVisibleInsetsChanged;
6207
6208 /**
6209 * Insets that are covered by system windows
6210 */
6211 final Rect mContentInsets = new Rect();
6212 final Rect mLastContentInsets = new Rect();
6213 boolean mContentInsetsChanged;
6214
6215 /**
6216 * Set to true if we are waiting for this window to receive its
6217 * given internal insets before laying out other windows based on it.
6218 */
6219 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 /**
6222 * These are the content insets that were given during layout for
6223 * this window, to be applied to windows behind it.
6224 */
6225 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006227 /**
6228 * These are the visible insets that were given during layout for
6229 * this window, to be applied to windows behind it.
6230 */
6231 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 /**
6234 * Flag indicating whether the touchable region should be adjusted by
6235 * the visible insets; if false the area outside the visible insets is
6236 * NOT touchable, so we must use those to adjust the frame during hit
6237 * tests.
6238 */
6239 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 // Current transformation being applied.
6242 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6243 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6244 float mHScale=1, mVScale=1;
6245 float mLastHScale=1, mLastVScale=1;
6246 final Matrix mTmpMatrix = new Matrix();
6247
6248 // "Real" frame that the application sees.
6249 final Rect mFrame = new Rect();
6250 final Rect mLastFrame = new Rect();
6251
6252 final Rect mContainingFrame = new Rect();
6253 final Rect mDisplayFrame = new Rect();
6254 final Rect mContentFrame = new Rect();
6255 final Rect mVisibleFrame = new Rect();
6256
6257 float mShownAlpha = 1;
6258 float mAlpha = 1;
6259 float mLastAlpha = 1;
6260
6261 // Set to true if, when the window gets displayed, it should perform
6262 // an enter animation.
6263 boolean mEnterAnimationPending;
6264
6265 // Currently running animation.
6266 boolean mAnimating;
6267 boolean mLocalAnimating;
6268 Animation mAnimation;
6269 boolean mAnimationIsEntrance;
6270 boolean mHasTransformation;
6271 boolean mHasLocalTransformation;
6272 final Transformation mTransformation = new Transformation();
6273
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006274 // If a window showing a wallpaper: the requested offset for the
6275 // wallpaper; if a wallpaper window: the currently applied offset.
6276 float mWallpaperX = -1;
6277 float mWallpaperY = -1;
6278
6279 // Wallpaper windows: pixels offset based on above variables.
6280 int mXOffset;
6281 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 // This is set after IWindowSession.relayout() has been called at
6284 // least once for the window. It allows us to detect the situation
6285 // where we don't yet have a surface, but should have one soon, so
6286 // we can give the window focus before waiting for the relayout.
6287 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 // This is set after the Surface has been created but before the
6290 // window has been drawn. During this time the surface is hidden.
6291 boolean mDrawPending;
6292
6293 // This is set after the window has finished drawing for the first
6294 // time but before its surface is shown. The surface will be
6295 // displayed when the next layout is run.
6296 boolean mCommitDrawPending;
6297
6298 // This is set during the time after the window's drawing has been
6299 // committed, and before its surface is actually shown. It is used
6300 // to delay showing the surface until all windows in a token are ready
6301 // to be shown.
6302 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 // Set when the window has been shown in the screen the first time.
6305 boolean mHasDrawn;
6306
6307 // Currently running an exit animation?
6308 boolean mExiting;
6309
6310 // Currently on the mDestroySurface list?
6311 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 // Completely remove from window manager after exit animation?
6314 boolean mRemoveOnExit;
6315
6316 // Set when the orientation is changing and this window has not yet
6317 // been updated for the new orientation.
6318 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 // Is this window now (or just being) removed?
6321 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006323 WindowState(Session s, IWindow c, WindowToken token,
6324 WindowState attachedWindow, WindowManager.LayoutParams a,
6325 int viewVisibility) {
6326 mSession = s;
6327 mClient = c;
6328 mToken = token;
6329 mAttrs.copyFrom(a);
6330 mViewVisibility = viewVisibility;
6331 DeathRecipient deathRecipient = new DeathRecipient();
6332 mAlpha = a.alpha;
6333 if (localLOGV) Log.v(
6334 TAG, "Window " + this + " client=" + c.asBinder()
6335 + " token=" + token + " (" + mAttrs.token + ")");
6336 try {
6337 c.asBinder().linkToDeath(deathRecipient, 0);
6338 } catch (RemoteException e) {
6339 mDeathRecipient = null;
6340 mAttachedWindow = null;
6341 mLayoutAttached = false;
6342 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006343 mIsWallpaper = false;
6344 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 mBaseLayer = 0;
6346 mSubLayer = 0;
6347 return;
6348 }
6349 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6352 mAttrs.type <= LAST_SUB_WINDOW)) {
6353 // The multiplier here is to reserve space for multiple
6354 // windows in the same type layer.
6355 mBaseLayer = mPolicy.windowTypeToLayerLw(
6356 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6357 + TYPE_LAYER_OFFSET;
6358 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6359 mAttachedWindow = attachedWindow;
6360 mAttachedWindow.mChildWindows.add(this);
6361 mLayoutAttached = mAttrs.type !=
6362 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6363 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6364 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006365 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6366 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 } else {
6368 // The multiplier here is to reserve space for multiple
6369 // windows in the same type layer.
6370 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6371 * TYPE_LAYER_MULTIPLIER
6372 + TYPE_LAYER_OFFSET;
6373 mSubLayer = 0;
6374 mAttachedWindow = null;
6375 mLayoutAttached = false;
6376 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6377 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006378 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6379 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 }
6381
6382 WindowState appWin = this;
6383 while (appWin.mAttachedWindow != null) {
6384 appWin = mAttachedWindow;
6385 }
6386 WindowToken appToken = appWin.mToken;
6387 while (appToken.appWindowToken == null) {
6388 WindowToken parent = mTokenMap.get(appToken.token);
6389 if (parent == null || appToken == parent) {
6390 break;
6391 }
6392 appToken = parent;
6393 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006394 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 mAppToken = appToken.appWindowToken;
6396
6397 mSurface = null;
6398 mRequestedWidth = 0;
6399 mRequestedHeight = 0;
6400 mLastRequestedWidth = 0;
6401 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006402 mXOffset = 0;
6403 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 mLayer = 0;
6405 mAnimLayer = 0;
6406 mLastLayer = 0;
6407 }
6408
6409 void attach() {
6410 if (localLOGV) Log.v(
6411 TAG, "Attaching " + this + " token=" + mToken
6412 + ", list=" + mToken.windows);
6413 mSession.windowAddedLocked();
6414 }
6415
6416 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6417 mHaveFrame = true;
6418
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006419 final Rect container = mContainingFrame;
6420 container.set(pf);
6421
6422 final Rect display = mDisplayFrame;
6423 display.set(df);
6424
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006425 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006426 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006427 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6428 display.intersect(mCompatibleScreenFrame);
6429 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006430 }
6431
6432 final int pw = container.right - container.left;
6433 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434
6435 int w,h;
6436 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6437 w = mAttrs.width < 0 ? pw : mAttrs.width;
6438 h = mAttrs.height< 0 ? ph : mAttrs.height;
6439 } else {
6440 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6441 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6442 }
Romain Guy06882f82009-06-10 13:36:04 -07006443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 final Rect content = mContentFrame;
6445 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 final Rect visible = mVisibleFrame;
6448 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006451 final int fw = frame.width();
6452 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6455 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6456
6457 Gravity.apply(mAttrs.gravity, w, h, container,
6458 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6459 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6460
6461 //System.out.println("Out: " + mFrame);
6462
6463 // Now make sure the window fits in the overall display.
6464 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 // Make sure the content and visible frames are inside of the
6467 // final window frame.
6468 if (content.left < frame.left) content.left = frame.left;
6469 if (content.top < frame.top) content.top = frame.top;
6470 if (content.right > frame.right) content.right = frame.right;
6471 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6472 if (visible.left < frame.left) visible.left = frame.left;
6473 if (visible.top < frame.top) visible.top = frame.top;
6474 if (visible.right > frame.right) visible.right = frame.right;
6475 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 final Rect contentInsets = mContentInsets;
6478 contentInsets.left = content.left-frame.left;
6479 contentInsets.top = content.top-frame.top;
6480 contentInsets.right = frame.right-content.right;
6481 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 final Rect visibleInsets = mVisibleInsets;
6484 visibleInsets.left = visible.left-frame.left;
6485 visibleInsets.top = visible.top-frame.top;
6486 visibleInsets.right = frame.right-visible.right;
6487 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006488
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006489 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())
6490 && mWallpaperTarget != null) {
6491 updateWallpaperOffsetLocked(mWallpaperTarget, this,
6492 mDisplay.getWidth(), mDisplay.getHeight());
6493 }
6494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 if (localLOGV) {
6496 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6497 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6498 Log.v(TAG, "Resolving (mRequestedWidth="
6499 + mRequestedWidth + ", mRequestedheight="
6500 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6501 + "): frame=" + mFrame.toShortString()
6502 + " ci=" + contentInsets.toShortString()
6503 + " vi=" + visibleInsets.toShortString());
6504 //}
6505 }
6506 }
Romain Guy06882f82009-06-10 13:36:04 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 public Rect getFrameLw() {
6509 return mFrame;
6510 }
6511
6512 public Rect getShownFrameLw() {
6513 return mShownFrame;
6514 }
6515
6516 public Rect getDisplayFrameLw() {
6517 return mDisplayFrame;
6518 }
6519
6520 public Rect getContentFrameLw() {
6521 return mContentFrame;
6522 }
6523
6524 public Rect getVisibleFrameLw() {
6525 return mVisibleFrame;
6526 }
6527
6528 public boolean getGivenInsetsPendingLw() {
6529 return mGivenInsetsPending;
6530 }
6531
6532 public Rect getGivenContentInsetsLw() {
6533 return mGivenContentInsets;
6534 }
Romain Guy06882f82009-06-10 13:36:04 -07006535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 public Rect getGivenVisibleInsetsLw() {
6537 return mGivenVisibleInsets;
6538 }
Romain Guy06882f82009-06-10 13:36:04 -07006539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 public WindowManager.LayoutParams getAttrs() {
6541 return mAttrs;
6542 }
6543
6544 public int getSurfaceLayer() {
6545 return mLayer;
6546 }
Romain Guy06882f82009-06-10 13:36:04 -07006547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 public IApplicationToken getAppToken() {
6549 return mAppToken != null ? mAppToken.appToken : null;
6550 }
6551
6552 public boolean hasAppShownWindows() {
6553 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6554 }
6555
6556 public boolean hasAppStartingIcon() {
6557 return mAppToken != null ? (mAppToken.startingData != null) : false;
6558 }
6559
6560 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6561 return mAppToken != null ? mAppToken.startingWindow : null;
6562 }
6563
6564 public void setAnimation(Animation anim) {
6565 if (localLOGV) Log.v(
6566 TAG, "Setting animation in " + this + ": " + anim);
6567 mAnimating = false;
6568 mLocalAnimating = false;
6569 mAnimation = anim;
6570 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6571 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6572 }
6573
6574 public void clearAnimation() {
6575 if (mAnimation != null) {
6576 mAnimating = true;
6577 mLocalAnimating = false;
6578 mAnimation = null;
6579 }
6580 }
Romain Guy06882f82009-06-10 13:36:04 -07006581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 Surface createSurfaceLocked() {
6583 if (mSurface == null) {
6584 mDrawPending = true;
6585 mCommitDrawPending = false;
6586 mReadyToShow = false;
6587 if (mAppToken != null) {
6588 mAppToken.allDrawn = false;
6589 }
6590
6591 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006592 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006593 flags |= Surface.PUSH_BUFFERS;
6594 }
6595
6596 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6597 flags |= Surface.SECURE;
6598 }
6599 if (DEBUG_VISIBILITY) Log.v(
6600 TAG, "Creating surface in session "
6601 + mSession.mSurfaceSession + " window " + this
6602 + " w=" + mFrame.width()
6603 + " h=" + mFrame.height() + " format="
6604 + mAttrs.format + " flags=" + flags);
6605
6606 int w = mFrame.width();
6607 int h = mFrame.height();
6608 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6609 // for a scaled surface, we always want the requested
6610 // size.
6611 w = mRequestedWidth;
6612 h = mRequestedHeight;
6613 }
6614
6615 try {
6616 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006617 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 0, w, h, mAttrs.format, flags);
6619 } catch (Surface.OutOfResourcesException e) {
6620 Log.w(TAG, "OutOfResourcesException creating surface");
6621 reclaimSomeSurfaceMemoryLocked(this, "create");
6622 return null;
6623 } catch (Exception e) {
6624 Log.e(TAG, "Exception creating surface", e);
6625 return null;
6626 }
Romain Guy06882f82009-06-10 13:36:04 -07006627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 if (localLOGV) Log.v(
6629 TAG, "Got surface: " + mSurface
6630 + ", set left=" + mFrame.left + " top=" + mFrame.top
6631 + ", animLayer=" + mAnimLayer);
6632 if (SHOW_TRANSACTIONS) {
6633 Log.i(TAG, ">>> OPEN TRANSACTION");
6634 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6635 + mAttrs.getTitle() + ") pos=(" +
6636 mFrame.left + "," + mFrame.top + ") (" +
6637 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6638 mAnimLayer + " HIDE");
6639 }
6640 Surface.openTransaction();
6641 try {
6642 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006643 mSurface.setPosition(mFrame.left + mXOffset,
6644 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 mSurface.setLayer(mAnimLayer);
6646 mSurface.hide();
6647 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6648 mSurface.setFlags(Surface.SURFACE_DITHER,
6649 Surface.SURFACE_DITHER);
6650 }
6651 } catch (RuntimeException e) {
6652 Log.w(TAG, "Error creating surface in " + w, e);
6653 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6654 }
6655 mLastHidden = true;
6656 } finally {
6657 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6658 Surface.closeTransaction();
6659 }
6660 if (localLOGV) Log.v(
6661 TAG, "Created surface " + this);
6662 }
6663 return mSurface;
6664 }
Romain Guy06882f82009-06-10 13:36:04 -07006665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666 void destroySurfaceLocked() {
6667 // Window is no longer on-screen, so can no longer receive
6668 // key events... if we were waiting for it to finish
6669 // handling a key event, the wait is over!
6670 mKeyWaiter.finishedKey(mSession, mClient, true,
6671 KeyWaiter.RETURN_NOTHING);
6672 mKeyWaiter.releasePendingPointerLocked(mSession);
6673 mKeyWaiter.releasePendingTrackballLocked(mSession);
6674
6675 if (mAppToken != null && this == mAppToken.startingWindow) {
6676 mAppToken.startingDisplayed = false;
6677 }
Romain Guy06882f82009-06-10 13:36:04 -07006678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 if (localLOGV) Log.v(
6680 TAG, "Window " + this
6681 + " destroying surface " + mSurface + ", session " + mSession);
6682 if (mSurface != null) {
6683 try {
6684 if (SHOW_TRANSACTIONS) {
6685 RuntimeException ex = new RuntimeException();
6686 ex.fillInStackTrace();
6687 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6688 + mAttrs.getTitle() + ")", ex);
6689 }
6690 mSurface.clear();
6691 } catch (RuntimeException e) {
6692 Log.w(TAG, "Exception thrown when destroying Window " + this
6693 + " surface " + mSurface + " session " + mSession
6694 + ": " + e.toString());
6695 }
6696 mSurface = null;
6697 mDrawPending = false;
6698 mCommitDrawPending = false;
6699 mReadyToShow = false;
6700
6701 int i = mChildWindows.size();
6702 while (i > 0) {
6703 i--;
6704 WindowState c = (WindowState)mChildWindows.get(i);
6705 c.mAttachedHidden = true;
6706 }
6707 }
6708 }
6709
6710 boolean finishDrawingLocked() {
6711 if (mDrawPending) {
6712 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6713 TAG, "finishDrawingLocked: " + mSurface);
6714 mCommitDrawPending = true;
6715 mDrawPending = false;
6716 return true;
6717 }
6718 return false;
6719 }
6720
6721 // This must be called while inside a transaction.
6722 void commitFinishDrawingLocked(long currentTime) {
6723 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6724 if (!mCommitDrawPending) {
6725 return;
6726 }
6727 mCommitDrawPending = false;
6728 mReadyToShow = true;
6729 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6730 final AppWindowToken atoken = mAppToken;
6731 if (atoken == null || atoken.allDrawn || starting) {
6732 performShowLocked();
6733 }
6734 }
6735
6736 // This must be called while inside a transaction.
6737 boolean performShowLocked() {
6738 if (DEBUG_VISIBILITY) {
6739 RuntimeException e = new RuntimeException();
6740 e.fillInStackTrace();
6741 Log.v(TAG, "performShow on " + this
6742 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6743 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6744 }
6745 if (mReadyToShow && isReadyForDisplay()) {
6746 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6747 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6748 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6749 + " during animation: policyVis=" + mPolicyVisibility
6750 + " attHidden=" + mAttachedHidden
6751 + " tok.hiddenRequested="
6752 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6753 + " tok.idden="
6754 + (mAppToken != null ? mAppToken.hidden : false)
6755 + " animating=" + mAnimating
6756 + " tok animating="
6757 + (mAppToken != null ? mAppToken.animating : false));
6758 if (!showSurfaceRobustlyLocked(this)) {
6759 return false;
6760 }
6761 mLastAlpha = -1;
6762 mHasDrawn = true;
6763 mLastHidden = false;
6764 mReadyToShow = false;
6765 enableScreenIfNeededLocked();
6766
6767 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 int i = mChildWindows.size();
6770 while (i > 0) {
6771 i--;
6772 WindowState c = (WindowState)mChildWindows.get(i);
6773 if (c.mSurface != null && c.mAttachedHidden) {
6774 c.mAttachedHidden = false;
6775 c.performShowLocked();
6776 }
6777 }
Romain Guy06882f82009-06-10 13:36:04 -07006778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 if (mAttrs.type != TYPE_APPLICATION_STARTING
6780 && mAppToken != null) {
6781 mAppToken.firstWindowDrawn = true;
6782 if (mAnimation == null && mAppToken.startingData != null) {
6783 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6784 + mToken
6785 + ": first real window is shown, no animation");
6786 mFinishedStarting.add(mAppToken);
6787 mH.sendEmptyMessage(H.FINISHED_STARTING);
6788 }
6789 mAppToken.updateReportedVisibilityLocked();
6790 }
6791 }
6792 return true;
6793 }
Romain Guy06882f82009-06-10 13:36:04 -07006794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 // This must be called while inside a transaction. Returns true if
6796 // there is more animation to run.
6797 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6798 if (!mDisplayFrozen) {
6799 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6802 mHasTransformation = true;
6803 mHasLocalTransformation = true;
6804 if (!mLocalAnimating) {
6805 if (DEBUG_ANIM) Log.v(
6806 TAG, "Starting animation in " + this +
6807 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6808 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6809 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6810 mAnimation.setStartTime(currentTime);
6811 mLocalAnimating = true;
6812 mAnimating = true;
6813 }
6814 mTransformation.clear();
6815 final boolean more = mAnimation.getTransformation(
6816 currentTime, mTransformation);
6817 if (DEBUG_ANIM) Log.v(
6818 TAG, "Stepped animation in " + this +
6819 ": more=" + more + ", xform=" + mTransformation);
6820 if (more) {
6821 // we're not done!
6822 return true;
6823 }
6824 if (DEBUG_ANIM) Log.v(
6825 TAG, "Finished animation in " + this +
6826 " @ " + currentTime);
6827 mAnimation = null;
6828 //WindowManagerService.this.dump();
6829 }
6830 mHasLocalTransformation = false;
6831 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
6832 && mAppToken.hasTransformation) {
6833 // When our app token is animating, we kind-of pretend like
6834 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6835 // part of this check means that we will only do this if
6836 // our window is not currently exiting, or it is not
6837 // locally animating itself. The idea being that one that
6838 // is exiting and doing a local animation should be removed
6839 // once that animation is done.
6840 mAnimating = true;
6841 mHasTransformation = true;
6842 mTransformation.clear();
6843 return false;
6844 } else if (mHasTransformation) {
6845 // Little trick to get through the path below to act like
6846 // we have finished an animation.
6847 mAnimating = true;
6848 } else if (isAnimating()) {
6849 mAnimating = true;
6850 }
6851 } else if (mAnimation != null) {
6852 // If the display is frozen, and there is a pending animation,
6853 // clear it and make sure we run the cleanup code.
6854 mAnimating = true;
6855 mLocalAnimating = true;
6856 mAnimation = null;
6857 }
Romain Guy06882f82009-06-10 13:36:04 -07006858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 if (!mAnimating && !mLocalAnimating) {
6860 return false;
6861 }
6862
6863 if (DEBUG_ANIM) Log.v(
6864 TAG, "Animation done in " + this + ": exiting=" + mExiting
6865 + ", reportedVisible="
6866 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 mAnimating = false;
6869 mLocalAnimating = false;
6870 mAnimation = null;
6871 mAnimLayer = mLayer;
6872 if (mIsImWindow) {
6873 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006874 } else if (mIsWallpaper) {
6875 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006876 }
6877 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6878 + " anim layer: " + mAnimLayer);
6879 mHasTransformation = false;
6880 mHasLocalTransformation = false;
6881 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6882 mTransformation.clear();
6883 if (mHasDrawn
6884 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6885 && mAppToken != null
6886 && mAppToken.firstWindowDrawn
6887 && mAppToken.startingData != null) {
6888 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6889 + mToken + ": first real window done animating");
6890 mFinishedStarting.add(mAppToken);
6891 mH.sendEmptyMessage(H.FINISHED_STARTING);
6892 }
Romain Guy06882f82009-06-10 13:36:04 -07006893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 finishExit();
6895
6896 if (mAppToken != null) {
6897 mAppToken.updateReportedVisibilityLocked();
6898 }
6899
6900 return false;
6901 }
6902
6903 void finishExit() {
6904 if (DEBUG_ANIM) Log.v(
6905 TAG, "finishExit in " + this
6906 + ": exiting=" + mExiting
6907 + " remove=" + mRemoveOnExit
6908 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 final int N = mChildWindows.size();
6911 for (int i=0; i<N; i++) {
6912 ((WindowState)mChildWindows.get(i)).finishExit();
6913 }
Romain Guy06882f82009-06-10 13:36:04 -07006914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 if (!mExiting) {
6916 return;
6917 }
Romain Guy06882f82009-06-10 13:36:04 -07006918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 if (isWindowAnimating()) {
6920 return;
6921 }
6922
6923 if (localLOGV) Log.v(
6924 TAG, "Exit animation finished in " + this
6925 + ": remove=" + mRemoveOnExit);
6926 if (mSurface != null) {
6927 mDestroySurface.add(this);
6928 mDestroying = true;
6929 if (SHOW_TRANSACTIONS) Log.i(
6930 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6931 try {
6932 mSurface.hide();
6933 } catch (RuntimeException e) {
6934 Log.w(TAG, "Error hiding surface in " + this, e);
6935 }
6936 mLastHidden = true;
6937 mKeyWaiter.releasePendingPointerLocked(mSession);
6938 }
6939 mExiting = false;
6940 if (mRemoveOnExit) {
6941 mPendingRemove.add(this);
6942 mRemoveOnExit = false;
6943 }
6944 }
Romain Guy06882f82009-06-10 13:36:04 -07006945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
6947 if (dsdx < .99999f || dsdx > 1.00001f) return false;
6948 if (dtdy < .99999f || dtdy > 1.00001f) return false;
6949 if (dtdx < -.000001f || dtdx > .000001f) return false;
6950 if (dsdy < -.000001f || dsdy > .000001f) return false;
6951 return true;
6952 }
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 void computeShownFrameLocked() {
6955 final boolean selfTransformation = mHasLocalTransformation;
6956 Transformation attachedTransformation =
6957 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
6958 ? mAttachedWindow.mTransformation : null;
6959 Transformation appTransformation =
6960 (mAppToken != null && mAppToken.hasTransformation)
6961 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006962
6963 // Wallpapers are animated based on the "real" window they
6964 // are currently targeting.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07006965 if (mAttrs.type == TYPE_WALLPAPER && mUpcomingWallpaperTarget == null
6966 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006967 if (mWallpaperTarget.mHasLocalTransformation) {
6968 attachedTransformation = mWallpaperTarget.mTransformation;
6969 }
6970 if (mWallpaperTarget.mAppToken != null &&
6971 mWallpaperTarget.mAppToken.hasTransformation) {
6972 appTransformation = mWallpaperTarget.mAppToken.transformation;
6973 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006974 if (DEBUG_WALLPAPER && attachedTransformation != null) {
6975 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
6976 }
6977 if (DEBUG_WALLPAPER && appTransformation != null) {
6978 Log.v(TAG, "WP target app xform: " + appTransformation);
6979 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006980 }
6981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 if (selfTransformation || attachedTransformation != null
6983 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07006984 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 final Rect frame = mFrame;
6986 final float tmpFloats[] = mTmpFloats;
6987 final Matrix tmpMatrix = mTmpMatrix;
6988
6989 // Compute the desired transformation.
6990 tmpMatrix.setTranslate(frame.left, frame.top);
6991 if (selfTransformation) {
6992 tmpMatrix.preConcat(mTransformation.getMatrix());
6993 }
6994 if (attachedTransformation != null) {
6995 tmpMatrix.preConcat(attachedTransformation.getMatrix());
6996 }
6997 if (appTransformation != null) {
6998 tmpMatrix.preConcat(appTransformation.getMatrix());
6999 }
7000
7001 // "convert" it into SurfaceFlinger's format
7002 // (a 2x2 matrix + an offset)
7003 // Here we must not transform the position of the surface
7004 // since it is already included in the transformation.
7005 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 tmpMatrix.getValues(tmpFloats);
7008 mDsDx = tmpFloats[Matrix.MSCALE_X];
7009 mDtDx = tmpFloats[Matrix.MSKEW_X];
7010 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7011 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007012 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7013 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 int w = frame.width();
7015 int h = frame.height();
7016 mShownFrame.set(x, y, x+w, y+h);
7017
7018 // Now set the alpha... but because our current hardware
7019 // can't do alpha transformation on a non-opaque surface,
7020 // turn it off if we are running an animation that is also
7021 // transforming since it is more important to have that
7022 // animation be smooth.
7023 mShownAlpha = mAlpha;
7024 if (!mLimitedAlphaCompositing
7025 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7026 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7027 && x == frame.left && y == frame.top))) {
7028 //Log.i(TAG, "Applying alpha transform");
7029 if (selfTransformation) {
7030 mShownAlpha *= mTransformation.getAlpha();
7031 }
7032 if (attachedTransformation != null) {
7033 mShownAlpha *= attachedTransformation.getAlpha();
7034 }
7035 if (appTransformation != null) {
7036 mShownAlpha *= appTransformation.getAlpha();
7037 }
7038 } else {
7039 //Log.i(TAG, "Not applying alpha transform");
7040 }
Romain Guy06882f82009-06-10 13:36:04 -07007041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 if (localLOGV) Log.v(
7043 TAG, "Continuing animation in " + this +
7044 ": " + mShownFrame +
7045 ", alpha=" + mTransformation.getAlpha());
7046 return;
7047 }
Romain Guy06882f82009-06-10 13:36:04 -07007048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007050 if (mXOffset != 0 || mYOffset != 0) {
7051 mShownFrame.offset(mXOffset, mYOffset);
7052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 mShownAlpha = mAlpha;
7054 mDsDx = 1;
7055 mDtDx = 0;
7056 mDsDy = 0;
7057 mDtDy = 1;
7058 }
Romain Guy06882f82009-06-10 13:36:04 -07007059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 /**
7061 * Is this window visible? It is not visible if there is no
7062 * surface, or we are in the process of running an exit animation
7063 * that will remove the surface, or its app token has been hidden.
7064 */
7065 public boolean isVisibleLw() {
7066 final AppWindowToken atoken = mAppToken;
7067 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7068 && (atoken == null || !atoken.hiddenRequested)
7069 && !mExiting && !mDestroying;
7070 }
7071
7072 /**
7073 * Is this window visible, ignoring its app token? It is not visible
7074 * if there is no surface, or we are in the process of running an exit animation
7075 * that will remove the surface.
7076 */
7077 public boolean isWinVisibleLw() {
7078 final AppWindowToken atoken = mAppToken;
7079 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7080 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7081 && !mExiting && !mDestroying;
7082 }
7083
7084 /**
7085 * The same as isVisible(), but follows the current hidden state of
7086 * the associated app token, not the pending requested hidden state.
7087 */
7088 boolean isVisibleNow() {
7089 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007090 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 }
7092
7093 /**
7094 * Same as isVisible(), but we also count it as visible between the
7095 * call to IWindowSession.add() and the first relayout().
7096 */
7097 boolean isVisibleOrAdding() {
7098 final AppWindowToken atoken = mAppToken;
7099 return (mSurface != null
7100 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7101 && mPolicyVisibility && !mAttachedHidden
7102 && (atoken == null || !atoken.hiddenRequested)
7103 && !mExiting && !mDestroying;
7104 }
7105
7106 /**
7107 * Is this window currently on-screen? It is on-screen either if it
7108 * is visible or it is currently running an animation before no longer
7109 * being visible.
7110 */
7111 boolean isOnScreen() {
7112 final AppWindowToken atoken = mAppToken;
7113 if (atoken != null) {
7114 return mSurface != null && mPolicyVisibility && !mDestroying
7115 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007116 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 } else {
7118 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007119 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
7121 }
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 /**
7124 * Like isOnScreen(), but we don't return true if the window is part
7125 * of a transition that has not yet been started.
7126 */
7127 boolean isReadyForDisplay() {
7128 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007129 final boolean animating = atoken != null
7130 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007132 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7133 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007134 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 }
7136
7137 /** Is the window or its container currently animating? */
7138 boolean isAnimating() {
7139 final WindowState attached = mAttachedWindow;
7140 final AppWindowToken atoken = mAppToken;
7141 return mAnimation != null
7142 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007143 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007144 (atoken.animation != null
7145 || atoken.inPendingTransaction));
7146 }
7147
7148 /** Is this window currently animating? */
7149 boolean isWindowAnimating() {
7150 return mAnimation != null;
7151 }
7152
7153 /**
7154 * Like isOnScreen, but returns false if the surface hasn't yet
7155 * been drawn.
7156 */
7157 public boolean isDisplayedLw() {
7158 final AppWindowToken atoken = mAppToken;
7159 return mSurface != null && mPolicyVisibility && !mDestroying
7160 && !mDrawPending && !mCommitDrawPending
7161 && ((!mAttachedHidden &&
7162 (atoken == null || !atoken.hiddenRequested))
7163 || mAnimating);
7164 }
7165
7166 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7167 boolean shownFrame, boolean onlyOpaque) {
7168 if (mSurface == null) {
7169 return false;
7170 }
7171 if (mAppToken != null && !mAppToken.appFullscreen) {
7172 return false;
7173 }
7174 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7175 return false;
7176 }
7177 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007178
7179 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7180 return frame.left <= mCompatibleScreenFrame.left &&
7181 frame.top <= mCompatibleScreenFrame.top &&
7182 frame.right >= mCompatibleScreenFrame.right &&
7183 frame.bottom >= mCompatibleScreenFrame.bottom;
7184 } else {
7185 return frame.left <= 0 && frame.top <= 0
7186 && frame.right >= screenWidth
7187 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 }
Romain Guy06882f82009-06-10 13:36:04 -07007190
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007191 /**
7192 * Return true if the window is opaque and fully drawn.
7193 */
7194 boolean isOpaqueDrawn() {
7195 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7196 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7197 }
7198
7199 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7200 return
7201 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007202 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7203 // only if it's visible
7204 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007205 // and only if the application fills the compatible screen
7206 mFrame.left <= mCompatibleScreenFrame.left &&
7207 mFrame.top <= mCompatibleScreenFrame.top &&
7208 mFrame.right >= mCompatibleScreenFrame.right &&
7209 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007210 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007211 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007212 }
7213
7214 boolean isFullscreen(int screenWidth, int screenHeight) {
7215 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007216 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007217 }
7218
7219 void removeLocked() {
7220 if (mAttachedWindow != null) {
7221 mAttachedWindow.mChildWindows.remove(this);
7222 }
7223 destroySurfaceLocked();
7224 mSession.windowRemovedLocked();
7225 try {
7226 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7227 } catch (RuntimeException e) {
7228 // Ignore if it has already been removed (usually because
7229 // we are doing this as part of processing a death note.)
7230 }
7231 }
7232
7233 private class DeathRecipient implements IBinder.DeathRecipient {
7234 public void binderDied() {
7235 try {
7236 synchronized(mWindowMap) {
7237 WindowState win = windowForClientLocked(mSession, mClient);
7238 Log.i(TAG, "WIN DEATH: " + win);
7239 if (win != null) {
7240 removeWindowLocked(mSession, win);
7241 }
7242 }
7243 } catch (IllegalArgumentException ex) {
7244 // This will happen if the window has already been
7245 // removed.
7246 }
7247 }
7248 }
7249
7250 /** Returns true if this window desires key events. */
7251 public final boolean canReceiveKeys() {
7252 return isVisibleOrAdding()
7253 && (mViewVisibility == View.VISIBLE)
7254 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7255 }
7256
7257 public boolean hasDrawnLw() {
7258 return mHasDrawn;
7259 }
7260
7261 public boolean showLw(boolean doAnimation) {
7262 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7263 mPolicyVisibility = true;
7264 mPolicyVisibilityAfterAnim = true;
7265 if (doAnimation) {
7266 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7267 }
7268 requestAnimationLocked(0);
7269 return true;
7270 }
7271 return false;
7272 }
7273
7274 public boolean hideLw(boolean doAnimation) {
7275 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7276 : mPolicyVisibility;
7277 if (current) {
7278 if (doAnimation) {
7279 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7280 if (mAnimation == null) {
7281 doAnimation = false;
7282 }
7283 }
7284 if (doAnimation) {
7285 mPolicyVisibilityAfterAnim = false;
7286 } else {
7287 mPolicyVisibilityAfterAnim = false;
7288 mPolicyVisibility = false;
7289 }
7290 requestAnimationLocked(0);
7291 return true;
7292 }
7293 return false;
7294 }
7295
7296 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007297 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007298
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007299 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7300 pw.print(" mClient="); pw.println(mClient.asBinder());
7301 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7302 if (mAttachedWindow != null || mLayoutAttached) {
7303 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7304 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7305 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007306 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7307 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7308 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007309 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7310 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007311 }
7312 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7313 pw.print(" mSubLayer="); pw.print(mSubLayer);
7314 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7315 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7316 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7317 pw.print("="); pw.print(mAnimLayer);
7318 pw.print(" mLastLayer="); pw.println(mLastLayer);
7319 if (mSurface != null) {
7320 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7321 }
7322 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7323 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7324 if (mAppToken != null) {
7325 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7326 }
7327 if (mTargetAppToken != null) {
7328 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7329 }
7330 pw.print(prefix); pw.print("mViewVisibility=0x");
7331 pw.print(Integer.toHexString(mViewVisibility));
7332 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007333 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7334 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007335 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7336 pw.print(prefix); pw.print("mPolicyVisibility=");
7337 pw.print(mPolicyVisibility);
7338 pw.print(" mPolicyVisibilityAfterAnim=");
7339 pw.print(mPolicyVisibilityAfterAnim);
7340 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7341 }
7342 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007343 pw.print(" h="); pw.println(mRequestedHeight);
7344 if (mXOffset != 0 || mYOffset != 0) {
7345 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7346 pw.print(" y="); pw.println(mYOffset);
7347 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007348 pw.print(prefix); pw.print("mGivenContentInsets=");
7349 mGivenContentInsets.printShortString(pw);
7350 pw.print(" mGivenVisibleInsets=");
7351 mGivenVisibleInsets.printShortString(pw);
7352 pw.println();
7353 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7354 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7355 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7356 }
7357 pw.print(prefix); pw.print("mShownFrame=");
7358 mShownFrame.printShortString(pw);
7359 pw.print(" last="); mLastShownFrame.printShortString(pw);
7360 pw.println();
7361 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7362 pw.print(" last="); mLastFrame.printShortString(pw);
7363 pw.println();
7364 pw.print(prefix); pw.print("mContainingFrame=");
7365 mContainingFrame.printShortString(pw);
7366 pw.print(" mDisplayFrame=");
7367 mDisplayFrame.printShortString(pw);
7368 pw.println();
7369 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7370 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7371 pw.println();
7372 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7373 pw.print(" last="); mLastContentInsets.printShortString(pw);
7374 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7375 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7376 pw.println();
7377 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7378 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7379 pw.print(" mAlpha="); pw.print(mAlpha);
7380 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7381 }
7382 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7383 || mAnimation != null) {
7384 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7385 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7386 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7387 pw.print(" mAnimation="); pw.println(mAnimation);
7388 }
7389 if (mHasTransformation || mHasLocalTransformation) {
7390 pw.print(prefix); pw.print("XForm: has=");
7391 pw.print(mHasTransformation);
7392 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7393 pw.print(" "); mTransformation.printShortString(pw);
7394 pw.println();
7395 }
7396 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7397 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7398 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7399 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7400 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7401 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7402 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7403 pw.print(" mDestroying="); pw.print(mDestroying);
7404 pw.print(" mRemoved="); pw.println(mRemoved);
7405 }
7406 if (mOrientationChanging || mAppFreezing) {
7407 pw.print(prefix); pw.print("mOrientationChanging=");
7408 pw.print(mOrientationChanging);
7409 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7410 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007411 if (mHScale != 1 || mVScale != 1) {
7412 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7413 pw.print(" mVScale="); pw.println(mVScale);
7414 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007415 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007416 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7417 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 }
7420
7421 @Override
7422 public String toString() {
7423 return "Window{"
7424 + Integer.toHexString(System.identityHashCode(this))
7425 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7426 }
7427 }
Romain Guy06882f82009-06-10 13:36:04 -07007428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 // -------------------------------------------------------------
7430 // Window Token State
7431 // -------------------------------------------------------------
7432
7433 class WindowToken {
7434 // The actual token.
7435 final IBinder token;
7436
7437 // The type of window this token is for, as per WindowManager.LayoutParams.
7438 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 // Set if this token was explicitly added by a client, so should
7441 // not be removed when all windows are removed.
7442 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007443
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007444 // For printing.
7445 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 // If this is an AppWindowToken, this is non-null.
7448 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 // All of the windows associated with this token.
7451 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7452
7453 // Is key dispatching paused for this token?
7454 boolean paused = false;
7455
7456 // Should this token's windows be hidden?
7457 boolean hidden;
7458
7459 // Temporary for finding which tokens no longer have visible windows.
7460 boolean hasVisible;
7461
7462 WindowToken(IBinder _token, int type, boolean _explicit) {
7463 token = _token;
7464 windowType = type;
7465 explicit = _explicit;
7466 }
7467
7468 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007469 pw.print(prefix); pw.print("token="); pw.println(token);
7470 pw.print(prefix); pw.print("windows="); pw.println(windows);
7471 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7472 pw.print(" hidden="); pw.print(hidden);
7473 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 }
7475
7476 @Override
7477 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007478 if (stringName == null) {
7479 StringBuilder sb = new StringBuilder();
7480 sb.append("WindowToken{");
7481 sb.append(Integer.toHexString(System.identityHashCode(this)));
7482 sb.append(" token="); sb.append(token); sb.append('}');
7483 stringName = sb.toString();
7484 }
7485 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007486 }
7487 };
7488
7489 class AppWindowToken extends WindowToken {
7490 // Non-null only for application tokens.
7491 final IApplicationToken appToken;
7492
7493 // All of the windows and child windows that are included in this
7494 // application token. Note this list is NOT sorted!
7495 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7496
7497 int groupId = -1;
7498 boolean appFullscreen;
7499 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 // These are used for determining when all windows associated with
7502 // an activity have been drawn, so they can be made visible together
7503 // at the same time.
7504 int lastTransactionSequence = mTransactionSequence-1;
7505 int numInterestingWindows;
7506 int numDrawnWindows;
7507 boolean inPendingTransaction;
7508 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 // Is this token going to be hidden in a little while? If so, it
7511 // won't be taken into account for setting the screen orientation.
7512 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 // Is this window's surface needed? This is almost like hidden, except
7515 // it will sometimes be true a little earlier: when the token has
7516 // been shown, but is still waiting for its app transition to execute
7517 // before making its windows shown.
7518 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 // Have we told the window clients to hide themselves?
7521 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 // Last visibility state we reported to the app token.
7524 boolean reportedVisible;
7525
7526 // Set to true when the token has been removed from the window mgr.
7527 boolean removed;
7528
7529 // Have we been asked to have this token keep the screen frozen?
7530 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532 boolean animating;
7533 Animation animation;
7534 boolean hasTransformation;
7535 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 // Offset to the window of all layers in the token, for use by
7538 // AppWindowToken animations.
7539 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 // Information about an application starting window if displayed.
7542 StartingData startingData;
7543 WindowState startingWindow;
7544 View startingView;
7545 boolean startingDisplayed;
7546 boolean startingMoved;
7547 boolean firstWindowDrawn;
7548
7549 AppWindowToken(IApplicationToken _token) {
7550 super(_token.asBinder(),
7551 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7552 appWindowToken = this;
7553 appToken = _token;
7554 }
Romain Guy06882f82009-06-10 13:36:04 -07007555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 public void setAnimation(Animation anim) {
7557 if (localLOGV) Log.v(
7558 TAG, "Setting animation in " + this + ": " + anim);
7559 animation = anim;
7560 animating = false;
7561 anim.restrictDuration(MAX_ANIMATION_DURATION);
7562 anim.scaleCurrentDuration(mTransitionAnimationScale);
7563 int zorder = anim.getZAdjustment();
7564 int adj = 0;
7565 if (zorder == Animation.ZORDER_TOP) {
7566 adj = TYPE_LAYER_OFFSET;
7567 } else if (zorder == Animation.ZORDER_BOTTOM) {
7568 adj = -TYPE_LAYER_OFFSET;
7569 }
Romain Guy06882f82009-06-10 13:36:04 -07007570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 if (animLayerAdjustment != adj) {
7572 animLayerAdjustment = adj;
7573 updateLayers();
7574 }
7575 }
Romain Guy06882f82009-06-10 13:36:04 -07007576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 public void setDummyAnimation() {
7578 if (animation == null) {
7579 if (localLOGV) Log.v(
7580 TAG, "Setting dummy animation in " + this);
7581 animation = sDummyAnimation;
7582 }
7583 }
7584
7585 public void clearAnimation() {
7586 if (animation != null) {
7587 animation = null;
7588 animating = true;
7589 }
7590 }
Romain Guy06882f82009-06-10 13:36:04 -07007591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 void updateLayers() {
7593 final int N = allAppWindows.size();
7594 final int adj = animLayerAdjustment;
7595 for (int i=0; i<N; i++) {
7596 WindowState w = allAppWindows.get(i);
7597 w.mAnimLayer = w.mLayer + adj;
7598 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7599 + w.mAnimLayer);
7600 if (w == mInputMethodTarget) {
7601 setInputMethodAnimLayerAdjustment(adj);
7602 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007603 if (w == mWallpaperTarget && mUpcomingWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007604 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 }
7607 }
Romain Guy06882f82009-06-10 13:36:04 -07007608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 void sendAppVisibilityToClients() {
7610 final int N = allAppWindows.size();
7611 for (int i=0; i<N; i++) {
7612 WindowState win = allAppWindows.get(i);
7613 if (win == startingWindow && clientHidden) {
7614 // Don't hide the starting window.
7615 continue;
7616 }
7617 try {
7618 if (DEBUG_VISIBILITY) Log.v(TAG,
7619 "Setting visibility of " + win + ": " + (!clientHidden));
7620 win.mClient.dispatchAppVisibility(!clientHidden);
7621 } catch (RemoteException e) {
7622 }
7623 }
7624 }
Romain Guy06882f82009-06-10 13:36:04 -07007625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 void showAllWindowsLocked() {
7627 final int NW = allAppWindows.size();
7628 for (int i=0; i<NW; i++) {
7629 WindowState w = allAppWindows.get(i);
7630 if (DEBUG_VISIBILITY) Log.v(TAG,
7631 "performing show on: " + w);
7632 w.performShowLocked();
7633 }
7634 }
Romain Guy06882f82009-06-10 13:36:04 -07007635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 // This must be called while inside a transaction.
7637 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7638 if (!mDisplayFrozen) {
7639 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 if (animation == sDummyAnimation) {
7642 // This guy is going to animate, but not yet. For now count
7643 // it is not animating for purposes of scheduling transactions;
7644 // when it is really time to animate, this will be set to
7645 // a real animation and the next call will execute normally.
7646 return false;
7647 }
Romain Guy06882f82009-06-10 13:36:04 -07007648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7650 if (!animating) {
7651 if (DEBUG_ANIM) Log.v(
7652 TAG, "Starting animation in " + this +
7653 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7654 + " scale=" + mTransitionAnimationScale
7655 + " allDrawn=" + allDrawn + " animating=" + animating);
7656 animation.initialize(dw, dh, dw, dh);
7657 animation.setStartTime(currentTime);
7658 animating = true;
7659 }
7660 transformation.clear();
7661 final boolean more = animation.getTransformation(
7662 currentTime, transformation);
7663 if (DEBUG_ANIM) Log.v(
7664 TAG, "Stepped animation in " + this +
7665 ": more=" + more + ", xform=" + transformation);
7666 if (more) {
7667 // we're done!
7668 hasTransformation = true;
7669 return true;
7670 }
7671 if (DEBUG_ANIM) Log.v(
7672 TAG, "Finished animation in " + this +
7673 " @ " + currentTime);
7674 animation = null;
7675 }
7676 } else if (animation != null) {
7677 // If the display is frozen, and there is a pending animation,
7678 // clear it and make sure we run the cleanup code.
7679 animating = true;
7680 animation = null;
7681 }
7682
7683 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 if (!animating) {
7686 return false;
7687 }
7688
7689 clearAnimation();
7690 animating = false;
7691 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7692 moveInputMethodWindowsIfNeededLocked(true);
7693 }
Romain Guy06882f82009-06-10 13:36:04 -07007694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 if (DEBUG_ANIM) Log.v(
7696 TAG, "Animation done in " + this
7697 + ": reportedVisible=" + reportedVisible);
7698
7699 transformation.clear();
7700 if (animLayerAdjustment != 0) {
7701 animLayerAdjustment = 0;
7702 updateLayers();
7703 }
Romain Guy06882f82009-06-10 13:36:04 -07007704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 final int N = windows.size();
7706 for (int i=0; i<N; i++) {
7707 ((WindowState)windows.get(i)).finishExit();
7708 }
7709 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 return false;
7712 }
7713
7714 void updateReportedVisibilityLocked() {
7715 if (appToken == null) {
7716 return;
7717 }
Romain Guy06882f82009-06-10 13:36:04 -07007718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 int numInteresting = 0;
7720 int numVisible = 0;
7721 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7724 final int N = allAppWindows.size();
7725 for (int i=0; i<N; i++) {
7726 WindowState win = allAppWindows.get(i);
7727 if (win == startingWindow || win.mAppFreezing) {
7728 continue;
7729 }
7730 if (DEBUG_VISIBILITY) {
7731 Log.v(TAG, "Win " + win + ": isDisplayed="
7732 + win.isDisplayedLw()
7733 + ", isAnimating=" + win.isAnimating());
7734 if (!win.isDisplayedLw()) {
7735 Log.v(TAG, "Not displayed: s=" + win.mSurface
7736 + " pv=" + win.mPolicyVisibility
7737 + " dp=" + win.mDrawPending
7738 + " cdp=" + win.mCommitDrawPending
7739 + " ah=" + win.mAttachedHidden
7740 + " th="
7741 + (win.mAppToken != null
7742 ? win.mAppToken.hiddenRequested : false)
7743 + " a=" + win.mAnimating);
7744 }
7745 }
7746 numInteresting++;
7747 if (win.isDisplayedLw()) {
7748 if (!win.isAnimating()) {
7749 numVisible++;
7750 }
7751 nowGone = false;
7752 } else if (win.isAnimating()) {
7753 nowGone = false;
7754 }
7755 }
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7758 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7759 + numInteresting + " visible=" + numVisible);
7760 if (nowVisible != reportedVisible) {
7761 if (DEBUG_VISIBILITY) Log.v(
7762 TAG, "Visibility changed in " + this
7763 + ": vis=" + nowVisible);
7764 reportedVisible = nowVisible;
7765 Message m = mH.obtainMessage(
7766 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7767 nowVisible ? 1 : 0,
7768 nowGone ? 1 : 0,
7769 this);
7770 mH.sendMessage(m);
7771 }
7772 }
Romain Guy06882f82009-06-10 13:36:04 -07007773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 void dump(PrintWriter pw, String prefix) {
7775 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007776 if (appToken != null) {
7777 pw.print(prefix); pw.println("app=true");
7778 }
7779 if (allAppWindows.size() > 0) {
7780 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7781 }
7782 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
7783 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7784 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7785 pw.print(" clientHidden="); pw.print(clientHidden);
7786 pw.print(" willBeHidden="); pw.print(willBeHidden);
7787 pw.print(" reportedVisible="); pw.println(reportedVisible);
7788 if (paused || freezingScreen) {
7789 pw.print(prefix); pw.print("paused="); pw.print(paused);
7790 pw.print(" freezingScreen="); pw.println(freezingScreen);
7791 }
7792 if (numInterestingWindows != 0 || numDrawnWindows != 0
7793 || inPendingTransaction || allDrawn) {
7794 pw.print(prefix); pw.print("numInterestingWindows=");
7795 pw.print(numInterestingWindows);
7796 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7797 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7798 pw.print(" allDrawn="); pw.println(allDrawn);
7799 }
7800 if (animating || animation != null) {
7801 pw.print(prefix); pw.print("animating="); pw.print(animating);
7802 pw.print(" animation="); pw.println(animation);
7803 }
7804 if (animLayerAdjustment != 0) {
7805 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7806 }
7807 if (hasTransformation) {
7808 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7809 pw.print(" transformation="); transformation.printShortString(pw);
7810 pw.println();
7811 }
7812 if (startingData != null || removed || firstWindowDrawn) {
7813 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7814 pw.print(" removed="); pw.print(removed);
7815 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7816 }
7817 if (startingWindow != null || startingView != null
7818 || startingDisplayed || startingMoved) {
7819 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7820 pw.print(" startingView="); pw.print(startingView);
7821 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7822 pw.print(" startingMoved"); pw.println(startingMoved);
7823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 }
7825
7826 @Override
7827 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007828 if (stringName == null) {
7829 StringBuilder sb = new StringBuilder();
7830 sb.append("AppWindowToken{");
7831 sb.append(Integer.toHexString(System.identityHashCode(this)));
7832 sb.append(" token="); sb.append(token); sb.append('}');
7833 stringName = sb.toString();
7834 }
7835 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 }
7837 }
Romain Guy06882f82009-06-10 13:36:04 -07007838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 public static WindowManager.LayoutParams findAnimations(
7840 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007841 ArrayList<AppWindowToken> openingTokenList1,
7842 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007844
7845 // First, check if there is a compatible window in opening/closing
7846 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007847 WindowManager.LayoutParams animParams = null;
7848 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007849 animParams = findCompatibleWindowParams(openingTokenList1);
7850 if (animParams == null) {
7851 animParams = findCompatibleWindowParams(closingTokenList2);
7852 }
7853 if (animParams != null) {
7854 return animParams;
7855 }
7856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 //Log.i(TAG, "Looking for animations...");
7858 for (int i=order.size()-1; i>=0; i--) {
7859 AppWindowToken wtoken = order.get(i);
7860 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007861 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 int j = wtoken.windows.size();
7863 while (j > 0) {
7864 j--;
7865 WindowState win = wtoken.windows.get(j);
7866 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7867 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7868 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7869 //Log.i(TAG, "Found base or application window, done!");
7870 if (wtoken.appFullscreen) {
7871 return win.mAttrs;
7872 }
7873 if (animSrc < 2) {
7874 animParams = win.mAttrs;
7875 animSrc = 2;
7876 }
7877 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7878 //Log.i(TAG, "Found normal window, we may use this...");
7879 animParams = win.mAttrs;
7880 animSrc = 1;
7881 }
7882 }
7883 }
7884 }
Romain Guy06882f82009-06-10 13:36:04 -07007885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 return animParams;
7887 }
Romain Guy06882f82009-06-10 13:36:04 -07007888
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007889 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7890 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7891 AppWindowToken wtoken = tokenList.get(appCount);
7892 // Just checking one window is sufficient as all windows have the compatible flag
7893 // if the application is in compatibility mode.
7894 if (wtoken.windows.size() > 0) {
7895 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7896 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7897 return params;
7898 }
7899 }
7900 }
7901 return null;
7902 }
7903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 // -------------------------------------------------------------
7905 // DummyAnimation
7906 // -------------------------------------------------------------
7907
7908 // This is an animation that does nothing: it just immediately finishes
7909 // itself every time it is called. It is used as a stub animation in cases
7910 // where we want to synchronize multiple things that may be animating.
7911 static final class DummyAnimation extends Animation {
7912 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7913 return false;
7914 }
7915 }
7916 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 // -------------------------------------------------------------
7919 // Async Handler
7920 // -------------------------------------------------------------
7921
7922 static final class StartingData {
7923 final String pkg;
7924 final int theme;
7925 final CharSequence nonLocalizedLabel;
7926 final int labelRes;
7927 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7930 int _labelRes, int _icon) {
7931 pkg = _pkg;
7932 theme = _theme;
7933 nonLocalizedLabel = _nonLocalizedLabel;
7934 labelRes = _labelRes;
7935 icon = _icon;
7936 }
7937 }
7938
7939 private final class H extends Handler {
7940 public static final int REPORT_FOCUS_CHANGE = 2;
7941 public static final int REPORT_LOSING_FOCUS = 3;
7942 public static final int ANIMATE = 4;
7943 public static final int ADD_STARTING = 5;
7944 public static final int REMOVE_STARTING = 6;
7945 public static final int FINISHED_STARTING = 7;
7946 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 public static final int WINDOW_FREEZE_TIMEOUT = 11;
7948 public static final int HOLD_SCREEN_CHANGED = 12;
7949 public static final int APP_TRANSITION_TIMEOUT = 13;
7950 public static final int PERSIST_ANIMATION_SCALE = 14;
7951 public static final int FORCE_GC = 15;
7952 public static final int ENABLE_SCREEN = 16;
7953 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07007954 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07007955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07007957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 public H() {
7959 }
Romain Guy06882f82009-06-10 13:36:04 -07007960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 @Override
7962 public void handleMessage(Message msg) {
7963 switch (msg.what) {
7964 case REPORT_FOCUS_CHANGE: {
7965 WindowState lastFocus;
7966 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07007967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 synchronized(mWindowMap) {
7969 lastFocus = mLastFocus;
7970 newFocus = mCurrentFocus;
7971 if (lastFocus == newFocus) {
7972 // Focus is not changing, so nothing to do.
7973 return;
7974 }
7975 mLastFocus = newFocus;
7976 //Log.i(TAG, "Focus moving from " + lastFocus
7977 // + " to " + newFocus);
7978 if (newFocus != null && lastFocus != null
7979 && !newFocus.isDisplayedLw()) {
7980 //Log.i(TAG, "Delaying loss of focus...");
7981 mLosingFocus.add(lastFocus);
7982 lastFocus = null;
7983 }
7984 }
7985
7986 if (lastFocus != newFocus) {
7987 //System.out.println("Changing focus from " + lastFocus
7988 // + " to " + newFocus);
7989 if (newFocus != null) {
7990 try {
7991 //Log.i(TAG, "Gaining focus: " + newFocus);
7992 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
7993 } catch (RemoteException e) {
7994 // Ignore if process has died.
7995 }
7996 }
7997
7998 if (lastFocus != null) {
7999 try {
8000 //Log.i(TAG, "Losing focus: " + lastFocus);
8001 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8002 } catch (RemoteException e) {
8003 // Ignore if process has died.
8004 }
8005 }
8006 }
8007 } break;
8008
8009 case REPORT_LOSING_FOCUS: {
8010 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 synchronized(mWindowMap) {
8013 losers = mLosingFocus;
8014 mLosingFocus = new ArrayList<WindowState>();
8015 }
8016
8017 final int N = losers.size();
8018 for (int i=0; i<N; i++) {
8019 try {
8020 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8021 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8022 } catch (RemoteException e) {
8023 // Ignore if process has died.
8024 }
8025 }
8026 } break;
8027
8028 case ANIMATE: {
8029 synchronized(mWindowMap) {
8030 mAnimationPending = false;
8031 performLayoutAndPlaceSurfacesLocked();
8032 }
8033 } break;
8034
8035 case ADD_STARTING: {
8036 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8037 final StartingData sd = wtoken.startingData;
8038
8039 if (sd == null) {
8040 // Animation has been canceled... do nothing.
8041 return;
8042 }
Romain Guy06882f82009-06-10 13:36:04 -07008043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8045 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 View view = null;
8048 try {
8049 view = mPolicy.addStartingWindow(
8050 wtoken.token, sd.pkg,
8051 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8052 sd.icon);
8053 } catch (Exception e) {
8054 Log.w(TAG, "Exception when adding starting window", e);
8055 }
8056
8057 if (view != null) {
8058 boolean abort = false;
8059
8060 synchronized(mWindowMap) {
8061 if (wtoken.removed || wtoken.startingData == null) {
8062 // If the window was successfully added, then
8063 // we need to remove it.
8064 if (wtoken.startingWindow != null) {
8065 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8066 "Aborted starting " + wtoken
8067 + ": removed=" + wtoken.removed
8068 + " startingData=" + wtoken.startingData);
8069 wtoken.startingWindow = null;
8070 wtoken.startingData = null;
8071 abort = true;
8072 }
8073 } else {
8074 wtoken.startingView = view;
8075 }
8076 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8077 "Added starting " + wtoken
8078 + ": startingWindow="
8079 + wtoken.startingWindow + " startingView="
8080 + wtoken.startingView);
8081 }
8082
8083 if (abort) {
8084 try {
8085 mPolicy.removeStartingWindow(wtoken.token, view);
8086 } catch (Exception e) {
8087 Log.w(TAG, "Exception when removing starting window", e);
8088 }
8089 }
8090 }
8091 } break;
8092
8093 case REMOVE_STARTING: {
8094 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8095 IBinder token = null;
8096 View view = null;
8097 synchronized (mWindowMap) {
8098 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8099 + wtoken + ": startingWindow="
8100 + wtoken.startingWindow + " startingView="
8101 + wtoken.startingView);
8102 if (wtoken.startingWindow != null) {
8103 view = wtoken.startingView;
8104 token = wtoken.token;
8105 wtoken.startingData = null;
8106 wtoken.startingView = null;
8107 wtoken.startingWindow = null;
8108 }
8109 }
8110 if (view != null) {
8111 try {
8112 mPolicy.removeStartingWindow(token, view);
8113 } catch (Exception e) {
8114 Log.w(TAG, "Exception when removing starting window", e);
8115 }
8116 }
8117 } break;
8118
8119 case FINISHED_STARTING: {
8120 IBinder token = null;
8121 View view = null;
8122 while (true) {
8123 synchronized (mWindowMap) {
8124 final int N = mFinishedStarting.size();
8125 if (N <= 0) {
8126 break;
8127 }
8128 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8129
8130 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8131 "Finished starting " + wtoken
8132 + ": startingWindow=" + wtoken.startingWindow
8133 + " startingView=" + wtoken.startingView);
8134
8135 if (wtoken.startingWindow == null) {
8136 continue;
8137 }
8138
8139 view = wtoken.startingView;
8140 token = wtoken.token;
8141 wtoken.startingData = null;
8142 wtoken.startingView = null;
8143 wtoken.startingWindow = null;
8144 }
8145
8146 try {
8147 mPolicy.removeStartingWindow(token, view);
8148 } catch (Exception e) {
8149 Log.w(TAG, "Exception when removing starting window", e);
8150 }
8151 }
8152 } break;
8153
8154 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8155 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8156
8157 boolean nowVisible = msg.arg1 != 0;
8158 boolean nowGone = msg.arg2 != 0;
8159
8160 try {
8161 if (DEBUG_VISIBILITY) Log.v(
8162 TAG, "Reporting visible in " + wtoken
8163 + " visible=" + nowVisible
8164 + " gone=" + nowGone);
8165 if (nowVisible) {
8166 wtoken.appToken.windowsVisible();
8167 } else {
8168 wtoken.appToken.windowsGone();
8169 }
8170 } catch (RemoteException ex) {
8171 }
8172 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 case WINDOW_FREEZE_TIMEOUT: {
8175 synchronized (mWindowMap) {
8176 Log.w(TAG, "Window freeze timeout expired.");
8177 int i = mWindows.size();
8178 while (i > 0) {
8179 i--;
8180 WindowState w = (WindowState)mWindows.get(i);
8181 if (w.mOrientationChanging) {
8182 w.mOrientationChanging = false;
8183 Log.w(TAG, "Force clearing orientation change: " + w);
8184 }
8185 }
8186 performLayoutAndPlaceSurfacesLocked();
8187 }
8188 break;
8189 }
Romain Guy06882f82009-06-10 13:36:04 -07008190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191 case HOLD_SCREEN_CHANGED: {
8192 Session oldHold;
8193 Session newHold;
8194 synchronized (mWindowMap) {
8195 oldHold = mLastReportedHold;
8196 newHold = (Session)msg.obj;
8197 mLastReportedHold = newHold;
8198 }
Romain Guy06882f82009-06-10 13:36:04 -07008199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 if (oldHold != newHold) {
8201 try {
8202 if (oldHold != null) {
8203 mBatteryStats.noteStopWakelock(oldHold.mUid,
8204 "window",
8205 BatteryStats.WAKE_TYPE_WINDOW);
8206 }
8207 if (newHold != null) {
8208 mBatteryStats.noteStartWakelock(newHold.mUid,
8209 "window",
8210 BatteryStats.WAKE_TYPE_WINDOW);
8211 }
8212 } catch (RemoteException e) {
8213 }
8214 }
8215 break;
8216 }
Romain Guy06882f82009-06-10 13:36:04 -07008217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218 case APP_TRANSITION_TIMEOUT: {
8219 synchronized (mWindowMap) {
8220 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8221 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8222 "*** APP TRANSITION TIMEOUT");
8223 mAppTransitionReady = true;
8224 mAppTransitionTimeout = true;
8225 performLayoutAndPlaceSurfacesLocked();
8226 }
8227 }
8228 break;
8229 }
Romain Guy06882f82009-06-10 13:36:04 -07008230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 case PERSIST_ANIMATION_SCALE: {
8232 Settings.System.putFloat(mContext.getContentResolver(),
8233 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8234 Settings.System.putFloat(mContext.getContentResolver(),
8235 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8236 break;
8237 }
Romain Guy06882f82009-06-10 13:36:04 -07008238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 case FORCE_GC: {
8240 synchronized(mWindowMap) {
8241 if (mAnimationPending) {
8242 // If we are animating, don't do the gc now but
8243 // delay a bit so we don't interrupt the animation.
8244 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8245 2000);
8246 return;
8247 }
8248 // If we are currently rotating the display, it will
8249 // schedule a new message when done.
8250 if (mDisplayFrozen) {
8251 return;
8252 }
8253 mFreezeGcPending = 0;
8254 }
8255 Runtime.getRuntime().gc();
8256 break;
8257 }
Romain Guy06882f82009-06-10 13:36:04 -07008258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 case ENABLE_SCREEN: {
8260 performEnableScreen();
8261 break;
8262 }
Romain Guy06882f82009-06-10 13:36:04 -07008263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 case APP_FREEZE_TIMEOUT: {
8265 synchronized (mWindowMap) {
8266 Log.w(TAG, "App freeze timeout expired.");
8267 int i = mAppTokens.size();
8268 while (i > 0) {
8269 i--;
8270 AppWindowToken tok = mAppTokens.get(i);
8271 if (tok.freezingScreen) {
8272 Log.w(TAG, "Force clearing freeze: " + tok);
8273 unsetAppFreezingScreenLocked(tok, true, true);
8274 }
8275 }
8276 }
8277 break;
8278 }
Romain Guy06882f82009-06-10 13:36:04 -07008279
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008280 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008281 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008282 sendNewConfiguration();
8283 }
8284 break;
8285 }
Romain Guy06882f82009-06-10 13:36:04 -07008286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 }
8288 }
8289 }
8290
8291 // -------------------------------------------------------------
8292 // IWindowManager API
8293 // -------------------------------------------------------------
8294
8295 public IWindowSession openSession(IInputMethodClient client,
8296 IInputContext inputContext) {
8297 if (client == null) throw new IllegalArgumentException("null client");
8298 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8299 return new Session(client, inputContext);
8300 }
8301
8302 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8303 synchronized (mWindowMap) {
8304 // The focus for the client is the window immediately below
8305 // where we would place the input method window.
8306 int idx = findDesiredInputMethodWindowIndexLocked(false);
8307 WindowState imFocus;
8308 if (idx > 0) {
8309 imFocus = (WindowState)mWindows.get(idx-1);
8310 if (imFocus != null) {
8311 if (imFocus.mSession.mClient != null &&
8312 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8313 return true;
8314 }
8315 }
8316 }
8317 }
8318 return false;
8319 }
Romain Guy06882f82009-06-10 13:36:04 -07008320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 // -------------------------------------------------------------
8322 // Internals
8323 // -------------------------------------------------------------
8324
8325 final WindowState windowForClientLocked(Session session, IWindow client) {
8326 return windowForClientLocked(session, client.asBinder());
8327 }
Romain Guy06882f82009-06-10 13:36:04 -07008328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 final WindowState windowForClientLocked(Session session, IBinder client) {
8330 WindowState win = mWindowMap.get(client);
8331 if (localLOGV) Log.v(
8332 TAG, "Looking up client " + client + ": " + win);
8333 if (win == null) {
8334 RuntimeException ex = new RuntimeException();
8335 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8336 return null;
8337 }
8338 if (session != null && win.mSession != session) {
8339 RuntimeException ex = new RuntimeException();
8340 Log.w(TAG, "Requested window " + client + " is in session " +
8341 win.mSession + ", not " + session, ex);
8342 return null;
8343 }
8344
8345 return win;
8346 }
8347
8348 private final void assignLayersLocked() {
8349 int N = mWindows.size();
8350 int curBaseLayer = 0;
8351 int curLayer = 0;
8352 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 for (i=0; i<N; i++) {
8355 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008356 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8357 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 curLayer += WINDOW_LAYER_MULTIPLIER;
8359 w.mLayer = curLayer;
8360 } else {
8361 curBaseLayer = curLayer = w.mBaseLayer;
8362 w.mLayer = curLayer;
8363 }
8364 if (w.mTargetAppToken != null) {
8365 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8366 } else if (w.mAppToken != null) {
8367 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8368 } else {
8369 w.mAnimLayer = w.mLayer;
8370 }
8371 if (w.mIsImWindow) {
8372 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008373 } else if (w.mIsWallpaper) {
8374 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 }
8376 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8377 + w.mAnimLayer);
8378 //System.out.println(
8379 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8380 }
8381 }
8382
8383 private boolean mInLayout = false;
8384 private final void performLayoutAndPlaceSurfacesLocked() {
8385 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008386 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 throw new RuntimeException("Recursive call!");
8388 }
8389 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8390 return;
8391 }
8392
8393 boolean recoveringMemory = false;
8394 if (mForceRemoves != null) {
8395 recoveringMemory = true;
8396 // Wait a little it for things to settle down, and off we go.
8397 for (int i=0; i<mForceRemoves.size(); i++) {
8398 WindowState ws = mForceRemoves.get(i);
8399 Log.i(TAG, "Force removing: " + ws);
8400 removeWindowInnerLocked(ws.mSession, ws);
8401 }
8402 mForceRemoves = null;
8403 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8404 Object tmp = new Object();
8405 synchronized (tmp) {
8406 try {
8407 tmp.wait(250);
8408 } catch (InterruptedException e) {
8409 }
8410 }
8411 }
Romain Guy06882f82009-06-10 13:36:04 -07008412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 mInLayout = true;
8414 try {
8415 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 int i = mPendingRemove.size()-1;
8418 if (i >= 0) {
8419 while (i >= 0) {
8420 WindowState w = mPendingRemove.get(i);
8421 removeWindowInnerLocked(w.mSession, w);
8422 i--;
8423 }
8424 mPendingRemove.clear();
8425
8426 mInLayout = false;
8427 assignLayersLocked();
8428 mLayoutNeeded = true;
8429 performLayoutAndPlaceSurfacesLocked();
8430
8431 } else {
8432 mInLayout = false;
8433 if (mLayoutNeeded) {
8434 requestAnimationLocked(0);
8435 }
8436 }
8437 } catch (RuntimeException e) {
8438 mInLayout = false;
8439 Log.e(TAG, "Unhandled exception while layout out windows", e);
8440 }
8441 }
8442
8443 private final void performLayoutLockedInner() {
8444 final int dw = mDisplay.getWidth();
8445 final int dh = mDisplay.getHeight();
8446
8447 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008448 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449 int i;
8450
8451 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008452
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008453 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 mPolicy.beginLayoutLw(dw, dh);
8455
8456 // First perform layout of any root windows (not attached
8457 // to another window).
8458 int topAttached = -1;
8459 for (i = N-1; i >= 0; i--) {
8460 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008461
8462 // Don't do layout of a window if it is not visible, or
8463 // soon won't be visible, to avoid wasting time and funky
8464 // changes while a window is animating away.
8465 final AppWindowToken atoken = win.mAppToken;
8466 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008467 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008468 || win.mRootToken.hidden
8469 || (atoken != null && atoken.hiddenRequested)
8470 || !win.mPolicyVisibility
8471 || win.mAttachedHidden
8472 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473
8474 // If this view is GONE, then skip it -- keep the current
8475 // frame, and let the caller know so they can ignore it
8476 // if they want. (We do the normal layout for INVISIBLE
8477 // windows, since that means "perform layout as normal,
8478 // just don't display").
8479 if (!gone || !win.mHaveFrame) {
8480 if (!win.mLayoutAttached) {
8481 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8482 } else {
8483 if (topAttached < 0) topAttached = i;
8484 }
8485 }
8486 }
Romain Guy06882f82009-06-10 13:36:04 -07008487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 // Now perform layout of attached windows, which usually
8489 // depend on the position of the window they are attached to.
8490 // XXX does not deal with windows that are attached to windows
8491 // that are themselves attached.
8492 for (i = topAttached; i >= 0; i--) {
8493 WindowState win = (WindowState) mWindows.get(i);
8494
8495 // If this view is GONE, then skip it -- keep the current
8496 // frame, and let the caller know so they can ignore it
8497 // if they want. (We do the normal layout for INVISIBLE
8498 // windows, since that means "perform layout as normal,
8499 // just don't display").
8500 if (win.mLayoutAttached) {
8501 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8502 || !win.mHaveFrame) {
8503 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8504 }
8505 }
8506 }
8507
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008508 if (!mPolicy.finishLayoutLw()) {
8509 mLayoutNeeded = false;
8510 } else if (repeats > 2) {
8511 Log.w(TAG, "Layout repeat aborted after too many iterations");
8512 mLayoutNeeded = false;
8513 } else {
8514 repeats++;
8515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 }
8517 }
Romain Guy06882f82009-06-10 13:36:04 -07008518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 private final void performLayoutAndPlaceSurfacesLockedInner(
8520 boolean recoveringMemory) {
8521 final long currentTime = SystemClock.uptimeMillis();
8522 final int dw = mDisplay.getWidth();
8523 final int dh = mDisplay.getHeight();
8524
8525 final int N = mWindows.size();
8526 int i;
8527
8528 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 if (mFxSession == null) {
8532 mFxSession = new SurfaceSession();
8533 }
Romain Guy06882f82009-06-10 13:36:04 -07008534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8536
8537 // Initialize state of exiting tokens.
8538 for (i=mExitingTokens.size()-1; i>=0; i--) {
8539 mExitingTokens.get(i).hasVisible = false;
8540 }
8541
8542 // Initialize state of exiting applications.
8543 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8544 mExitingAppTokens.get(i).hasVisible = false;
8545 }
8546
8547 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 boolean orientationChangeComplete = true;
8549 Session holdScreen = null;
8550 float screenBrightness = -1;
8551 boolean focusDisplayed = false;
8552 boolean animating = false;
8553
8554 Surface.openTransaction();
8555 try {
8556 boolean restart;
8557
8558 do {
8559 final int transactionSequence = ++mTransactionSequence;
8560
8561 // Update animations of all applications, including those
8562 // associated with exiting/removed apps
8563 boolean tokensAnimating = false;
8564 final int NAT = mAppTokens.size();
8565 for (i=0; i<NAT; i++) {
8566 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8567 tokensAnimating = true;
8568 }
8569 }
8570 final int NEAT = mExitingAppTokens.size();
8571 for (i=0; i<NEAT; i++) {
8572 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8573 tokensAnimating = true;
8574 }
8575 }
8576
8577 animating = tokensAnimating;
8578 restart = false;
8579
8580 boolean tokenMayBeDrawn = false;
8581
8582 mPolicy.beginAnimationLw(dw, dh);
8583
8584 for (i=N-1; i>=0; i--) {
8585 WindowState w = (WindowState)mWindows.get(i);
8586
8587 final WindowManager.LayoutParams attrs = w.mAttrs;
8588
8589 if (w.mSurface != null) {
8590 // Execute animation.
8591 w.commitFinishDrawingLocked(currentTime);
8592 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8593 animating = true;
8594 //w.dump(" ");
8595 }
8596
8597 mPolicy.animatingWindowLw(w, attrs);
8598 }
8599
8600 final AppWindowToken atoken = w.mAppToken;
8601 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8602 if (atoken.lastTransactionSequence != transactionSequence) {
8603 atoken.lastTransactionSequence = transactionSequence;
8604 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8605 atoken.startingDisplayed = false;
8606 }
8607 if ((w.isOnScreen() || w.mAttrs.type
8608 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8609 && !w.mExiting && !w.mDestroying) {
8610 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8611 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8612 + w.isDisplayedLw()
8613 + ", isAnimating=" + w.isAnimating());
8614 if (!w.isDisplayedLw()) {
8615 Log.v(TAG, "Not displayed: s=" + w.mSurface
8616 + " pv=" + w.mPolicyVisibility
8617 + " dp=" + w.mDrawPending
8618 + " cdp=" + w.mCommitDrawPending
8619 + " ah=" + w.mAttachedHidden
8620 + " th=" + atoken.hiddenRequested
8621 + " a=" + w.mAnimating);
8622 }
8623 }
8624 if (w != atoken.startingWindow) {
8625 if (!atoken.freezingScreen || !w.mAppFreezing) {
8626 atoken.numInterestingWindows++;
8627 if (w.isDisplayedLw()) {
8628 atoken.numDrawnWindows++;
8629 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8630 "tokenMayBeDrawn: " + atoken
8631 + " freezingScreen=" + atoken.freezingScreen
8632 + " mAppFreezing=" + w.mAppFreezing);
8633 tokenMayBeDrawn = true;
8634 }
8635 }
8636 } else if (w.isDisplayedLw()) {
8637 atoken.startingDisplayed = true;
8638 }
8639 }
8640 } else if (w.mReadyToShow) {
8641 w.performShowLocked();
8642 }
8643 }
8644
8645 if (mPolicy.finishAnimationLw()) {
8646 restart = true;
8647 }
8648
8649 if (tokenMayBeDrawn) {
8650 // See if any windows have been drawn, so they (and others
8651 // associated with them) can now be shown.
8652 final int NT = mTokenList.size();
8653 for (i=0; i<NT; i++) {
8654 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8655 if (wtoken == null) {
8656 continue;
8657 }
8658 if (wtoken.freezingScreen) {
8659 int numInteresting = wtoken.numInterestingWindows;
8660 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8661 if (DEBUG_VISIBILITY) Log.v(TAG,
8662 "allDrawn: " + wtoken
8663 + " interesting=" + numInteresting
8664 + " drawn=" + wtoken.numDrawnWindows);
8665 wtoken.showAllWindowsLocked();
8666 unsetAppFreezingScreenLocked(wtoken, false, true);
8667 orientationChangeComplete = true;
8668 }
8669 } else if (!wtoken.allDrawn) {
8670 int numInteresting = wtoken.numInterestingWindows;
8671 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8672 if (DEBUG_VISIBILITY) Log.v(TAG,
8673 "allDrawn: " + wtoken
8674 + " interesting=" + numInteresting
8675 + " drawn=" + wtoken.numDrawnWindows);
8676 wtoken.allDrawn = true;
8677 restart = true;
8678
8679 // We can now show all of the drawn windows!
8680 if (!mOpeningApps.contains(wtoken)) {
8681 wtoken.showAllWindowsLocked();
8682 }
8683 }
8684 }
8685 }
8686 }
8687
8688 // If we are ready to perform an app transition, check through
8689 // all of the app tokens to be shown and see if they are ready
8690 // to go.
8691 if (mAppTransitionReady) {
8692 int NN = mOpeningApps.size();
8693 boolean goodToGo = true;
8694 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8695 "Checking " + NN + " opening apps (frozen="
8696 + mDisplayFrozen + " timeout="
8697 + mAppTransitionTimeout + ")...");
8698 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8699 // If the display isn't frozen, wait to do anything until
8700 // all of the apps are ready. Otherwise just go because
8701 // we'll unfreeze the display when everyone is ready.
8702 for (i=0; i<NN && goodToGo; i++) {
8703 AppWindowToken wtoken = mOpeningApps.get(i);
8704 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8705 "Check opening app" + wtoken + ": allDrawn="
8706 + wtoken.allDrawn + " startingDisplayed="
8707 + wtoken.startingDisplayed);
8708 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8709 && !wtoken.startingMoved) {
8710 goodToGo = false;
8711 }
8712 }
8713 }
8714 if (goodToGo) {
8715 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8716 int transit = mNextAppTransition;
8717 if (mSkipAppTransitionAnimation) {
8718 transit = WindowManagerPolicy.TRANSIT_NONE;
8719 }
8720 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8721 mAppTransitionReady = false;
8722 mAppTransitionTimeout = false;
8723 mStartingIconInTransition = false;
8724 mSkipAppTransitionAnimation = false;
8725
8726 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8727
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008728 boolean wallpaperMoved = adjustWallpaperWindowsLocked();
8729 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8730 "Old wallpaper target=" + mWallpaperTarget
8731 + ", upcoming target=" + mUpcomingWallpaperTarget);
8732 if (mUpcomingWallpaperTarget != mWallpaperTarget &&
8733 mUpcomingWallpaperTarget != null &&
8734 mWallpaperTarget != null) {
8735 // Need to determine if both the closing and
8736 // opening app token sets are wallpaper targets,
8737 // in which case special animations are needed
8738 // (since the wallpaper needs to stay static
8739 // behind them).
8740 int found = 0;
8741 NN = mOpeningApps.size();
8742 for (i=0; i<NN; i++) {
8743 AppWindowToken wtoken = mOpeningApps.get(i);
8744 if (mUpcomingWallpaperTarget.mAppToken == wtoken) {
8745 found |= 1;
8746 }
8747 if (mWallpaperTarget.mAppToken == wtoken) {
8748 found |= 1;
8749 }
8750 }
8751 NN = mClosingApps.size();
8752 for (i=0; i<NN; i++) {
8753 AppWindowToken wtoken = mClosingApps.get(i);
8754 if (mUpcomingWallpaperTarget.mAppToken == wtoken) {
8755 found |= 2;
8756 }
8757 if (mWallpaperTarget.mAppToken == wtoken) {
8758 found |= 2;
8759 }
8760 }
8761
8762 if (found == 3) {
8763 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8764 "Wallpaper animation!");
8765 switch (transit) {
8766 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
8767 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
8768 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
8769 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN;
8770 break;
8771 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
8772 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
8773 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
8774 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE;
8775 break;
8776 }
8777 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8778 "New transit: " + transit);
8779 }
8780 }
8781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 // We need to figure out which animation to use...
8783 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8784 mOpeningApps, mClosingApps);
8785
8786 NN = mOpeningApps.size();
8787 for (i=0; i<NN; i++) {
8788 AppWindowToken wtoken = mOpeningApps.get(i);
8789 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8790 "Now opening app" + wtoken);
8791 wtoken.reportedVisible = false;
8792 wtoken.inPendingTransaction = false;
8793 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8794 wtoken.updateReportedVisibilityLocked();
8795 wtoken.showAllWindowsLocked();
8796 }
8797 NN = mClosingApps.size();
8798 for (i=0; i<NN; i++) {
8799 AppWindowToken wtoken = mClosingApps.get(i);
8800 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8801 "Now closing app" + wtoken);
8802 wtoken.inPendingTransaction = false;
8803 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8804 wtoken.updateReportedVisibilityLocked();
8805 // Force the allDrawn flag, because we want to start
8806 // this guy's animations regardless of whether it's
8807 // gotten drawn.
8808 wtoken.allDrawn = true;
8809 }
8810
8811 mOpeningApps.clear();
8812 mClosingApps.clear();
8813
8814 // This has changed the visibility of windows, so perform
8815 // a new layout to get them all up-to-date.
8816 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008817 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8818 assignLayersLocked();
8819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 performLayoutLockedInner();
8821 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8822
8823 restart = true;
8824 }
8825 }
8826 } while (restart);
8827
8828 // THIRD LOOP: Update the surfaces of all windows.
8829
8830 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8831
8832 boolean obscured = false;
8833 boolean blurring = false;
8834 boolean dimming = false;
8835 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008836 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008837 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838
8839 for (i=N-1; i>=0; i--) {
8840 WindowState w = (WindowState)mWindows.get(i);
8841
8842 boolean displayed = false;
8843 final WindowManager.LayoutParams attrs = w.mAttrs;
8844 final int attrFlags = attrs.flags;
8845
8846 if (w.mSurface != null) {
8847 w.computeShownFrameLocked();
8848 if (localLOGV) Log.v(
8849 TAG, "Placing surface #" + i + " " + w.mSurface
8850 + ": new=" + w.mShownFrame + ", old="
8851 + w.mLastShownFrame);
8852
8853 boolean resize;
8854 int width, height;
8855 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8856 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8857 w.mLastRequestedHeight != w.mRequestedHeight;
8858 // for a scaled surface, we just want to use
8859 // the requested size.
8860 width = w.mRequestedWidth;
8861 height = w.mRequestedHeight;
8862 w.mLastRequestedWidth = width;
8863 w.mLastRequestedHeight = height;
8864 w.mLastShownFrame.set(w.mShownFrame);
8865 try {
8866 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8867 } catch (RuntimeException e) {
8868 Log.w(TAG, "Error positioning surface in " + w, e);
8869 if (!recoveringMemory) {
8870 reclaimSomeSurfaceMemoryLocked(w, "position");
8871 }
8872 }
8873 } else {
8874 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8875 width = w.mShownFrame.width();
8876 height = w.mShownFrame.height();
8877 w.mLastShownFrame.set(w.mShownFrame);
8878 if (resize) {
8879 if (SHOW_TRANSACTIONS) Log.i(
8880 TAG, " SURFACE " + w.mSurface + ": ("
8881 + w.mShownFrame.left + ","
8882 + w.mShownFrame.top + ") ("
8883 + w.mShownFrame.width() + "x"
8884 + w.mShownFrame.height() + ")");
8885 }
8886 }
8887
8888 if (resize) {
8889 if (width < 1) width = 1;
8890 if (height < 1) height = 1;
8891 if (w.mSurface != null) {
8892 try {
8893 w.mSurface.setSize(width, height);
8894 w.mSurface.setPosition(w.mShownFrame.left,
8895 w.mShownFrame.top);
8896 } catch (RuntimeException e) {
8897 // If something goes wrong with the surface (such
8898 // as running out of memory), don't take down the
8899 // entire system.
8900 Log.e(TAG, "Failure updating surface of " + w
8901 + "size=(" + width + "x" + height
8902 + "), pos=(" + w.mShownFrame.left
8903 + "," + w.mShownFrame.top + ")", e);
8904 if (!recoveringMemory) {
8905 reclaimSomeSurfaceMemoryLocked(w, "size");
8906 }
8907 }
8908 }
8909 }
8910 if (!w.mAppFreezing) {
8911 w.mContentInsetsChanged =
8912 !w.mLastContentInsets.equals(w.mContentInsets);
8913 w.mVisibleInsetsChanged =
8914 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008915 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 || w.mContentInsetsChanged
8917 || w.mVisibleInsetsChanged) {
8918 w.mLastFrame.set(w.mFrame);
8919 w.mLastContentInsets.set(w.mContentInsets);
8920 w.mLastVisibleInsets.set(w.mVisibleInsets);
8921 // If the orientation is changing, then we need to
8922 // hold off on unfreezing the display until this
8923 // window has been redrawn; to do that, we need
8924 // to go through the process of getting informed
8925 // by the application when it has finished drawing.
8926 if (w.mOrientationChanging) {
8927 if (DEBUG_ORIENTATION) Log.v(TAG,
8928 "Orientation start waiting for draw in "
8929 + w + ", surface " + w.mSurface);
8930 w.mDrawPending = true;
8931 w.mCommitDrawPending = false;
8932 w.mReadyToShow = false;
8933 if (w.mAppToken != null) {
8934 w.mAppToken.allDrawn = false;
8935 }
8936 }
Romain Guy06882f82009-06-10 13:36:04 -07008937 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 "Resizing window " + w + " to " + w.mFrame);
8939 mResizingWindows.add(w);
8940 } else if (w.mOrientationChanging) {
8941 if (!w.mDrawPending && !w.mCommitDrawPending) {
8942 if (DEBUG_ORIENTATION) Log.v(TAG,
8943 "Orientation not waiting for draw in "
8944 + w + ", surface " + w.mSurface);
8945 w.mOrientationChanging = false;
8946 }
8947 }
8948 }
8949
8950 if (w.mAttachedHidden) {
8951 if (!w.mLastHidden) {
8952 //dump();
8953 w.mLastHidden = true;
8954 if (SHOW_TRANSACTIONS) Log.i(
8955 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
8956 if (w.mSurface != null) {
8957 try {
8958 w.mSurface.hide();
8959 } catch (RuntimeException e) {
8960 Log.w(TAG, "Exception hiding surface in " + w);
8961 }
8962 }
8963 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8964 }
8965 // If we are waiting for this window to handle an
8966 // orientation change, well, it is hidden, so
8967 // doesn't really matter. Note that this does
8968 // introduce a potential glitch if the window
8969 // becomes unhidden before it has drawn for the
8970 // new orientation.
8971 if (w.mOrientationChanging) {
8972 w.mOrientationChanging = false;
8973 if (DEBUG_ORIENTATION) Log.v(TAG,
8974 "Orientation change skips hidden " + w);
8975 }
8976 } else if (!w.isReadyForDisplay()) {
8977 if (!w.mLastHidden) {
8978 //dump();
8979 w.mLastHidden = true;
8980 if (SHOW_TRANSACTIONS) Log.i(
8981 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
8982 if (w.mSurface != null) {
8983 try {
8984 w.mSurface.hide();
8985 } catch (RuntimeException e) {
8986 Log.w(TAG, "Exception exception hiding surface in " + w);
8987 }
8988 }
8989 mKeyWaiter.releasePendingPointerLocked(w.mSession);
8990 }
8991 // If we are waiting for this window to handle an
8992 // orientation change, well, it is hidden, so
8993 // doesn't really matter. Note that this does
8994 // introduce a potential glitch if the window
8995 // becomes unhidden before it has drawn for the
8996 // new orientation.
8997 if (w.mOrientationChanging) {
8998 w.mOrientationChanging = false;
8999 if (DEBUG_ORIENTATION) Log.v(TAG,
9000 "Orientation change skips hidden " + w);
9001 }
9002 } else if (w.mLastLayer != w.mAnimLayer
9003 || w.mLastAlpha != w.mShownAlpha
9004 || w.mLastDsDx != w.mDsDx
9005 || w.mLastDtDx != w.mDtDx
9006 || w.mLastDsDy != w.mDsDy
9007 || w.mLastDtDy != w.mDtDy
9008 || w.mLastHScale != w.mHScale
9009 || w.mLastVScale != w.mVScale
9010 || w.mLastHidden) {
9011 displayed = true;
9012 w.mLastAlpha = w.mShownAlpha;
9013 w.mLastLayer = w.mAnimLayer;
9014 w.mLastDsDx = w.mDsDx;
9015 w.mLastDtDx = w.mDtDx;
9016 w.mLastDsDy = w.mDsDy;
9017 w.mLastDtDy = w.mDtDy;
9018 w.mLastHScale = w.mHScale;
9019 w.mLastVScale = w.mVScale;
9020 if (SHOW_TRANSACTIONS) Log.i(
9021 TAG, " SURFACE " + w.mSurface + ": alpha="
9022 + w.mShownAlpha + " layer=" + w.mAnimLayer);
9023 if (w.mSurface != null) {
9024 try {
9025 w.mSurface.setAlpha(w.mShownAlpha);
9026 w.mSurface.setLayer(w.mAnimLayer);
9027 w.mSurface.setMatrix(
9028 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9029 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9030 } catch (RuntimeException e) {
9031 Log.w(TAG, "Error updating surface in " + w, e);
9032 if (!recoveringMemory) {
9033 reclaimSomeSurfaceMemoryLocked(w, "update");
9034 }
9035 }
9036 }
9037
9038 if (w.mLastHidden && !w.mDrawPending
9039 && !w.mCommitDrawPending
9040 && !w.mReadyToShow) {
9041 if (SHOW_TRANSACTIONS) Log.i(
9042 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9043 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9044 + " during relayout");
9045 if (showSurfaceRobustlyLocked(w)) {
9046 w.mHasDrawn = true;
9047 w.mLastHidden = false;
9048 } else {
9049 w.mOrientationChanging = false;
9050 }
9051 }
9052 if (w.mSurface != null) {
9053 w.mToken.hasVisible = true;
9054 }
9055 } else {
9056 displayed = true;
9057 }
9058
9059 if (displayed) {
9060 if (!covered) {
9061 if (attrs.width == LayoutParams.FILL_PARENT
9062 && attrs.height == LayoutParams.FILL_PARENT) {
9063 covered = true;
9064 }
9065 }
9066 if (w.mOrientationChanging) {
9067 if (w.mDrawPending || w.mCommitDrawPending) {
9068 orientationChangeComplete = false;
9069 if (DEBUG_ORIENTATION) Log.v(TAG,
9070 "Orientation continue waiting for draw in " + w);
9071 } else {
9072 w.mOrientationChanging = false;
9073 if (DEBUG_ORIENTATION) Log.v(TAG,
9074 "Orientation change complete in " + w);
9075 }
9076 }
9077 w.mToken.hasVisible = true;
9078 }
9079 } else if (w.mOrientationChanging) {
9080 if (DEBUG_ORIENTATION) Log.v(TAG,
9081 "Orientation change skips hidden " + w);
9082 w.mOrientationChanging = false;
9083 }
9084
9085 final boolean canBeSeen = w.isDisplayedLw();
9086
9087 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9088 focusDisplayed = true;
9089 }
9090
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009091 final boolean obscuredChanged = w.mObscured != obscured;
9092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009094 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 if (w.mSurface != null) {
9096 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9097 holdScreen = w.mSession;
9098 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009099 if (!syswin && w.mAttrs.screenBrightness >= 0
9100 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 screenBrightness = w.mAttrs.screenBrightness;
9102 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009103 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9104 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9105 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9106 syswin = true;
9107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009109
9110 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009111 if ((opaqueDrawn && w.isFullscreen(dw, dh))
9112 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 // This window completely covers everything behind it,
9114 // so we want to leave all of them as unblurred (for
9115 // performance reasons).
9116 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009117 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9118 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009119 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009120 obscured = true;
9121 if (mBackgroundFillerSurface == null) {
9122 try {
9123 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9124 0, dw, dh,
9125 PixelFormat.OPAQUE,
9126 Surface.FX_SURFACE_NORMAL);
9127 } catch (Exception e) {
9128 Log.e(TAG, "Exception creating filler surface", e);
9129 }
9130 }
9131 try {
9132 mBackgroundFillerSurface.setPosition(0, 0);
9133 mBackgroundFillerSurface.setSize(dw, dh);
9134 // Using the same layer as Dim because they will never be shown at the
9135 // same time.
9136 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9137 mBackgroundFillerSurface.show();
9138 } catch (RuntimeException e) {
9139 Log.e(TAG, "Exception showing filler surface");
9140 }
9141 backgroundFillerShown = true;
9142 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009143 } else if (canBeSeen && !obscured &&
9144 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9145 if (localLOGV) Log.v(TAG, "Win " + w
9146 + ": blurring=" + blurring
9147 + " obscured=" + obscured
9148 + " displayed=" + displayed);
9149 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9150 if (!dimming) {
9151 //Log.i(TAG, "DIM BEHIND: " + w);
9152 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009153 if (mDimAnimator == null) {
9154 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009156 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009158 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159 }
9160 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9161 if (!blurring) {
9162 //Log.i(TAG, "BLUR BEHIND: " + w);
9163 blurring = true;
9164 mBlurShown = true;
9165 if (mBlurSurface == null) {
9166 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9167 + mBlurSurface + ": CREATE");
9168 try {
Romain Guy06882f82009-06-10 13:36:04 -07009169 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 -1, 16, 16,
9171 PixelFormat.OPAQUE,
9172 Surface.FX_SURFACE_BLUR);
9173 } catch (Exception e) {
9174 Log.e(TAG, "Exception creating Blur surface", e);
9175 }
9176 }
9177 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9178 + mBlurSurface + ": SHOW pos=(0,0) (" +
9179 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9180 if (mBlurSurface != null) {
9181 mBlurSurface.setPosition(0, 0);
9182 mBlurSurface.setSize(dw, dh);
9183 try {
9184 mBlurSurface.show();
9185 } catch (RuntimeException e) {
9186 Log.w(TAG, "Failure showing blur surface", e);
9187 }
9188 }
9189 }
9190 mBlurSurface.setLayer(w.mAnimLayer-2);
9191 }
9192 }
9193 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009194
9195 if (obscuredChanged && mWallpaperTarget == w) {
9196 // This is the wallpaper target and its obscured state
9197 // changed... make sure the current wallaper's visibility
9198 // has been updated accordingly.
9199 updateWallpaperVisibilityLocked();
9200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009202
9203 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9204 mBackgroundFillerShown = false;
9205 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9206 try {
9207 mBackgroundFillerSurface.hide();
9208 } catch (RuntimeException e) {
9209 Log.e(TAG, "Exception hiding filler surface", e);
9210 }
9211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009213 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9214 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 }
Romain Guy06882f82009-06-10 13:36:04 -07009216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 if (!blurring && mBlurShown) {
9218 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9219 + ": HIDE");
9220 try {
9221 mBlurSurface.hide();
9222 } catch (IllegalArgumentException e) {
9223 Log.w(TAG, "Illegal argument exception hiding blur surface");
9224 }
9225 mBlurShown = false;
9226 }
9227
9228 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9229 } catch (RuntimeException e) {
9230 Log.e(TAG, "Unhandled exception in Window Manager", e);
9231 }
9232
9233 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9236 "With display frozen, orientationChangeComplete="
9237 + orientationChangeComplete);
9238 if (orientationChangeComplete) {
9239 if (mWindowsFreezingScreen) {
9240 mWindowsFreezingScreen = false;
9241 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9242 }
9243 if (mAppsFreezingScreen == 0) {
9244 stopFreezingDisplayLocked();
9245 }
9246 }
Romain Guy06882f82009-06-10 13:36:04 -07009247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 i = mResizingWindows.size();
9249 if (i > 0) {
9250 do {
9251 i--;
9252 WindowState win = mResizingWindows.get(i);
9253 try {
9254 win.mClient.resized(win.mFrame.width(),
9255 win.mFrame.height(), win.mLastContentInsets,
9256 win.mLastVisibleInsets, win.mDrawPending);
9257 win.mContentInsetsChanged = false;
9258 win.mVisibleInsetsChanged = false;
9259 } catch (RemoteException e) {
9260 win.mOrientationChanging = false;
9261 }
9262 } while (i > 0);
9263 mResizingWindows.clear();
9264 }
Romain Guy06882f82009-06-10 13:36:04 -07009265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009267 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 i = mDestroySurface.size();
9269 if (i > 0) {
9270 do {
9271 i--;
9272 WindowState win = mDestroySurface.get(i);
9273 win.mDestroying = false;
9274 if (mInputMethodWindow == win) {
9275 mInputMethodWindow = null;
9276 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009277 if (win == mWallpaperTarget) {
9278 wallpaperDestroyed = true;
9279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 win.destroySurfaceLocked();
9281 } while (i > 0);
9282 mDestroySurface.clear();
9283 }
9284
9285 // Time to remove any exiting tokens?
9286 for (i=mExitingTokens.size()-1; i>=0; i--) {
9287 WindowToken token = mExitingTokens.get(i);
9288 if (!token.hasVisible) {
9289 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009290 if (token.windowType == TYPE_WALLPAPER) {
9291 mWallpaperTokens.remove(token);
9292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 }
9294 }
9295
9296 // Time to remove any exiting applications?
9297 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9298 AppWindowToken token = mExitingAppTokens.get(i);
9299 if (!token.hasVisible && !mClosingApps.contains(token)) {
9300 mAppTokens.remove(token);
9301 mExitingAppTokens.remove(i);
9302 }
9303 }
9304
9305 if (focusDisplayed) {
9306 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9307 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009308 if (wallpaperDestroyed) {
9309 wallpaperDestroyed = adjustWallpaperWindowsLocked();
9310 }
9311 if (wallpaperDestroyed) {
9312 requestAnimationLocked(0);
9313 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9315 }
9316 mQueue.setHoldScreenLocked(holdScreen != null);
9317 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9318 mPowerManager.setScreenBrightnessOverride(-1);
9319 } else {
9320 mPowerManager.setScreenBrightnessOverride((int)
9321 (screenBrightness * Power.BRIGHTNESS_ON));
9322 }
9323 if (holdScreen != mHoldingScreenOn) {
9324 mHoldingScreenOn = holdScreen;
9325 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9326 mH.sendMessage(m);
9327 }
9328 }
9329
9330 void requestAnimationLocked(long delay) {
9331 if (!mAnimationPending) {
9332 mAnimationPending = true;
9333 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9334 }
9335 }
Romain Guy06882f82009-06-10 13:36:04 -07009336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 /**
9338 * Have the surface flinger show a surface, robustly dealing with
9339 * error conditions. In particular, if there is not enough memory
9340 * to show the surface, then we will try to get rid of other surfaces
9341 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009342 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 * @return Returns true if the surface was successfully shown.
9344 */
9345 boolean showSurfaceRobustlyLocked(WindowState win) {
9346 try {
9347 if (win.mSurface != null) {
9348 win.mSurface.show();
9349 }
9350 return true;
9351 } catch (RuntimeException e) {
9352 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9353 }
Romain Guy06882f82009-06-10 13:36:04 -07009354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 return false;
9358 }
Romain Guy06882f82009-06-10 13:36:04 -07009359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9361 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9364 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 if (mForceRemoves == null) {
9367 mForceRemoves = new ArrayList<WindowState>();
9368 }
Romain Guy06882f82009-06-10 13:36:04 -07009369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 long callingIdentity = Binder.clearCallingIdentity();
9371 try {
9372 // There was some problem... first, do a sanity check of the
9373 // window list to make sure we haven't left any dangling surfaces
9374 // around.
9375 int N = mWindows.size();
9376 boolean leakedSurface = false;
9377 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9378 for (int i=0; i<N; i++) {
9379 WindowState ws = (WindowState)mWindows.get(i);
9380 if (ws.mSurface != null) {
9381 if (!mSessions.contains(ws.mSession)) {
9382 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9383 + ws + " surface=" + ws.mSurface
9384 + " token=" + win.mToken
9385 + " pid=" + ws.mSession.mPid
9386 + " uid=" + ws.mSession.mUid);
9387 ws.mSurface.clear();
9388 ws.mSurface = null;
9389 mForceRemoves.add(ws);
9390 i--;
9391 N--;
9392 leakedSurface = true;
9393 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9394 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9395 + ws + " surface=" + ws.mSurface
9396 + " token=" + win.mAppToken);
9397 ws.mSurface.clear();
9398 ws.mSurface = null;
9399 leakedSurface = true;
9400 }
9401 }
9402 }
Romain Guy06882f82009-06-10 13:36:04 -07009403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 boolean killedApps = false;
9405 if (!leakedSurface) {
9406 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9407 SparseIntArray pidCandidates = new SparseIntArray();
9408 for (int i=0; i<N; i++) {
9409 WindowState ws = (WindowState)mWindows.get(i);
9410 if (ws.mSurface != null) {
9411 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9412 }
9413 }
9414 if (pidCandidates.size() > 0) {
9415 int[] pids = new int[pidCandidates.size()];
9416 for (int i=0; i<pids.length; i++) {
9417 pids[i] = pidCandidates.keyAt(i);
9418 }
9419 try {
9420 if (mActivityManager.killPidsForMemory(pids)) {
9421 killedApps = true;
9422 }
9423 } catch (RemoteException e) {
9424 }
9425 }
9426 }
Romain Guy06882f82009-06-10 13:36:04 -07009427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009428 if (leakedSurface || killedApps) {
9429 // We managed to reclaim some memory, so get rid of the trouble
9430 // surface and ask the app to request another one.
9431 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9432 if (surface != null) {
9433 surface.clear();
9434 win.mSurface = null;
9435 }
Romain Guy06882f82009-06-10 13:36:04 -07009436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 try {
9438 win.mClient.dispatchGetNewSurface();
9439 } catch (RemoteException e) {
9440 }
9441 }
9442 } finally {
9443 Binder.restoreCallingIdentity(callingIdentity);
9444 }
9445 }
Romain Guy06882f82009-06-10 13:36:04 -07009446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 private boolean updateFocusedWindowLocked(int mode) {
9448 WindowState newFocus = computeFocusedWindowLocked();
9449 if (mCurrentFocus != newFocus) {
9450 // This check makes sure that we don't already have the focus
9451 // change message pending.
9452 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9453 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9454 if (localLOGV) Log.v(
9455 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9456 final WindowState oldFocus = mCurrentFocus;
9457 mCurrentFocus = newFocus;
9458 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460 final WindowState imWindow = mInputMethodWindow;
9461 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009462 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009464 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9465 mLayoutNeeded = true;
9466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9468 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009469 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9470 // Client will do the layout, but we need to assign layers
9471 // for handleNewWindowLocked() below.
9472 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 }
9474 }
Romain Guy06882f82009-06-10 13:36:04 -07009475
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009476 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9477 mKeyWaiter.handleNewWindowLocked(newFocus);
9478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 return true;
9480 }
9481 return false;
9482 }
9483
9484 private WindowState computeFocusedWindowLocked() {
9485 WindowState result = null;
9486 WindowState win;
9487
9488 int i = mWindows.size() - 1;
9489 int nextAppIndex = mAppTokens.size()-1;
9490 WindowToken nextApp = nextAppIndex >= 0
9491 ? mAppTokens.get(nextAppIndex) : null;
9492
9493 while (i >= 0) {
9494 win = (WindowState)mWindows.get(i);
9495
9496 if (localLOGV || DEBUG_FOCUS) Log.v(
9497 TAG, "Looking for focus: " + i
9498 + " = " + win
9499 + ", flags=" + win.mAttrs.flags
9500 + ", canReceive=" + win.canReceiveKeys());
9501
9502 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 // If this window's application has been removed, just skip it.
9505 if (thisApp != null && thisApp.removed) {
9506 i--;
9507 continue;
9508 }
Romain Guy06882f82009-06-10 13:36:04 -07009509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 // If there is a focused app, don't allow focus to go to any
9511 // windows below it. If this is an application window, step
9512 // through the app tokens until we find its app.
9513 if (thisApp != null && nextApp != null && thisApp != nextApp
9514 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9515 int origAppIndex = nextAppIndex;
9516 while (nextAppIndex > 0) {
9517 if (nextApp == mFocusedApp) {
9518 // Whoops, we are below the focused app... no focus
9519 // for you!
9520 if (localLOGV || DEBUG_FOCUS) Log.v(
9521 TAG, "Reached focused app: " + mFocusedApp);
9522 return null;
9523 }
9524 nextAppIndex--;
9525 nextApp = mAppTokens.get(nextAppIndex);
9526 if (nextApp == thisApp) {
9527 break;
9528 }
9529 }
9530 if (thisApp != nextApp) {
9531 // Uh oh, the app token doesn't exist! This shouldn't
9532 // happen, but if it does we can get totally hosed...
9533 // so restart at the original app.
9534 nextAppIndex = origAppIndex;
9535 nextApp = mAppTokens.get(nextAppIndex);
9536 }
9537 }
9538
9539 // Dispatch to this window if it is wants key events.
9540 if (win.canReceiveKeys()) {
9541 if (DEBUG_FOCUS) Log.v(
9542 TAG, "Found focus @ " + i + " = " + win);
9543 result = win;
9544 break;
9545 }
9546
9547 i--;
9548 }
9549
9550 return result;
9551 }
9552
9553 private void startFreezingDisplayLocked() {
9554 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009555 // Freezing the display also suspends key event delivery, to
9556 // keep events from going astray while the display is reconfigured.
9557 // If someone has changed orientation again while the screen is
9558 // still frozen, the events will continue to be blocked while the
9559 // successive orientation change is processed. To prevent spurious
9560 // ANRs, we reset the event dispatch timeout in this case.
9561 synchronized (mKeyWaiter) {
9562 mKeyWaiter.mWasFrozen = true;
9563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 return;
9565 }
Romain Guy06882f82009-06-10 13:36:04 -07009566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 long now = SystemClock.uptimeMillis();
9570 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9571 if (mFreezeGcPending != 0) {
9572 if (now > (mFreezeGcPending+1000)) {
9573 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9574 mH.removeMessages(H.FORCE_GC);
9575 Runtime.getRuntime().gc();
9576 mFreezeGcPending = now;
9577 }
9578 } else {
9579 mFreezeGcPending = now;
9580 }
Romain Guy06882f82009-06-10 13:36:04 -07009581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009582 mDisplayFrozen = true;
9583 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9584 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9585 mAppTransitionReady = true;
9586 }
Romain Guy06882f82009-06-10 13:36:04 -07009587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 if (PROFILE_ORIENTATION) {
9589 File file = new File("/data/system/frozen");
9590 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9591 }
9592 Surface.freezeDisplay(0);
9593 }
Romain Guy06882f82009-06-10 13:36:04 -07009594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 private void stopFreezingDisplayLocked() {
9596 if (!mDisplayFrozen) {
9597 return;
9598 }
Romain Guy06882f82009-06-10 13:36:04 -07009599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 mDisplayFrozen = false;
9601 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9602 if (PROFILE_ORIENTATION) {
9603 Debug.stopMethodTracing();
9604 }
9605 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009606
Chris Tate2ad63a92009-03-25 17:36:48 -07009607 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9608 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009609 synchronized (mKeyWaiter) {
9610 mKeyWaiter.mWasFrozen = true;
9611 mKeyWaiter.notifyAll();
9612 }
9613
9614 // A little kludge: a lot could have happened while the
9615 // display was frozen, so now that we are coming back we
9616 // do a gc so that any remote references the system
9617 // processes holds on others can be released if they are
9618 // no longer needed.
9619 mH.removeMessages(H.FORCE_GC);
9620 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9621 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 mScreenFrozenLock.release();
9624 }
Romain Guy06882f82009-06-10 13:36:04 -07009625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009626 @Override
9627 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9628 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9629 != PackageManager.PERMISSION_GRANTED) {
9630 pw.println("Permission Denial: can't dump WindowManager from from pid="
9631 + Binder.getCallingPid()
9632 + ", uid=" + Binder.getCallingUid());
9633 return;
9634 }
Romain Guy06882f82009-06-10 13:36:04 -07009635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009636 synchronized(mWindowMap) {
9637 pw.println("Current Window Manager state:");
9638 for (int i=mWindows.size()-1; i>=0; i--) {
9639 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009640 pw.print(" Window #"); pw.print(i); pw.print(' ');
9641 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 w.dump(pw, " ");
9643 }
9644 if (mInputMethodDialogs.size() > 0) {
9645 pw.println(" ");
9646 pw.println(" Input method dialogs:");
9647 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9648 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009649 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 }
9651 }
9652 if (mPendingRemove.size() > 0) {
9653 pw.println(" ");
9654 pw.println(" Remove pending for:");
9655 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9656 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009657 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9658 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659 w.dump(pw, " ");
9660 }
9661 }
9662 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9663 pw.println(" ");
9664 pw.println(" Windows force removing:");
9665 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9666 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009667 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9668 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 w.dump(pw, " ");
9670 }
9671 }
9672 if (mDestroySurface.size() > 0) {
9673 pw.println(" ");
9674 pw.println(" Windows waiting to destroy their surface:");
9675 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9676 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009677 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9678 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 w.dump(pw, " ");
9680 }
9681 }
9682 if (mLosingFocus.size() > 0) {
9683 pw.println(" ");
9684 pw.println(" Windows losing focus:");
9685 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9686 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009687 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9688 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 w.dump(pw, " ");
9690 }
9691 }
9692 if (mSessions.size() > 0) {
9693 pw.println(" ");
9694 pw.println(" All active sessions:");
9695 Iterator<Session> it = mSessions.iterator();
9696 while (it.hasNext()) {
9697 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009698 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 s.dump(pw, " ");
9700 }
9701 }
9702 if (mTokenMap.size() > 0) {
9703 pw.println(" ");
9704 pw.println(" All tokens:");
9705 Iterator<WindowToken> it = mTokenMap.values().iterator();
9706 while (it.hasNext()) {
9707 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009708 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709 token.dump(pw, " ");
9710 }
9711 }
9712 if (mTokenList.size() > 0) {
9713 pw.println(" ");
9714 pw.println(" Window token list:");
9715 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009716 pw.print(" #"); pw.print(i); pw.print(": ");
9717 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 }
9719 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009720 if (mWallpaperTokens.size() > 0) {
9721 pw.println(" ");
9722 pw.println(" Wallpaper tokens:");
9723 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9724 WindowToken token = mWallpaperTokens.get(i);
9725 pw.print(" Wallpaper #"); pw.print(i);
9726 pw.print(' '); pw.print(token); pw.println(':');
9727 token.dump(pw, " ");
9728 }
9729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009730 if (mAppTokens.size() > 0) {
9731 pw.println(" ");
9732 pw.println(" Application tokens in Z order:");
9733 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009734 pw.print(" App #"); pw.print(i); pw.print(": ");
9735 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736 }
9737 }
9738 if (mFinishedStarting.size() > 0) {
9739 pw.println(" ");
9740 pw.println(" Finishing start of application tokens:");
9741 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9742 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009743 pw.print(" Finished Starting #"); pw.print(i);
9744 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 token.dump(pw, " ");
9746 }
9747 }
9748 if (mExitingTokens.size() > 0) {
9749 pw.println(" ");
9750 pw.println(" Exiting tokens:");
9751 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9752 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009753 pw.print(" Exiting #"); pw.print(i);
9754 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 token.dump(pw, " ");
9756 }
9757 }
9758 if (mExitingAppTokens.size() > 0) {
9759 pw.println(" ");
9760 pw.println(" Exiting application tokens:");
9761 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9762 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009763 pw.print(" Exiting App #"); pw.print(i);
9764 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009765 token.dump(pw, " ");
9766 }
9767 }
9768 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009769 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9770 pw.print(" mLastFocus="); pw.println(mLastFocus);
9771 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9772 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9773 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -07009774 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009775 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9776 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9777 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9778 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9779 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009780 if (mDimAnimator != null) {
9781 mDimAnimator.printTo(pw);
9782 } else {
9783 pw.print( " no DimAnimator ");
9784 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009785 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009786 pw.print(mInputMethodAnimLayerAdjustment);
9787 pw.print(" mWallpaperAnimLayerAdjustment=");
9788 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009789 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9790 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9791 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9792 pw.print(" mRotation="); pw.print(mRotation);
9793 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9794 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9795 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9796 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9797 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9798 pw.print(" mNextAppTransition=0x");
9799 pw.print(Integer.toHexString(mNextAppTransition));
9800 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9801 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9802 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9803 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9804 if (mOpeningApps.size() > 0) {
9805 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9806 }
9807 if (mClosingApps.size() > 0) {
9808 pw.print(" mClosingApps="); pw.println(mClosingApps);
9809 }
9810 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9811 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009812 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009813 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9814 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9815 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9816 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9817 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9818 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 }
9820 }
9821
9822 public void monitor() {
9823 synchronized (mWindowMap) { }
9824 synchronized (mKeyguardDisabled) { }
9825 synchronized (mKeyWaiter) { }
9826 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009827
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009828 public void virtualKeyFeedback(KeyEvent event) {
9829 mPolicy.keyFeedbackFromInput(event);
9830 }
9831
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009832 /**
9833 * DimAnimator class that controls the dim animation. This holds the surface and
9834 * all state used for dim animation.
9835 */
9836 private static class DimAnimator {
9837 Surface mDimSurface;
9838 boolean mDimShown = false;
9839 float mDimCurrentAlpha;
9840 float mDimTargetAlpha;
9841 float mDimDeltaPerMs;
9842 long mLastDimAnimTime;
9843
9844 DimAnimator (SurfaceSession session) {
9845 if (mDimSurface == null) {
9846 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9847 + mDimSurface + ": CREATE");
9848 try {
9849 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9850 Surface.FX_SURFACE_DIM);
9851 } catch (Exception e) {
9852 Log.e(TAG, "Exception creating Dim surface", e);
9853 }
9854 }
9855 }
9856
9857 /**
9858 * Show the dim surface.
9859 */
9860 void show(int dw, int dh) {
9861 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9862 dw + "x" + dh + ")");
9863 mDimShown = true;
9864 try {
9865 mDimSurface.setPosition(0, 0);
9866 mDimSurface.setSize(dw, dh);
9867 mDimSurface.show();
9868 } catch (RuntimeException e) {
9869 Log.w(TAG, "Failure showing dim surface", e);
9870 }
9871 }
9872
9873 /**
9874 * Set's the dim surface's layer and update dim parameters that will be used in
9875 * {@link updateSurface} after all windows are examined.
9876 */
9877 void updateParameters(WindowState w, long currentTime) {
9878 mDimSurface.setLayer(w.mAnimLayer-1);
9879
9880 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9881 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9882 if (mDimTargetAlpha != target) {
9883 // If the desired dim level has changed, then
9884 // start an animation to it.
9885 mLastDimAnimTime = currentTime;
9886 long duration = (w.mAnimating && w.mAnimation != null)
9887 ? w.mAnimation.computeDurationHint()
9888 : DEFAULT_DIM_DURATION;
9889 if (target > mDimTargetAlpha) {
9890 // This is happening behind the activity UI,
9891 // so we can make it run a little longer to
9892 // give a stronger impression without disrupting
9893 // the user.
9894 duration *= DIM_DURATION_MULTIPLIER;
9895 }
9896 if (duration < 1) {
9897 // Don't divide by zero
9898 duration = 1;
9899 }
9900 mDimTargetAlpha = target;
9901 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9902 }
9903 }
9904
9905 /**
9906 * Updating the surface's alpha. Returns true if the animation continues, or returns
9907 * false when the animation is finished and the dim surface is hidden.
9908 */
9909 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9910 if (!dimming) {
9911 if (mDimTargetAlpha != 0) {
9912 mLastDimAnimTime = currentTime;
9913 mDimTargetAlpha = 0;
9914 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9915 }
9916 }
9917
9918 boolean animating = false;
9919 if (mLastDimAnimTime != 0) {
9920 mDimCurrentAlpha += mDimDeltaPerMs
9921 * (currentTime-mLastDimAnimTime);
9922 boolean more = true;
9923 if (displayFrozen) {
9924 // If the display is frozen, there is no reason to animate.
9925 more = false;
9926 } else if (mDimDeltaPerMs > 0) {
9927 if (mDimCurrentAlpha > mDimTargetAlpha) {
9928 more = false;
9929 }
9930 } else if (mDimDeltaPerMs < 0) {
9931 if (mDimCurrentAlpha < mDimTargetAlpha) {
9932 more = false;
9933 }
9934 } else {
9935 more = false;
9936 }
9937
9938 // Do we need to continue animating?
9939 if (more) {
9940 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9941 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
9942 mLastDimAnimTime = currentTime;
9943 mDimSurface.setAlpha(mDimCurrentAlpha);
9944 animating = true;
9945 } else {
9946 mDimCurrentAlpha = mDimTargetAlpha;
9947 mLastDimAnimTime = 0;
9948 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9949 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
9950 mDimSurface.setAlpha(mDimCurrentAlpha);
9951 if (!dimming) {
9952 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
9953 + ": HIDE");
9954 try {
9955 mDimSurface.hide();
9956 } catch (RuntimeException e) {
9957 Log.w(TAG, "Illegal argument exception hiding dim surface");
9958 }
9959 mDimShown = false;
9960 }
9961 }
9962 }
9963 return animating;
9964 }
9965
9966 public void printTo(PrintWriter pw) {
9967 pw.print(" mDimShown="); pw.print(mDimShown);
9968 pw.print(" current="); pw.print(mDimCurrentAlpha);
9969 pw.print(" target="); pw.print(mDimTargetAlpha);
9970 pw.print(" delta="); pw.print(mDimDeltaPerMs);
9971 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
9972 }
9973 }
9974
9975 /**
9976 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
9977 * This is used for opening/closing transition for apps in compatible mode.
9978 */
9979 private static class FadeInOutAnimation extends Animation {
9980 int mWidth;
9981 boolean mFadeIn;
9982
9983 public FadeInOutAnimation(boolean fadeIn) {
9984 setInterpolator(new AccelerateInterpolator());
9985 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
9986 mFadeIn = fadeIn;
9987 }
9988
9989 @Override
9990 protected void applyTransformation(float interpolatedTime, Transformation t) {
9991 float x = interpolatedTime;
9992 if (!mFadeIn) {
9993 x = 1.0f - x; // reverse the interpolation for fade out
9994 }
9995 if (x < 0.5) {
9996 // move the window out of the screen.
9997 t.getMatrix().setTranslate(mWidth, 0);
9998 } else {
9999 t.getMatrix().setTranslate(0, 0);// show
10000 t.setAlpha((x - 0.5f) * 2);
10001 }
10002 }
10003
10004 @Override
10005 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10006 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10007 mWidth = width;
10008 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010009
10010 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010011 public int getZAdjustment() {
10012 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010013 }
10014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015}