blob: f8ba058480e58a3ad1293b1ca3f6f3612a80edb2 [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 Hackborn3be63c02009-08-20 19:31:38 -0700408 // If non-null, we are in the middle of animating from one wallpaper target
409 // to another, and this is the lower one in Z-order.
410 WindowState mLowerWallpaperTarget = null;
411 // If non-null, we are in the middle of animating from one wallpaper target
412 // to another, and this is the higher one in Z-order.
413 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700414 int mWallpaperAnimLayerAdjustment;
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 AppWindowToken mFocusedApp = null;
417
418 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 float mWindowAnimationScale = 1.0f;
421 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 final KeyWaiter mKeyWaiter = new KeyWaiter();
424 final KeyQ mQueue;
425 final InputDispatcherThread mInputThread;
426
427 // Who is holding the screen on.
428 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Whether the UI is currently running in touch mode (not showing
432 * navigational focus because the user is directly pressing the screen).
433 */
434 boolean mInTouchMode = false;
435
436 private ViewServer mViewServer;
437
438 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700439
Dianne Hackbornc485a602009-03-24 22:39:49 -0700440 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700441 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700442
443 // The frame use to limit the size of the app running in compatibility mode.
444 Rect mCompatibleScreenFrame = new Rect();
445 // The surface used to fill the outer rim of the app running in compatibility mode.
446 Surface mBackgroundFillerSurface = null;
447 boolean mBackgroundFillerShown = false;
448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 public static WindowManagerService main(Context context,
450 PowerManagerService pm, boolean haveInputMethods) {
451 WMThread thr = new WMThread(context, pm, haveInputMethods);
452 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 synchronized (thr) {
455 while (thr.mService == null) {
456 try {
457 thr.wait();
458 } catch (InterruptedException e) {
459 }
460 }
461 }
Romain Guy06882f82009-06-10 13:36:04 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 return thr.mService;
464 }
Romain Guy06882f82009-06-10 13:36:04 -0700465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 static class WMThread extends Thread {
467 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 private final Context mContext;
470 private final PowerManagerService mPM;
471 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public WMThread(Context context, PowerManagerService pm,
474 boolean haveInputMethods) {
475 super("WindowManager");
476 mContext = context;
477 mPM = pm;
478 mHaveInputMethods = haveInputMethods;
479 }
Romain Guy06882f82009-06-10 13:36:04 -0700480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 public void run() {
482 Looper.prepare();
483 WindowManagerService s = new WindowManagerService(mContext, mPM,
484 mHaveInputMethods);
485 android.os.Process.setThreadPriority(
486 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 synchronized (this) {
489 mService = s;
490 notifyAll();
491 }
Romain Guy06882f82009-06-10 13:36:04 -0700492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 Looper.loop();
494 }
495 }
496
497 static class PolicyThread extends Thread {
498 private final WindowManagerPolicy mPolicy;
499 private final WindowManagerService mService;
500 private final Context mContext;
501 private final PowerManagerService mPM;
502 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public PolicyThread(WindowManagerPolicy policy,
505 WindowManagerService service, Context context,
506 PowerManagerService pm) {
507 super("WindowManagerPolicy");
508 mPolicy = policy;
509 mService = service;
510 mContext = context;
511 mPM = pm;
512 }
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public void run() {
515 Looper.prepare();
516 //Looper.myLooper().setMessageLogging(new LogPrinter(
517 // Log.VERBOSE, "WindowManagerPolicy"));
518 android.os.Process.setThreadPriority(
519 android.os.Process.THREAD_PRIORITY_FOREGROUND);
520 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 synchronized (this) {
523 mRunning = true;
524 notifyAll();
525 }
Romain Guy06882f82009-06-10 13:36:04 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 Looper.loop();
528 }
529 }
530
531 private WindowManagerService(Context context, PowerManagerService pm,
532 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700533 if (MEASURE_LATENCY) {
534 lt = new LatencyTimer(100, 1000);
535 }
536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 mContext = context;
538 mHaveInputMethods = haveInputMethods;
539 mLimitedAlphaCompositing = context.getResources().getBoolean(
540 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 mPowerManager = pm;
543 mPowerManager.setPolicy(mPolicy);
544 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
545 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
546 "SCREEN_FROZEN");
547 mScreenFrozenLock.setReferenceCounted(false);
548
549 mActivityManager = ActivityManagerNative.getDefault();
550 mBatteryStats = BatteryStatsService.getService();
551
552 // Get persisted window scale setting
553 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
554 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
555 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
556 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700557
Michael Chan9f028e62009-08-04 17:37:46 -0700558 int max_events_per_sec = 35;
559 try {
560 max_events_per_sec = Integer.parseInt(SystemProperties
561 .get("windowsmgr.max_events_per_sec"));
562 if (max_events_per_sec < 1) {
563 max_events_per_sec = 35;
564 }
565 } catch (NumberFormatException e) {
566 }
567 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mQueue = new KeyQ();
570
571 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
574 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 synchronized (thr) {
577 while (!thr.mRunning) {
578 try {
579 thr.wait();
580 } catch (InterruptedException e) {
581 }
582 }
583 }
Romain Guy06882f82009-06-10 13:36:04 -0700584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 // Add ourself to the Watchdog monitors.
588 Watchdog.getInstance().addMonitor(this);
589 }
590
591 @Override
592 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
593 throws RemoteException {
594 try {
595 return super.onTransact(code, data, reply, flags);
596 } catch (RuntimeException e) {
597 // The window manager only throws security exceptions, so let's
598 // log all others.
599 if (!(e instanceof SecurityException)) {
600 Log.e(TAG, "Window Manager Crash", e);
601 }
602 throw e;
603 }
604 }
605
606 private void placeWindowAfter(Object pos, WindowState window) {
607 final int i = mWindows.indexOf(pos);
608 if (localLOGV || DEBUG_FOCUS) Log.v(
609 TAG, "Adding window " + window + " at "
610 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
611 mWindows.add(i+1, window);
612 }
613
614 private void placeWindowBefore(Object pos, WindowState window) {
615 final int i = mWindows.indexOf(pos);
616 if (localLOGV || DEBUG_FOCUS) Log.v(
617 TAG, "Adding window " + window + " at "
618 + i + " of " + mWindows.size() + " (before " + pos + ")");
619 mWindows.add(i, window);
620 }
621
622 //This method finds out the index of a window that has the same app token as
623 //win. used for z ordering the windows in mWindows
624 private int findIdxBasedOnAppTokens(WindowState win) {
625 //use a local variable to cache mWindows
626 ArrayList localmWindows = mWindows;
627 int jmax = localmWindows.size();
628 if(jmax == 0) {
629 return -1;
630 }
631 for(int j = (jmax-1); j >= 0; j--) {
632 WindowState wentry = (WindowState)localmWindows.get(j);
633 if(wentry.mAppToken == win.mAppToken) {
634 return j;
635 }
636 }
637 return -1;
638 }
Romain Guy06882f82009-06-10 13:36:04 -0700639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
641 final IWindow client = win.mClient;
642 final WindowToken token = win.mToken;
643 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 final int N = localmWindows.size();
646 final WindowState attached = win.mAttachedWindow;
647 int i;
648 if (attached == null) {
649 int tokenWindowsPos = token.windows.size();
650 if (token.appWindowToken != null) {
651 int index = tokenWindowsPos-1;
652 if (index >= 0) {
653 // If this application has existing windows, we
654 // simply place the new window on top of them... but
655 // keep the starting window on top.
656 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
657 // Base windows go behind everything else.
658 placeWindowBefore(token.windows.get(0), win);
659 tokenWindowsPos = 0;
660 } else {
661 AppWindowToken atoken = win.mAppToken;
662 if (atoken != null &&
663 token.windows.get(index) == atoken.startingWindow) {
664 placeWindowBefore(token.windows.get(index), win);
665 tokenWindowsPos--;
666 } else {
667 int newIdx = findIdxBasedOnAppTokens(win);
668 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700669 //there is a window above this one associated with the same
670 //apptoken note that the window could be a floating window
671 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 //windows associated with this token.
673 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676 }
677 } else {
678 if (localLOGV) Log.v(
679 TAG, "Figuring out where to add app window "
680 + client.asBinder() + " (token=" + token + ")");
681 // Figure out where the window should go, based on the
682 // order of applications.
683 final int NA = mAppTokens.size();
684 Object pos = null;
685 for (i=NA-1; i>=0; i--) {
686 AppWindowToken t = mAppTokens.get(i);
687 if (t == token) {
688 i--;
689 break;
690 }
691 if (t.windows.size() > 0) {
692 pos = t.windows.get(0);
693 }
694 }
695 // We now know the index into the apps. If we found
696 // an app window above, that gives us the position; else
697 // we need to look some more.
698 if (pos != null) {
699 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700700 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 mTokenMap.get(((WindowState)pos).mClient.asBinder());
702 if (atoken != null) {
703 final int NC = atoken.windows.size();
704 if (NC > 0) {
705 WindowState bottom = atoken.windows.get(0);
706 if (bottom.mSubLayer < 0) {
707 pos = bottom;
708 }
709 }
710 }
711 placeWindowBefore(pos, win);
712 } else {
713 while (i >= 0) {
714 AppWindowToken t = mAppTokens.get(i);
715 final int NW = t.windows.size();
716 if (NW > 0) {
717 pos = t.windows.get(NW-1);
718 break;
719 }
720 i--;
721 }
722 if (pos != null) {
723 // Move in front of any windows attached to this
724 // one.
725 WindowToken atoken =
726 mTokenMap.get(((WindowState)pos).mClient.asBinder());
727 if (atoken != null) {
728 final int NC = atoken.windows.size();
729 if (NC > 0) {
730 WindowState top = atoken.windows.get(NC-1);
731 if (top.mSubLayer >= 0) {
732 pos = top;
733 }
734 }
735 }
736 placeWindowAfter(pos, win);
737 } else {
738 // Just search for the start of this layer.
739 final int myLayer = win.mBaseLayer;
740 for (i=0; i<N; i++) {
741 WindowState w = (WindowState)localmWindows.get(i);
742 if (w.mBaseLayer > myLayer) {
743 break;
744 }
745 }
746 if (localLOGV || DEBUG_FOCUS) Log.v(
747 TAG, "Adding window " + win + " at "
748 + i + " of " + N);
749 localmWindows.add(i, win);
750 }
751 }
752 }
753 } else {
754 // Figure out where window should go, based on layer.
755 final int myLayer = win.mBaseLayer;
756 for (i=N-1; i>=0; i--) {
757 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
758 i++;
759 break;
760 }
761 }
762 if (i < 0) i = 0;
763 if (localLOGV || DEBUG_FOCUS) Log.v(
764 TAG, "Adding window " + win + " at "
765 + i + " of " + N);
766 localmWindows.add(i, win);
767 }
768 if (addToToken) {
769 token.windows.add(tokenWindowsPos, win);
770 }
771
772 } else {
773 // Figure out this window's ordering relative to the window
774 // it is attached to.
775 final int NA = token.windows.size();
776 final int sublayer = win.mSubLayer;
777 int largestSublayer = Integer.MIN_VALUE;
778 WindowState windowWithLargestSublayer = null;
779 for (i=0; i<NA; i++) {
780 WindowState w = token.windows.get(i);
781 final int wSublayer = w.mSubLayer;
782 if (wSublayer >= largestSublayer) {
783 largestSublayer = wSublayer;
784 windowWithLargestSublayer = w;
785 }
786 if (sublayer < 0) {
787 // For negative sublayers, we go below all windows
788 // in the same sublayer.
789 if (wSublayer >= sublayer) {
790 if (addToToken) {
791 token.windows.add(i, win);
792 }
793 placeWindowBefore(
794 wSublayer >= 0 ? attached : w, win);
795 break;
796 }
797 } else {
798 // For positive sublayers, we go above all windows
799 // in the same sublayer.
800 if (wSublayer > sublayer) {
801 if (addToToken) {
802 token.windows.add(i, win);
803 }
804 placeWindowBefore(w, win);
805 break;
806 }
807 }
808 }
809 if (i >= NA) {
810 if (addToToken) {
811 token.windows.add(win);
812 }
813 if (sublayer < 0) {
814 placeWindowBefore(attached, win);
815 } else {
816 placeWindowAfter(largestSublayer >= 0
817 ? windowWithLargestSublayer
818 : attached,
819 win);
820 }
821 }
822 }
Romain Guy06882f82009-06-10 13:36:04 -0700823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 if (win.mAppToken != null && addToToken) {
825 win.mAppToken.allAppWindows.add(win);
826 }
827 }
Romain Guy06882f82009-06-10 13:36:04 -0700828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 static boolean canBeImeTarget(WindowState w) {
830 final int fl = w.mAttrs.flags
831 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
832 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
833 return w.isVisibleOrAdding();
834 }
835 return false;
836 }
Romain Guy06882f82009-06-10 13:36:04 -0700837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
839 final ArrayList localmWindows = mWindows;
840 final int N = localmWindows.size();
841 WindowState w = null;
842 int i = N;
843 while (i > 0) {
844 i--;
845 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
848 // + Integer.toHexString(w.mAttrs.flags));
849 if (canBeImeTarget(w)) {
850 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 // Yet more tricksyness! If this window is a "starting"
853 // window, we do actually want to be on top of it, but
854 // it is not -really- where input will go. So if the caller
855 // is not actually looking to move the IME, look down below
856 // for a real window to target...
857 if (!willMove
858 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
859 && i > 0) {
860 WindowState wb = (WindowState)localmWindows.get(i-1);
861 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
862 i--;
863 w = wb;
864 }
865 }
866 break;
867 }
868 }
Romain Guy06882f82009-06-10 13:36:04 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
873 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 if (willMove && w != null) {
876 final WindowState curTarget = mInputMethodTarget;
877 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 // Now some fun for dealing with window animations that
880 // modify the Z order. We need to look at all windows below
881 // the current target that are in this app, finding the highest
882 // visible one in layering.
883 AppWindowToken token = curTarget.mAppToken;
884 WindowState highestTarget = null;
885 int highestPos = 0;
886 if (token.animating || token.animation != null) {
887 int pos = 0;
888 pos = localmWindows.indexOf(curTarget);
889 while (pos >= 0) {
890 WindowState win = (WindowState)localmWindows.get(pos);
891 if (win.mAppToken != token) {
892 break;
893 }
894 if (!win.mRemoved) {
895 if (highestTarget == null || win.mAnimLayer >
896 highestTarget.mAnimLayer) {
897 highestTarget = win;
898 highestPos = pos;
899 }
900 }
901 pos--;
902 }
903 }
Romain Guy06882f82009-06-10 13:36:04 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700906 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 + mNextAppTransition + " " + highestTarget
908 + " animating=" + highestTarget.isAnimating()
909 + " layer=" + highestTarget.mAnimLayer
910 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
913 // If we are currently setting up for an animation,
914 // hold everything until we can find out what will happen.
915 mInputMethodTargetWaitingAnim = true;
916 mInputMethodTarget = highestTarget;
917 return highestPos + 1;
918 } else if (highestTarget.isAnimating() &&
919 highestTarget.mAnimLayer > w.mAnimLayer) {
920 // If the window we are currently targeting is involved
921 // with an animation, and it is on top of the next target
922 // we will be over, then hold off on moving until
923 // that is done.
924 mInputMethodTarget = highestTarget;
925 return highestPos + 1;
926 }
927 }
928 }
929 }
Romain Guy06882f82009-06-10 13:36:04 -0700930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 //Log.i(TAG, "Placing input method @" + (i+1));
932 if (w != null) {
933 if (willMove) {
934 RuntimeException e = new RuntimeException();
935 e.fillInStackTrace();
936 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
937 + mInputMethodTarget + " to " + w, e);
938 mInputMethodTarget = w;
939 if (w.mAppToken != null) {
940 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
941 } else {
942 setInputMethodAnimLayerAdjustment(0);
943 }
944 }
945 return i+1;
946 }
947 if (willMove) {
948 RuntimeException e = new RuntimeException();
949 e.fillInStackTrace();
950 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
951 + mInputMethodTarget + " to null", e);
952 mInputMethodTarget = null;
953 setInputMethodAnimLayerAdjustment(0);
954 }
955 return -1;
956 }
Romain Guy06882f82009-06-10 13:36:04 -0700957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 void addInputMethodWindowToListLocked(WindowState win) {
959 int pos = findDesiredInputMethodWindowIndexLocked(true);
960 if (pos >= 0) {
961 win.mTargetAppToken = mInputMethodTarget.mAppToken;
962 mWindows.add(pos, win);
963 moveInputMethodDialogsLocked(pos+1);
964 return;
965 }
966 win.mTargetAppToken = null;
967 addWindowToListInOrderLocked(win, true);
968 moveInputMethodDialogsLocked(pos);
969 }
Romain Guy06882f82009-06-10 13:36:04 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 void setInputMethodAnimLayerAdjustment(int adj) {
972 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
973 mInputMethodAnimLayerAdjustment = adj;
974 WindowState imw = mInputMethodWindow;
975 if (imw != null) {
976 imw.mAnimLayer = imw.mLayer + adj;
977 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
978 + " anim layer: " + imw.mAnimLayer);
979 int wi = imw.mChildWindows.size();
980 while (wi > 0) {
981 wi--;
982 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
983 cw.mAnimLayer = cw.mLayer + adj;
984 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
985 + " anim layer: " + cw.mAnimLayer);
986 }
987 }
988 int di = mInputMethodDialogs.size();
989 while (di > 0) {
990 di --;
991 imw = mInputMethodDialogs.get(di);
992 imw.mAnimLayer = imw.mLayer + adj;
993 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
994 + " anim layer: " + imw.mAnimLayer);
995 }
996 }
Romain Guy06882f82009-06-10 13:36:04 -0700997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
999 int wpos = mWindows.indexOf(win);
1000 if (wpos >= 0) {
1001 if (wpos < interestingPos) interestingPos--;
1002 mWindows.remove(wpos);
1003 int NC = win.mChildWindows.size();
1004 while (NC > 0) {
1005 NC--;
1006 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1007 int cpos = mWindows.indexOf(cw);
1008 if (cpos >= 0) {
1009 if (cpos < interestingPos) interestingPos--;
1010 mWindows.remove(cpos);
1011 }
1012 }
1013 }
1014 return interestingPos;
1015 }
Romain Guy06882f82009-06-10 13:36:04 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 private void reAddWindowToListInOrderLocked(WindowState win) {
1018 addWindowToListInOrderLocked(win, false);
1019 // This is a hack to get all of the child windows added as well
1020 // at the right position. Child windows should be rare and
1021 // this case should be rare, so it shouldn't be that big a deal.
1022 int wpos = mWindows.indexOf(win);
1023 if (wpos >= 0) {
1024 mWindows.remove(wpos);
1025 reAddWindowLocked(wpos, win);
1026 }
1027 }
Romain Guy06882f82009-06-10 13:36:04 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 void logWindowList(String prefix) {
1030 int N = mWindows.size();
1031 while (N > 0) {
1032 N--;
1033 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1034 }
1035 }
Romain Guy06882f82009-06-10 13:36:04 -07001036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 void moveInputMethodDialogsLocked(int pos) {
1038 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 final int N = dialogs.size();
1041 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1042 for (int i=0; i<N; i++) {
1043 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1044 }
1045 if (DEBUG_INPUT_METHOD) {
1046 Log.v(TAG, "Window list w/pos=" + pos);
1047 logWindowList(" ");
1048 }
Romain Guy06882f82009-06-10 13:36:04 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (pos >= 0) {
1051 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1052 if (pos < mWindows.size()) {
1053 WindowState wp = (WindowState)mWindows.get(pos);
1054 if (wp == mInputMethodWindow) {
1055 pos++;
1056 }
1057 }
1058 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1059 for (int i=0; i<N; i++) {
1060 WindowState win = dialogs.get(i);
1061 win.mTargetAppToken = targetAppToken;
1062 pos = reAddWindowLocked(pos, win);
1063 }
1064 if (DEBUG_INPUT_METHOD) {
1065 Log.v(TAG, "Final window list:");
1066 logWindowList(" ");
1067 }
1068 return;
1069 }
1070 for (int i=0; i<N; i++) {
1071 WindowState win = dialogs.get(i);
1072 win.mTargetAppToken = null;
1073 reAddWindowToListInOrderLocked(win);
1074 if (DEBUG_INPUT_METHOD) {
1075 Log.v(TAG, "No IM target, final list:");
1076 logWindowList(" ");
1077 }
1078 }
1079 }
Romain Guy06882f82009-06-10 13:36:04 -07001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1082 final WindowState imWin = mInputMethodWindow;
1083 final int DN = mInputMethodDialogs.size();
1084 if (imWin == null && DN == 0) {
1085 return false;
1086 }
Romain Guy06882f82009-06-10 13:36:04 -07001087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1089 if (imPos >= 0) {
1090 // In this case, the input method windows are to be placed
1091 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 // First check to see if the input method windows are already
1094 // located here, and contiguous.
1095 final int N = mWindows.size();
1096 WindowState firstImWin = imPos < N
1097 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 // Figure out the actual input method window that should be
1100 // at the bottom of their stack.
1101 WindowState baseImWin = imWin != null
1102 ? imWin : mInputMethodDialogs.get(0);
1103 if (baseImWin.mChildWindows.size() > 0) {
1104 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1105 if (cw.mSubLayer < 0) baseImWin = cw;
1106 }
Romain Guy06882f82009-06-10 13:36:04 -07001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (firstImWin == baseImWin) {
1109 // The windows haven't moved... but are they still contiguous?
1110 // First find the top IM window.
1111 int pos = imPos+1;
1112 while (pos < N) {
1113 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1114 break;
1115 }
1116 pos++;
1117 }
1118 pos++;
1119 // Now there should be no more input method windows above.
1120 while (pos < N) {
1121 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1122 break;
1123 }
1124 pos++;
1125 }
1126 if (pos >= N) {
1127 // All is good!
1128 return false;
1129 }
1130 }
Romain Guy06882f82009-06-10 13:36:04 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 if (imWin != null) {
1133 if (DEBUG_INPUT_METHOD) {
1134 Log.v(TAG, "Moving IM from " + imPos);
1135 logWindowList(" ");
1136 }
1137 imPos = tmpRemoveWindowLocked(imPos, imWin);
1138 if (DEBUG_INPUT_METHOD) {
1139 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1140 logWindowList(" ");
1141 }
1142 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1143 reAddWindowLocked(imPos, imWin);
1144 if (DEBUG_INPUT_METHOD) {
1145 Log.v(TAG, "List after moving IM to " + imPos + ":");
1146 logWindowList(" ");
1147 }
1148 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1149 } else {
1150 moveInputMethodDialogsLocked(imPos);
1151 }
Romain Guy06882f82009-06-10 13:36:04 -07001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 } else {
1154 // In this case, the input method windows go in a fixed layer,
1155 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 if (imWin != null) {
1158 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1159 tmpRemoveWindowLocked(0, imWin);
1160 imWin.mTargetAppToken = null;
1161 reAddWindowToListInOrderLocked(imWin);
1162 if (DEBUG_INPUT_METHOD) {
1163 Log.v(TAG, "List with no IM target:");
1164 logWindowList(" ");
1165 }
1166 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1167 } else {
1168 moveInputMethodDialogsLocked(-1);;
1169 }
Romain Guy06882f82009-06-10 13:36:04 -07001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
Romain Guy06882f82009-06-10 13:36:04 -07001172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (needAssignLayers) {
1174 assignLayersLocked();
1175 }
Romain Guy06882f82009-06-10 13:36:04 -07001176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 return true;
1178 }
Romain Guy06882f82009-06-10 13:36:04 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 void adjustInputMethodDialogsLocked() {
1181 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1182 }
Romain Guy06882f82009-06-10 13:36:04 -07001183
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001184 boolean adjustWallpaperWindowsLocked() {
1185 boolean changed = false;
1186
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001187 final int dw = mDisplay.getWidth();
1188 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001189
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001190 // First find top-most window that has asked to be on top of the
1191 // wallpaper; all wallpapers go behind it.
1192 final ArrayList localmWindows = mWindows;
1193 int N = localmWindows.size();
1194 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001195 WindowState foundW = null;
1196 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001197 int i = N;
1198 while (i > 0) {
1199 i--;
1200 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001201 if (w.mAppToken != null) {
1202 // If this window's app token is hidden and not animating,
1203 // it is of no interest to us.
1204 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1205 if (DEBUG_WALLPAPER) Log.v(TAG,
1206 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001207 continue;
1208 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001209 // If this window's app token is ot fullscreen, also irrelevant.
1210 if (!w.mAppToken.appFullscreen) {
1211 if (DEBUG_WALLPAPER) Log.v(TAG,
1212 "Skipping non-fullscreen token: " + w);
1213 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001214 }
1215 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001216 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1217 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1218 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001219 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
1220 && !w.mDrawPending && !w.mCommitDrawPending) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001221 if (DEBUG_WALLPAPER) Log.v(TAG,
1222 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001223 foundW = w;
1224 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001225 if (w == mWallpaperTarget && w.mAppToken != null
1226 && w.mAppToken.animation != null) {
1227 // The current wallpaper target is animating, so we'll
1228 // look behind it for another possible target and figure
1229 // out what is going on below.
1230 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1231 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001232 continue;
1233 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001234 break;
1235 }
1236 }
1237
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001238 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1239 // If we are currently waiting for an app transition, and either
1240 // the current target or the next target are involved with it,
1241 // then hold off on doing anything with the wallpaper.
1242 // Note that we are checking here for just whether the target
1243 // is part of an app token... which is potentially overly aggressive
1244 // (the app token may not be involved in the transition), but good
1245 // enough (we'll just wait until whatever transition is pending
1246 // executes).
1247 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001248 if (DEBUG_WALLPAPER) Log.v(TAG,
1249 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001250 return false;
1251 }
1252 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001253 if (DEBUG_WALLPAPER) Log.v(TAG,
1254 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001255 return false;
1256 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001257 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001258
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001259 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001260 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001261 Log.v(TAG, "New wallpaper target: " + foundW
1262 + " oldTarget: " + mWallpaperTarget);
1263 }
1264
1265 mLowerWallpaperTarget = null;
1266 mUpperWallpaperTarget = null;
1267
1268 WindowState oldW = mWallpaperTarget;
1269 mWallpaperTarget = foundW;
1270
1271 // Now what is happening... if the current and new targets are
1272 // animating, then we are in our super special mode!
1273 if (foundW != null && foundW.mAppToken != null && oldW != null
1274 && oldW.mAppToken != null) {
1275 if (DEBUG_WALLPAPER) {
1276 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1277 + " old animation: " + oldW.mAppToken.animation);
1278 }
1279 if (foundW.mAppToken.animation != null
1280 && oldW.mAppToken.animation != null) {
1281 int oldI = localmWindows.indexOf(oldW);
1282 if (DEBUG_WALLPAPER) {
1283 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1284 }
1285 if (oldI >= 0) {
1286 if (DEBUG_WALLPAPER) {
1287 Log.v(TAG, "Animating wallpapers: old#" + oldI
1288 + "=" + oldW + "; new#" + foundI
1289 + "=" + foundW);
1290 }
1291
1292 // Set the new target correctly.
1293 if (foundW.mAppToken.hiddenRequested) {
1294 if (DEBUG_WALLPAPER) {
1295 Log.v(TAG, "Old wallpaper still the target.");
1296 }
1297 mWallpaperTarget = oldW;
1298 }
1299
1300 // Now set the upper and lower wallpaper targets
1301 // correctly, and make sure that we are positioning
1302 // the wallpaper below the lower.
1303 if (foundI > oldI) {
1304 // The new target is on top of the old one.
1305 if (DEBUG_WALLPAPER) {
1306 Log.v(TAG, "Found target above old target.");
1307 }
1308 mUpperWallpaperTarget = foundW;
1309 mLowerWallpaperTarget = oldW;
1310 foundW = oldW;
1311 foundI = oldI;
1312 } else {
1313 // The new target is below the old one.
1314 if (DEBUG_WALLPAPER) {
1315 Log.v(TAG, "Found target below old target.");
1316 }
1317 mUpperWallpaperTarget = oldW;
1318 mLowerWallpaperTarget = foundW;
1319 }
1320 }
1321 }
1322 }
1323
1324 } else {
1325 // Is it time to stop animating?
1326 if (mLowerWallpaperTarget == null
1327 || mLowerWallpaperTarget.mAppToken.animation == null
1328 || mUpperWallpaperTarget == null
1329 || mUpperWallpaperTarget.mAppToken.animation == null) {
1330 if (DEBUG_WALLPAPER) {
1331 Log.v(TAG, "No longer animating wallpaper targets!");
1332 }
1333 mLowerWallpaperTarget = null;
1334 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 }
1336 }
1337
1338 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001339 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001340 // The window is visible to the compositor... but is it visible
1341 // to the user? That is what the wallpaper cares about.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001342 visible = !foundW.mObscured;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001343 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001344
1345 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001346 // its layer adjustment. Only do this if we are not transfering
1347 // between two wallpaper targets.
1348 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001349 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001350 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001351
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001352 // Now w is the window we are supposed to be behind... but we
1353 // need to be sure to also be behind any of its attached windows,
1354 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001355 while (foundI > 0) {
1356 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1357 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001358 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001359 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001360 // This window is not related to the previous one in any
1361 // interesting way, so stop here.
1362 break;
1363 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001364 foundW = wb;
1365 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001366 }
1367 }
1368
1369 // Okay i is the position immediately above the wallpaper. Look at
1370 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001371 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001372
1373 // Start stepping backwards from here, ensuring that our wallpaper windows
1374 // are correctly placed.
1375 int curTokenIndex = mWallpaperTokens.size();
1376 while (curTokenIndex > 0) {
1377 curTokenIndex--;
1378 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1379 int curWallpaperIndex = token.windows.size();
1380 while (curWallpaperIndex > 0) {
1381 curWallpaperIndex--;
1382 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001383
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001384 if (visible) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001385 updateWallpaperOffsetLocked(mWallpaperTarget,
1386 wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001387 }
1388
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001389 // First, make sure the client has the current visibility
1390 // state.
1391 if (wallpaper.mWallpaperVisible != visible) {
1392 wallpaper.mWallpaperVisible = visible;
1393 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001394 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001395 "Setting visibility of wallpaper " + wallpaper
1396 + ": " + visible);
1397 wallpaper.mClient.dispatchAppVisibility(visible);
1398 } catch (RemoteException e) {
1399 }
1400 }
1401
1402 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001403 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1404 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001405
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001406 // First, if this window is at the current index, then all
1407 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001408 if (wallpaper == foundW) {
1409 foundI--;
1410 foundW = foundI > 0
1411 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001412 continue;
1413 }
1414
1415 // The window didn't match... the current wallpaper window,
1416 // wherever it is, is in the wrong place, so make sure it is
1417 // not in the list.
1418 int oldIndex = localmWindows.indexOf(wallpaper);
1419 if (oldIndex >= 0) {
1420 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001421 if (oldIndex < foundI) {
1422 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001423 }
1424 }
1425
1426 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001427 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001428 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001429
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001430 localmWindows.add(foundI, wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001431 changed = true;
1432 }
1433 }
1434
1435 return changed;
1436 }
1437
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001438 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001439 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1440 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001441 mWallpaperAnimLayerAdjustment = adj;
1442 int curTokenIndex = mWallpaperTokens.size();
1443 while (curTokenIndex > 0) {
1444 curTokenIndex--;
1445 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1446 int curWallpaperIndex = token.windows.size();
1447 while (curWallpaperIndex > 0) {
1448 curWallpaperIndex--;
1449 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1450 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001451 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1452 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001453 }
1454 }
1455 }
1456
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001457 boolean updateWallpaperOffsetLocked(WindowState target,
1458 WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001459 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001460 boolean rawChanged = false;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001461 if (target.mWallpaperX >= 0) {
1462 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1463 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1464 changed = wallpaperWin.mXOffset != offset;
1465 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001466 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1467 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001468 wallpaperWin.mXOffset = offset;
1469 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001470 if (wallpaperWin.mWallpaperX != target.mWallpaperX) {
1471 wallpaperWin.mWallpaperX = target.mWallpaperX;
1472 rawChanged = true;
1473 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001474 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001475
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001476 if (target.mWallpaperY >= 0) {
1477 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1478 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1479 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001480 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1481 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001482 changed = true;
1483 wallpaperWin.mYOffset = offset;
1484 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001485 if (wallpaperWin.mWallpaperY != target.mWallpaperY) {
1486 wallpaperWin.mWallpaperY = target.mWallpaperY;
1487 rawChanged = true;
1488 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001489 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001490
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001491 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001492 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001493 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1494 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1495 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001496 wallpaperWin.mClient.dispatchWallpaperOffsets(
1497 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1498 } catch (RemoteException e) {
1499 }
1500 }
1501
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001502 return changed;
1503 }
1504
1505 boolean updateWallpaperOffsetLocked() {
1506 final int dw = mDisplay.getWidth();
1507 final int dh = mDisplay.getHeight();
1508
1509 boolean changed = false;
1510
1511 WindowState target = mWallpaperTarget;
1512 if (target != null) {
1513 int curTokenIndex = mWallpaperTokens.size();
1514 while (curTokenIndex > 0) {
1515 curTokenIndex--;
1516 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1517 int curWallpaperIndex = token.windows.size();
1518 while (curWallpaperIndex > 0) {
1519 curWallpaperIndex--;
1520 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1521 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1522 wallpaper.computeShownFrameLocked();
1523 changed = true;
1524 }
1525 }
1526 }
1527 }
1528
1529 return changed;
1530 }
1531
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001532 void updateWallpaperVisibilityLocked() {
1533 final boolean visible = mWallpaperTarget != null
1534 && !mWallpaperTarget.mObscured;
1535 final int dw = mDisplay.getWidth();
1536 final int dh = mDisplay.getHeight();
1537
1538 int curTokenIndex = mWallpaperTokens.size();
1539 while (curTokenIndex > 0) {
1540 curTokenIndex--;
1541 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1542 int curWallpaperIndex = token.windows.size();
1543 while (curWallpaperIndex > 0) {
1544 curWallpaperIndex--;
1545 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1546 if (visible) {
1547 updateWallpaperOffsetLocked(mWallpaperTarget,
1548 wallpaper, dw, dh);
1549 }
1550
1551 if (wallpaper.mWallpaperVisible != visible) {
1552 wallpaper.mWallpaperVisible = visible;
1553 try {
1554 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
1555 "Setting visibility of wallpaper " + wallpaper
1556 + ": " + visible);
1557 wallpaper.mClient.dispatchAppVisibility(visible);
1558 } catch (RemoteException e) {
1559 }
1560 }
1561 }
1562 }
1563 }
1564
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001565 void sendPointerToWallpaperLocked(WindowState srcWin,
1566 MotionEvent pointer, long eventTime) {
1567 int curTokenIndex = mWallpaperTokens.size();
1568 while (curTokenIndex > 0) {
1569 curTokenIndex--;
1570 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1571 int curWallpaperIndex = token.windows.size();
1572 while (curWallpaperIndex > 0) {
1573 curWallpaperIndex--;
1574 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1575 if ((wallpaper.mAttrs.flags &
1576 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1577 continue;
1578 }
1579 try {
1580 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1581 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1582 srcWin.mFrame.top-wallpaper.mFrame.top);
1583 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1584 } catch (RemoteException e) {
1585 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1586 }
1587 }
1588 }
1589 }
1590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 public int addWindow(Session session, IWindow client,
1592 WindowManager.LayoutParams attrs, int viewVisibility,
1593 Rect outContentInsets) {
1594 int res = mPolicy.checkAddPermission(attrs);
1595 if (res != WindowManagerImpl.ADD_OKAY) {
1596 return res;
1597 }
Romain Guy06882f82009-06-10 13:36:04 -07001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 boolean reportNewConfig = false;
1600 WindowState attachedWindow = null;
1601 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 synchronized(mWindowMap) {
1604 // Instantiating a Display requires talking with the simulator,
1605 // so don't do it until we know the system is mostly up and
1606 // running.
1607 if (mDisplay == null) {
1608 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1609 mDisplay = wm.getDefaultDisplay();
1610 mQueue.setDisplay(mDisplay);
1611 reportNewConfig = true;
1612 }
Romain Guy06882f82009-06-10 13:36:04 -07001613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 if (mWindowMap.containsKey(client.asBinder())) {
1615 Log.w(TAG, "Window " + client + " is already added");
1616 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1617 }
1618
1619 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001620 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 if (attachedWindow == null) {
1622 Log.w(TAG, "Attempted to add window with token that is not a window: "
1623 + attrs.token + ". Aborting.");
1624 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1625 }
1626 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1627 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1628 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1629 + attrs.token + ". Aborting.");
1630 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1631 }
1632 }
1633
1634 boolean addToken = false;
1635 WindowToken token = mTokenMap.get(attrs.token);
1636 if (token == null) {
1637 if (attrs.type >= FIRST_APPLICATION_WINDOW
1638 && attrs.type <= LAST_APPLICATION_WINDOW) {
1639 Log.w(TAG, "Attempted to add application window with unknown token "
1640 + attrs.token + ". Aborting.");
1641 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1642 }
1643 if (attrs.type == TYPE_INPUT_METHOD) {
1644 Log.w(TAG, "Attempted to add input method window with unknown token "
1645 + attrs.token + ". Aborting.");
1646 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1647 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001648 if (attrs.type == TYPE_WALLPAPER) {
1649 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1650 + attrs.token + ". Aborting.");
1651 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 token = new WindowToken(attrs.token, -1, false);
1654 addToken = true;
1655 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1656 && attrs.type <= LAST_APPLICATION_WINDOW) {
1657 AppWindowToken atoken = token.appWindowToken;
1658 if (atoken == null) {
1659 Log.w(TAG, "Attempted to add window with non-application token "
1660 + token + ". Aborting.");
1661 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1662 } else if (atoken.removed) {
1663 Log.w(TAG, "Attempted to add window with exiting application token "
1664 + token + ". Aborting.");
1665 return WindowManagerImpl.ADD_APP_EXITING;
1666 }
1667 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1668 // No need for this guy!
1669 if (localLOGV) Log.v(
1670 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1671 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1672 }
1673 } else if (attrs.type == TYPE_INPUT_METHOD) {
1674 if (token.windowType != TYPE_INPUT_METHOD) {
1675 Log.w(TAG, "Attempted to add input method window with bad token "
1676 + attrs.token + ". Aborting.");
1677 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1678 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001679 } else if (attrs.type == TYPE_WALLPAPER) {
1680 if (token.windowType != TYPE_WALLPAPER) {
1681 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1682 + attrs.token + ". Aborting.");
1683 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686
1687 win = new WindowState(session, client, token,
1688 attachedWindow, attrs, viewVisibility);
1689 if (win.mDeathRecipient == null) {
1690 // Client has apparently died, so there is no reason to
1691 // continue.
1692 Log.w(TAG, "Adding window client " + client.asBinder()
1693 + " that is dead, aborting.");
1694 return WindowManagerImpl.ADD_APP_EXITING;
1695 }
1696
1697 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 res = mPolicy.prepareAddWindowLw(win, attrs);
1700 if (res != WindowManagerImpl.ADD_OKAY) {
1701 return res;
1702 }
1703
1704 // From now on, no exceptions or errors allowed!
1705
1706 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 if (addToken) {
1711 mTokenMap.put(attrs.token, token);
1712 mTokenList.add(token);
1713 }
1714 win.attach();
1715 mWindowMap.put(client.asBinder(), win);
1716
1717 if (attrs.type == TYPE_APPLICATION_STARTING &&
1718 token.appWindowToken != null) {
1719 token.appWindowToken.startingWindow = win;
1720 }
1721
1722 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (attrs.type == TYPE_INPUT_METHOD) {
1725 mInputMethodWindow = win;
1726 addInputMethodWindowToListLocked(win);
1727 imMayMove = false;
1728 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1729 mInputMethodDialogs.add(win);
1730 addWindowToListInOrderLocked(win, true);
1731 adjustInputMethodDialogsLocked();
1732 imMayMove = false;
1733 } else {
1734 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001735 if (attrs.type == TYPE_WALLPAPER ||
1736 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1737 adjustWallpaperWindowsLocked();
1738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Romain Guy06882f82009-06-10 13:36:04 -07001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 if (mInTouchMode) {
1746 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1747 }
1748 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1749 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1750 }
Romain Guy06882f82009-06-10 13:36:04 -07001751
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001752 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001754 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1755 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 imMayMove = false;
1757 }
1758 }
Romain Guy06882f82009-06-10 13:36:04 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001761 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
Romain Guy06882f82009-06-10 13:36:04 -07001763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 assignLayersLocked();
1765 // Don't do layout here, the window must call
1766 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 //dump();
1769
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001770 if (focusChanged) {
1771 if (mCurrentFocus != null) {
1772 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1773 }
1774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 if (localLOGV) Log.v(
1776 TAG, "New client " + client.asBinder()
1777 + ": window=" + win);
1778 }
1779
1780 // sendNewConfiguration() checks caller permissions so we must call it with
1781 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1782 // identity anyway, so it's safe to just clear & restore around this whole
1783 // block.
1784 final long origId = Binder.clearCallingIdentity();
1785 if (reportNewConfig) {
1786 sendNewConfiguration();
1787 } else {
1788 // Update Orientation after adding a window, only if the window needs to be
1789 // displayed right away
1790 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001791 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 sendNewConfiguration();
1793 }
1794 }
1795 }
1796 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 return res;
1799 }
Romain Guy06882f82009-06-10 13:36:04 -07001800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 public void removeWindow(Session session, IWindow client) {
1802 synchronized(mWindowMap) {
1803 WindowState win = windowForClientLocked(session, client);
1804 if (win == null) {
1805 return;
1806 }
1807 removeWindowLocked(session, win);
1808 }
1809 }
Romain Guy06882f82009-06-10 13:36:04 -07001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 public void removeWindowLocked(Session session, WindowState win) {
1812
1813 if (localLOGV || DEBUG_FOCUS) Log.v(
1814 TAG, "Remove " + win + " client="
1815 + Integer.toHexString(System.identityHashCode(
1816 win.mClient.asBinder()))
1817 + ", surface=" + win.mSurface);
1818
1819 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 if (DEBUG_APP_TRANSITIONS) Log.v(
1822 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1823 + " mExiting=" + win.mExiting
1824 + " isAnimating=" + win.isAnimating()
1825 + " app-animation="
1826 + (win.mAppToken != null ? win.mAppToken.animation : null)
1827 + " inPendingTransaction="
1828 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1829 + " mDisplayFrozen=" + mDisplayFrozen);
1830 // Visibility of the removed window. Will be used later to update orientation later on.
1831 boolean wasVisible = false;
1832 // First, see if we need to run an animation. If we do, we have
1833 // to hold off on removing the window until the animation is done.
1834 // If the display is frozen, just remove immediately, since the
1835 // animation wouldn't be seen.
1836 if (win.mSurface != null && !mDisplayFrozen) {
1837 // If we are not currently running the exit animation, we
1838 // need to see about starting one.
1839 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1842 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1843 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1844 }
1845 // Try starting an animation.
1846 if (applyAnimationLocked(win, transit, false)) {
1847 win.mExiting = true;
1848 }
1849 }
1850 if (win.mExiting || win.isAnimating()) {
1851 // The exit animation is running... wait for it!
1852 //Log.i(TAG, "*** Running exit animation...");
1853 win.mExiting = true;
1854 win.mRemoveOnExit = true;
1855 mLayoutNeeded = true;
1856 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1857 performLayoutAndPlaceSurfacesLocked();
1858 if (win.mAppToken != null) {
1859 win.mAppToken.updateReportedVisibilityLocked();
1860 }
1861 //dump();
1862 Binder.restoreCallingIdentity(origId);
1863 return;
1864 }
1865 }
1866
1867 removeWindowInnerLocked(session, win);
1868 // Removing a visible window will effect the computed orientation
1869 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001870 if (wasVisible && computeForcedAppOrientationLocked()
1871 != mForcedAppOrientation) {
1872 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1875 Binder.restoreCallingIdentity(origId);
1876 }
Romain Guy06882f82009-06-10 13:36:04 -07001877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001879 mKeyWaiter.finishedKey(session, win.mClient, true,
1880 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1882 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 if (mInputMethodTarget == win) {
1887 moveInputMethodWindowsIfNeededLocked(false);
1888 }
Romain Guy06882f82009-06-10 13:36:04 -07001889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 mPolicy.removeWindowLw(win);
1891 win.removeLocked();
1892
1893 mWindowMap.remove(win.mClient.asBinder());
1894 mWindows.remove(win);
1895
1896 if (mInputMethodWindow == win) {
1897 mInputMethodWindow = null;
1898 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1899 mInputMethodDialogs.remove(win);
1900 }
Romain Guy06882f82009-06-10 13:36:04 -07001901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 final WindowToken token = win.mToken;
1903 final AppWindowToken atoken = win.mAppToken;
1904 token.windows.remove(win);
1905 if (atoken != null) {
1906 atoken.allAppWindows.remove(win);
1907 }
1908 if (localLOGV) Log.v(
1909 TAG, "**** Removing window " + win + ": count="
1910 + token.windows.size());
1911 if (token.windows.size() == 0) {
1912 if (!token.explicit) {
1913 mTokenMap.remove(token.token);
1914 mTokenList.remove(token);
1915 } else if (atoken != null) {
1916 atoken.firstWindowDrawn = false;
1917 }
1918 }
1919
1920 if (atoken != null) {
1921 if (atoken.startingWindow == win) {
1922 atoken.startingWindow = null;
1923 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1924 // If this is the last window and we had requested a starting
1925 // transition window, well there is no point now.
1926 atoken.startingData = null;
1927 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1928 // If this is the last window except for a starting transition
1929 // window, we need to get rid of the starting transition.
1930 if (DEBUG_STARTING_WINDOW) {
1931 Log.v(TAG, "Schedule remove starting " + token
1932 + ": no more real windows");
1933 }
1934 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1935 mH.sendMessage(m);
1936 }
1937 }
Romain Guy06882f82009-06-10 13:36:04 -07001938
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001939 if (win.mAttrs.type == TYPE_WALLPAPER ||
1940 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1941 adjustWallpaperWindowsLocked();
1942 }
1943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 if (!mInLayout) {
1945 assignLayersLocked();
1946 mLayoutNeeded = true;
1947 performLayoutAndPlaceSurfacesLocked();
1948 if (win.mAppToken != null) {
1949 win.mAppToken.updateReportedVisibilityLocked();
1950 }
1951 }
1952 }
1953
1954 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1955 long origId = Binder.clearCallingIdentity();
1956 try {
1957 synchronized (mWindowMap) {
1958 WindowState w = windowForClientLocked(session, client);
1959 if ((w != null) && (w.mSurface != null)) {
1960 Surface.openTransaction();
1961 try {
1962 w.mSurface.setTransparentRegionHint(region);
1963 } finally {
1964 Surface.closeTransaction();
1965 }
1966 }
1967 }
1968 } finally {
1969 Binder.restoreCallingIdentity(origId);
1970 }
1971 }
1972
1973 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001974 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 Rect visibleInsets) {
1976 long origId = Binder.clearCallingIdentity();
1977 try {
1978 synchronized (mWindowMap) {
1979 WindowState w = windowForClientLocked(session, client);
1980 if (w != null) {
1981 w.mGivenInsetsPending = false;
1982 w.mGivenContentInsets.set(contentInsets);
1983 w.mGivenVisibleInsets.set(visibleInsets);
1984 w.mTouchableInsets = touchableInsets;
1985 mLayoutNeeded = true;
1986 performLayoutAndPlaceSurfacesLocked();
1987 }
1988 }
1989 } finally {
1990 Binder.restoreCallingIdentity(origId);
1991 }
1992 }
Romain Guy06882f82009-06-10 13:36:04 -07001993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 public void getWindowDisplayFrame(Session session, IWindow client,
1995 Rect outDisplayFrame) {
1996 synchronized(mWindowMap) {
1997 WindowState win = windowForClientLocked(session, client);
1998 if (win == null) {
1999 outDisplayFrame.setEmpty();
2000 return;
2001 }
2002 outDisplayFrame.set(win.mDisplayFrame);
2003 }
2004 }
2005
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002006 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2007 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2008 window.mWallpaperX = x;
2009 window.mWallpaperY = y;
2010
2011 if (mWallpaperTarget == window) {
2012 if (updateWallpaperOffsetLocked()) {
2013 performLayoutAndPlaceSurfacesLocked();
2014 }
2015 }
2016 }
2017 }
2018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 public int relayoutWindow(Session session, IWindow client,
2020 WindowManager.LayoutParams attrs, int requestedWidth,
2021 int requestedHeight, int viewVisibility, boolean insetsPending,
2022 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2023 Surface outSurface) {
2024 boolean displayed = false;
2025 boolean inTouchMode;
2026 Configuration newConfig = null;
2027 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 synchronized(mWindowMap) {
2030 WindowState win = windowForClientLocked(session, client);
2031 if (win == null) {
2032 return 0;
2033 }
2034 win.mRequestedWidth = requestedWidth;
2035 win.mRequestedHeight = requestedHeight;
2036
2037 if (attrs != null) {
2038 mPolicy.adjustWindowParamsLw(attrs);
2039 }
Romain Guy06882f82009-06-10 13:36:04 -07002040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 int attrChanges = 0;
2042 int flagChanges = 0;
2043 if (attrs != null) {
2044 flagChanges = win.mAttrs.flags ^= attrs.flags;
2045 attrChanges = win.mAttrs.copyFrom(attrs);
2046 }
2047
2048 if (localLOGV) Log.v(
2049 TAG, "Relayout given client " + client.asBinder()
2050 + " (" + win.mAttrs.getTitle() + ")");
2051
2052
2053 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2054 win.mAlpha = attrs.alpha;
2055 }
2056
2057 final boolean scaledWindow =
2058 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2059
2060 if (scaledWindow) {
2061 // requested{Width|Height} Surface's physical size
2062 // attrs.{width|height} Size on screen
2063 win.mHScale = (attrs.width != requestedWidth) ?
2064 (attrs.width / (float)requestedWidth) : 1.0f;
2065 win.mVScale = (attrs.height != requestedHeight) ?
2066 (attrs.height / (float)requestedHeight) : 1.0f;
2067 }
2068
2069 boolean imMayMove = (flagChanges&(
2070 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2071 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 boolean focusMayChange = win.mViewVisibility != viewVisibility
2074 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2075 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002076
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002077 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2078 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 win.mRelayoutCalled = true;
2081 final int oldVisibility = win.mViewVisibility;
2082 win.mViewVisibility = viewVisibility;
2083 if (viewVisibility == View.VISIBLE &&
2084 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2085 displayed = !win.isVisibleLw();
2086 if (win.mExiting) {
2087 win.mExiting = false;
2088 win.mAnimation = null;
2089 }
2090 if (win.mDestroying) {
2091 win.mDestroying = false;
2092 mDestroySurface.remove(win);
2093 }
2094 if (oldVisibility == View.GONE) {
2095 win.mEnterAnimationPending = true;
2096 }
2097 if (displayed && win.mSurface != null && !win.mDrawPending
2098 && !win.mCommitDrawPending && !mDisplayFrozen) {
2099 applyEnterAnimationLocked(win);
2100 }
2101 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2102 // To change the format, we need to re-build the surface.
2103 win.destroySurfaceLocked();
2104 displayed = true;
2105 }
2106 try {
2107 Surface surface = win.createSurfaceLocked();
2108 if (surface != null) {
2109 outSurface.copyFrom(surface);
2110 } else {
2111 outSurface.clear();
2112 }
2113 } catch (Exception e) {
2114 Log.w(TAG, "Exception thrown when creating surface for client "
2115 + client + " (" + win.mAttrs.getTitle() + ")",
2116 e);
2117 Binder.restoreCallingIdentity(origId);
2118 return 0;
2119 }
2120 if (displayed) {
2121 focusMayChange = true;
2122 }
2123 if (win.mAttrs.type == TYPE_INPUT_METHOD
2124 && mInputMethodWindow == null) {
2125 mInputMethodWindow = win;
2126 imMayMove = true;
2127 }
2128 } else {
2129 win.mEnterAnimationPending = false;
2130 if (win.mSurface != null) {
2131 // If we are not currently running the exit animation, we
2132 // need to see about starting one.
2133 if (!win.mExiting) {
2134 // Try starting an animation; if there isn't one, we
2135 // can destroy the surface right away.
2136 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2137 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2138 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2139 }
2140 if (win.isWinVisibleLw() &&
2141 applyAnimationLocked(win, transit, false)) {
2142 win.mExiting = true;
2143 mKeyWaiter.finishedKey(session, client, true,
2144 KeyWaiter.RETURN_NOTHING);
2145 } else if (win.isAnimating()) {
2146 // Currently in a hide animation... turn this into
2147 // an exit.
2148 win.mExiting = true;
2149 } else {
2150 if (mInputMethodWindow == win) {
2151 mInputMethodWindow = null;
2152 }
2153 win.destroySurfaceLocked();
2154 }
2155 }
2156 }
2157 outSurface.clear();
2158 }
2159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 if (focusMayChange) {
2161 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2162 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 imMayMove = false;
2164 }
2165 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2166 }
Romain Guy06882f82009-06-10 13:36:04 -07002167
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002168 // updateFocusedWindowLocked() already assigned layers so we only need to
2169 // reassign them at this point if the IM window state gets shuffled
2170 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 if (imMayMove) {
2173 if (moveInputMethodWindowsIfNeededLocked(false)) {
2174 assignLayers = true;
2175 }
2176 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002177 if (wallpaperMayMove) {
2178 if (adjustWallpaperWindowsLocked()) {
2179 assignLayers = true;
2180 }
2181 }
Romain Guy06882f82009-06-10 13:36:04 -07002182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 mLayoutNeeded = true;
2184 win.mGivenInsetsPending = insetsPending;
2185 if (assignLayers) {
2186 assignLayersLocked();
2187 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002188 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 performLayoutAndPlaceSurfacesLocked();
2190 if (win.mAppToken != null) {
2191 win.mAppToken.updateReportedVisibilityLocked();
2192 }
2193 outFrame.set(win.mFrame);
2194 outContentInsets.set(win.mContentInsets);
2195 outVisibleInsets.set(win.mVisibleInsets);
2196 if (localLOGV) Log.v(
2197 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002198 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 + ", requestedHeight=" + requestedHeight
2200 + ", viewVisibility=" + viewVisibility
2201 + "\nRelayout returning frame=" + outFrame
2202 + ", surface=" + outSurface);
2203
2204 if (localLOGV || DEBUG_FOCUS) Log.v(
2205 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2206
2207 inTouchMode = mInTouchMode;
2208 }
2209
2210 if (newConfig != null) {
2211 sendNewConfiguration();
2212 }
Romain Guy06882f82009-06-10 13:36:04 -07002213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2217 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2218 }
2219
2220 public void finishDrawingWindow(Session session, IWindow client) {
2221 final long origId = Binder.clearCallingIdentity();
2222 synchronized(mWindowMap) {
2223 WindowState win = windowForClientLocked(session, client);
2224 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002225 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2226 adjustWallpaperWindowsLocked();
2227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 mLayoutNeeded = true;
2229 performLayoutAndPlaceSurfacesLocked();
2230 }
2231 }
2232 Binder.restoreCallingIdentity(origId);
2233 }
2234
2235 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2236 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2237 + (lp != null ? lp.packageName : null)
2238 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2239 if (lp != null && lp.windowAnimations != 0) {
2240 // If this is a system resource, don't try to load it from the
2241 // application resources. It is nice to avoid loading application
2242 // resources if we can.
2243 String packageName = lp.packageName != null ? lp.packageName : "android";
2244 int resId = lp.windowAnimations;
2245 if ((resId&0xFF000000) == 0x01000000) {
2246 packageName = "android";
2247 }
2248 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2249 + packageName);
2250 return AttributeCache.instance().get(packageName, resId,
2251 com.android.internal.R.styleable.WindowAnimation);
2252 }
2253 return null;
2254 }
Romain Guy06882f82009-06-10 13:36:04 -07002255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 private void applyEnterAnimationLocked(WindowState win) {
2257 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2258 if (win.mEnterAnimationPending) {
2259 win.mEnterAnimationPending = false;
2260 transit = WindowManagerPolicy.TRANSIT_ENTER;
2261 }
2262
2263 applyAnimationLocked(win, transit, true);
2264 }
2265
2266 private boolean applyAnimationLocked(WindowState win,
2267 int transit, boolean isEntrance) {
2268 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2269 // If we are trying to apply an animation, but already running
2270 // an animation of the same type, then just leave that one alone.
2271 return true;
2272 }
Romain Guy06882f82009-06-10 13:36:04 -07002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 // Only apply an animation if the display isn't frozen. If it is
2275 // frozen, there is no reason to animate and it can cause strange
2276 // artifacts when we unfreeze the display if some different animation
2277 // is running.
2278 if (!mDisplayFrozen) {
2279 int anim = mPolicy.selectAnimationLw(win, transit);
2280 int attr = -1;
2281 Animation a = null;
2282 if (anim != 0) {
2283 a = AnimationUtils.loadAnimation(mContext, anim);
2284 } else {
2285 switch (transit) {
2286 case WindowManagerPolicy.TRANSIT_ENTER:
2287 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2288 break;
2289 case WindowManagerPolicy.TRANSIT_EXIT:
2290 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2291 break;
2292 case WindowManagerPolicy.TRANSIT_SHOW:
2293 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2294 break;
2295 case WindowManagerPolicy.TRANSIT_HIDE:
2296 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2297 break;
2298 }
2299 if (attr >= 0) {
2300 a = loadAnimation(win.mAttrs, attr);
2301 }
2302 }
2303 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2304 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2305 + " mAnimation=" + win.mAnimation
2306 + " isEntrance=" + isEntrance);
2307 if (a != null) {
2308 if (DEBUG_ANIM) {
2309 RuntimeException e = new RuntimeException();
2310 e.fillInStackTrace();
2311 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2312 }
2313 win.setAnimation(a);
2314 win.mAnimationIsEntrance = isEntrance;
2315 }
2316 } else {
2317 win.clearAnimation();
2318 }
2319
2320 return win.mAnimation != null;
2321 }
2322
2323 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2324 int anim = 0;
2325 Context context = mContext;
2326 if (animAttr >= 0) {
2327 AttributeCache.Entry ent = getCachedAnimations(lp);
2328 if (ent != null) {
2329 context = ent.context;
2330 anim = ent.array.getResourceId(animAttr, 0);
2331 }
2332 }
2333 if (anim != 0) {
2334 return AnimationUtils.loadAnimation(context, anim);
2335 }
2336 return null;
2337 }
Romain Guy06882f82009-06-10 13:36:04 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 private boolean applyAnimationLocked(AppWindowToken wtoken,
2340 WindowManager.LayoutParams lp, int transit, boolean enter) {
2341 // Only apply an animation if the display isn't frozen. If it is
2342 // frozen, there is no reason to animate and it can cause strange
2343 // artifacts when we unfreeze the display if some different animation
2344 // is running.
2345 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002346 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002347 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002348 a = new FadeInOutAnimation(enter);
2349 if (DEBUG_ANIM) Log.v(TAG,
2350 "applying FadeInOutAnimation for a window in compatibility mode");
2351 } else {
2352 int animAttr = 0;
2353 switch (transit) {
2354 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2355 animAttr = enter
2356 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2357 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2358 break;
2359 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2360 animAttr = enter
2361 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2362 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2363 break;
2364 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2365 animAttr = enter
2366 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2367 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2368 break;
2369 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2370 animAttr = enter
2371 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2372 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2373 break;
2374 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2375 animAttr = enter
2376 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2377 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2378 break;
2379 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2380 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002381 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002382 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2383 break;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002384 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN:
2385 animAttr = enter
2386 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenEnterAnimation
2387 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenExitAnimation;
2388 break;
2389 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE:
2390 animAttr = enter
2391 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseEnterAnimation
2392 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseExitAnimation;
2393 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002394 }
2395 a = loadAnimation(lp, animAttr);
2396 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2397 + " anim=" + a
2398 + " animAttr=0x" + Integer.toHexString(animAttr)
2399 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 if (a != null) {
2402 if (DEBUG_ANIM) {
2403 RuntimeException e = new RuntimeException();
2404 e.fillInStackTrace();
2405 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2406 }
2407 wtoken.setAnimation(a);
2408 }
2409 } else {
2410 wtoken.clearAnimation();
2411 }
2412
2413 return wtoken.animation != null;
2414 }
2415
2416 // -------------------------------------------------------------
2417 // Application Window Tokens
2418 // -------------------------------------------------------------
2419
2420 public void validateAppTokens(List tokens) {
2421 int v = tokens.size()-1;
2422 int m = mAppTokens.size()-1;
2423 while (v >= 0 && m >= 0) {
2424 AppWindowToken wtoken = mAppTokens.get(m);
2425 if (wtoken.removed) {
2426 m--;
2427 continue;
2428 }
2429 if (tokens.get(v) != wtoken.token) {
2430 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2431 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2432 }
2433 v--;
2434 m--;
2435 }
2436 while (v >= 0) {
2437 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2438 v--;
2439 }
2440 while (m >= 0) {
2441 AppWindowToken wtoken = mAppTokens.get(m);
2442 if (!wtoken.removed) {
2443 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2444 }
2445 m--;
2446 }
2447 }
2448
2449 boolean checkCallingPermission(String permission, String func) {
2450 // Quick check: if the calling permission is me, it's all okay.
2451 if (Binder.getCallingPid() == Process.myPid()) {
2452 return true;
2453 }
Romain Guy06882f82009-06-10 13:36:04 -07002454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 if (mContext.checkCallingPermission(permission)
2456 == PackageManager.PERMISSION_GRANTED) {
2457 return true;
2458 }
2459 String msg = "Permission Denial: " + func + " from pid="
2460 + Binder.getCallingPid()
2461 + ", uid=" + Binder.getCallingUid()
2462 + " requires " + permission;
2463 Log.w(TAG, msg);
2464 return false;
2465 }
Romain Guy06882f82009-06-10 13:36:04 -07002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 AppWindowToken findAppWindowToken(IBinder token) {
2468 WindowToken wtoken = mTokenMap.get(token);
2469 if (wtoken == null) {
2470 return null;
2471 }
2472 return wtoken.appWindowToken;
2473 }
Romain Guy06882f82009-06-10 13:36:04 -07002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 public void addWindowToken(IBinder token, int type) {
2476 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2477 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002478 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 }
Romain Guy06882f82009-06-10 13:36:04 -07002480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 synchronized(mWindowMap) {
2482 WindowToken wtoken = mTokenMap.get(token);
2483 if (wtoken != null) {
2484 Log.w(TAG, "Attempted to add existing input method token: " + token);
2485 return;
2486 }
2487 wtoken = new WindowToken(token, type, true);
2488 mTokenMap.put(token, wtoken);
2489 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002490 if (type == TYPE_WALLPAPER) {
2491 mWallpaperTokens.add(wtoken);
2492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494 }
Romain Guy06882f82009-06-10 13:36:04 -07002495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 public void removeWindowToken(IBinder token) {
2497 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2498 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002499 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 }
2501
2502 final long origId = Binder.clearCallingIdentity();
2503 synchronized(mWindowMap) {
2504 WindowToken wtoken = mTokenMap.remove(token);
2505 mTokenList.remove(wtoken);
2506 if (wtoken != null) {
2507 boolean delayed = false;
2508 if (!wtoken.hidden) {
2509 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 final int N = wtoken.windows.size();
2512 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 for (int i=0; i<N; i++) {
2515 WindowState win = wtoken.windows.get(i);
2516
2517 if (win.isAnimating()) {
2518 delayed = true;
2519 }
Romain Guy06882f82009-06-10 13:36:04 -07002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 if (win.isVisibleNow()) {
2522 applyAnimationLocked(win,
2523 WindowManagerPolicy.TRANSIT_EXIT, false);
2524 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2525 KeyWaiter.RETURN_NOTHING);
2526 changed = true;
2527 }
2528 }
2529
2530 if (changed) {
2531 mLayoutNeeded = true;
2532 performLayoutAndPlaceSurfacesLocked();
2533 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2534 }
Romain Guy06882f82009-06-10 13:36:04 -07002535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 if (delayed) {
2537 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002538 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2539 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
2541 }
Romain Guy06882f82009-06-10 13:36:04 -07002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 } else {
2544 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2545 }
2546 }
2547 Binder.restoreCallingIdentity(origId);
2548 }
2549
2550 public void addAppToken(int addPos, IApplicationToken token,
2551 int groupId, int requestedOrientation, boolean fullscreen) {
2552 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2553 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002554 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
Romain Guy06882f82009-06-10 13:36:04 -07002556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 synchronized(mWindowMap) {
2558 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2559 if (wtoken != null) {
2560 Log.w(TAG, "Attempted to add existing app token: " + token);
2561 return;
2562 }
2563 wtoken = new AppWindowToken(token);
2564 wtoken.groupId = groupId;
2565 wtoken.appFullscreen = fullscreen;
2566 wtoken.requestedOrientation = requestedOrientation;
2567 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002568 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 mTokenMap.put(token.asBinder(), wtoken);
2570 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 // Application tokens start out hidden.
2573 wtoken.hidden = true;
2574 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 //dump();
2577 }
2578 }
Romain Guy06882f82009-06-10 13:36:04 -07002579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 public void setAppGroupId(IBinder token, int groupId) {
2581 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2582 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002583 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585
2586 synchronized(mWindowMap) {
2587 AppWindowToken wtoken = findAppWindowToken(token);
2588 if (wtoken == null) {
2589 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2590 return;
2591 }
2592 wtoken.groupId = groupId;
2593 }
2594 }
Romain Guy06882f82009-06-10 13:36:04 -07002595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 public int getOrientationFromWindowsLocked() {
2597 int pos = mWindows.size() - 1;
2598 while (pos >= 0) {
2599 WindowState wtoken = (WindowState) mWindows.get(pos);
2600 pos--;
2601 if (wtoken.mAppToken != null) {
2602 // We hit an application window. so the orientation will be determined by the
2603 // app window. No point in continuing further.
2604 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2605 }
2606 if (!wtoken.isVisibleLw()) {
2607 continue;
2608 }
2609 int req = wtoken.mAttrs.screenOrientation;
2610 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2611 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2612 continue;
2613 } else {
2614 return req;
2615 }
2616 }
2617 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2618 }
Romain Guy06882f82009-06-10 13:36:04 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 public int getOrientationFromAppTokensLocked() {
2621 int pos = mAppTokens.size() - 1;
2622 int curGroup = 0;
2623 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002624 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002626 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 while (pos >= 0) {
2628 AppWindowToken wtoken = mAppTokens.get(pos);
2629 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002630 // if we're about to tear down this window and not seek for
2631 // the behind activity, don't use it for orientation
2632 if (!findingBehind
2633 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002634 continue;
2635 }
2636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 if (!haveGroup) {
2638 // We ignore any hidden applications on the top.
2639 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2640 continue;
2641 }
2642 haveGroup = true;
2643 curGroup = wtoken.groupId;
2644 lastOrientation = wtoken.requestedOrientation;
2645 } else if (curGroup != wtoken.groupId) {
2646 // If we have hit a new application group, and the bottom
2647 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002648 // the orientation behind it, and the last app was
2649 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002651 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2652 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 return lastOrientation;
2654 }
2655 }
2656 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002657 // If this application is fullscreen, and didn't explicitly say
2658 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002660 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002661 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002662 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 return or;
2664 }
2665 // If this application has requested an explicit orientation,
2666 // then use it.
2667 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2668 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2669 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2670 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2671 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2672 return or;
2673 }
Owen Lin3413b892009-05-01 17:12:32 -07002674 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
2676 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2677 }
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002680 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002681 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2682 "updateOrientationFromAppTokens()")) {
2683 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2684 }
2685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 Configuration config;
2687 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002688 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2689 freezeThisOneIfNeeded);
2690 Binder.restoreCallingIdentity(ident);
2691 return config;
2692 }
2693
2694 Configuration updateOrientationFromAppTokensUnchecked(
2695 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2696 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002698 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
2700 if (config != null) {
2701 mLayoutNeeded = true;
2702 performLayoutAndPlaceSurfacesLocked();
2703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 return config;
2705 }
Romain Guy06882f82009-06-10 13:36:04 -07002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 /*
2708 * The orientation is computed from non-application windows first. If none of
2709 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002710 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2712 * android.os.IBinder)
2713 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002714 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002715 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 long ident = Binder.clearCallingIdentity();
2718 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002719 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 if (req != mForcedAppOrientation) {
2722 changed = true;
2723 mForcedAppOrientation = req;
2724 //send a message to Policy indicating orientation change to take
2725 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002726 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
Romain Guy06882f82009-06-10 13:36:04 -07002728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 if (changed) {
2730 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002731 WindowManagerPolicy.USE_LAST_ROTATION,
2732 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 if (changed) {
2734 if (freezeThisOneIfNeeded != null) {
2735 AppWindowToken wtoken = findAppWindowToken(
2736 freezeThisOneIfNeeded);
2737 if (wtoken != null) {
2738 startAppFreezingScreenLocked(wtoken,
2739 ActivityInfo.CONFIG_ORIENTATION);
2740 }
2741 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002742 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
2744 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002745
2746 // No obvious action we need to take, but if our current
2747 // state mismatches the activity maanager's, update it
2748 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002749 mTempConfiguration.setToDefaults();
2750 if (computeNewConfigurationLocked(mTempConfiguration)) {
2751 if (appConfig.diff(mTempConfiguration) != 0) {
2752 Log.i(TAG, "Config changed: " + mTempConfiguration);
2753 return new Configuration(mTempConfiguration);
2754 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002755 }
2756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 } finally {
2758 Binder.restoreCallingIdentity(ident);
2759 }
Romain Guy06882f82009-06-10 13:36:04 -07002760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 return null;
2762 }
Romain Guy06882f82009-06-10 13:36:04 -07002763
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002764 int computeForcedAppOrientationLocked() {
2765 int req = getOrientationFromWindowsLocked();
2766 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2767 req = getOrientationFromAppTokensLocked();
2768 }
2769 return req;
2770 }
Romain Guy06882f82009-06-10 13:36:04 -07002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2773 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2774 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002775 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
Romain Guy06882f82009-06-10 13:36:04 -07002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 synchronized(mWindowMap) {
2779 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2780 if (wtoken == null) {
2781 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2782 return;
2783 }
Romain Guy06882f82009-06-10 13:36:04 -07002784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 wtoken.requestedOrientation = requestedOrientation;
2786 }
2787 }
Romain Guy06882f82009-06-10 13:36:04 -07002788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 public int getAppOrientation(IApplicationToken token) {
2790 synchronized(mWindowMap) {
2791 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2792 if (wtoken == null) {
2793 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2794 }
Romain Guy06882f82009-06-10 13:36:04 -07002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 return wtoken.requestedOrientation;
2797 }
2798 }
Romain Guy06882f82009-06-10 13:36:04 -07002799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2801 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2802 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002803 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 }
2805
2806 synchronized(mWindowMap) {
2807 boolean changed = false;
2808 if (token == null) {
2809 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2810 changed = mFocusedApp != null;
2811 mFocusedApp = null;
2812 mKeyWaiter.tickle();
2813 } else {
2814 AppWindowToken newFocus = findAppWindowToken(token);
2815 if (newFocus == null) {
2816 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2817 return;
2818 }
2819 changed = mFocusedApp != newFocus;
2820 mFocusedApp = newFocus;
2821 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2822 mKeyWaiter.tickle();
2823 }
2824
2825 if (moveFocusNow && changed) {
2826 final long origId = Binder.clearCallingIdentity();
2827 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2828 Binder.restoreCallingIdentity(origId);
2829 }
2830 }
2831 }
2832
2833 public void prepareAppTransition(int transit) {
2834 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2835 "prepareAppTransition()")) {
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 }
Romain Guy06882f82009-06-10 13:36:04 -07002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 synchronized(mWindowMap) {
2840 if (DEBUG_APP_TRANSITIONS) Log.v(
2841 TAG, "Prepare app transition: transit=" + transit
2842 + " mNextAppTransition=" + mNextAppTransition);
2843 if (!mDisplayFrozen) {
2844 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2845 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002846 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2847 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2848 // Opening a new task always supersedes a close for the anim.
2849 mNextAppTransition = transit;
2850 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2851 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2852 // Opening a new activity always supersedes a close for the anim.
2853 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 }
2855 mAppTransitionReady = false;
2856 mAppTransitionTimeout = false;
2857 mStartingIconInTransition = false;
2858 mSkipAppTransitionAnimation = false;
2859 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2860 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2861 5000);
2862 }
2863 }
2864 }
2865
2866 public int getPendingAppTransition() {
2867 return mNextAppTransition;
2868 }
Romain Guy06882f82009-06-10 13:36:04 -07002869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 public void executeAppTransition() {
2871 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2872 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002873 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 }
Romain Guy06882f82009-06-10 13:36:04 -07002875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 synchronized(mWindowMap) {
2877 if (DEBUG_APP_TRANSITIONS) Log.v(
2878 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2879 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2880 mAppTransitionReady = true;
2881 final long origId = Binder.clearCallingIdentity();
2882 performLayoutAndPlaceSurfacesLocked();
2883 Binder.restoreCallingIdentity(origId);
2884 }
2885 }
2886 }
2887
2888 public void setAppStartingWindow(IBinder token, String pkg,
2889 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2890 IBinder transferFrom, boolean createIfNeeded) {
2891 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2892 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002893 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 }
2895
2896 synchronized(mWindowMap) {
2897 if (DEBUG_STARTING_WINDOW) Log.v(
2898 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2899 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 AppWindowToken wtoken = findAppWindowToken(token);
2902 if (wtoken == null) {
2903 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2904 return;
2905 }
2906
2907 // If the display is frozen, we won't do anything until the
2908 // actual window is displayed so there is no reason to put in
2909 // the starting window.
2910 if (mDisplayFrozen) {
2911 return;
2912 }
Romain Guy06882f82009-06-10 13:36:04 -07002913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 if (wtoken.startingData != null) {
2915 return;
2916 }
Romain Guy06882f82009-06-10 13:36:04 -07002917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 if (transferFrom != null) {
2919 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2920 if (ttoken != null) {
2921 WindowState startingWindow = ttoken.startingWindow;
2922 if (startingWindow != null) {
2923 if (mStartingIconInTransition) {
2924 // In this case, the starting icon has already
2925 // been displayed, so start letting windows get
2926 // shown immediately without any more transitions.
2927 mSkipAppTransitionAnimation = true;
2928 }
2929 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2930 "Moving existing starting from " + ttoken
2931 + " to " + wtoken);
2932 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 // Transfer the starting window over to the new
2935 // token.
2936 wtoken.startingData = ttoken.startingData;
2937 wtoken.startingView = ttoken.startingView;
2938 wtoken.startingWindow = startingWindow;
2939 ttoken.startingData = null;
2940 ttoken.startingView = null;
2941 ttoken.startingWindow = null;
2942 ttoken.startingMoved = true;
2943 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002944 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 startingWindow.mAppToken = wtoken;
2946 mWindows.remove(startingWindow);
2947 ttoken.windows.remove(startingWindow);
2948 ttoken.allAppWindows.remove(startingWindow);
2949 addWindowToListInOrderLocked(startingWindow, true);
2950 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 // Propagate other interesting state between the
2953 // tokens. If the old token is displayed, we should
2954 // immediately force the new one to be displayed. If
2955 // it is animating, we need to move that animation to
2956 // the new one.
2957 if (ttoken.allDrawn) {
2958 wtoken.allDrawn = true;
2959 }
2960 if (ttoken.firstWindowDrawn) {
2961 wtoken.firstWindowDrawn = true;
2962 }
2963 if (!ttoken.hidden) {
2964 wtoken.hidden = false;
2965 wtoken.hiddenRequested = false;
2966 wtoken.willBeHidden = false;
2967 }
2968 if (wtoken.clientHidden != ttoken.clientHidden) {
2969 wtoken.clientHidden = ttoken.clientHidden;
2970 wtoken.sendAppVisibilityToClients();
2971 }
2972 if (ttoken.animation != null) {
2973 wtoken.animation = ttoken.animation;
2974 wtoken.animating = ttoken.animating;
2975 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2976 ttoken.animation = null;
2977 ttoken.animLayerAdjustment = 0;
2978 wtoken.updateLayers();
2979 ttoken.updateLayers();
2980 }
Romain Guy06882f82009-06-10 13:36:04 -07002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 mLayoutNeeded = true;
2984 performLayoutAndPlaceSurfacesLocked();
2985 Binder.restoreCallingIdentity(origId);
2986 return;
2987 } else if (ttoken.startingData != null) {
2988 // The previous app was getting ready to show a
2989 // starting window, but hasn't yet done so. Steal it!
2990 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2991 "Moving pending starting from " + ttoken
2992 + " to " + wtoken);
2993 wtoken.startingData = ttoken.startingData;
2994 ttoken.startingData = null;
2995 ttoken.startingMoved = true;
2996 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2997 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2998 // want to process the message ASAP, before any other queued
2999 // messages.
3000 mH.sendMessageAtFrontOfQueue(m);
3001 return;
3002 }
3003 }
3004 }
3005
3006 // There is no existing starting window, and the caller doesn't
3007 // want us to create one, so that's it!
3008 if (!createIfNeeded) {
3009 return;
3010 }
Romain Guy06882f82009-06-10 13:36:04 -07003011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 mStartingIconInTransition = true;
3013 wtoken.startingData = new StartingData(
3014 pkg, theme, nonLocalizedLabel,
3015 labelRes, icon);
3016 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3017 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3018 // want to process the message ASAP, before any other queued
3019 // messages.
3020 mH.sendMessageAtFrontOfQueue(m);
3021 }
3022 }
3023
3024 public void setAppWillBeHidden(IBinder token) {
3025 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3026 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003027 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 }
3029
3030 AppWindowToken wtoken;
3031
3032 synchronized(mWindowMap) {
3033 wtoken = findAppWindowToken(token);
3034 if (wtoken == null) {
3035 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3036 return;
3037 }
3038 wtoken.willBeHidden = true;
3039 }
3040 }
Romain Guy06882f82009-06-10 13:36:04 -07003041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3043 boolean visible, int transit, boolean performLayout) {
3044 boolean delayed = false;
3045
3046 if (wtoken.clientHidden == visible) {
3047 wtoken.clientHidden = !visible;
3048 wtoken.sendAppVisibilityToClients();
3049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 wtoken.willBeHidden = false;
3052 if (wtoken.hidden == visible) {
3053 final int N = wtoken.allAppWindows.size();
3054 boolean changed = false;
3055 if (DEBUG_APP_TRANSITIONS) Log.v(
3056 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3057 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3062 if (wtoken.animation == sDummyAnimation) {
3063 wtoken.animation = null;
3064 }
3065 applyAnimationLocked(wtoken, lp, transit, visible);
3066 changed = true;
3067 if (wtoken.animation != null) {
3068 delayed = runningAppAnimation = true;
3069 }
3070 }
Romain Guy06882f82009-06-10 13:36:04 -07003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 for (int i=0; i<N; i++) {
3073 WindowState win = wtoken.allAppWindows.get(i);
3074 if (win == wtoken.startingWindow) {
3075 continue;
3076 }
3077
3078 if (win.isAnimating()) {
3079 delayed = true;
3080 }
Romain Guy06882f82009-06-10 13:36:04 -07003081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3083 //win.dump(" ");
3084 if (visible) {
3085 if (!win.isVisibleNow()) {
3086 if (!runningAppAnimation) {
3087 applyAnimationLocked(win,
3088 WindowManagerPolicy.TRANSIT_ENTER, true);
3089 }
3090 changed = true;
3091 }
3092 } else if (win.isVisibleNow()) {
3093 if (!runningAppAnimation) {
3094 applyAnimationLocked(win,
3095 WindowManagerPolicy.TRANSIT_EXIT, false);
3096 }
3097 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3098 KeyWaiter.RETURN_NOTHING);
3099 changed = true;
3100 }
3101 }
3102
3103 wtoken.hidden = wtoken.hiddenRequested = !visible;
3104 if (!visible) {
3105 unsetAppFreezingScreenLocked(wtoken, true, true);
3106 } else {
3107 // If we are being set visible, and the starting window is
3108 // not yet displayed, then make sure it doesn't get displayed.
3109 WindowState swin = wtoken.startingWindow;
3110 if (swin != null && (swin.mDrawPending
3111 || swin.mCommitDrawPending)) {
3112 swin.mPolicyVisibility = false;
3113 swin.mPolicyVisibilityAfterAnim = false;
3114 }
3115 }
Romain Guy06882f82009-06-10 13:36:04 -07003116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3118 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3119 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 if (changed && performLayout) {
3122 mLayoutNeeded = true;
3123 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 performLayoutAndPlaceSurfacesLocked();
3125 }
3126 }
3127
3128 if (wtoken.animation != null) {
3129 delayed = true;
3130 }
Romain Guy06882f82009-06-10 13:36:04 -07003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 return delayed;
3133 }
3134
3135 public void setAppVisibility(IBinder token, boolean visible) {
3136 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3137 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003138 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 }
3140
3141 AppWindowToken wtoken;
3142
3143 synchronized(mWindowMap) {
3144 wtoken = findAppWindowToken(token);
3145 if (wtoken == null) {
3146 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3147 return;
3148 }
3149
3150 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3151 RuntimeException e = new RuntimeException();
3152 e.fillInStackTrace();
3153 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3154 + "): mNextAppTransition=" + mNextAppTransition
3155 + " hidden=" + wtoken.hidden
3156 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3157 }
Romain Guy06882f82009-06-10 13:36:04 -07003158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 // If we are preparing an app transition, then delay changing
3160 // the visibility of this token until we execute that transition.
3161 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3162 // Already in requested state, don't do anything more.
3163 if (wtoken.hiddenRequested != visible) {
3164 return;
3165 }
3166 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 if (DEBUG_APP_TRANSITIONS) Log.v(
3169 TAG, "Setting dummy animation on: " + wtoken);
3170 wtoken.setDummyAnimation();
3171 mOpeningApps.remove(wtoken);
3172 mClosingApps.remove(wtoken);
3173 wtoken.inPendingTransaction = true;
3174 if (visible) {
3175 mOpeningApps.add(wtoken);
3176 wtoken.allDrawn = false;
3177 wtoken.startingDisplayed = false;
3178 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07003179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 if (wtoken.clientHidden) {
3181 // In the case where we are making an app visible
3182 // but holding off for a transition, we still need
3183 // to tell the client to make its windows visible so
3184 // they get drawn. Otherwise, we will wait on
3185 // performing the transition until all windows have
3186 // been drawn, they never will be, and we are sad.
3187 wtoken.clientHidden = false;
3188 wtoken.sendAppVisibilityToClients();
3189 }
3190 } else {
3191 mClosingApps.add(wtoken);
3192 }
3193 return;
3194 }
Romain Guy06882f82009-06-10 13:36:04 -07003195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 final long origId = Binder.clearCallingIdentity();
3197 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3198 wtoken.updateReportedVisibilityLocked();
3199 Binder.restoreCallingIdentity(origId);
3200 }
3201 }
3202
3203 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3204 boolean unfreezeSurfaceNow, boolean force) {
3205 if (wtoken.freezingScreen) {
3206 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3207 + " force=" + force);
3208 final int N = wtoken.allAppWindows.size();
3209 boolean unfrozeWindows = false;
3210 for (int i=0; i<N; i++) {
3211 WindowState w = wtoken.allAppWindows.get(i);
3212 if (w.mAppFreezing) {
3213 w.mAppFreezing = false;
3214 if (w.mSurface != null && !w.mOrientationChanging) {
3215 w.mOrientationChanging = true;
3216 }
3217 unfrozeWindows = true;
3218 }
3219 }
3220 if (force || unfrozeWindows) {
3221 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3222 wtoken.freezingScreen = false;
3223 mAppsFreezingScreen--;
3224 }
3225 if (unfreezeSurfaceNow) {
3226 if (unfrozeWindows) {
3227 mLayoutNeeded = true;
3228 performLayoutAndPlaceSurfacesLocked();
3229 }
3230 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3231 stopFreezingDisplayLocked();
3232 }
3233 }
3234 }
3235 }
Romain Guy06882f82009-06-10 13:36:04 -07003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3238 int configChanges) {
3239 if (DEBUG_ORIENTATION) {
3240 RuntimeException e = new RuntimeException();
3241 e.fillInStackTrace();
3242 Log.i(TAG, "Set freezing of " + wtoken.appToken
3243 + ": hidden=" + wtoken.hidden + " freezing="
3244 + wtoken.freezingScreen, e);
3245 }
3246 if (!wtoken.hiddenRequested) {
3247 if (!wtoken.freezingScreen) {
3248 wtoken.freezingScreen = true;
3249 mAppsFreezingScreen++;
3250 if (mAppsFreezingScreen == 1) {
3251 startFreezingDisplayLocked();
3252 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3253 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3254 5000);
3255 }
3256 }
3257 final int N = wtoken.allAppWindows.size();
3258 for (int i=0; i<N; i++) {
3259 WindowState w = wtoken.allAppWindows.get(i);
3260 w.mAppFreezing = true;
3261 }
3262 }
3263 }
Romain Guy06882f82009-06-10 13:36:04 -07003264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 public void startAppFreezingScreen(IBinder token, int configChanges) {
3266 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3267 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003268 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 }
3270
3271 synchronized(mWindowMap) {
3272 if (configChanges == 0 && !mDisplayFrozen) {
3273 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3274 return;
3275 }
Romain Guy06882f82009-06-10 13:36:04 -07003276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 AppWindowToken wtoken = findAppWindowToken(token);
3278 if (wtoken == null || wtoken.appToken == null) {
3279 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3280 return;
3281 }
3282 final long origId = Binder.clearCallingIdentity();
3283 startAppFreezingScreenLocked(wtoken, configChanges);
3284 Binder.restoreCallingIdentity(origId);
3285 }
3286 }
Romain Guy06882f82009-06-10 13:36:04 -07003287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 public void stopAppFreezingScreen(IBinder token, boolean force) {
3289 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3290 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003291 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 }
3293
3294 synchronized(mWindowMap) {
3295 AppWindowToken wtoken = findAppWindowToken(token);
3296 if (wtoken == null || wtoken.appToken == null) {
3297 return;
3298 }
3299 final long origId = Binder.clearCallingIdentity();
3300 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3301 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3302 unsetAppFreezingScreenLocked(wtoken, true, force);
3303 Binder.restoreCallingIdentity(origId);
3304 }
3305 }
Romain Guy06882f82009-06-10 13:36:04 -07003306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 public void removeAppToken(IBinder token) {
3308 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3309 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003310 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 }
3312
3313 AppWindowToken wtoken = null;
3314 AppWindowToken startingToken = null;
3315 boolean delayed = false;
3316
3317 final long origId = Binder.clearCallingIdentity();
3318 synchronized(mWindowMap) {
3319 WindowToken basewtoken = mTokenMap.remove(token);
3320 mTokenList.remove(basewtoken);
3321 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3322 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3323 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3324 wtoken.inPendingTransaction = false;
3325 mOpeningApps.remove(wtoken);
3326 if (mClosingApps.contains(wtoken)) {
3327 delayed = true;
3328 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3329 mClosingApps.add(wtoken);
3330 delayed = true;
3331 }
3332 if (DEBUG_APP_TRANSITIONS) Log.v(
3333 TAG, "Removing app " + wtoken + " delayed=" + delayed
3334 + " animation=" + wtoken.animation
3335 + " animating=" + wtoken.animating);
3336 if (delayed) {
3337 // set the token aside because it has an active animation to be finished
3338 mExitingAppTokens.add(wtoken);
3339 }
3340 mAppTokens.remove(wtoken);
3341 wtoken.removed = true;
3342 if (wtoken.startingData != null) {
3343 startingToken = wtoken;
3344 }
3345 unsetAppFreezingScreenLocked(wtoken, true, true);
3346 if (mFocusedApp == wtoken) {
3347 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3348 mFocusedApp = null;
3349 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3350 mKeyWaiter.tickle();
3351 }
3352 } else {
3353 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3354 }
Romain Guy06882f82009-06-10 13:36:04 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 if (!delayed && wtoken != null) {
3357 wtoken.updateReportedVisibilityLocked();
3358 }
3359 }
3360 Binder.restoreCallingIdentity(origId);
3361
3362 if (startingToken != null) {
3363 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3364 + startingToken + ": app token removed");
3365 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3366 mH.sendMessage(m);
3367 }
3368 }
3369
3370 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3371 final int NW = token.windows.size();
3372 for (int i=0; i<NW; i++) {
3373 WindowState win = token.windows.get(i);
3374 mWindows.remove(win);
3375 int j = win.mChildWindows.size();
3376 while (j > 0) {
3377 j--;
3378 mWindows.remove(win.mChildWindows.get(j));
3379 }
3380 }
3381 return NW > 0;
3382 }
3383
3384 void dumpAppTokensLocked() {
3385 for (int i=mAppTokens.size()-1; i>=0; i--) {
3386 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3387 }
3388 }
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 void dumpWindowsLocked() {
3391 for (int i=mWindows.size()-1; i>=0; i--) {
3392 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3393 }
3394 }
Romain Guy06882f82009-06-10 13:36:04 -07003395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 private int findWindowOffsetLocked(int tokenPos) {
3397 final int NW = mWindows.size();
3398
3399 if (tokenPos >= mAppTokens.size()) {
3400 int i = NW;
3401 while (i > 0) {
3402 i--;
3403 WindowState win = (WindowState)mWindows.get(i);
3404 if (win.getAppToken() != null) {
3405 return i+1;
3406 }
3407 }
3408 }
3409
3410 while (tokenPos > 0) {
3411 // Find the first app token below the new position that has
3412 // a window displayed.
3413 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3414 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3415 + tokenPos + " -- " + wtoken.token);
3416 int i = wtoken.windows.size();
3417 while (i > 0) {
3418 i--;
3419 WindowState win = wtoken.windows.get(i);
3420 int j = win.mChildWindows.size();
3421 while (j > 0) {
3422 j--;
3423 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3424 if (cwin.mSubLayer >= 0 ) {
3425 for (int pos=NW-1; pos>=0; pos--) {
3426 if (mWindows.get(pos) == cwin) {
3427 if (DEBUG_REORDER) Log.v(TAG,
3428 "Found child win @" + (pos+1));
3429 return pos+1;
3430 }
3431 }
3432 }
3433 }
3434 for (int pos=NW-1; pos>=0; pos--) {
3435 if (mWindows.get(pos) == win) {
3436 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3437 return pos+1;
3438 }
3439 }
3440 }
3441 tokenPos--;
3442 }
3443
3444 return 0;
3445 }
3446
3447 private final int reAddWindowLocked(int index, WindowState win) {
3448 final int NCW = win.mChildWindows.size();
3449 boolean added = false;
3450 for (int j=0; j<NCW; j++) {
3451 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3452 if (!added && cwin.mSubLayer >= 0) {
3453 mWindows.add(index, win);
3454 index++;
3455 added = true;
3456 }
3457 mWindows.add(index, cwin);
3458 index++;
3459 }
3460 if (!added) {
3461 mWindows.add(index, win);
3462 index++;
3463 }
3464 return index;
3465 }
Romain Guy06882f82009-06-10 13:36:04 -07003466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3468 final int NW = token.windows.size();
3469 for (int i=0; i<NW; i++) {
3470 index = reAddWindowLocked(index, token.windows.get(i));
3471 }
3472 return index;
3473 }
3474
3475 public void moveAppToken(int index, IBinder token) {
3476 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3477 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003478 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480
3481 synchronized(mWindowMap) {
3482 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3483 if (DEBUG_REORDER) dumpAppTokensLocked();
3484 final AppWindowToken wtoken = findAppWindowToken(token);
3485 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3486 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3487 + token + " (" + wtoken + ")");
3488 return;
3489 }
3490 mAppTokens.add(index, wtoken);
3491 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3492 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 final long origId = Binder.clearCallingIdentity();
3495 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3496 if (DEBUG_REORDER) dumpWindowsLocked();
3497 if (tmpRemoveAppWindowsLocked(wtoken)) {
3498 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3499 if (DEBUG_REORDER) dumpWindowsLocked();
3500 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3501 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3502 if (DEBUG_REORDER) dumpWindowsLocked();
3503 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 mLayoutNeeded = true;
3505 performLayoutAndPlaceSurfacesLocked();
3506 }
3507 Binder.restoreCallingIdentity(origId);
3508 }
3509 }
3510
3511 private void removeAppTokensLocked(List<IBinder> tokens) {
3512 // XXX This should be done more efficiently!
3513 // (take advantage of the fact that both lists should be
3514 // ordered in the same way.)
3515 int N = tokens.size();
3516 for (int i=0; i<N; i++) {
3517 IBinder token = tokens.get(i);
3518 final AppWindowToken wtoken = findAppWindowToken(token);
3519 if (!mAppTokens.remove(wtoken)) {
3520 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3521 + token + " (" + wtoken + ")");
3522 i--;
3523 N--;
3524 }
3525 }
3526 }
3527
3528 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3529 // First remove all of the windows from the list.
3530 final int N = tokens.size();
3531 int i;
3532 for (i=0; i<N; i++) {
3533 WindowToken token = mTokenMap.get(tokens.get(i));
3534 if (token != null) {
3535 tmpRemoveAppWindowsLocked(token);
3536 }
3537 }
3538
3539 // Where to start adding?
3540 int pos = findWindowOffsetLocked(tokenPos);
3541
3542 // And now add them back at the correct place.
3543 for (i=0; i<N; i++) {
3544 WindowToken token = mTokenMap.get(tokens.get(i));
3545 if (token != null) {
3546 pos = reAddAppWindowsLocked(pos, token);
3547 }
3548 }
3549
3550 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 mLayoutNeeded = true;
3552 performLayoutAndPlaceSurfacesLocked();
3553
3554 //dump();
3555 }
3556
3557 public void moveAppTokensToTop(List<IBinder> tokens) {
3558 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3559 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003560 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 }
3562
3563 final long origId = Binder.clearCallingIdentity();
3564 synchronized(mWindowMap) {
3565 removeAppTokensLocked(tokens);
3566 final int N = tokens.size();
3567 for (int i=0; i<N; i++) {
3568 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3569 if (wt != null) {
3570 mAppTokens.add(wt);
3571 }
3572 }
3573 moveAppWindowsLocked(tokens, mAppTokens.size());
3574 }
3575 Binder.restoreCallingIdentity(origId);
3576 }
3577
3578 public void moveAppTokensToBottom(List<IBinder> tokens) {
3579 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3580 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003581 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 }
3583
3584 final long origId = Binder.clearCallingIdentity();
3585 synchronized(mWindowMap) {
3586 removeAppTokensLocked(tokens);
3587 final int N = tokens.size();
3588 int pos = 0;
3589 for (int i=0; i<N; i++) {
3590 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3591 if (wt != null) {
3592 mAppTokens.add(pos, wt);
3593 pos++;
3594 }
3595 }
3596 moveAppWindowsLocked(tokens, 0);
3597 }
3598 Binder.restoreCallingIdentity(origId);
3599 }
3600
3601 // -------------------------------------------------------------
3602 // Misc IWindowSession methods
3603 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 public void disableKeyguard(IBinder token, String tag) {
3606 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3607 != PackageManager.PERMISSION_GRANTED) {
3608 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3609 }
3610 mKeyguardDisabled.acquire(token, tag);
3611 }
3612
3613 public void reenableKeyguard(IBinder token) {
3614 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3615 != PackageManager.PERMISSION_GRANTED) {
3616 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3617 }
3618 synchronized (mKeyguardDisabled) {
3619 mKeyguardDisabled.release(token);
3620
3621 if (!mKeyguardDisabled.isAcquired()) {
3622 // if we are the last one to reenable the keyguard wait until
3623 // we have actaully finished reenabling until returning
3624 mWaitingUntilKeyguardReenabled = true;
3625 while (mWaitingUntilKeyguardReenabled) {
3626 try {
3627 mKeyguardDisabled.wait();
3628 } catch (InterruptedException e) {
3629 Thread.currentThread().interrupt();
3630 }
3631 }
3632 }
3633 }
3634 }
3635
3636 /**
3637 * @see android.app.KeyguardManager#exitKeyguardSecurely
3638 */
3639 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3640 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3641 != PackageManager.PERMISSION_GRANTED) {
3642 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3643 }
3644 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3645 public void onKeyguardExitResult(boolean success) {
3646 try {
3647 callback.onKeyguardExitResult(success);
3648 } catch (RemoteException e) {
3649 // Client has died, we don't care.
3650 }
3651 }
3652 });
3653 }
3654
3655 public boolean inKeyguardRestrictedInputMode() {
3656 return mPolicy.inKeyguardRestrictedKeyInputMode();
3657 }
Romain Guy06882f82009-06-10 13:36:04 -07003658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 static float fixScale(float scale) {
3660 if (scale < 0) scale = 0;
3661 else if (scale > 20) scale = 20;
3662 return Math.abs(scale);
3663 }
Romain Guy06882f82009-06-10 13:36:04 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 public void setAnimationScale(int which, float scale) {
3666 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3667 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003668 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670
3671 if (scale < 0) scale = 0;
3672 else if (scale > 20) scale = 20;
3673 scale = Math.abs(scale);
3674 switch (which) {
3675 case 0: mWindowAnimationScale = fixScale(scale); break;
3676 case 1: mTransitionAnimationScale = fixScale(scale); break;
3677 }
Romain Guy06882f82009-06-10 13:36:04 -07003678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 // Persist setting
3680 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3681 }
Romain Guy06882f82009-06-10 13:36:04 -07003682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 public void setAnimationScales(float[] scales) {
3684 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3685 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003686 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 }
3688
3689 if (scales != null) {
3690 if (scales.length >= 1) {
3691 mWindowAnimationScale = fixScale(scales[0]);
3692 }
3693 if (scales.length >= 2) {
3694 mTransitionAnimationScale = fixScale(scales[1]);
3695 }
3696 }
Romain Guy06882f82009-06-10 13:36:04 -07003697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 // Persist setting
3699 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3700 }
Romain Guy06882f82009-06-10 13:36:04 -07003701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 public float getAnimationScale(int which) {
3703 switch (which) {
3704 case 0: return mWindowAnimationScale;
3705 case 1: return mTransitionAnimationScale;
3706 }
3707 return 0;
3708 }
Romain Guy06882f82009-06-10 13:36:04 -07003709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 public float[] getAnimationScales() {
3711 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3712 }
Romain Guy06882f82009-06-10 13:36:04 -07003713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 public int getSwitchState(int sw) {
3715 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3716 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003717 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 }
3719 return KeyInputQueue.getSwitchState(sw);
3720 }
Romain Guy06882f82009-06-10 13:36:04 -07003721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 public int getSwitchStateForDevice(int devid, int sw) {
3723 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3724 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003725 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727 return KeyInputQueue.getSwitchState(devid, sw);
3728 }
Romain Guy06882f82009-06-10 13:36:04 -07003729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 public int getScancodeState(int sw) {
3731 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3732 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003733 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
3735 return KeyInputQueue.getScancodeState(sw);
3736 }
Romain Guy06882f82009-06-10 13:36:04 -07003737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 public int getScancodeStateForDevice(int devid, int sw) {
3739 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3740 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003741 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 }
3743 return KeyInputQueue.getScancodeState(devid, sw);
3744 }
Romain Guy06882f82009-06-10 13:36:04 -07003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 public int getKeycodeState(int sw) {
3747 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3748 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003749 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 }
3751 return KeyInputQueue.getKeycodeState(sw);
3752 }
Romain Guy06882f82009-06-10 13:36:04 -07003753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 public int getKeycodeStateForDevice(int devid, int sw) {
3755 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3756 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003757 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759 return KeyInputQueue.getKeycodeState(devid, sw);
3760 }
Romain Guy06882f82009-06-10 13:36:04 -07003761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3763 return KeyInputQueue.hasKeys(keycodes, keyExists);
3764 }
Romain Guy06882f82009-06-10 13:36:04 -07003765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 public void enableScreenAfterBoot() {
3767 synchronized(mWindowMap) {
3768 if (mSystemBooted) {
3769 return;
3770 }
3771 mSystemBooted = true;
3772 }
Romain Guy06882f82009-06-10 13:36:04 -07003773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 performEnableScreen();
3775 }
Romain Guy06882f82009-06-10 13:36:04 -07003776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 public void enableScreenIfNeededLocked() {
3778 if (mDisplayEnabled) {
3779 return;
3780 }
3781 if (!mSystemBooted) {
3782 return;
3783 }
3784 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3785 }
Romain Guy06882f82009-06-10 13:36:04 -07003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 public void performEnableScreen() {
3788 synchronized(mWindowMap) {
3789 if (mDisplayEnabled) {
3790 return;
3791 }
3792 if (!mSystemBooted) {
3793 return;
3794 }
Romain Guy06882f82009-06-10 13:36:04 -07003795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 // Don't enable the screen until all existing windows
3797 // have been drawn.
3798 final int N = mWindows.size();
3799 for (int i=0; i<N; i++) {
3800 WindowState w = (WindowState)mWindows.get(i);
3801 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3802 return;
3803 }
3804 }
Romain Guy06882f82009-06-10 13:36:04 -07003805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 mDisplayEnabled = true;
3807 if (false) {
3808 Log.i(TAG, "ENABLING SCREEN!");
3809 StringWriter sw = new StringWriter();
3810 PrintWriter pw = new PrintWriter(sw);
3811 this.dump(null, pw, null);
3812 Log.i(TAG, sw.toString());
3813 }
3814 try {
3815 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3816 if (surfaceFlinger != null) {
3817 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3818 Parcel data = Parcel.obtain();
3819 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3820 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3821 data, null, 0);
3822 data.recycle();
3823 }
3824 } catch (RemoteException ex) {
3825 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3826 }
3827 }
Romain Guy06882f82009-06-10 13:36:04 -07003828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003832 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3833 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 }
Romain Guy06882f82009-06-10 13:36:04 -07003835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 public void setInTouchMode(boolean mode) {
3837 synchronized(mWindowMap) {
3838 mInTouchMode = mode;
3839 }
3840 }
3841
Romain Guy06882f82009-06-10 13:36:04 -07003842 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003843 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003845 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003846 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 }
3848
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003849 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 }
Romain Guy06882f82009-06-10 13:36:04 -07003851
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003852 public void setRotationUnchecked(int rotation,
3853 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 if(DEBUG_ORIENTATION) Log.v(TAG,
3855 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 long origId = Binder.clearCallingIdentity();
3858 boolean changed;
3859 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003860 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 }
Romain Guy06882f82009-06-10 13:36:04 -07003862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 if (changed) {
3864 sendNewConfiguration();
3865 synchronized(mWindowMap) {
3866 mLayoutNeeded = true;
3867 performLayoutAndPlaceSurfacesLocked();
3868 }
3869 } else if (alwaysSendConfiguration) {
3870 //update configuration ignoring orientation change
3871 sendNewConfiguration();
3872 }
Romain Guy06882f82009-06-10 13:36:04 -07003873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 Binder.restoreCallingIdentity(origId);
3875 }
Romain Guy06882f82009-06-10 13:36:04 -07003876
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003877 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 boolean changed;
3879 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3880 rotation = mRequestedRotation;
3881 } else {
3882 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003883 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
3885 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003886 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 mRotation, mDisplayEnabled);
3888 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3889 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003892 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 "Rotation changed to " + rotation
3894 + " from " + mRotation
3895 + " (forceApp=" + mForcedAppOrientation
3896 + ", req=" + mRequestedRotation + ")");
3897 mRotation = rotation;
3898 mWindowsFreezingScreen = true;
3899 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3900 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3901 2000);
3902 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003903 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 mQueue.setOrientation(rotation);
3905 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003906 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
3908 for (int i=mWindows.size()-1; i>=0; i--) {
3909 WindowState w = (WindowState)mWindows.get(i);
3910 if (w.mSurface != null) {
3911 w.mOrientationChanging = true;
3912 }
3913 }
3914 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3915 try {
3916 mRotationWatchers.get(i).onRotationChanged(rotation);
3917 } catch (RemoteException e) {
3918 }
3919 }
3920 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 return changed;
3923 }
Romain Guy06882f82009-06-10 13:36:04 -07003924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 public int getRotation() {
3926 return mRotation;
3927 }
3928
3929 public int watchRotation(IRotationWatcher watcher) {
3930 final IBinder watcherBinder = watcher.asBinder();
3931 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3932 public void binderDied() {
3933 synchronized (mWindowMap) {
3934 for (int i=0; i<mRotationWatchers.size(); i++) {
3935 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003936 IRotationWatcher removed = mRotationWatchers.remove(i);
3937 if (removed != null) {
3938 removed.asBinder().unlinkToDeath(this, 0);
3939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 i--;
3941 }
3942 }
3943 }
3944 }
3945 };
Romain Guy06882f82009-06-10 13:36:04 -07003946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947 synchronized (mWindowMap) {
3948 try {
3949 watcher.asBinder().linkToDeath(dr, 0);
3950 mRotationWatchers.add(watcher);
3951 } catch (RemoteException e) {
3952 // Client died, no cleanup needed.
3953 }
Romain Guy06882f82009-06-10 13:36:04 -07003954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 return mRotation;
3956 }
3957 }
3958
3959 /**
3960 * Starts the view server on the specified port.
3961 *
3962 * @param port The port to listener to.
3963 *
3964 * @return True if the server was successfully started, false otherwise.
3965 *
3966 * @see com.android.server.ViewServer
3967 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3968 */
3969 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003970 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 return false;
3972 }
3973
3974 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3975 return false;
3976 }
3977
3978 if (port < 1024) {
3979 return false;
3980 }
3981
3982 if (mViewServer != null) {
3983 if (!mViewServer.isRunning()) {
3984 try {
3985 return mViewServer.start();
3986 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003987 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 }
3989 }
3990 return false;
3991 }
3992
3993 try {
3994 mViewServer = new ViewServer(this, port);
3995 return mViewServer.start();
3996 } catch (IOException e) {
3997 Log.w(TAG, "View server did not start");
3998 }
3999 return false;
4000 }
4001
Romain Guy06882f82009-06-10 13:36:04 -07004002 private boolean isSystemSecure() {
4003 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4004 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4005 }
4006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 /**
4008 * Stops the view server if it exists.
4009 *
4010 * @return True if the server stopped, false if it wasn't started or
4011 * couldn't be stopped.
4012 *
4013 * @see com.android.server.ViewServer
4014 */
4015 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004016 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 return false;
4018 }
4019
4020 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4021 return false;
4022 }
4023
4024 if (mViewServer != null) {
4025 return mViewServer.stop();
4026 }
4027 return false;
4028 }
4029
4030 /**
4031 * Indicates whether the view server is running.
4032 *
4033 * @return True if the server is running, false otherwise.
4034 *
4035 * @see com.android.server.ViewServer
4036 */
4037 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004038 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 return false;
4040 }
4041
4042 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4043 return false;
4044 }
4045
4046 return mViewServer != null && mViewServer.isRunning();
4047 }
4048
4049 /**
4050 * Lists all availble windows in the system. The listing is written in the
4051 * specified Socket's output stream with the following syntax:
4052 * windowHashCodeInHexadecimal windowName
4053 * Each line of the ouput represents a different window.
4054 *
4055 * @param client The remote client to send the listing to.
4056 * @return False if an error occured, true otherwise.
4057 */
4058 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004059 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 return false;
4061 }
4062
4063 boolean result = true;
4064
4065 Object[] windows;
4066 synchronized (mWindowMap) {
4067 windows = new Object[mWindows.size()];
4068 //noinspection unchecked
4069 windows = mWindows.toArray(windows);
4070 }
4071
4072 BufferedWriter out = null;
4073
4074 // Any uncaught exception will crash the system process
4075 try {
4076 OutputStream clientStream = client.getOutputStream();
4077 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4078
4079 final int count = windows.length;
4080 for (int i = 0; i < count; i++) {
4081 final WindowState w = (WindowState) windows[i];
4082 out.write(Integer.toHexString(System.identityHashCode(w)));
4083 out.write(' ');
4084 out.append(w.mAttrs.getTitle());
4085 out.write('\n');
4086 }
4087
4088 out.write("DONE.\n");
4089 out.flush();
4090 } catch (Exception e) {
4091 result = false;
4092 } finally {
4093 if (out != null) {
4094 try {
4095 out.close();
4096 } catch (IOException e) {
4097 result = false;
4098 }
4099 }
4100 }
4101
4102 return result;
4103 }
4104
4105 /**
4106 * Sends a command to a target window. The result of the command, if any, will be
4107 * written in the output stream of the specified socket.
4108 *
4109 * The parameters must follow this syntax:
4110 * windowHashcode extra
4111 *
4112 * Where XX is the length in characeters of the windowTitle.
4113 *
4114 * The first parameter is the target window. The window with the specified hashcode
4115 * will be the target. If no target can be found, nothing happens. The extra parameters
4116 * will be delivered to the target window and as parameters to the command itself.
4117 *
4118 * @param client The remote client to sent the result, if any, to.
4119 * @param command The command to execute.
4120 * @param parameters The command parameters.
4121 *
4122 * @return True if the command was successfully delivered, false otherwise. This does
4123 * not indicate whether the command itself was successful.
4124 */
4125 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004126 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 return false;
4128 }
4129
4130 boolean success = true;
4131 Parcel data = null;
4132 Parcel reply = null;
4133
4134 // Any uncaught exception will crash the system process
4135 try {
4136 // Find the hashcode of the window
4137 int index = parameters.indexOf(' ');
4138 if (index == -1) {
4139 index = parameters.length();
4140 }
4141 final String code = parameters.substring(0, index);
4142 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4143
4144 // Extract the command's parameter after the window description
4145 if (index < parameters.length()) {
4146 parameters = parameters.substring(index + 1);
4147 } else {
4148 parameters = "";
4149 }
4150
4151 final WindowManagerService.WindowState window = findWindow(hashCode);
4152 if (window == null) {
4153 return false;
4154 }
4155
4156 data = Parcel.obtain();
4157 data.writeInterfaceToken("android.view.IWindow");
4158 data.writeString(command);
4159 data.writeString(parameters);
4160 data.writeInt(1);
4161 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4162
4163 reply = Parcel.obtain();
4164
4165 final IBinder binder = window.mClient.asBinder();
4166 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4167 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4168
4169 reply.readException();
4170
4171 } catch (Exception e) {
4172 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4173 success = false;
4174 } finally {
4175 if (data != null) {
4176 data.recycle();
4177 }
4178 if (reply != null) {
4179 reply.recycle();
4180 }
4181 }
4182
4183 return success;
4184 }
4185
4186 private WindowState findWindow(int hashCode) {
4187 if (hashCode == -1) {
4188 return getFocusedWindow();
4189 }
4190
4191 synchronized (mWindowMap) {
4192 final ArrayList windows = mWindows;
4193 final int count = windows.size();
4194
4195 for (int i = 0; i < count; i++) {
4196 WindowState w = (WindowState) windows.get(i);
4197 if (System.identityHashCode(w) == hashCode) {
4198 return w;
4199 }
4200 }
4201 }
4202
4203 return null;
4204 }
4205
4206 /*
4207 * Instruct the Activity Manager to fetch the current configuration and broadcast
4208 * that to config-changed listeners if appropriate.
4209 */
4210 void sendNewConfiguration() {
4211 try {
4212 mActivityManager.updateConfiguration(null);
4213 } catch (RemoteException e) {
4214 }
4215 }
Romain Guy06882f82009-06-10 13:36:04 -07004216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217 public Configuration computeNewConfiguration() {
4218 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004219 return computeNewConfigurationLocked();
4220 }
4221 }
Romain Guy06882f82009-06-10 13:36:04 -07004222
Dianne Hackbornc485a602009-03-24 22:39:49 -07004223 Configuration computeNewConfigurationLocked() {
4224 Configuration config = new Configuration();
4225 if (!computeNewConfigurationLocked(config)) {
4226 return null;
4227 }
4228 Log.i(TAG, "Config changed: " + config);
4229 long now = SystemClock.uptimeMillis();
4230 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
4231 if (mFreezeGcPending != 0) {
4232 if (now > (mFreezeGcPending+1000)) {
4233 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
4234 mH.removeMessages(H.FORCE_GC);
4235 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 mFreezeGcPending = now;
4237 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004238 } else {
4239 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004241 return config;
4242 }
Romain Guy06882f82009-06-10 13:36:04 -07004243
Dianne Hackbornc485a602009-03-24 22:39:49 -07004244 boolean computeNewConfigurationLocked(Configuration config) {
4245 if (mDisplay == null) {
4246 return false;
4247 }
4248 mQueue.getInputConfiguration(config);
4249 final int dw = mDisplay.getWidth();
4250 final int dh = mDisplay.getHeight();
4251 int orientation = Configuration.ORIENTATION_SQUARE;
4252 if (dw < dh) {
4253 orientation = Configuration.ORIENTATION_PORTRAIT;
4254 } else if (dw > dh) {
4255 orientation = Configuration.ORIENTATION_LANDSCAPE;
4256 }
4257 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004258
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004259 DisplayMetrics dm = new DisplayMetrics();
4260 mDisplay.getMetrics(dm);
4261 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4262
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004263 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004264 // Note we only do this once because at this point we don't
4265 // expect the screen to change in this way at runtime, and want
4266 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004267 int longSize = dw;
4268 int shortSize = dh;
4269 if (longSize < shortSize) {
4270 int tmp = longSize;
4271 longSize = shortSize;
4272 shortSize = tmp;
4273 }
4274 longSize = (int)(longSize/dm.density);
4275 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004276
Dianne Hackborn723738c2009-06-25 19:48:04 -07004277 // These semi-magic numbers define our compatibility modes for
4278 // applications with different screens. Don't change unless you
4279 // make sure to test lots and lots of apps!
4280 if (longSize < 470) {
4281 // This is shorter than an HVGA normal density screen (which
4282 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004283 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4284 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004285 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004286 // Is this a large screen?
4287 if (longSize > 640 && shortSize >= 480) {
4288 // VGA or larger screens at medium density are the point
4289 // at which we consider it to be a large screen.
4290 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4291 } else {
4292 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4293
4294 // If this screen is wider than normal HVGA, or taller
4295 // than FWVGA, then for old apps we want to run in size
4296 // compatibility mode.
4297 if (shortSize > 321 || longSize > 570) {
4298 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4299 }
4300 }
4301
4302 // Is this a long screen?
4303 if (((longSize*3)/5) >= (shortSize-1)) {
4304 // Anything wider than WVGA (5:3) is considering to be long.
4305 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4306 } else {
4307 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4308 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004309 }
4310 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004311 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004312
Dianne Hackbornc485a602009-03-24 22:39:49 -07004313 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4314 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4315 mPolicy.adjustConfigurationLw(config);
4316 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 }
Romain Guy06882f82009-06-10 13:36:04 -07004318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 // -------------------------------------------------------------
4320 // Input Events and Focus Management
4321 // -------------------------------------------------------------
4322
4323 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004324 long curTime = SystemClock.uptimeMillis();
4325
Michael Chane10de972009-05-18 11:24:50 -07004326 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004327 if (mLastTouchEventType == eventType &&
4328 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4329 return;
4330 }
4331 mLastUserActivityCallTime = curTime;
4332 mLastTouchEventType = eventType;
4333 }
4334
4335 if (targetWin == null
4336 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4337 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
4339 }
4340
4341 // tells if it's a cheek event or not -- this function is stateful
4342 private static final int EVENT_NONE = 0;
4343 private static final int EVENT_UNKNOWN = 0;
4344 private static final int EVENT_CHEEK = 0;
4345 private static final int EVENT_IGNORE_DURATION = 300; // ms
4346 private static final float CHEEK_THRESHOLD = 0.6f;
4347 private int mEventState = EVENT_NONE;
4348 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 private int eventType(MotionEvent ev) {
4351 float size = ev.getSize();
4352 switch (ev.getAction()) {
4353 case MotionEvent.ACTION_DOWN:
4354 mEventSize = size;
4355 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4356 case MotionEvent.ACTION_UP:
4357 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004358 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 case MotionEvent.ACTION_MOVE:
4360 final int N = ev.getHistorySize();
4361 if (size > mEventSize) mEventSize = size;
4362 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4363 for (int i=0; i<N; i++) {
4364 size = ev.getHistoricalSize(i);
4365 if (size > mEventSize) mEventSize = size;
4366 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4367 }
4368 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4369 return TOUCH_EVENT;
4370 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004371 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 }
4373 default:
4374 // not good
4375 return OTHER_EVENT;
4376 }
4377 }
4378
4379 /**
4380 * @return Returns true if event was dispatched, false if it was dropped for any reason
4381 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004382 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4384 "dispatchPointer " + ev);
4385
Michael Chan53071d62009-05-13 17:29:48 -07004386 if (MEASURE_LATENCY) {
4387 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4388 }
4389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004391 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392
Michael Chan53071d62009-05-13 17:29:48 -07004393 if (MEASURE_LATENCY) {
4394 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4395 }
4396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 if (action == MotionEvent.ACTION_UP) {
4400 // let go of our target
4401 mKeyWaiter.mMotionTarget = null;
4402 mPowerManager.logPointerUpEvent();
4403 } else if (action == MotionEvent.ACTION_DOWN) {
4404 mPowerManager.logPointerDownEvent();
4405 }
4406
4407 if (targetObj == null) {
4408 // In this case we are either dropping the event, or have received
4409 // a move or up without a down. It is common to receive move
4410 // events in such a way, since this means the user is moving the
4411 // pointer without actually pressing down. All other cases should
4412 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004413 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4415 }
4416 if (qev != null) {
4417 mQueue.recycleEvent(qev);
4418 }
4419 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004420 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
4422 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4423 if (qev != null) {
4424 mQueue.recycleEvent(qev);
4425 }
4426 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004427 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 }
Romain Guy06882f82009-06-10 13:36:04 -07004429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004433 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434
4435 //Log.i(TAG, "Sending " + ev + " to " + target);
4436
4437 if (uid != 0 && uid != target.mSession.mUid) {
4438 if (mContext.checkPermission(
4439 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4440 != PackageManager.PERMISSION_GRANTED) {
4441 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4442 + pid + " uid " + uid + " to window " + target
4443 + " owned by uid " + target.mSession.mUid);
4444 if (qev != null) {
4445 mQueue.recycleEvent(qev);
4446 }
4447 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004448 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 }
4450 }
4451
Michael Chan53071d62009-05-13 17:29:48 -07004452 if (MEASURE_LATENCY) {
4453 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4454 }
4455
Romain Guy06882f82009-06-10 13:36:04 -07004456 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4458 //target wants to ignore fat touch events
4459 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4460 //explicit flag to return without processing event further
4461 boolean returnFlag = false;
4462 if((action == MotionEvent.ACTION_DOWN)) {
4463 mFatTouch = false;
4464 if(cheekPress) {
4465 mFatTouch = true;
4466 returnFlag = true;
4467 }
4468 } else {
4469 if(action == MotionEvent.ACTION_UP) {
4470 if(mFatTouch) {
4471 //earlier even was invalid doesnt matter if current up is cheekpress or not
4472 mFatTouch = false;
4473 returnFlag = true;
4474 } else if(cheekPress) {
4475 //cancel the earlier event
4476 ev.setAction(MotionEvent.ACTION_CANCEL);
4477 action = MotionEvent.ACTION_CANCEL;
4478 }
4479 } else if(action == MotionEvent.ACTION_MOVE) {
4480 if(mFatTouch) {
4481 //two cases here
4482 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004483 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 returnFlag = true;
4485 } else if(cheekPress) {
4486 //valid down followed by invalid moves
4487 //an invalid move have to cancel earlier action
4488 ev.setAction(MotionEvent.ACTION_CANCEL);
4489 action = MotionEvent.ACTION_CANCEL;
4490 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4491 //note that the subsequent invalid moves will not get here
4492 mFatTouch = true;
4493 }
4494 }
4495 } //else if action
4496 if(returnFlag) {
4497 //recycle que, ev
4498 if (qev != null) {
4499 mQueue.recycleEvent(qev);
4500 }
4501 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004502 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
4504 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004505
Michael Chan9f028e62009-08-04 17:37:46 -07004506 // Enable this for testing the "right" value
4507 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004508 int max_events_per_sec = 35;
4509 try {
4510 max_events_per_sec = Integer.parseInt(SystemProperties
4511 .get("windowsmgr.max_events_per_sec"));
4512 if (max_events_per_sec < 1) {
4513 max_events_per_sec = 35;
4514 }
4515 } catch (NumberFormatException e) {
4516 }
4517 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4518 }
4519
4520 /*
4521 * Throttle events to minimize CPU usage when there's a flood of events
4522 * e.g. constant contact with the screen
4523 */
4524 if (action == MotionEvent.ACTION_MOVE) {
4525 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4526 long now = SystemClock.uptimeMillis();
4527 if (now < nextEventTime) {
4528 try {
4529 Thread.sleep(nextEventTime - now);
4530 } catch (InterruptedException e) {
4531 }
4532 mLastTouchEventTime = nextEventTime;
4533 } else {
4534 mLastTouchEventTime = now;
4535 }
4536 }
4537
Michael Chan53071d62009-05-13 17:29:48 -07004538 if (MEASURE_LATENCY) {
4539 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4540 }
4541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 synchronized(mWindowMap) {
4543 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4544 mKeyWaiter.bindTargetWindowLocked(target,
4545 KeyWaiter.RETURN_PENDING_POINTER, qev);
4546 ev = null;
4547 } else {
4548 if (action == MotionEvent.ACTION_DOWN) {
4549 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4550 if (out != null) {
4551 MotionEvent oev = MotionEvent.obtain(ev);
4552 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4553 do {
4554 final Rect frame = out.mFrame;
4555 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4556 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004557 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 } catch (android.os.RemoteException e) {
4559 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4560 }
4561 oev.offsetLocation((float)frame.left, (float)frame.top);
4562 out = out.mNextOutsideTouch;
4563 } while (out != null);
4564 mKeyWaiter.mOutsideTouchTargets = null;
4565 }
4566 }
4567 final Rect frame = target.mFrame;
4568 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4569 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004570
4571 // If we are on top of the wallpaper, then the wallpaper also
4572 // gets to see this movement.
4573 if (mWallpaperTarget == target) {
4574 sendPointerToWallpaperLocked(target, ev, eventTime);
4575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 }
4577 }
Romain Guy06882f82009-06-10 13:36:04 -07004578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 // finally offset the event to the target's coordinate system and
4580 // dispatch the event.
4581 try {
4582 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4583 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4584 }
Michael Chan53071d62009-05-13 17:29:48 -07004585
4586 if (MEASURE_LATENCY) {
4587 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4588 }
4589
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004590 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004591
4592 if (MEASURE_LATENCY) {
4593 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4594 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004595 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 } catch (android.os.RemoteException e) {
4597 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4598 mKeyWaiter.mMotionTarget = null;
4599 try {
4600 removeWindow(target.mSession, target.mClient);
4601 } catch (java.util.NoSuchElementException ex) {
4602 // This will happen if the window has already been
4603 // removed.
4604 }
4605 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004606 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 }
Romain Guy06882f82009-06-10 13:36:04 -07004608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 /**
4610 * @return Returns true if event was dispatched, false if it was dropped for any reason
4611 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004612 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 if (DEBUG_INPUT) Log.v(
4614 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004617 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 if (focusObj == null) {
4619 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4620 if (qev != null) {
4621 mQueue.recycleEvent(qev);
4622 }
4623 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004624 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 }
4626 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4627 if (qev != null) {
4628 mQueue.recycleEvent(qev);
4629 }
4630 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004631 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 }
Romain Guy06882f82009-06-10 13:36:04 -07004633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 if (uid != 0 && uid != focus.mSession.mUid) {
4637 if (mContext.checkPermission(
4638 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4639 != PackageManager.PERMISSION_GRANTED) {
4640 Log.w(TAG, "Permission denied: injecting key event from pid "
4641 + pid + " uid " + uid + " to window " + focus
4642 + " owned by uid " + focus.mSession.mUid);
4643 if (qev != null) {
4644 mQueue.recycleEvent(qev);
4645 }
4646 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004647 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
4649 }
Romain Guy06882f82009-06-10 13:36:04 -07004650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004651 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 synchronized(mWindowMap) {
4654 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4655 mKeyWaiter.bindTargetWindowLocked(focus,
4656 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4657 // We don't deliver movement events to the client, we hold
4658 // them and wait for them to call back.
4659 ev = null;
4660 } else {
4661 mKeyWaiter.bindTargetWindowLocked(focus);
4662 }
4663 }
Romain Guy06882f82009-06-10 13:36:04 -07004664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004666 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004667 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 } catch (android.os.RemoteException e) {
4669 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4670 try {
4671 removeWindow(focus.mSession, focus.mClient);
4672 } catch (java.util.NoSuchElementException ex) {
4673 // This will happen if the window has already been
4674 // removed.
4675 }
4676 }
Romain Guy06882f82009-06-10 13:36:04 -07004677
Dianne Hackborncfaef692009-06-15 14:24:44 -07004678 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004679 }
Romain Guy06882f82009-06-10 13:36:04 -07004680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 /**
4682 * @return Returns true if event was dispatched, false if it was dropped for any reason
4683 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004684 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4686
4687 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004688 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 if (focusObj == null) {
4690 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004691 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 }
4693 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004694 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
Romain Guy06882f82009-06-10 13:36:04 -07004696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 if (DEBUG_INPUT) Log.v(
4700 TAG, "Dispatching to " + focus + ": " + event);
4701
4702 if (uid != 0 && uid != focus.mSession.mUid) {
4703 if (mContext.checkPermission(
4704 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4705 != PackageManager.PERMISSION_GRANTED) {
4706 Log.w(TAG, "Permission denied: injecting key event from pid "
4707 + pid + " uid " + uid + " to window " + focus
4708 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004709 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 }
4711 }
Romain Guy06882f82009-06-10 13:36:04 -07004712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 synchronized(mWindowMap) {
4714 mKeyWaiter.bindTargetWindowLocked(focus);
4715 }
4716
4717 // NOSHIP extra state logging
4718 mKeyWaiter.recordDispatchState(event, focus);
4719 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 try {
4722 if (DEBUG_INPUT || DEBUG_FOCUS) {
4723 Log.v(TAG, "Delivering key " + event.getKeyCode()
4724 + " to " + focus);
4725 }
4726 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004727 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 } catch (android.os.RemoteException e) {
4729 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4730 try {
4731 removeWindow(focus.mSession, focus.mClient);
4732 } catch (java.util.NoSuchElementException ex) {
4733 // This will happen if the window has already been
4734 // removed.
4735 }
4736 }
Romain Guy06882f82009-06-10 13:36:04 -07004737
Dianne Hackborncfaef692009-06-15 14:24:44 -07004738 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 }
Romain Guy06882f82009-06-10 13:36:04 -07004740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 public void pauseKeyDispatching(IBinder _token) {
4742 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4743 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004744 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 }
4746
4747 synchronized (mWindowMap) {
4748 WindowToken token = mTokenMap.get(_token);
4749 if (token != null) {
4750 mKeyWaiter.pauseDispatchingLocked(token);
4751 }
4752 }
4753 }
4754
4755 public void resumeKeyDispatching(IBinder _token) {
4756 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4757 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004758 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759 }
4760
4761 synchronized (mWindowMap) {
4762 WindowToken token = mTokenMap.get(_token);
4763 if (token != null) {
4764 mKeyWaiter.resumeDispatchingLocked(token);
4765 }
4766 }
4767 }
4768
4769 public void setEventDispatching(boolean enabled) {
4770 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4771 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004772 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 }
4774
4775 synchronized (mWindowMap) {
4776 mKeyWaiter.setEventDispatchingLocked(enabled);
4777 }
4778 }
Romain Guy06882f82009-06-10 13:36:04 -07004779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 /**
4781 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004782 *
4783 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 * {@link SystemClock#uptimeMillis()} as the timebase.)
4785 * @param sync If true, wait for the event to be completed before returning to the caller.
4786 * @return Returns true if event was dispatched, false if it was dropped for any reason
4787 */
4788 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4789 long downTime = ev.getDownTime();
4790 long eventTime = ev.getEventTime();
4791
4792 int action = ev.getAction();
4793 int code = ev.getKeyCode();
4794 int repeatCount = ev.getRepeatCount();
4795 int metaState = ev.getMetaState();
4796 int deviceId = ev.getDeviceId();
4797 int scancode = ev.getScanCode();
4798
4799 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4800 if (downTime == 0) downTime = eventTime;
4801
4802 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004803 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004805 final int pid = Binder.getCallingPid();
4806 final int uid = Binder.getCallingUid();
4807 final long ident = Binder.clearCallingIdentity();
4808 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004810 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004811 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004812 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004813 switch (result) {
4814 case INJECT_NO_PERMISSION:
4815 throw new SecurityException(
4816 "Injecting to another application requires INJECT_EVENT permission");
4817 case INJECT_SUCCEEDED:
4818 return true;
4819 }
4820 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
4822
4823 /**
4824 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004825 *
4826 * @param ev A motion event describing the pointer (touch) action. (As noted in
4827 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 * {@link SystemClock#uptimeMillis()} as the timebase.)
4829 * @param sync If true, wait for the event to be completed before returning to the caller.
4830 * @return Returns true if event was dispatched, false if it was dropped for any reason
4831 */
4832 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004833 final int pid = Binder.getCallingPid();
4834 final int uid = Binder.getCallingUid();
4835 final long ident = Binder.clearCallingIdentity();
4836 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004838 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004840 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004841 switch (result) {
4842 case INJECT_NO_PERMISSION:
4843 throw new SecurityException(
4844 "Injecting to another application requires INJECT_EVENT permission");
4845 case INJECT_SUCCEEDED:
4846 return true;
4847 }
4848 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 }
Romain Guy06882f82009-06-10 13:36:04 -07004850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004851 /**
4852 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004853 *
4854 * @param ev A motion event describing the trackball action. (As noted in
4855 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856 * {@link SystemClock#uptimeMillis()} as the timebase.)
4857 * @param sync If true, wait for the event to be completed before returning to the caller.
4858 * @return Returns true if event was dispatched, false if it was dropped for any reason
4859 */
4860 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004861 final int pid = Binder.getCallingPid();
4862 final int uid = Binder.getCallingUid();
4863 final long ident = Binder.clearCallingIdentity();
4864 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004866 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004868 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004869 switch (result) {
4870 case INJECT_NO_PERMISSION:
4871 throw new SecurityException(
4872 "Injecting to another application requires INJECT_EVENT permission");
4873 case INJECT_SUCCEEDED:
4874 return true;
4875 }
4876 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 }
Romain Guy06882f82009-06-10 13:36:04 -07004878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 private WindowState getFocusedWindow() {
4880 synchronized (mWindowMap) {
4881 return getFocusedWindowLocked();
4882 }
4883 }
4884
4885 private WindowState getFocusedWindowLocked() {
4886 return mCurrentFocus;
4887 }
Romain Guy06882f82009-06-10 13:36:04 -07004888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 /**
4890 * This class holds the state for dispatching key events. This state
4891 * is protected by the KeyWaiter instance, NOT by the window lock. You
4892 * can be holding the main window lock while acquire the KeyWaiter lock,
4893 * but not the other way around.
4894 */
4895 final class KeyWaiter {
4896 // NOSHIP debugging
4897 public class DispatchState {
4898 private KeyEvent event;
4899 private WindowState focus;
4900 private long time;
4901 private WindowState lastWin;
4902 private IBinder lastBinder;
4903 private boolean finished;
4904 private boolean gotFirstWindow;
4905 private boolean eventDispatching;
4906 private long timeToSwitch;
4907 private boolean wasFrozen;
4908 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004909 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4912 focus = theFocus;
4913 event = theEvent;
4914 time = System.currentTimeMillis();
4915 // snapshot KeyWaiter state
4916 lastWin = mLastWin;
4917 lastBinder = mLastBinder;
4918 finished = mFinished;
4919 gotFirstWindow = mGotFirstWindow;
4920 eventDispatching = mEventDispatching;
4921 timeToSwitch = mTimeToSwitch;
4922 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004923 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 // cache the paused state at ctor time as well
4925 if (theFocus == null || theFocus.mToken == null) {
4926 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4927 focusPaused = false;
4928 } else {
4929 focusPaused = theFocus.mToken.paused;
4930 }
4931 }
Romain Guy06882f82009-06-10 13:36:04 -07004932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 public String toString() {
4934 return "{{" + event + " to " + focus + " @ " + time
4935 + " lw=" + lastWin + " lb=" + lastBinder
4936 + " fin=" + finished + " gfw=" + gotFirstWindow
4937 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004938 + " wf=" + wasFrozen + " fp=" + focusPaused
4939 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 }
4941 };
4942 private DispatchState mDispatchState = null;
4943 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4944 mDispatchState = new DispatchState(theEvent, theFocus);
4945 }
4946 // END NOSHIP
4947
4948 public static final int RETURN_NOTHING = 0;
4949 public static final int RETURN_PENDING_POINTER = 1;
4950 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 final Object SKIP_TARGET_TOKEN = new Object();
4953 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 private WindowState mLastWin = null;
4956 private IBinder mLastBinder = null;
4957 private boolean mFinished = true;
4958 private boolean mGotFirstWindow = false;
4959 private boolean mEventDispatching = true;
4960 private long mTimeToSwitch = 0;
4961 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 // Target of Motion events
4964 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 // Windows above the target who would like to receive an "outside"
4967 // touch event for any down events outside of them.
4968 WindowState mOutsideTouchTargets;
4969
4970 /**
4971 * Wait for the last event dispatch to complete, then find the next
4972 * target that should receive the given event and wait for that one
4973 * to be ready to receive it.
4974 */
4975 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4976 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004977 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 long startTime = SystemClock.uptimeMillis();
4979 long keyDispatchingTimeout = 5 * 1000;
4980 long waitedFor = 0;
4981
4982 while (true) {
4983 // Figure out which window we care about. It is either the
4984 // last window we are waiting to have process the event or,
4985 // if none, then the next window we think the event should go
4986 // to. Note: we retrieve mLastWin outside of the lock, so
4987 // it may change before we lock. Thus we must check it again.
4988 WindowState targetWin = mLastWin;
4989 boolean targetIsNew = targetWin == null;
4990 if (DEBUG_INPUT) Log.v(
4991 TAG, "waitForLastKey: mFinished=" + mFinished +
4992 ", mLastWin=" + mLastWin);
4993 if (targetIsNew) {
4994 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004995 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 if (target == SKIP_TARGET_TOKEN) {
4997 // The user has pressed a special key, and we are
4998 // dropping all pending events before it.
4999 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5000 + " " + nextMotion);
5001 return null;
5002 }
5003 if (target == CONSUMED_EVENT_TOKEN) {
5004 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5005 + " " + nextMotion);
5006 return target;
5007 }
5008 targetWin = (WindowState)target;
5009 }
Romain Guy06882f82009-06-10 13:36:04 -07005010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 // Now: is it okay to send the next event to this window?
5014 synchronized (this) {
5015 // First: did we come here based on the last window not
5016 // being null, but it changed by the time we got here?
5017 // If so, try again.
5018 if (!targetIsNew && mLastWin == null) {
5019 continue;
5020 }
Romain Guy06882f82009-06-10 13:36:04 -07005021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 // We never dispatch events if not finished with the
5023 // last one, or the display is frozen.
5024 if (mFinished && !mDisplayFrozen) {
5025 // If event dispatching is disabled, then we
5026 // just consume the events.
5027 if (!mEventDispatching) {
5028 if (DEBUG_INPUT) Log.v(TAG,
5029 "Skipping event; dispatching disabled: "
5030 + nextKey + " " + nextMotion);
5031 return null;
5032 }
5033 if (targetWin != null) {
5034 // If this is a new target, and that target is not
5035 // paused or unresponsive, then all looks good to
5036 // handle the event.
5037 if (targetIsNew && !targetWin.mToken.paused) {
5038 return targetWin;
5039 }
Romain Guy06882f82009-06-10 13:36:04 -07005040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 // If we didn't find a target window, and there is no
5042 // focused app window, then just eat the events.
5043 } else if (mFocusedApp == null) {
5044 if (DEBUG_INPUT) Log.v(TAG,
5045 "Skipping event; no focused app: "
5046 + nextKey + " " + nextMotion);
5047 return null;
5048 }
5049 }
Romain Guy06882f82009-06-10 13:36:04 -07005050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 if (DEBUG_INPUT) Log.v(
5052 TAG, "Waiting for last key in " + mLastBinder
5053 + " target=" + targetWin
5054 + " mFinished=" + mFinished
5055 + " mDisplayFrozen=" + mDisplayFrozen
5056 + " targetIsNew=" + targetIsNew
5057 + " paused="
5058 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005059 + " mFocusedApp=" + mFocusedApp
5060 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 targetApp = targetWin != null
5063 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 long curTimeout = keyDispatchingTimeout;
5066 if (mTimeToSwitch != 0) {
5067 long now = SystemClock.uptimeMillis();
5068 if (mTimeToSwitch <= now) {
5069 // If an app switch key has been pressed, and we have
5070 // waited too long for the current app to finish
5071 // processing keys, then wait no more!
5072 doFinishedKeyLocked(true);
5073 continue;
5074 }
5075 long switchTimeout = mTimeToSwitch - now;
5076 if (curTimeout > switchTimeout) {
5077 curTimeout = switchTimeout;
5078 }
5079 }
Romain Guy06882f82009-06-10 13:36:04 -07005080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 try {
5082 // after that continue
5083 // processing keys, so we don't get stuck.
5084 if (DEBUG_INPUT) Log.v(
5085 TAG, "Waiting for key dispatch: " + curTimeout);
5086 wait(curTimeout);
5087 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5088 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005089 + startTime + " switchTime=" + mTimeToSwitch
5090 + " target=" + targetWin + " mLW=" + mLastWin
5091 + " mLB=" + mLastBinder + " fin=" + mFinished
5092 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 } catch (InterruptedException e) {
5094 }
5095 }
5096
5097 // If we were frozen during configuration change, restart the
5098 // timeout checks from now; otherwise look at whether we timed
5099 // out before awakening.
5100 if (mWasFrozen) {
5101 waitedFor = 0;
5102 mWasFrozen = false;
5103 } else {
5104 waitedFor = SystemClock.uptimeMillis() - startTime;
5105 }
5106
5107 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5108 IApplicationToken at = null;
5109 synchronized (this) {
5110 Log.w(TAG, "Key dispatching timed out sending to " +
5111 (targetWin != null ? targetWin.mAttrs.getTitle()
5112 : "<null>"));
5113 // NOSHIP debugging
5114 Log.w(TAG, "Dispatch state: " + mDispatchState);
5115 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5116 // END NOSHIP
5117 //dump();
5118 if (targetWin != null) {
5119 at = targetWin.getAppToken();
5120 } else if (targetApp != null) {
5121 at = targetApp.appToken;
5122 }
5123 }
5124
5125 boolean abort = true;
5126 if (at != null) {
5127 try {
5128 long timeout = at.getKeyDispatchingTimeout();
5129 if (timeout > waitedFor) {
5130 // we did not wait the proper amount of time for this application.
5131 // set the timeout to be the real timeout and wait again.
5132 keyDispatchingTimeout = timeout - waitedFor;
5133 continue;
5134 } else {
5135 abort = at.keyDispatchingTimedOut();
5136 }
5137 } catch (RemoteException ex) {
5138 }
5139 }
5140
5141 synchronized (this) {
5142 if (abort && (mLastWin == targetWin || targetWin == null)) {
5143 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005144 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (DEBUG_INPUT) Log.v(TAG,
5146 "Window " + mLastWin +
5147 " timed out on key input");
5148 if (mLastWin.mToken.paused) {
5149 Log.w(TAG, "Un-pausing dispatching to this window");
5150 mLastWin.mToken.paused = false;
5151 }
5152 }
5153 if (mMotionTarget == targetWin) {
5154 mMotionTarget = null;
5155 }
5156 mLastWin = null;
5157 mLastBinder = null;
5158 if (failIfTimeout || targetWin == null) {
5159 return null;
5160 }
5161 } else {
5162 Log.w(TAG, "Continuing to wait for key to be dispatched");
5163 startTime = SystemClock.uptimeMillis();
5164 }
5165 }
5166 }
5167 }
5168 }
Romain Guy06882f82009-06-10 13:36:04 -07005169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005171 MotionEvent nextMotion, boolean isPointerEvent,
5172 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 if (nextKey != null) {
5176 // Find the target window for a normal key event.
5177 final int keycode = nextKey.getKeyCode();
5178 final int repeatCount = nextKey.getRepeatCount();
5179 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5180 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005183 if (callingUid == 0 ||
5184 mContext.checkPermission(
5185 android.Manifest.permission.INJECT_EVENTS,
5186 callingPid, callingUid)
5187 == PackageManager.PERMISSION_GRANTED) {
5188 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005189 nextKey.getMetaState(), down, repeatCount,
5190 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 Log.w(TAG, "Event timeout during app switch: dropping "
5193 + nextKey);
5194 return SKIP_TARGET_TOKEN;
5195 }
Romain Guy06882f82009-06-10 13:36:04 -07005196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 WindowState focus = null;
5200 synchronized(mWindowMap) {
5201 focus = getFocusedWindowLocked();
5202 }
Romain Guy06882f82009-06-10 13:36:04 -07005203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005205
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005206 if (callingUid == 0 ||
5207 (focus != null && callingUid == focus.mSession.mUid) ||
5208 mContext.checkPermission(
5209 android.Manifest.permission.INJECT_EVENTS,
5210 callingPid, callingUid)
5211 == PackageManager.PERMISSION_GRANTED) {
5212 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005213 keycode, nextKey.getMetaState(), down, repeatCount,
5214 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005215 return CONSUMED_EVENT_TOKEN;
5216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 }
Romain Guy06882f82009-06-10 13:36:04 -07005218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 } else if (!isPointerEvent) {
5222 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5223 if (!dispatch) {
5224 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5225 + nextMotion);
5226 return SKIP_TARGET_TOKEN;
5227 }
Romain Guy06882f82009-06-10 13:36:04 -07005228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 WindowState focus = null;
5230 synchronized(mWindowMap) {
5231 focus = getFocusedWindowLocked();
5232 }
Romain Guy06882f82009-06-10 13:36:04 -07005233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5235 return focus;
5236 }
Romain Guy06882f82009-06-10 13:36:04 -07005237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 if (nextMotion == null) {
5239 return SKIP_TARGET_TOKEN;
5240 }
Romain Guy06882f82009-06-10 13:36:04 -07005241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5243 KeyEvent.KEYCODE_UNKNOWN);
5244 if (!dispatch) {
5245 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5246 + nextMotion);
5247 return SKIP_TARGET_TOKEN;
5248 }
Romain Guy06882f82009-06-10 13:36:04 -07005249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 // Find the target window for a pointer event.
5251 int action = nextMotion.getAction();
5252 final float xf = nextMotion.getX();
5253 final float yf = nextMotion.getY();
5254 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 final boolean screenWasOff = qev != null
5257 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 synchronized(mWindowMap) {
5262 synchronized (this) {
5263 if (action == MotionEvent.ACTION_DOWN) {
5264 if (mMotionTarget != null) {
5265 // this is weird, we got a pen down, but we thought it was
5266 // already down!
5267 // XXX: We should probably send an ACTION_UP to the current
5268 // target.
5269 Log.w(TAG, "Pointer down received while already down in: "
5270 + mMotionTarget);
5271 mMotionTarget = null;
5272 }
Romain Guy06882f82009-06-10 13:36:04 -07005273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 // ACTION_DOWN is special, because we need to lock next events to
5275 // the window we'll land onto.
5276 final int x = (int)xf;
5277 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 final ArrayList windows = mWindows;
5280 final int N = windows.size();
5281 WindowState topErrWindow = null;
5282 final Rect tmpRect = mTempRect;
5283 for (int i=N-1; i>=0; i--) {
5284 WindowState child = (WindowState)windows.get(i);
5285 //Log.i(TAG, "Checking dispatch to: " + child);
5286 final int flags = child.mAttrs.flags;
5287 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5288 if (topErrWindow == null) {
5289 topErrWindow = child;
5290 }
5291 }
5292 if (!child.isVisibleLw()) {
5293 //Log.i(TAG, "Not visible!");
5294 continue;
5295 }
5296 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5297 //Log.i(TAG, "Not touchable!");
5298 if ((flags & WindowManager.LayoutParams
5299 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5300 child.mNextOutsideTouch = mOutsideTouchTargets;
5301 mOutsideTouchTargets = child;
5302 }
5303 continue;
5304 }
5305 tmpRect.set(child.mFrame);
5306 if (child.mTouchableInsets == ViewTreeObserver
5307 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5308 // The touch is inside of the window if it is
5309 // inside the frame, AND the content part of that
5310 // frame that was given by the application.
5311 tmpRect.left += child.mGivenContentInsets.left;
5312 tmpRect.top += child.mGivenContentInsets.top;
5313 tmpRect.right -= child.mGivenContentInsets.right;
5314 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5315 } else if (child.mTouchableInsets == ViewTreeObserver
5316 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5317 // The touch is inside of the window if it is
5318 // inside the frame, AND the visible part of that
5319 // frame that was given by the application.
5320 tmpRect.left += child.mGivenVisibleInsets.left;
5321 tmpRect.top += child.mGivenVisibleInsets.top;
5322 tmpRect.right -= child.mGivenVisibleInsets.right;
5323 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5324 }
5325 final int touchFlags = flags &
5326 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5327 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5328 if (tmpRect.contains(x, y) || touchFlags == 0) {
5329 //Log.i(TAG, "Using this target!");
5330 if (!screenWasOff || (flags &
5331 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5332 mMotionTarget = child;
5333 } else {
5334 //Log.i(TAG, "Waking, skip!");
5335 mMotionTarget = null;
5336 }
5337 break;
5338 }
Romain Guy06882f82009-06-10 13:36:04 -07005339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 if ((flags & WindowManager.LayoutParams
5341 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5342 child.mNextOutsideTouch = mOutsideTouchTargets;
5343 mOutsideTouchTargets = child;
5344 //Log.i(TAG, "Adding to outside target list: " + child);
5345 }
5346 }
5347
5348 // if there's an error window but it's not accepting
5349 // focus (typically because it is not yet visible) just
5350 // wait for it -- any other focused window may in fact
5351 // be in ANR state.
5352 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5353 mMotionTarget = null;
5354 }
5355 }
Romain Guy06882f82009-06-10 13:36:04 -07005356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 target = mMotionTarget;
5358 }
5359 }
Romain Guy06882f82009-06-10 13:36:04 -07005360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 // Pointer events are a little different -- if there isn't a
5364 // target found for any event, then just drop it.
5365 return target != null ? target : SKIP_TARGET_TOKEN;
5366 }
Romain Guy06882f82009-06-10 13:36:04 -07005367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 boolean checkShouldDispatchKey(int keycode) {
5369 synchronized (this) {
5370 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5371 mTimeToSwitch = 0;
5372 return true;
5373 }
5374 if (mTimeToSwitch != 0
5375 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5376 return false;
5377 }
5378 return true;
5379 }
5380 }
Romain Guy06882f82009-06-10 13:36:04 -07005381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 void bindTargetWindowLocked(WindowState win,
5383 int pendingWhat, QueuedEvent pendingMotion) {
5384 synchronized (this) {
5385 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5386 }
5387 }
Romain Guy06882f82009-06-10 13:36:04 -07005388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 void bindTargetWindowLocked(WindowState win) {
5390 synchronized (this) {
5391 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5392 }
5393 }
5394
5395 void bindTargetWindowLockedLocked(WindowState win,
5396 int pendingWhat, QueuedEvent pendingMotion) {
5397 mLastWin = win;
5398 mLastBinder = win.mClient.asBinder();
5399 mFinished = false;
5400 if (pendingMotion != null) {
5401 final Session s = win.mSession;
5402 if (pendingWhat == RETURN_PENDING_POINTER) {
5403 releasePendingPointerLocked(s);
5404 s.mPendingPointerMove = pendingMotion;
5405 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005406 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 "bindTargetToWindow " + s.mPendingPointerMove);
5408 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5409 releasePendingTrackballLocked(s);
5410 s.mPendingTrackballMove = pendingMotion;
5411 s.mPendingTrackballWindow = win;
5412 }
5413 }
5414 }
Romain Guy06882f82009-06-10 13:36:04 -07005415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 void releasePendingPointerLocked(Session s) {
5417 if (DEBUG_INPUT) Log.v(TAG,
5418 "releasePendingPointer " + s.mPendingPointerMove);
5419 if (s.mPendingPointerMove != null) {
5420 mQueue.recycleEvent(s.mPendingPointerMove);
5421 s.mPendingPointerMove = null;
5422 }
5423 }
Romain Guy06882f82009-06-10 13:36:04 -07005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 void releasePendingTrackballLocked(Session s) {
5426 if (s.mPendingTrackballMove != null) {
5427 mQueue.recycleEvent(s.mPendingTrackballMove);
5428 s.mPendingTrackballMove = null;
5429 }
5430 }
Romain Guy06882f82009-06-10 13:36:04 -07005431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5433 int returnWhat) {
5434 if (DEBUG_INPUT) Log.v(
5435 TAG, "finishedKey: client=" + client + ", force=" + force);
5436
5437 if (client == null) {
5438 return null;
5439 }
5440
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005441 MotionEvent res = null;
5442 QueuedEvent qev = null;
5443 WindowState win = null;
5444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 synchronized (this) {
5446 if (DEBUG_INPUT) Log.v(
5447 TAG, "finishedKey: client=" + client.asBinder()
5448 + ", force=" + force + ", last=" + mLastBinder
5449 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 if (returnWhat == RETURN_PENDING_POINTER) {
5452 qev = session.mPendingPointerMove;
5453 win = session.mPendingPointerWindow;
5454 session.mPendingPointerMove = null;
5455 session.mPendingPointerWindow = null;
5456 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5457 qev = session.mPendingTrackballMove;
5458 win = session.mPendingTrackballWindow;
5459 session.mPendingTrackballMove = null;
5460 session.mPendingTrackballWindow = null;
5461 }
Romain Guy06882f82009-06-10 13:36:04 -07005462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 if (mLastBinder == client.asBinder()) {
5464 if (DEBUG_INPUT) Log.v(
5465 TAG, "finishedKey: last paused="
5466 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5467 if (mLastWin != null && (!mLastWin.mToken.paused || force
5468 || !mEventDispatching)) {
5469 doFinishedKeyLocked(false);
5470 } else {
5471 // Make sure to wake up anyone currently waiting to
5472 // dispatch a key, so they can re-evaluate their
5473 // current situation.
5474 mFinished = true;
5475 notifyAll();
5476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 }
Romain Guy06882f82009-06-10 13:36:04 -07005478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005480 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 if (DEBUG_INPUT) Log.v(TAG,
5482 "Returning pending motion: " + res);
5483 mQueue.recycleEvent(qev);
5484 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5485 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005489
5490 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5491 synchronized (mWindowMap) {
5492 if (mWallpaperTarget == win) {
5493 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5494 }
5495 }
5496 }
5497
5498 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 }
5500
5501 void tickle() {
5502 synchronized (this) {
5503 notifyAll();
5504 }
5505 }
Romain Guy06882f82009-06-10 13:36:04 -07005506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005507 void handleNewWindowLocked(WindowState newWindow) {
5508 if (!newWindow.canReceiveKeys()) {
5509 return;
5510 }
5511 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005512 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005513 TAG, "New key dispatch window: win="
5514 + newWindow.mClient.asBinder()
5515 + ", last=" + mLastBinder
5516 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5517 + "), finished=" + mFinished + ", paused="
5518 + newWindow.mToken.paused);
5519
5520 // Displaying a window implicitly causes dispatching to
5521 // be unpaused. (This is to protect against bugs if someone
5522 // pauses dispatching but forgets to resume.)
5523 newWindow.mToken.paused = false;
5524
5525 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526
5527 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5528 if (DEBUG_INPUT) Log.v(TAG,
5529 "New SYSTEM_ERROR window; resetting state");
5530 mLastWin = null;
5531 mLastBinder = null;
5532 mMotionTarget = null;
5533 mFinished = true;
5534 } else if (mLastWin != null) {
5535 // If the new window is above the window we are
5536 // waiting on, then stop waiting and let key dispatching
5537 // start on the new guy.
5538 if (DEBUG_INPUT) Log.v(
5539 TAG, "Last win layer=" + mLastWin.mLayer
5540 + ", new win layer=" + newWindow.mLayer);
5541 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005542 // The new window is above the old; finish pending input to the last
5543 // window and start directing it to the new one.
5544 mLastWin.mToken.paused = false;
5545 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005547 // Either the new window is lower, so there is no need to wake key waiters,
5548 // or we just finished key input to the previous window, which implicitly
5549 // notified the key waiters. In both cases, we don't need to issue the
5550 // notification here.
5551 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 }
5553
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005554 // Now that we've put a new window state in place, make the event waiter
5555 // take notice and retarget its attentions.
5556 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 }
5558 }
5559
5560 void pauseDispatchingLocked(WindowToken token) {
5561 synchronized (this)
5562 {
5563 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5564 token.paused = true;
5565
5566 /*
5567 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5568 mPaused = true;
5569 } else {
5570 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005571 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005573 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005575 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 }
5577 }
5578 */
5579 }
5580 }
5581
5582 void resumeDispatchingLocked(WindowToken token) {
5583 synchronized (this) {
5584 if (token.paused) {
5585 if (DEBUG_INPUT) Log.v(
5586 TAG, "Resuming WindowToken " + token
5587 + ", last=" + mLastBinder
5588 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5589 + "), finished=" + mFinished + ", paused="
5590 + token.paused);
5591 token.paused = false;
5592 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5593 doFinishedKeyLocked(true);
5594 } else {
5595 notifyAll();
5596 }
5597 }
5598 }
5599 }
5600
5601 void setEventDispatchingLocked(boolean enabled) {
5602 synchronized (this) {
5603 mEventDispatching = enabled;
5604 notifyAll();
5605 }
5606 }
Romain Guy06882f82009-06-10 13:36:04 -07005607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 void appSwitchComing() {
5609 synchronized (this) {
5610 // Don't wait for more than .5 seconds for app to finish
5611 // processing the pending events.
5612 long now = SystemClock.uptimeMillis() + 500;
5613 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5614 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5615 mTimeToSwitch = now;
5616 }
5617 notifyAll();
5618 }
5619 }
Romain Guy06882f82009-06-10 13:36:04 -07005620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 private final void doFinishedKeyLocked(boolean doRecycle) {
5622 if (mLastWin != null) {
5623 releasePendingPointerLocked(mLastWin.mSession);
5624 releasePendingTrackballLocked(mLastWin.mSession);
5625 }
Romain Guy06882f82009-06-10 13:36:04 -07005626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 if (mLastWin == null || !mLastWin.mToken.paused
5628 || !mLastWin.isVisibleLw()) {
5629 // If the current window has been paused, we aren't -really-
5630 // finished... so let the waiters still wait.
5631 mLastWin = null;
5632 mLastBinder = null;
5633 }
5634 mFinished = true;
5635 notifyAll();
5636 }
5637 }
5638
5639 private class KeyQ extends KeyInputQueue
5640 implements KeyInputQueue.FilterCallback {
5641 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005644 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5646 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5647 "KEEP_SCREEN_ON_FLAG");
5648 mHoldingScreen.setReferenceCounted(false);
5649 }
5650
5651 @Override
5652 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5653 if (mPolicy.preprocessInputEventTq(event)) {
5654 return true;
5655 }
Romain Guy06882f82009-06-10 13:36:04 -07005656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 switch (event.type) {
5658 case RawInputEvent.EV_KEY: {
5659 // XXX begin hack
5660 if (DEBUG) {
5661 if (event.keycode == KeyEvent.KEYCODE_G) {
5662 if (event.value != 0) {
5663 // G down
5664 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5665 }
5666 return false;
5667 }
5668 if (event.keycode == KeyEvent.KEYCODE_D) {
5669 if (event.value != 0) {
5670 //dump();
5671 }
5672 return false;
5673 }
5674 }
5675 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 boolean screenIsOff = !mPowerManager.screenIsOn();
5678 boolean screenIsDim = !mPowerManager.screenIsBright();
5679 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5682 mPowerManager.goToSleep(event.when);
5683 }
5684
5685 if (screenIsOff) {
5686 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5687 }
5688 if (screenIsDim) {
5689 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5690 }
5691 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5692 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005693 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 }
Romain Guy06882f82009-06-10 13:36:04 -07005695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5697 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5698 filterQueue(this);
5699 mKeyWaiter.appSwitchComing();
5700 }
5701 return true;
5702 } else {
5703 return false;
5704 }
5705 }
Romain Guy06882f82009-06-10 13:36:04 -07005706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 case RawInputEvent.EV_REL: {
5708 boolean screenIsOff = !mPowerManager.screenIsOn();
5709 boolean screenIsDim = !mPowerManager.screenIsBright();
5710 if (screenIsOff) {
5711 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5712 device.classes, event)) {
5713 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5714 return false;
5715 }
5716 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5717 }
5718 if (screenIsDim) {
5719 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5720 }
5721 return true;
5722 }
Romain Guy06882f82009-06-10 13:36:04 -07005723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 case RawInputEvent.EV_ABS: {
5725 boolean screenIsOff = !mPowerManager.screenIsOn();
5726 boolean screenIsDim = !mPowerManager.screenIsBright();
5727 if (screenIsOff) {
5728 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5729 device.classes, event)) {
5730 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5731 return false;
5732 }
5733 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5734 }
5735 if (screenIsDim) {
5736 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5737 }
5738 return true;
5739 }
Romain Guy06882f82009-06-10 13:36:04 -07005740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 default:
5742 return true;
5743 }
5744 }
5745
5746 public int filterEvent(QueuedEvent ev) {
5747 switch (ev.classType) {
5748 case RawInputEvent.CLASS_KEYBOARD:
5749 KeyEvent ke = (KeyEvent)ev.event;
5750 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5751 Log.w(TAG, "Dropping movement key during app switch: "
5752 + ke.getKeyCode() + ", action=" + ke.getAction());
5753 return FILTER_REMOVE;
5754 }
5755 return FILTER_ABORT;
5756 default:
5757 return FILTER_KEEP;
5758 }
5759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 /**
5762 * Must be called with the main window manager lock held.
5763 */
5764 void setHoldScreenLocked(boolean holding) {
5765 boolean state = mHoldingScreen.isHeld();
5766 if (holding != state) {
5767 if (holding) {
5768 mHoldingScreen.acquire();
5769 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005770 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 mHoldingScreen.release();
5772 }
5773 }
5774 }
Michael Chan53071d62009-05-13 17:29:48 -07005775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776
5777 public boolean detectSafeMode() {
5778 mSafeMode = mPolicy.detectSafeMode();
5779 return mSafeMode;
5780 }
Romain Guy06882f82009-06-10 13:36:04 -07005781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 public void systemReady() {
5783 mPolicy.systemReady();
5784 }
Romain Guy06882f82009-06-10 13:36:04 -07005785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 private final class InputDispatcherThread extends Thread {
5787 // Time to wait when there is nothing to do: 9999 seconds.
5788 static final int LONG_WAIT=9999*1000;
5789
5790 public InputDispatcherThread() {
5791 super("InputDispatcher");
5792 }
Romain Guy06882f82009-06-10 13:36:04 -07005793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794 @Override
5795 public void run() {
5796 while (true) {
5797 try {
5798 process();
5799 } catch (Exception e) {
5800 Log.e(TAG, "Exception in input dispatcher", e);
5801 }
5802 }
5803 }
Romain Guy06882f82009-06-10 13:36:04 -07005804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 private void process() {
5806 android.os.Process.setThreadPriority(
5807 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 // The last key event we saw
5810 KeyEvent lastKey = null;
5811
5812 // Last keydown time for auto-repeating keys
5813 long lastKeyTime = SystemClock.uptimeMillis();
5814 long nextKeyTime = lastKeyTime+LONG_WAIT;
5815
Romain Guy06882f82009-06-10 13:36:04 -07005816 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 int keyRepeatCount = 0;
5818
5819 // Need to report that configuration has changed?
5820 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 while (true) {
5823 long curTime = SystemClock.uptimeMillis();
5824
5825 if (DEBUG_INPUT) Log.v(
5826 TAG, "Waiting for next key: now=" + curTime
5827 + ", repeat @ " + nextKeyTime);
5828
5829 // Retrieve next event, waiting only as long as the next
5830 // repeat timeout. If the configuration has changed, then
5831 // don't wait at all -- we'll report the change as soon as
5832 // we have processed all events.
5833 QueuedEvent ev = mQueue.getEvent(
5834 (int)((!configChanged && curTime < nextKeyTime)
5835 ? (nextKeyTime-curTime) : 0));
5836
5837 if (DEBUG_INPUT && ev != null) Log.v(
5838 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5839
Michael Chan53071d62009-05-13 17:29:48 -07005840 if (MEASURE_LATENCY) {
5841 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5842 }
5843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 try {
5845 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005846 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 int eventType;
5848 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5849 eventType = eventType((MotionEvent)ev.event);
5850 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5851 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5852 eventType = LocalPowerManager.BUTTON_EVENT;
5853 } else {
5854 eventType = LocalPowerManager.OTHER_EVENT;
5855 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005856 try {
Michael Chan53071d62009-05-13 17:29:48 -07005857 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005858 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005859 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005860 mBatteryStats.noteInputEvent();
5861 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005862 } catch (RemoteException e) {
5863 // Ignore
5864 }
Michael Chane10de972009-05-18 11:24:50 -07005865
5866 if (eventType != TOUCH_EVENT
5867 && eventType != LONG_TOUCH_EVENT
5868 && eventType != CHEEK_EVENT) {
5869 mPowerManager.userActivity(curTime, false,
5870 eventType, false);
5871 } else if (mLastTouchEventType != eventType
5872 || (curTime - mLastUserActivityCallTime)
5873 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5874 mLastUserActivityCallTime = curTime;
5875 mLastTouchEventType = eventType;
5876 mPowerManager.userActivity(curTime, false,
5877 eventType, false);
5878 }
5879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 switch (ev.classType) {
5881 case RawInputEvent.CLASS_KEYBOARD:
5882 KeyEvent ke = (KeyEvent)ev.event;
5883 if (ke.isDown()) {
5884 lastKey = ke;
5885 keyRepeatCount = 0;
5886 lastKeyTime = curTime;
5887 nextKeyTime = lastKeyTime
5888 + KEY_REPEAT_FIRST_DELAY;
5889 if (DEBUG_INPUT) Log.v(
5890 TAG, "Received key down: first repeat @ "
5891 + nextKeyTime);
5892 } else {
5893 lastKey = null;
5894 // Arbitrary long timeout.
5895 lastKeyTime = curTime;
5896 nextKeyTime = curTime + LONG_WAIT;
5897 if (DEBUG_INPUT) Log.v(
5898 TAG, "Received key up: ignore repeat @ "
5899 + nextKeyTime);
5900 }
5901 dispatchKey((KeyEvent)ev.event, 0, 0);
5902 mQueue.recycleEvent(ev);
5903 break;
5904 case RawInputEvent.CLASS_TOUCHSCREEN:
5905 //Log.i(TAG, "Read next event " + ev);
5906 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5907 break;
5908 case RawInputEvent.CLASS_TRACKBALL:
5909 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5910 break;
5911 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5912 configChanged = true;
5913 break;
5914 default:
5915 mQueue.recycleEvent(ev);
5916 break;
5917 }
Romain Guy06882f82009-06-10 13:36:04 -07005918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 } else if (configChanged) {
5920 configChanged = false;
5921 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 } else if (lastKey != null) {
5924 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 // Timeout occurred while key was down. If it is at or
5927 // past the key repeat time, dispatch the repeat.
5928 if (DEBUG_INPUT) Log.v(
5929 TAG, "Key timeout: repeat=" + nextKeyTime
5930 + ", now=" + curTime);
5931 if (curTime < nextKeyTime) {
5932 continue;
5933 }
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 lastKeyTime = nextKeyTime;
5936 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5937 keyRepeatCount++;
5938 if (DEBUG_INPUT) Log.v(
5939 TAG, "Key repeat: count=" + keyRepeatCount
5940 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005941 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 } else {
5944 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 lastKeyTime = curTime;
5947 nextKeyTime = curTime + LONG_WAIT;
5948 }
Romain Guy06882f82009-06-10 13:36:04 -07005949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 } catch (Exception e) {
5951 Log.e(TAG,
5952 "Input thread received uncaught exception: " + e, e);
5953 }
5954 }
5955 }
5956 }
5957
5958 // -------------------------------------------------------------
5959 // Client Session State
5960 // -------------------------------------------------------------
5961
5962 private final class Session extends IWindowSession.Stub
5963 implements IBinder.DeathRecipient {
5964 final IInputMethodClient mClient;
5965 final IInputContext mInputContext;
5966 final int mUid;
5967 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005968 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 SurfaceSession mSurfaceSession;
5970 int mNumWindow = 0;
5971 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 /**
5974 * Current pointer move event being dispatched to client window... must
5975 * hold key lock to access.
5976 */
5977 QueuedEvent mPendingPointerMove;
5978 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 /**
5981 * Current trackball move event being dispatched to client window... must
5982 * hold key lock to access.
5983 */
5984 QueuedEvent mPendingTrackballMove;
5985 WindowState mPendingTrackballWindow;
5986
5987 public Session(IInputMethodClient client, IInputContext inputContext) {
5988 mClient = client;
5989 mInputContext = inputContext;
5990 mUid = Binder.getCallingUid();
5991 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005992 StringBuilder sb = new StringBuilder();
5993 sb.append("Session{");
5994 sb.append(Integer.toHexString(System.identityHashCode(this)));
5995 sb.append(" uid ");
5996 sb.append(mUid);
5997 sb.append("}");
5998 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07005999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 synchronized (mWindowMap) {
6001 if (mInputMethodManager == null && mHaveInputMethods) {
6002 IBinder b = ServiceManager.getService(
6003 Context.INPUT_METHOD_SERVICE);
6004 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6005 }
6006 }
6007 long ident = Binder.clearCallingIdentity();
6008 try {
6009 // Note: it is safe to call in to the input method manager
6010 // here because we are not holding our lock.
6011 if (mInputMethodManager != null) {
6012 mInputMethodManager.addClient(client, inputContext,
6013 mUid, mPid);
6014 } else {
6015 client.setUsingInputMethod(false);
6016 }
6017 client.asBinder().linkToDeath(this, 0);
6018 } catch (RemoteException e) {
6019 // The caller has died, so we can just forget about this.
6020 try {
6021 if (mInputMethodManager != null) {
6022 mInputMethodManager.removeClient(client);
6023 }
6024 } catch (RemoteException ee) {
6025 }
6026 } finally {
6027 Binder.restoreCallingIdentity(ident);
6028 }
6029 }
Romain Guy06882f82009-06-10 13:36:04 -07006030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 @Override
6032 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6033 throws RemoteException {
6034 try {
6035 return super.onTransact(code, data, reply, flags);
6036 } catch (RuntimeException e) {
6037 // Log all 'real' exceptions thrown to the caller
6038 if (!(e instanceof SecurityException)) {
6039 Log.e(TAG, "Window Session Crash", e);
6040 }
6041 throw e;
6042 }
6043 }
6044
6045 public void binderDied() {
6046 // Note: it is safe to call in to the input method manager
6047 // here because we are not holding our lock.
6048 try {
6049 if (mInputMethodManager != null) {
6050 mInputMethodManager.removeClient(mClient);
6051 }
6052 } catch (RemoteException e) {
6053 }
6054 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006055 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 mClientDead = true;
6057 killSessionLocked();
6058 }
6059 }
6060
6061 public int add(IWindow window, WindowManager.LayoutParams attrs,
6062 int viewVisibility, Rect outContentInsets) {
6063 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6064 }
Romain Guy06882f82009-06-10 13:36:04 -07006065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 public void remove(IWindow window) {
6067 removeWindow(this, window);
6068 }
Romain Guy06882f82009-06-10 13:36:04 -07006069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6071 int requestedWidth, int requestedHeight, int viewFlags,
6072 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6073 Rect outVisibleInsets, Surface outSurface) {
6074 return relayoutWindow(this, window, attrs,
6075 requestedWidth, requestedHeight, viewFlags, insetsPending,
6076 outFrame, outContentInsets, outVisibleInsets, outSurface);
6077 }
Romain Guy06882f82009-06-10 13:36:04 -07006078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 public void setTransparentRegion(IWindow window, Region region) {
6080 setTransparentRegionWindow(this, window, region);
6081 }
Romain Guy06882f82009-06-10 13:36:04 -07006082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 public void setInsets(IWindow window, int touchableInsets,
6084 Rect contentInsets, Rect visibleInsets) {
6085 setInsetsWindow(this, window, touchableInsets, contentInsets,
6086 visibleInsets);
6087 }
Romain Guy06882f82009-06-10 13:36:04 -07006088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6090 getWindowDisplayFrame(this, window, outDisplayFrame);
6091 }
Romain Guy06882f82009-06-10 13:36:04 -07006092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 public void finishDrawing(IWindow window) {
6094 if (localLOGV) Log.v(
6095 TAG, "IWindow finishDrawing called for " + window);
6096 finishDrawingWindow(this, window);
6097 }
6098
6099 public void finishKey(IWindow window) {
6100 if (localLOGV) Log.v(
6101 TAG, "IWindow finishKey called for " + window);
6102 mKeyWaiter.finishedKey(this, window, false,
6103 KeyWaiter.RETURN_NOTHING);
6104 }
6105
6106 public MotionEvent getPendingPointerMove(IWindow window) {
6107 if (localLOGV) Log.v(
6108 TAG, "IWindow getPendingMotionEvent called for " + window);
6109 return mKeyWaiter.finishedKey(this, window, false,
6110 KeyWaiter.RETURN_PENDING_POINTER);
6111 }
Romain Guy06882f82009-06-10 13:36:04 -07006112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 public MotionEvent getPendingTrackballMove(IWindow window) {
6114 if (localLOGV) Log.v(
6115 TAG, "IWindow getPendingMotionEvent called for " + window);
6116 return mKeyWaiter.finishedKey(this, window, false,
6117 KeyWaiter.RETURN_PENDING_TRACKBALL);
6118 }
6119
6120 public void setInTouchMode(boolean mode) {
6121 synchronized(mWindowMap) {
6122 mInTouchMode = mode;
6123 }
6124 }
6125
6126 public boolean getInTouchMode() {
6127 synchronized(mWindowMap) {
6128 return mInTouchMode;
6129 }
6130 }
6131
6132 public boolean performHapticFeedback(IWindow window, int effectId,
6133 boolean always) {
6134 synchronized(mWindowMap) {
6135 long ident = Binder.clearCallingIdentity();
6136 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006137 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 windowForClientLocked(this, window), effectId, always);
6139 } finally {
6140 Binder.restoreCallingIdentity(ident);
6141 }
6142 }
6143 }
Romain Guy06882f82009-06-10 13:36:04 -07006144
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006145 public void setWallpaperPosition(IBinder window, float x, float y) {
6146 synchronized(mWindowMap) {
6147 long ident = Binder.clearCallingIdentity();
6148 try {
6149 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6150 x, y);
6151 } finally {
6152 Binder.restoreCallingIdentity(ident);
6153 }
6154 }
6155 }
6156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 void windowAddedLocked() {
6158 if (mSurfaceSession == null) {
6159 if (localLOGV) Log.v(
6160 TAG, "First window added to " + this + ", creating SurfaceSession");
6161 mSurfaceSession = new SurfaceSession();
6162 mSessions.add(this);
6163 }
6164 mNumWindow++;
6165 }
6166
6167 void windowRemovedLocked() {
6168 mNumWindow--;
6169 killSessionLocked();
6170 }
Romain Guy06882f82009-06-10 13:36:04 -07006171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 void killSessionLocked() {
6173 if (mNumWindow <= 0 && mClientDead) {
6174 mSessions.remove(this);
6175 if (mSurfaceSession != null) {
6176 if (localLOGV) Log.v(
6177 TAG, "Last window removed from " + this
6178 + ", destroying " + mSurfaceSession);
6179 try {
6180 mSurfaceSession.kill();
6181 } catch (Exception e) {
6182 Log.w(TAG, "Exception thrown when killing surface session "
6183 + mSurfaceSession + " in session " + this
6184 + ": " + e.toString());
6185 }
6186 mSurfaceSession = null;
6187 }
6188 }
6189 }
Romain Guy06882f82009-06-10 13:36:04 -07006190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006192 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6193 pw.print(" mClientDead="); pw.print(mClientDead);
6194 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6195 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6196 pw.print(prefix);
6197 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6198 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6199 }
6200 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6201 pw.print(prefix);
6202 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6203 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 }
6206
6207 @Override
6208 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006209 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 }
6211 }
6212
6213 // -------------------------------------------------------------
6214 // Client Window State
6215 // -------------------------------------------------------------
6216
6217 private final class WindowState implements WindowManagerPolicy.WindowState {
6218 final Session mSession;
6219 final IWindow mClient;
6220 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006221 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 AppWindowToken mAppToken;
6223 AppWindowToken mTargetAppToken;
6224 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6225 final DeathRecipient mDeathRecipient;
6226 final WindowState mAttachedWindow;
6227 final ArrayList mChildWindows = new ArrayList();
6228 final int mBaseLayer;
6229 final int mSubLayer;
6230 final boolean mLayoutAttached;
6231 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006232 final boolean mIsWallpaper;
6233 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 int mViewVisibility;
6235 boolean mPolicyVisibility = true;
6236 boolean mPolicyVisibilityAfterAnim = true;
6237 boolean mAppFreezing;
6238 Surface mSurface;
6239 boolean mAttachedHidden; // is our parent window hidden?
6240 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006241 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 int mRequestedWidth;
6243 int mRequestedHeight;
6244 int mLastRequestedWidth;
6245 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006246 int mLayer;
6247 int mAnimLayer;
6248 int mLastLayer;
6249 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006250 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251
6252 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 // Actual frame shown on-screen (may be modified by animation)
6255 final Rect mShownFrame = new Rect();
6256 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 /**
6259 * Insets that determine the actually visible area
6260 */
6261 final Rect mVisibleInsets = new Rect();
6262 final Rect mLastVisibleInsets = new Rect();
6263 boolean mVisibleInsetsChanged;
6264
6265 /**
6266 * Insets that are covered by system windows
6267 */
6268 final Rect mContentInsets = new Rect();
6269 final Rect mLastContentInsets = new Rect();
6270 boolean mContentInsetsChanged;
6271
6272 /**
6273 * Set to true if we are waiting for this window to receive its
6274 * given internal insets before laying out other windows based on it.
6275 */
6276 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 /**
6279 * These are the content insets that were given during layout for
6280 * this window, to be applied to windows behind it.
6281 */
6282 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 /**
6285 * These are the visible insets that were given during layout for
6286 * this window, to be applied to windows behind it.
6287 */
6288 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 /**
6291 * Flag indicating whether the touchable region should be adjusted by
6292 * the visible insets; if false the area outside the visible insets is
6293 * NOT touchable, so we must use those to adjust the frame during hit
6294 * tests.
6295 */
6296 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 // Current transformation being applied.
6299 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6300 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6301 float mHScale=1, mVScale=1;
6302 float mLastHScale=1, mLastVScale=1;
6303 final Matrix mTmpMatrix = new Matrix();
6304
6305 // "Real" frame that the application sees.
6306 final Rect mFrame = new Rect();
6307 final Rect mLastFrame = new Rect();
6308
6309 final Rect mContainingFrame = new Rect();
6310 final Rect mDisplayFrame = new Rect();
6311 final Rect mContentFrame = new Rect();
6312 final Rect mVisibleFrame = new Rect();
6313
6314 float mShownAlpha = 1;
6315 float mAlpha = 1;
6316 float mLastAlpha = 1;
6317
6318 // Set to true if, when the window gets displayed, it should perform
6319 // an enter animation.
6320 boolean mEnterAnimationPending;
6321
6322 // Currently running animation.
6323 boolean mAnimating;
6324 boolean mLocalAnimating;
6325 Animation mAnimation;
6326 boolean mAnimationIsEntrance;
6327 boolean mHasTransformation;
6328 boolean mHasLocalTransformation;
6329 final Transformation mTransformation = new Transformation();
6330
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006331 // If a window showing a wallpaper: the requested offset for the
6332 // wallpaper; if a wallpaper window: the currently applied offset.
6333 float mWallpaperX = -1;
6334 float mWallpaperY = -1;
6335
6336 // Wallpaper windows: pixels offset based on above variables.
6337 int mXOffset;
6338 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 // This is set after IWindowSession.relayout() has been called at
6341 // least once for the window. It allows us to detect the situation
6342 // where we don't yet have a surface, but should have one soon, so
6343 // we can give the window focus before waiting for the relayout.
6344 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 // This is set after the Surface has been created but before the
6347 // window has been drawn. During this time the surface is hidden.
6348 boolean mDrawPending;
6349
6350 // This is set after the window has finished drawing for the first
6351 // time but before its surface is shown. The surface will be
6352 // displayed when the next layout is run.
6353 boolean mCommitDrawPending;
6354
6355 // This is set during the time after the window's drawing has been
6356 // committed, and before its surface is actually shown. It is used
6357 // to delay showing the surface until all windows in a token are ready
6358 // to be shown.
6359 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 // Set when the window has been shown in the screen the first time.
6362 boolean mHasDrawn;
6363
6364 // Currently running an exit animation?
6365 boolean mExiting;
6366
6367 // Currently on the mDestroySurface list?
6368 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 // Completely remove from window manager after exit animation?
6371 boolean mRemoveOnExit;
6372
6373 // Set when the orientation is changing and this window has not yet
6374 // been updated for the new orientation.
6375 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 // Is this window now (or just being) removed?
6378 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 WindowState(Session s, IWindow c, WindowToken token,
6381 WindowState attachedWindow, WindowManager.LayoutParams a,
6382 int viewVisibility) {
6383 mSession = s;
6384 mClient = c;
6385 mToken = token;
6386 mAttrs.copyFrom(a);
6387 mViewVisibility = viewVisibility;
6388 DeathRecipient deathRecipient = new DeathRecipient();
6389 mAlpha = a.alpha;
6390 if (localLOGV) Log.v(
6391 TAG, "Window " + this + " client=" + c.asBinder()
6392 + " token=" + token + " (" + mAttrs.token + ")");
6393 try {
6394 c.asBinder().linkToDeath(deathRecipient, 0);
6395 } catch (RemoteException e) {
6396 mDeathRecipient = null;
6397 mAttachedWindow = null;
6398 mLayoutAttached = false;
6399 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006400 mIsWallpaper = false;
6401 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 mBaseLayer = 0;
6403 mSubLayer = 0;
6404 return;
6405 }
6406 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6409 mAttrs.type <= LAST_SUB_WINDOW)) {
6410 // The multiplier here is to reserve space for multiple
6411 // windows in the same type layer.
6412 mBaseLayer = mPolicy.windowTypeToLayerLw(
6413 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6414 + TYPE_LAYER_OFFSET;
6415 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6416 mAttachedWindow = attachedWindow;
6417 mAttachedWindow.mChildWindows.add(this);
6418 mLayoutAttached = mAttrs.type !=
6419 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6420 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6421 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006422 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6423 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 } else {
6425 // The multiplier here is to reserve space for multiple
6426 // windows in the same type layer.
6427 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6428 * TYPE_LAYER_MULTIPLIER
6429 + TYPE_LAYER_OFFSET;
6430 mSubLayer = 0;
6431 mAttachedWindow = null;
6432 mLayoutAttached = false;
6433 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6434 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006435 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6436 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 }
6438
6439 WindowState appWin = this;
6440 while (appWin.mAttachedWindow != null) {
6441 appWin = mAttachedWindow;
6442 }
6443 WindowToken appToken = appWin.mToken;
6444 while (appToken.appWindowToken == null) {
6445 WindowToken parent = mTokenMap.get(appToken.token);
6446 if (parent == null || appToken == parent) {
6447 break;
6448 }
6449 appToken = parent;
6450 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006451 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 mAppToken = appToken.appWindowToken;
6453
6454 mSurface = null;
6455 mRequestedWidth = 0;
6456 mRequestedHeight = 0;
6457 mLastRequestedWidth = 0;
6458 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006459 mXOffset = 0;
6460 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 mLayer = 0;
6462 mAnimLayer = 0;
6463 mLastLayer = 0;
6464 }
6465
6466 void attach() {
6467 if (localLOGV) Log.v(
6468 TAG, "Attaching " + this + " token=" + mToken
6469 + ", list=" + mToken.windows);
6470 mSession.windowAddedLocked();
6471 }
6472
6473 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6474 mHaveFrame = true;
6475
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006476 final Rect container = mContainingFrame;
6477 container.set(pf);
6478
6479 final Rect display = mDisplayFrame;
6480 display.set(df);
6481
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006482 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006483 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006484 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6485 display.intersect(mCompatibleScreenFrame);
6486 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006487 }
6488
6489 final int pw = container.right - container.left;
6490 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491
6492 int w,h;
6493 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6494 w = mAttrs.width < 0 ? pw : mAttrs.width;
6495 h = mAttrs.height< 0 ? ph : mAttrs.height;
6496 } else {
6497 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6498 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6499 }
Romain Guy06882f82009-06-10 13:36:04 -07006500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 final Rect content = mContentFrame;
6502 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 final Rect visible = mVisibleFrame;
6505 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006508 final int fw = frame.width();
6509 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6512 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6513
6514 Gravity.apply(mAttrs.gravity, w, h, container,
6515 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6516 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6517
6518 //System.out.println("Out: " + mFrame);
6519
6520 // Now make sure the window fits in the overall display.
6521 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 // Make sure the content and visible frames are inside of the
6524 // final window frame.
6525 if (content.left < frame.left) content.left = frame.left;
6526 if (content.top < frame.top) content.top = frame.top;
6527 if (content.right > frame.right) content.right = frame.right;
6528 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6529 if (visible.left < frame.left) visible.left = frame.left;
6530 if (visible.top < frame.top) visible.top = frame.top;
6531 if (visible.right > frame.right) visible.right = frame.right;
6532 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 final Rect contentInsets = mContentInsets;
6535 contentInsets.left = content.left-frame.left;
6536 contentInsets.top = content.top-frame.top;
6537 contentInsets.right = frame.right-content.right;
6538 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 final Rect visibleInsets = mVisibleInsets;
6541 visibleInsets.left = visible.left-frame.left;
6542 visibleInsets.top = visible.top-frame.top;
6543 visibleInsets.right = frame.right-visible.right;
6544 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006545
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006546 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())
6547 && mWallpaperTarget != null) {
6548 updateWallpaperOffsetLocked(mWallpaperTarget, this,
6549 mDisplay.getWidth(), mDisplay.getHeight());
6550 }
6551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006552 if (localLOGV) {
6553 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6554 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6555 Log.v(TAG, "Resolving (mRequestedWidth="
6556 + mRequestedWidth + ", mRequestedheight="
6557 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6558 + "): frame=" + mFrame.toShortString()
6559 + " ci=" + contentInsets.toShortString()
6560 + " vi=" + visibleInsets.toShortString());
6561 //}
6562 }
6563 }
Romain Guy06882f82009-06-10 13:36:04 -07006564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 public Rect getFrameLw() {
6566 return mFrame;
6567 }
6568
6569 public Rect getShownFrameLw() {
6570 return mShownFrame;
6571 }
6572
6573 public Rect getDisplayFrameLw() {
6574 return mDisplayFrame;
6575 }
6576
6577 public Rect getContentFrameLw() {
6578 return mContentFrame;
6579 }
6580
6581 public Rect getVisibleFrameLw() {
6582 return mVisibleFrame;
6583 }
6584
6585 public boolean getGivenInsetsPendingLw() {
6586 return mGivenInsetsPending;
6587 }
6588
6589 public Rect getGivenContentInsetsLw() {
6590 return mGivenContentInsets;
6591 }
Romain Guy06882f82009-06-10 13:36:04 -07006592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006593 public Rect getGivenVisibleInsetsLw() {
6594 return mGivenVisibleInsets;
6595 }
Romain Guy06882f82009-06-10 13:36:04 -07006596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 public WindowManager.LayoutParams getAttrs() {
6598 return mAttrs;
6599 }
6600
6601 public int getSurfaceLayer() {
6602 return mLayer;
6603 }
Romain Guy06882f82009-06-10 13:36:04 -07006604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 public IApplicationToken getAppToken() {
6606 return mAppToken != null ? mAppToken.appToken : null;
6607 }
6608
6609 public boolean hasAppShownWindows() {
6610 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6611 }
6612
6613 public boolean hasAppStartingIcon() {
6614 return mAppToken != null ? (mAppToken.startingData != null) : false;
6615 }
6616
6617 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6618 return mAppToken != null ? mAppToken.startingWindow : null;
6619 }
6620
6621 public void setAnimation(Animation anim) {
6622 if (localLOGV) Log.v(
6623 TAG, "Setting animation in " + this + ": " + anim);
6624 mAnimating = false;
6625 mLocalAnimating = false;
6626 mAnimation = anim;
6627 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6628 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6629 }
6630
6631 public void clearAnimation() {
6632 if (mAnimation != null) {
6633 mAnimating = true;
6634 mLocalAnimating = false;
6635 mAnimation = null;
6636 }
6637 }
Romain Guy06882f82009-06-10 13:36:04 -07006638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 Surface createSurfaceLocked() {
6640 if (mSurface == null) {
6641 mDrawPending = true;
6642 mCommitDrawPending = false;
6643 mReadyToShow = false;
6644 if (mAppToken != null) {
6645 mAppToken.allDrawn = false;
6646 }
6647
6648 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006649 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650 flags |= Surface.PUSH_BUFFERS;
6651 }
6652
6653 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6654 flags |= Surface.SECURE;
6655 }
6656 if (DEBUG_VISIBILITY) Log.v(
6657 TAG, "Creating surface in session "
6658 + mSession.mSurfaceSession + " window " + this
6659 + " w=" + mFrame.width()
6660 + " h=" + mFrame.height() + " format="
6661 + mAttrs.format + " flags=" + flags);
6662
6663 int w = mFrame.width();
6664 int h = mFrame.height();
6665 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6666 // for a scaled surface, we always want the requested
6667 // size.
6668 w = mRequestedWidth;
6669 h = mRequestedHeight;
6670 }
6671
6672 try {
6673 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006674 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 0, w, h, mAttrs.format, flags);
6676 } catch (Surface.OutOfResourcesException e) {
6677 Log.w(TAG, "OutOfResourcesException creating surface");
6678 reclaimSomeSurfaceMemoryLocked(this, "create");
6679 return null;
6680 } catch (Exception e) {
6681 Log.e(TAG, "Exception creating surface", e);
6682 return null;
6683 }
Romain Guy06882f82009-06-10 13:36:04 -07006684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 if (localLOGV) Log.v(
6686 TAG, "Got surface: " + mSurface
6687 + ", set left=" + mFrame.left + " top=" + mFrame.top
6688 + ", animLayer=" + mAnimLayer);
6689 if (SHOW_TRANSACTIONS) {
6690 Log.i(TAG, ">>> OPEN TRANSACTION");
6691 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6692 + mAttrs.getTitle() + ") pos=(" +
6693 mFrame.left + "," + mFrame.top + ") (" +
6694 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6695 mAnimLayer + " HIDE");
6696 }
6697 Surface.openTransaction();
6698 try {
6699 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006700 mSurface.setPosition(mFrame.left + mXOffset,
6701 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 mSurface.setLayer(mAnimLayer);
6703 mSurface.hide();
6704 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6705 mSurface.setFlags(Surface.SURFACE_DITHER,
6706 Surface.SURFACE_DITHER);
6707 }
6708 } catch (RuntimeException e) {
6709 Log.w(TAG, "Error creating surface in " + w, e);
6710 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6711 }
6712 mLastHidden = true;
6713 } finally {
6714 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6715 Surface.closeTransaction();
6716 }
6717 if (localLOGV) Log.v(
6718 TAG, "Created surface " + this);
6719 }
6720 return mSurface;
6721 }
Romain Guy06882f82009-06-10 13:36:04 -07006722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723 void destroySurfaceLocked() {
6724 // Window is no longer on-screen, so can no longer receive
6725 // key events... if we were waiting for it to finish
6726 // handling a key event, the wait is over!
6727 mKeyWaiter.finishedKey(mSession, mClient, true,
6728 KeyWaiter.RETURN_NOTHING);
6729 mKeyWaiter.releasePendingPointerLocked(mSession);
6730 mKeyWaiter.releasePendingTrackballLocked(mSession);
6731
6732 if (mAppToken != null && this == mAppToken.startingWindow) {
6733 mAppToken.startingDisplayed = false;
6734 }
Romain Guy06882f82009-06-10 13:36:04 -07006735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 if (mSurface != null) {
6737 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006738 if (DEBUG_VISIBILITY) {
6739 RuntimeException e = new RuntimeException();
6740 e.fillInStackTrace();
6741 Log.w(TAG, "Window " + this + " destroying surface "
6742 + mSurface + ", session " + mSession, e);
6743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 if (SHOW_TRANSACTIONS) {
6745 RuntimeException ex = new RuntimeException();
6746 ex.fillInStackTrace();
6747 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6748 + mAttrs.getTitle() + ")", ex);
6749 }
6750 mSurface.clear();
6751 } catch (RuntimeException e) {
6752 Log.w(TAG, "Exception thrown when destroying Window " + this
6753 + " surface " + mSurface + " session " + mSession
6754 + ": " + e.toString());
6755 }
6756 mSurface = null;
6757 mDrawPending = false;
6758 mCommitDrawPending = false;
6759 mReadyToShow = false;
6760
6761 int i = mChildWindows.size();
6762 while (i > 0) {
6763 i--;
6764 WindowState c = (WindowState)mChildWindows.get(i);
6765 c.mAttachedHidden = true;
6766 }
6767 }
6768 }
6769
6770 boolean finishDrawingLocked() {
6771 if (mDrawPending) {
6772 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6773 TAG, "finishDrawingLocked: " + mSurface);
6774 mCommitDrawPending = true;
6775 mDrawPending = false;
6776 return true;
6777 }
6778 return false;
6779 }
6780
6781 // This must be called while inside a transaction.
6782 void commitFinishDrawingLocked(long currentTime) {
6783 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6784 if (!mCommitDrawPending) {
6785 return;
6786 }
6787 mCommitDrawPending = false;
6788 mReadyToShow = true;
6789 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6790 final AppWindowToken atoken = mAppToken;
6791 if (atoken == null || atoken.allDrawn || starting) {
6792 performShowLocked();
6793 }
6794 }
6795
6796 // This must be called while inside a transaction.
6797 boolean performShowLocked() {
6798 if (DEBUG_VISIBILITY) {
6799 RuntimeException e = new RuntimeException();
6800 e.fillInStackTrace();
6801 Log.v(TAG, "performShow on " + this
6802 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6803 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6804 }
6805 if (mReadyToShow && isReadyForDisplay()) {
6806 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6807 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6808 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6809 + " during animation: policyVis=" + mPolicyVisibility
6810 + " attHidden=" + mAttachedHidden
6811 + " tok.hiddenRequested="
6812 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6813 + " tok.idden="
6814 + (mAppToken != null ? mAppToken.hidden : false)
6815 + " animating=" + mAnimating
6816 + " tok animating="
6817 + (mAppToken != null ? mAppToken.animating : false));
6818 if (!showSurfaceRobustlyLocked(this)) {
6819 return false;
6820 }
6821 mLastAlpha = -1;
6822 mHasDrawn = true;
6823 mLastHidden = false;
6824 mReadyToShow = false;
6825 enableScreenIfNeededLocked();
6826
6827 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 int i = mChildWindows.size();
6830 while (i > 0) {
6831 i--;
6832 WindowState c = (WindowState)mChildWindows.get(i);
6833 if (c.mSurface != null && c.mAttachedHidden) {
6834 c.mAttachedHidden = false;
6835 c.performShowLocked();
6836 }
6837 }
Romain Guy06882f82009-06-10 13:36:04 -07006838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 if (mAttrs.type != TYPE_APPLICATION_STARTING
6840 && mAppToken != null) {
6841 mAppToken.firstWindowDrawn = true;
6842 if (mAnimation == null && mAppToken.startingData != null) {
6843 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6844 + mToken
6845 + ": first real window is shown, no animation");
6846 mFinishedStarting.add(mAppToken);
6847 mH.sendEmptyMessage(H.FINISHED_STARTING);
6848 }
6849 mAppToken.updateReportedVisibilityLocked();
6850 }
6851 }
6852 return true;
6853 }
Romain Guy06882f82009-06-10 13:36:04 -07006854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 // This must be called while inside a transaction. Returns true if
6856 // there is more animation to run.
6857 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6858 if (!mDisplayFrozen) {
6859 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6862 mHasTransformation = true;
6863 mHasLocalTransformation = true;
6864 if (!mLocalAnimating) {
6865 if (DEBUG_ANIM) Log.v(
6866 TAG, "Starting animation in " + this +
6867 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6868 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6869 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6870 mAnimation.setStartTime(currentTime);
6871 mLocalAnimating = true;
6872 mAnimating = true;
6873 }
6874 mTransformation.clear();
6875 final boolean more = mAnimation.getTransformation(
6876 currentTime, mTransformation);
6877 if (DEBUG_ANIM) Log.v(
6878 TAG, "Stepped animation in " + this +
6879 ": more=" + more + ", xform=" + mTransformation);
6880 if (more) {
6881 // we're not done!
6882 return true;
6883 }
6884 if (DEBUG_ANIM) Log.v(
6885 TAG, "Finished animation in " + this +
6886 " @ " + currentTime);
6887 mAnimation = null;
6888 //WindowManagerService.this.dump();
6889 }
6890 mHasLocalTransformation = false;
6891 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006892 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 // When our app token is animating, we kind-of pretend like
6894 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6895 // part of this check means that we will only do this if
6896 // our window is not currently exiting, or it is not
6897 // locally animating itself. The idea being that one that
6898 // is exiting and doing a local animation should be removed
6899 // once that animation is done.
6900 mAnimating = true;
6901 mHasTransformation = true;
6902 mTransformation.clear();
6903 return false;
6904 } else if (mHasTransformation) {
6905 // Little trick to get through the path below to act like
6906 // we have finished an animation.
6907 mAnimating = true;
6908 } else if (isAnimating()) {
6909 mAnimating = true;
6910 }
6911 } else if (mAnimation != null) {
6912 // If the display is frozen, and there is a pending animation,
6913 // clear it and make sure we run the cleanup code.
6914 mAnimating = true;
6915 mLocalAnimating = true;
6916 mAnimation = null;
6917 }
Romain Guy06882f82009-06-10 13:36:04 -07006918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 if (!mAnimating && !mLocalAnimating) {
6920 return false;
6921 }
6922
6923 if (DEBUG_ANIM) Log.v(
6924 TAG, "Animation done in " + this + ": exiting=" + mExiting
6925 + ", reportedVisible="
6926 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 mAnimating = false;
6929 mLocalAnimating = false;
6930 mAnimation = null;
6931 mAnimLayer = mLayer;
6932 if (mIsImWindow) {
6933 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006934 } else if (mIsWallpaper) {
6935 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 }
6937 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6938 + " anim layer: " + mAnimLayer);
6939 mHasTransformation = false;
6940 mHasLocalTransformation = false;
6941 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6942 mTransformation.clear();
6943 if (mHasDrawn
6944 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6945 && mAppToken != null
6946 && mAppToken.firstWindowDrawn
6947 && mAppToken.startingData != null) {
6948 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6949 + mToken + ": first real window done animating");
6950 mFinishedStarting.add(mAppToken);
6951 mH.sendEmptyMessage(H.FINISHED_STARTING);
6952 }
Romain Guy06882f82009-06-10 13:36:04 -07006953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 finishExit();
6955
6956 if (mAppToken != null) {
6957 mAppToken.updateReportedVisibilityLocked();
6958 }
6959
6960 return false;
6961 }
6962
6963 void finishExit() {
6964 if (DEBUG_ANIM) Log.v(
6965 TAG, "finishExit in " + this
6966 + ": exiting=" + mExiting
6967 + " remove=" + mRemoveOnExit
6968 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 final int N = mChildWindows.size();
6971 for (int i=0; i<N; i++) {
6972 ((WindowState)mChildWindows.get(i)).finishExit();
6973 }
Romain Guy06882f82009-06-10 13:36:04 -07006974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 if (!mExiting) {
6976 return;
6977 }
Romain Guy06882f82009-06-10 13:36:04 -07006978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 if (isWindowAnimating()) {
6980 return;
6981 }
6982
6983 if (localLOGV) Log.v(
6984 TAG, "Exit animation finished in " + this
6985 + ": remove=" + mRemoveOnExit);
6986 if (mSurface != null) {
6987 mDestroySurface.add(this);
6988 mDestroying = true;
6989 if (SHOW_TRANSACTIONS) Log.i(
6990 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6991 try {
6992 mSurface.hide();
6993 } catch (RuntimeException e) {
6994 Log.w(TAG, "Error hiding surface in " + this, e);
6995 }
6996 mLastHidden = true;
6997 mKeyWaiter.releasePendingPointerLocked(mSession);
6998 }
6999 mExiting = false;
7000 if (mRemoveOnExit) {
7001 mPendingRemove.add(this);
7002 mRemoveOnExit = false;
7003 }
7004 }
Romain Guy06882f82009-06-10 13:36:04 -07007005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7007 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7008 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7009 if (dtdx < -.000001f || dtdx > .000001f) return false;
7010 if (dsdy < -.000001f || dsdy > .000001f) return false;
7011 return true;
7012 }
Romain Guy06882f82009-06-10 13:36:04 -07007013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 void computeShownFrameLocked() {
7015 final boolean selfTransformation = mHasLocalTransformation;
7016 Transformation attachedTransformation =
7017 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7018 ? mAttachedWindow.mTransformation : null;
7019 Transformation appTransformation =
7020 (mAppToken != null && mAppToken.hasTransformation)
7021 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007022
7023 // Wallpapers are animated based on the "real" window they
7024 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007025 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007026 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007027 if (mWallpaperTarget.mHasLocalTransformation) {
7028 attachedTransformation = mWallpaperTarget.mTransformation;
7029 }
7030 if (mWallpaperTarget.mAppToken != null &&
7031 mWallpaperTarget.mAppToken.hasTransformation) {
7032 appTransformation = mWallpaperTarget.mAppToken.transformation;
7033 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007034 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7035 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7036 }
7037 if (DEBUG_WALLPAPER && appTransformation != null) {
7038 Log.v(TAG, "WP target app xform: " + appTransformation);
7039 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007040 }
7041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 if (selfTransformation || attachedTransformation != null
7043 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007044 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 final Rect frame = mFrame;
7046 final float tmpFloats[] = mTmpFloats;
7047 final Matrix tmpMatrix = mTmpMatrix;
7048
7049 // Compute the desired transformation.
7050 tmpMatrix.setTranslate(frame.left, frame.top);
7051 if (selfTransformation) {
7052 tmpMatrix.preConcat(mTransformation.getMatrix());
7053 }
7054 if (attachedTransformation != null) {
7055 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7056 }
7057 if (appTransformation != null) {
7058 tmpMatrix.preConcat(appTransformation.getMatrix());
7059 }
7060
7061 // "convert" it into SurfaceFlinger's format
7062 // (a 2x2 matrix + an offset)
7063 // Here we must not transform the position of the surface
7064 // since it is already included in the transformation.
7065 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 tmpMatrix.getValues(tmpFloats);
7068 mDsDx = tmpFloats[Matrix.MSCALE_X];
7069 mDtDx = tmpFloats[Matrix.MSKEW_X];
7070 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7071 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007072 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7073 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 int w = frame.width();
7075 int h = frame.height();
7076 mShownFrame.set(x, y, x+w, y+h);
7077
7078 // Now set the alpha... but because our current hardware
7079 // can't do alpha transformation on a non-opaque surface,
7080 // turn it off if we are running an animation that is also
7081 // transforming since it is more important to have that
7082 // animation be smooth.
7083 mShownAlpha = mAlpha;
7084 if (!mLimitedAlphaCompositing
7085 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7086 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7087 && x == frame.left && y == frame.top))) {
7088 //Log.i(TAG, "Applying alpha transform");
7089 if (selfTransformation) {
7090 mShownAlpha *= mTransformation.getAlpha();
7091 }
7092 if (attachedTransformation != null) {
7093 mShownAlpha *= attachedTransformation.getAlpha();
7094 }
7095 if (appTransformation != null) {
7096 mShownAlpha *= appTransformation.getAlpha();
7097 }
7098 } else {
7099 //Log.i(TAG, "Not applying alpha transform");
7100 }
Romain Guy06882f82009-06-10 13:36:04 -07007101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 if (localLOGV) Log.v(
7103 TAG, "Continuing animation in " + this +
7104 ": " + mShownFrame +
7105 ", alpha=" + mTransformation.getAlpha());
7106 return;
7107 }
Romain Guy06882f82009-06-10 13:36:04 -07007108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007110 if (mXOffset != 0 || mYOffset != 0) {
7111 mShownFrame.offset(mXOffset, mYOffset);
7112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 mShownAlpha = mAlpha;
7114 mDsDx = 1;
7115 mDtDx = 0;
7116 mDsDy = 0;
7117 mDtDy = 1;
7118 }
Romain Guy06882f82009-06-10 13:36:04 -07007119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 /**
7121 * Is this window visible? It is not visible if there is no
7122 * surface, or we are in the process of running an exit animation
7123 * that will remove the surface, or its app token has been hidden.
7124 */
7125 public boolean isVisibleLw() {
7126 final AppWindowToken atoken = mAppToken;
7127 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7128 && (atoken == null || !atoken.hiddenRequested)
7129 && !mExiting && !mDestroying;
7130 }
7131
7132 /**
7133 * Is this window visible, ignoring its app token? It is not visible
7134 * if there is no surface, or we are in the process of running an exit animation
7135 * that will remove the surface.
7136 */
7137 public boolean isWinVisibleLw() {
7138 final AppWindowToken atoken = mAppToken;
7139 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7140 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7141 && !mExiting && !mDestroying;
7142 }
7143
7144 /**
7145 * The same as isVisible(), but follows the current hidden state of
7146 * the associated app token, not the pending requested hidden state.
7147 */
7148 boolean isVisibleNow() {
7149 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007150 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 }
7152
7153 /**
7154 * Same as isVisible(), but we also count it as visible between the
7155 * call to IWindowSession.add() and the first relayout().
7156 */
7157 boolean isVisibleOrAdding() {
7158 final AppWindowToken atoken = mAppToken;
7159 return (mSurface != null
7160 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7161 && mPolicyVisibility && !mAttachedHidden
7162 && (atoken == null || !atoken.hiddenRequested)
7163 && !mExiting && !mDestroying;
7164 }
7165
7166 /**
7167 * Is this window currently on-screen? It is on-screen either if it
7168 * is visible or it is currently running an animation before no longer
7169 * being visible.
7170 */
7171 boolean isOnScreen() {
7172 final AppWindowToken atoken = mAppToken;
7173 if (atoken != null) {
7174 return mSurface != null && mPolicyVisibility && !mDestroying
7175 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007176 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 } else {
7178 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007179 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 }
7181 }
Romain Guy06882f82009-06-10 13:36:04 -07007182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 /**
7184 * Like isOnScreen(), but we don't return true if the window is part
7185 * of a transition that has not yet been started.
7186 */
7187 boolean isReadyForDisplay() {
7188 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007189 final boolean animating = atoken != null
7190 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007192 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7193 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007194 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 }
7196
7197 /** Is the window or its container currently animating? */
7198 boolean isAnimating() {
7199 final WindowState attached = mAttachedWindow;
7200 final AppWindowToken atoken = mAppToken;
7201 return mAnimation != null
7202 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007203 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 (atoken.animation != null
7205 || atoken.inPendingTransaction));
7206 }
7207
7208 /** Is this window currently animating? */
7209 boolean isWindowAnimating() {
7210 return mAnimation != null;
7211 }
7212
7213 /**
7214 * Like isOnScreen, but returns false if the surface hasn't yet
7215 * been drawn.
7216 */
7217 public boolean isDisplayedLw() {
7218 final AppWindowToken atoken = mAppToken;
7219 return mSurface != null && mPolicyVisibility && !mDestroying
7220 && !mDrawPending && !mCommitDrawPending
7221 && ((!mAttachedHidden &&
7222 (atoken == null || !atoken.hiddenRequested))
7223 || mAnimating);
7224 }
7225
7226 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7227 boolean shownFrame, boolean onlyOpaque) {
7228 if (mSurface == null) {
7229 return false;
7230 }
7231 if (mAppToken != null && !mAppToken.appFullscreen) {
7232 return false;
7233 }
7234 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7235 return false;
7236 }
7237 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007238
7239 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7240 return frame.left <= mCompatibleScreenFrame.left &&
7241 frame.top <= mCompatibleScreenFrame.top &&
7242 frame.right >= mCompatibleScreenFrame.right &&
7243 frame.bottom >= mCompatibleScreenFrame.bottom;
7244 } else {
7245 return frame.left <= 0 && frame.top <= 0
7246 && frame.right >= screenWidth
7247 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 }
Romain Guy06882f82009-06-10 13:36:04 -07007250
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007251 /**
7252 * Return true if the window is opaque and fully drawn.
7253 */
7254 boolean isOpaqueDrawn() {
7255 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7256 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7257 }
7258
7259 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7260 return
7261 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007262 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7263 // only if it's visible
7264 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007265 // and only if the application fills the compatible screen
7266 mFrame.left <= mCompatibleScreenFrame.left &&
7267 mFrame.top <= mCompatibleScreenFrame.top &&
7268 mFrame.right >= mCompatibleScreenFrame.right &&
7269 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007270 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007271 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007272 }
7273
7274 boolean isFullscreen(int screenWidth, int screenHeight) {
7275 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007276 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 }
7278
7279 void removeLocked() {
7280 if (mAttachedWindow != null) {
7281 mAttachedWindow.mChildWindows.remove(this);
7282 }
7283 destroySurfaceLocked();
7284 mSession.windowRemovedLocked();
7285 try {
7286 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7287 } catch (RuntimeException e) {
7288 // Ignore if it has already been removed (usually because
7289 // we are doing this as part of processing a death note.)
7290 }
7291 }
7292
7293 private class DeathRecipient implements IBinder.DeathRecipient {
7294 public void binderDied() {
7295 try {
7296 synchronized(mWindowMap) {
7297 WindowState win = windowForClientLocked(mSession, mClient);
7298 Log.i(TAG, "WIN DEATH: " + win);
7299 if (win != null) {
7300 removeWindowLocked(mSession, win);
7301 }
7302 }
7303 } catch (IllegalArgumentException ex) {
7304 // This will happen if the window has already been
7305 // removed.
7306 }
7307 }
7308 }
7309
7310 /** Returns true if this window desires key events. */
7311 public final boolean canReceiveKeys() {
7312 return isVisibleOrAdding()
7313 && (mViewVisibility == View.VISIBLE)
7314 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7315 }
7316
7317 public boolean hasDrawnLw() {
7318 return mHasDrawn;
7319 }
7320
7321 public boolean showLw(boolean doAnimation) {
7322 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7323 mPolicyVisibility = true;
7324 mPolicyVisibilityAfterAnim = true;
7325 if (doAnimation) {
7326 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7327 }
7328 requestAnimationLocked(0);
7329 return true;
7330 }
7331 return false;
7332 }
7333
7334 public boolean hideLw(boolean doAnimation) {
7335 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7336 : mPolicyVisibility;
7337 if (current) {
7338 if (doAnimation) {
7339 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7340 if (mAnimation == null) {
7341 doAnimation = false;
7342 }
7343 }
7344 if (doAnimation) {
7345 mPolicyVisibilityAfterAnim = false;
7346 } else {
7347 mPolicyVisibilityAfterAnim = false;
7348 mPolicyVisibility = false;
7349 }
7350 requestAnimationLocked(0);
7351 return true;
7352 }
7353 return false;
7354 }
7355
7356 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007357 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007358
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007359 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7360 pw.print(" mClient="); pw.println(mClient.asBinder());
7361 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7362 if (mAttachedWindow != null || mLayoutAttached) {
7363 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7364 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7365 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007366 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7367 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7368 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007369 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7370 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007371 }
7372 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7373 pw.print(" mSubLayer="); pw.print(mSubLayer);
7374 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7375 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7376 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7377 pw.print("="); pw.print(mAnimLayer);
7378 pw.print(" mLastLayer="); pw.println(mLastLayer);
7379 if (mSurface != null) {
7380 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7381 }
7382 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7383 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7384 if (mAppToken != null) {
7385 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7386 }
7387 if (mTargetAppToken != null) {
7388 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7389 }
7390 pw.print(prefix); pw.print("mViewVisibility=0x");
7391 pw.print(Integer.toHexString(mViewVisibility));
7392 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007393 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7394 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007395 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7396 pw.print(prefix); pw.print("mPolicyVisibility=");
7397 pw.print(mPolicyVisibility);
7398 pw.print(" mPolicyVisibilityAfterAnim=");
7399 pw.print(mPolicyVisibilityAfterAnim);
7400 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7401 }
7402 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007403 pw.print(" h="); pw.println(mRequestedHeight);
7404 if (mXOffset != 0 || mYOffset != 0) {
7405 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7406 pw.print(" y="); pw.println(mYOffset);
7407 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007408 pw.print(prefix); pw.print("mGivenContentInsets=");
7409 mGivenContentInsets.printShortString(pw);
7410 pw.print(" mGivenVisibleInsets=");
7411 mGivenVisibleInsets.printShortString(pw);
7412 pw.println();
7413 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7414 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7415 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7416 }
7417 pw.print(prefix); pw.print("mShownFrame=");
7418 mShownFrame.printShortString(pw);
7419 pw.print(" last="); mLastShownFrame.printShortString(pw);
7420 pw.println();
7421 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7422 pw.print(" last="); mLastFrame.printShortString(pw);
7423 pw.println();
7424 pw.print(prefix); pw.print("mContainingFrame=");
7425 mContainingFrame.printShortString(pw);
7426 pw.print(" mDisplayFrame=");
7427 mDisplayFrame.printShortString(pw);
7428 pw.println();
7429 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7430 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7431 pw.println();
7432 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7433 pw.print(" last="); mLastContentInsets.printShortString(pw);
7434 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7435 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7436 pw.println();
7437 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7438 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7439 pw.print(" mAlpha="); pw.print(mAlpha);
7440 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7441 }
7442 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7443 || mAnimation != null) {
7444 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7445 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7446 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7447 pw.print(" mAnimation="); pw.println(mAnimation);
7448 }
7449 if (mHasTransformation || mHasLocalTransformation) {
7450 pw.print(prefix); pw.print("XForm: has=");
7451 pw.print(mHasTransformation);
7452 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7453 pw.print(" "); mTransformation.printShortString(pw);
7454 pw.println();
7455 }
7456 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7457 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7458 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7459 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7460 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7461 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7462 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7463 pw.print(" mDestroying="); pw.print(mDestroying);
7464 pw.print(" mRemoved="); pw.println(mRemoved);
7465 }
7466 if (mOrientationChanging || mAppFreezing) {
7467 pw.print(prefix); pw.print("mOrientationChanging=");
7468 pw.print(mOrientationChanging);
7469 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7470 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007471 if (mHScale != 1 || mVScale != 1) {
7472 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7473 pw.print(" mVScale="); pw.println(mVScale);
7474 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007475 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007476 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7477 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 }
7480
7481 @Override
7482 public String toString() {
7483 return "Window{"
7484 + Integer.toHexString(System.identityHashCode(this))
7485 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7486 }
7487 }
Romain Guy06882f82009-06-10 13:36:04 -07007488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 // -------------------------------------------------------------
7490 // Window Token State
7491 // -------------------------------------------------------------
7492
7493 class WindowToken {
7494 // The actual token.
7495 final IBinder token;
7496
7497 // The type of window this token is for, as per WindowManager.LayoutParams.
7498 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 // Set if this token was explicitly added by a client, so should
7501 // not be removed when all windows are removed.
7502 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007503
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007504 // For printing.
7505 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 // If this is an AppWindowToken, this is non-null.
7508 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 // All of the windows associated with this token.
7511 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7512
7513 // Is key dispatching paused for this token?
7514 boolean paused = false;
7515
7516 // Should this token's windows be hidden?
7517 boolean hidden;
7518
7519 // Temporary for finding which tokens no longer have visible windows.
7520 boolean hasVisible;
7521
7522 WindowToken(IBinder _token, int type, boolean _explicit) {
7523 token = _token;
7524 windowType = type;
7525 explicit = _explicit;
7526 }
7527
7528 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007529 pw.print(prefix); pw.print("token="); pw.println(token);
7530 pw.print(prefix); pw.print("windows="); pw.println(windows);
7531 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7532 pw.print(" hidden="); pw.print(hidden);
7533 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 }
7535
7536 @Override
7537 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007538 if (stringName == null) {
7539 StringBuilder sb = new StringBuilder();
7540 sb.append("WindowToken{");
7541 sb.append(Integer.toHexString(System.identityHashCode(this)));
7542 sb.append(" token="); sb.append(token); sb.append('}');
7543 stringName = sb.toString();
7544 }
7545 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 }
7547 };
7548
7549 class AppWindowToken extends WindowToken {
7550 // Non-null only for application tokens.
7551 final IApplicationToken appToken;
7552
7553 // All of the windows and child windows that are included in this
7554 // application token. Note this list is NOT sorted!
7555 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7556
7557 int groupId = -1;
7558 boolean appFullscreen;
7559 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 // These are used for determining when all windows associated with
7562 // an activity have been drawn, so they can be made visible together
7563 // at the same time.
7564 int lastTransactionSequence = mTransactionSequence-1;
7565 int numInterestingWindows;
7566 int numDrawnWindows;
7567 boolean inPendingTransaction;
7568 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 // Is this token going to be hidden in a little while? If so, it
7571 // won't be taken into account for setting the screen orientation.
7572 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 // Is this window's surface needed? This is almost like hidden, except
7575 // it will sometimes be true a little earlier: when the token has
7576 // been shown, but is still waiting for its app transition to execute
7577 // before making its windows shown.
7578 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 // Have we told the window clients to hide themselves?
7581 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 // Last visibility state we reported to the app token.
7584 boolean reportedVisible;
7585
7586 // Set to true when the token has been removed from the window mgr.
7587 boolean removed;
7588
7589 // Have we been asked to have this token keep the screen frozen?
7590 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 boolean animating;
7593 Animation animation;
7594 boolean hasTransformation;
7595 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 // Offset to the window of all layers in the token, for use by
7598 // AppWindowToken animations.
7599 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 // Information about an application starting window if displayed.
7602 StartingData startingData;
7603 WindowState startingWindow;
7604 View startingView;
7605 boolean startingDisplayed;
7606 boolean startingMoved;
7607 boolean firstWindowDrawn;
7608
7609 AppWindowToken(IApplicationToken _token) {
7610 super(_token.asBinder(),
7611 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7612 appWindowToken = this;
7613 appToken = _token;
7614 }
Romain Guy06882f82009-06-10 13:36:04 -07007615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 public void setAnimation(Animation anim) {
7617 if (localLOGV) Log.v(
7618 TAG, "Setting animation in " + this + ": " + anim);
7619 animation = anim;
7620 animating = false;
7621 anim.restrictDuration(MAX_ANIMATION_DURATION);
7622 anim.scaleCurrentDuration(mTransitionAnimationScale);
7623 int zorder = anim.getZAdjustment();
7624 int adj = 0;
7625 if (zorder == Animation.ZORDER_TOP) {
7626 adj = TYPE_LAYER_OFFSET;
7627 } else if (zorder == Animation.ZORDER_BOTTOM) {
7628 adj = -TYPE_LAYER_OFFSET;
7629 }
Romain Guy06882f82009-06-10 13:36:04 -07007630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 if (animLayerAdjustment != adj) {
7632 animLayerAdjustment = adj;
7633 updateLayers();
7634 }
7635 }
Romain Guy06882f82009-06-10 13:36:04 -07007636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 public void setDummyAnimation() {
7638 if (animation == null) {
7639 if (localLOGV) Log.v(
7640 TAG, "Setting dummy animation in " + this);
7641 animation = sDummyAnimation;
7642 }
7643 }
7644
7645 public void clearAnimation() {
7646 if (animation != null) {
7647 animation = null;
7648 animating = true;
7649 }
7650 }
Romain Guy06882f82009-06-10 13:36:04 -07007651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652 void updateLayers() {
7653 final int N = allAppWindows.size();
7654 final int adj = animLayerAdjustment;
7655 for (int i=0; i<N; i++) {
7656 WindowState w = allAppWindows.get(i);
7657 w.mAnimLayer = w.mLayer + adj;
7658 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7659 + w.mAnimLayer);
7660 if (w == mInputMethodTarget) {
7661 setInputMethodAnimLayerAdjustment(adj);
7662 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007663 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007664 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 }
7667 }
Romain Guy06882f82009-06-10 13:36:04 -07007668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 void sendAppVisibilityToClients() {
7670 final int N = allAppWindows.size();
7671 for (int i=0; i<N; i++) {
7672 WindowState win = allAppWindows.get(i);
7673 if (win == startingWindow && clientHidden) {
7674 // Don't hide the starting window.
7675 continue;
7676 }
7677 try {
7678 if (DEBUG_VISIBILITY) Log.v(TAG,
7679 "Setting visibility of " + win + ": " + (!clientHidden));
7680 win.mClient.dispatchAppVisibility(!clientHidden);
7681 } catch (RemoteException e) {
7682 }
7683 }
7684 }
Romain Guy06882f82009-06-10 13:36:04 -07007685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 void showAllWindowsLocked() {
7687 final int NW = allAppWindows.size();
7688 for (int i=0; i<NW; i++) {
7689 WindowState w = allAppWindows.get(i);
7690 if (DEBUG_VISIBILITY) Log.v(TAG,
7691 "performing show on: " + w);
7692 w.performShowLocked();
7693 }
7694 }
Romain Guy06882f82009-06-10 13:36:04 -07007695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 // This must be called while inside a transaction.
7697 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7698 if (!mDisplayFrozen) {
7699 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 if (animation == sDummyAnimation) {
7702 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007703 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 // when it is really time to animate, this will be set to
7705 // a real animation and the next call will execute normally.
7706 return false;
7707 }
Romain Guy06882f82009-06-10 13:36:04 -07007708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7710 if (!animating) {
7711 if (DEBUG_ANIM) Log.v(
7712 TAG, "Starting animation in " + this +
7713 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7714 + " scale=" + mTransitionAnimationScale
7715 + " allDrawn=" + allDrawn + " animating=" + animating);
7716 animation.initialize(dw, dh, dw, dh);
7717 animation.setStartTime(currentTime);
7718 animating = true;
7719 }
7720 transformation.clear();
7721 final boolean more = animation.getTransformation(
7722 currentTime, transformation);
7723 if (DEBUG_ANIM) Log.v(
7724 TAG, "Stepped animation in " + this +
7725 ": more=" + more + ", xform=" + transformation);
7726 if (more) {
7727 // we're done!
7728 hasTransformation = true;
7729 return true;
7730 }
7731 if (DEBUG_ANIM) Log.v(
7732 TAG, "Finished animation in " + this +
7733 " @ " + currentTime);
7734 animation = null;
7735 }
7736 } else if (animation != null) {
7737 // If the display is frozen, and there is a pending animation,
7738 // clear it and make sure we run the cleanup code.
7739 animating = true;
7740 animation = null;
7741 }
7742
7743 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 if (!animating) {
7746 return false;
7747 }
7748
7749 clearAnimation();
7750 animating = false;
7751 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7752 moveInputMethodWindowsIfNeededLocked(true);
7753 }
Romain Guy06882f82009-06-10 13:36:04 -07007754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 if (DEBUG_ANIM) Log.v(
7756 TAG, "Animation done in " + this
7757 + ": reportedVisible=" + reportedVisible);
7758
7759 transformation.clear();
7760 if (animLayerAdjustment != 0) {
7761 animLayerAdjustment = 0;
7762 updateLayers();
7763 }
Romain Guy06882f82009-06-10 13:36:04 -07007764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 final int N = windows.size();
7766 for (int i=0; i<N; i++) {
7767 ((WindowState)windows.get(i)).finishExit();
7768 }
7769 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 return false;
7772 }
7773
7774 void updateReportedVisibilityLocked() {
7775 if (appToken == null) {
7776 return;
7777 }
Romain Guy06882f82009-06-10 13:36:04 -07007778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 int numInteresting = 0;
7780 int numVisible = 0;
7781 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7784 final int N = allAppWindows.size();
7785 for (int i=0; i<N; i++) {
7786 WindowState win = allAppWindows.get(i);
7787 if (win == startingWindow || win.mAppFreezing) {
7788 continue;
7789 }
7790 if (DEBUG_VISIBILITY) {
7791 Log.v(TAG, "Win " + win + ": isDisplayed="
7792 + win.isDisplayedLw()
7793 + ", isAnimating=" + win.isAnimating());
7794 if (!win.isDisplayedLw()) {
7795 Log.v(TAG, "Not displayed: s=" + win.mSurface
7796 + " pv=" + win.mPolicyVisibility
7797 + " dp=" + win.mDrawPending
7798 + " cdp=" + win.mCommitDrawPending
7799 + " ah=" + win.mAttachedHidden
7800 + " th="
7801 + (win.mAppToken != null
7802 ? win.mAppToken.hiddenRequested : false)
7803 + " a=" + win.mAnimating);
7804 }
7805 }
7806 numInteresting++;
7807 if (win.isDisplayedLw()) {
7808 if (!win.isAnimating()) {
7809 numVisible++;
7810 }
7811 nowGone = false;
7812 } else if (win.isAnimating()) {
7813 nowGone = false;
7814 }
7815 }
Romain Guy06882f82009-06-10 13:36:04 -07007816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7818 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7819 + numInteresting + " visible=" + numVisible);
7820 if (nowVisible != reportedVisible) {
7821 if (DEBUG_VISIBILITY) Log.v(
7822 TAG, "Visibility changed in " + this
7823 + ": vis=" + nowVisible);
7824 reportedVisible = nowVisible;
7825 Message m = mH.obtainMessage(
7826 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7827 nowVisible ? 1 : 0,
7828 nowGone ? 1 : 0,
7829 this);
7830 mH.sendMessage(m);
7831 }
7832 }
Romain Guy06882f82009-06-10 13:36:04 -07007833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 void dump(PrintWriter pw, String prefix) {
7835 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007836 if (appToken != null) {
7837 pw.print(prefix); pw.println("app=true");
7838 }
7839 if (allAppWindows.size() > 0) {
7840 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7841 }
7842 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007843 pw.print(" appFullscreen="); pw.println(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007844 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7845 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7846 pw.print(" clientHidden="); pw.print(clientHidden);
7847 pw.print(" willBeHidden="); pw.print(willBeHidden);
7848 pw.print(" reportedVisible="); pw.println(reportedVisible);
7849 if (paused || freezingScreen) {
7850 pw.print(prefix); pw.print("paused="); pw.print(paused);
7851 pw.print(" freezingScreen="); pw.println(freezingScreen);
7852 }
7853 if (numInterestingWindows != 0 || numDrawnWindows != 0
7854 || inPendingTransaction || allDrawn) {
7855 pw.print(prefix); pw.print("numInterestingWindows=");
7856 pw.print(numInterestingWindows);
7857 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7858 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7859 pw.print(" allDrawn="); pw.println(allDrawn);
7860 }
7861 if (animating || animation != null) {
7862 pw.print(prefix); pw.print("animating="); pw.print(animating);
7863 pw.print(" animation="); pw.println(animation);
7864 }
7865 if (animLayerAdjustment != 0) {
7866 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7867 }
7868 if (hasTransformation) {
7869 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7870 pw.print(" transformation="); transformation.printShortString(pw);
7871 pw.println();
7872 }
7873 if (startingData != null || removed || firstWindowDrawn) {
7874 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7875 pw.print(" removed="); pw.print(removed);
7876 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7877 }
7878 if (startingWindow != null || startingView != null
7879 || startingDisplayed || startingMoved) {
7880 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7881 pw.print(" startingView="); pw.print(startingView);
7882 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7883 pw.print(" startingMoved"); pw.println(startingMoved);
7884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 }
7886
7887 @Override
7888 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007889 if (stringName == null) {
7890 StringBuilder sb = new StringBuilder();
7891 sb.append("AppWindowToken{");
7892 sb.append(Integer.toHexString(System.identityHashCode(this)));
7893 sb.append(" token="); sb.append(token); sb.append('}');
7894 stringName = sb.toString();
7895 }
7896 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 }
7898 }
Romain Guy06882f82009-06-10 13:36:04 -07007899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007900 public static WindowManager.LayoutParams findAnimations(
7901 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007902 ArrayList<AppWindowToken> openingTokenList1,
7903 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007905
7906 // First, check if there is a compatible window in opening/closing
7907 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 WindowManager.LayoutParams animParams = null;
7909 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007910 animParams = findCompatibleWindowParams(openingTokenList1);
7911 if (animParams == null) {
7912 animParams = findCompatibleWindowParams(closingTokenList2);
7913 }
7914 if (animParams != null) {
7915 return animParams;
7916 }
7917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 //Log.i(TAG, "Looking for animations...");
7919 for (int i=order.size()-1; i>=0; i--) {
7920 AppWindowToken wtoken = order.get(i);
7921 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007922 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 int j = wtoken.windows.size();
7924 while (j > 0) {
7925 j--;
7926 WindowState win = wtoken.windows.get(j);
7927 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7928 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7929 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7930 //Log.i(TAG, "Found base or application window, done!");
7931 if (wtoken.appFullscreen) {
7932 return win.mAttrs;
7933 }
7934 if (animSrc < 2) {
7935 animParams = win.mAttrs;
7936 animSrc = 2;
7937 }
7938 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7939 //Log.i(TAG, "Found normal window, we may use this...");
7940 animParams = win.mAttrs;
7941 animSrc = 1;
7942 }
7943 }
7944 }
7945 }
Romain Guy06882f82009-06-10 13:36:04 -07007946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 return animParams;
7948 }
Romain Guy06882f82009-06-10 13:36:04 -07007949
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007950 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7951 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7952 AppWindowToken wtoken = tokenList.get(appCount);
7953 // Just checking one window is sufficient as all windows have the compatible flag
7954 // if the application is in compatibility mode.
7955 if (wtoken.windows.size() > 0) {
7956 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7957 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7958 return params;
7959 }
7960 }
7961 }
7962 return null;
7963 }
7964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 // -------------------------------------------------------------
7966 // DummyAnimation
7967 // -------------------------------------------------------------
7968
7969 // This is an animation that does nothing: it just immediately finishes
7970 // itself every time it is called. It is used as a stub animation in cases
7971 // where we want to synchronize multiple things that may be animating.
7972 static final class DummyAnimation extends Animation {
7973 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7974 return false;
7975 }
7976 }
7977 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007979 // -------------------------------------------------------------
7980 // Async Handler
7981 // -------------------------------------------------------------
7982
7983 static final class StartingData {
7984 final String pkg;
7985 final int theme;
7986 final CharSequence nonLocalizedLabel;
7987 final int labelRes;
7988 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007990 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7991 int _labelRes, int _icon) {
7992 pkg = _pkg;
7993 theme = _theme;
7994 nonLocalizedLabel = _nonLocalizedLabel;
7995 labelRes = _labelRes;
7996 icon = _icon;
7997 }
7998 }
7999
8000 private final class H extends Handler {
8001 public static final int REPORT_FOCUS_CHANGE = 2;
8002 public static final int REPORT_LOSING_FOCUS = 3;
8003 public static final int ANIMATE = 4;
8004 public static final int ADD_STARTING = 5;
8005 public static final int REMOVE_STARTING = 6;
8006 public static final int FINISHED_STARTING = 7;
8007 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8009 public static final int HOLD_SCREEN_CHANGED = 12;
8010 public static final int APP_TRANSITION_TIMEOUT = 13;
8011 public static final int PERSIST_ANIMATION_SCALE = 14;
8012 public static final int FORCE_GC = 15;
8013 public static final int ENABLE_SCREEN = 16;
8014 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008015 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 public H() {
8020 }
Romain Guy06882f82009-06-10 13:36:04 -07008021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 @Override
8023 public void handleMessage(Message msg) {
8024 switch (msg.what) {
8025 case REPORT_FOCUS_CHANGE: {
8026 WindowState lastFocus;
8027 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 synchronized(mWindowMap) {
8030 lastFocus = mLastFocus;
8031 newFocus = mCurrentFocus;
8032 if (lastFocus == newFocus) {
8033 // Focus is not changing, so nothing to do.
8034 return;
8035 }
8036 mLastFocus = newFocus;
8037 //Log.i(TAG, "Focus moving from " + lastFocus
8038 // + " to " + newFocus);
8039 if (newFocus != null && lastFocus != null
8040 && !newFocus.isDisplayedLw()) {
8041 //Log.i(TAG, "Delaying loss of focus...");
8042 mLosingFocus.add(lastFocus);
8043 lastFocus = null;
8044 }
8045 }
8046
8047 if (lastFocus != newFocus) {
8048 //System.out.println("Changing focus from " + lastFocus
8049 // + " to " + newFocus);
8050 if (newFocus != null) {
8051 try {
8052 //Log.i(TAG, "Gaining focus: " + newFocus);
8053 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8054 } catch (RemoteException e) {
8055 // Ignore if process has died.
8056 }
8057 }
8058
8059 if (lastFocus != null) {
8060 try {
8061 //Log.i(TAG, "Losing focus: " + lastFocus);
8062 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8063 } catch (RemoteException e) {
8064 // Ignore if process has died.
8065 }
8066 }
8067 }
8068 } break;
8069
8070 case REPORT_LOSING_FOCUS: {
8071 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 synchronized(mWindowMap) {
8074 losers = mLosingFocus;
8075 mLosingFocus = new ArrayList<WindowState>();
8076 }
8077
8078 final int N = losers.size();
8079 for (int i=0; i<N; i++) {
8080 try {
8081 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8082 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8083 } catch (RemoteException e) {
8084 // Ignore if process has died.
8085 }
8086 }
8087 } break;
8088
8089 case ANIMATE: {
8090 synchronized(mWindowMap) {
8091 mAnimationPending = false;
8092 performLayoutAndPlaceSurfacesLocked();
8093 }
8094 } break;
8095
8096 case ADD_STARTING: {
8097 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8098 final StartingData sd = wtoken.startingData;
8099
8100 if (sd == null) {
8101 // Animation has been canceled... do nothing.
8102 return;
8103 }
Romain Guy06882f82009-06-10 13:36:04 -07008104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8106 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008108 View view = null;
8109 try {
8110 view = mPolicy.addStartingWindow(
8111 wtoken.token, sd.pkg,
8112 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8113 sd.icon);
8114 } catch (Exception e) {
8115 Log.w(TAG, "Exception when adding starting window", e);
8116 }
8117
8118 if (view != null) {
8119 boolean abort = false;
8120
8121 synchronized(mWindowMap) {
8122 if (wtoken.removed || wtoken.startingData == null) {
8123 // If the window was successfully added, then
8124 // we need to remove it.
8125 if (wtoken.startingWindow != null) {
8126 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8127 "Aborted starting " + wtoken
8128 + ": removed=" + wtoken.removed
8129 + " startingData=" + wtoken.startingData);
8130 wtoken.startingWindow = null;
8131 wtoken.startingData = null;
8132 abort = true;
8133 }
8134 } else {
8135 wtoken.startingView = view;
8136 }
8137 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8138 "Added starting " + wtoken
8139 + ": startingWindow="
8140 + wtoken.startingWindow + " startingView="
8141 + wtoken.startingView);
8142 }
8143
8144 if (abort) {
8145 try {
8146 mPolicy.removeStartingWindow(wtoken.token, view);
8147 } catch (Exception e) {
8148 Log.w(TAG, "Exception when removing starting window", e);
8149 }
8150 }
8151 }
8152 } break;
8153
8154 case REMOVE_STARTING: {
8155 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8156 IBinder token = null;
8157 View view = null;
8158 synchronized (mWindowMap) {
8159 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8160 + wtoken + ": startingWindow="
8161 + wtoken.startingWindow + " startingView="
8162 + wtoken.startingView);
8163 if (wtoken.startingWindow != null) {
8164 view = wtoken.startingView;
8165 token = wtoken.token;
8166 wtoken.startingData = null;
8167 wtoken.startingView = null;
8168 wtoken.startingWindow = null;
8169 }
8170 }
8171 if (view != null) {
8172 try {
8173 mPolicy.removeStartingWindow(token, view);
8174 } catch (Exception e) {
8175 Log.w(TAG, "Exception when removing starting window", e);
8176 }
8177 }
8178 } break;
8179
8180 case FINISHED_STARTING: {
8181 IBinder token = null;
8182 View view = null;
8183 while (true) {
8184 synchronized (mWindowMap) {
8185 final int N = mFinishedStarting.size();
8186 if (N <= 0) {
8187 break;
8188 }
8189 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8190
8191 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8192 "Finished starting " + wtoken
8193 + ": startingWindow=" + wtoken.startingWindow
8194 + " startingView=" + wtoken.startingView);
8195
8196 if (wtoken.startingWindow == null) {
8197 continue;
8198 }
8199
8200 view = wtoken.startingView;
8201 token = wtoken.token;
8202 wtoken.startingData = null;
8203 wtoken.startingView = null;
8204 wtoken.startingWindow = null;
8205 }
8206
8207 try {
8208 mPolicy.removeStartingWindow(token, view);
8209 } catch (Exception e) {
8210 Log.w(TAG, "Exception when removing starting window", e);
8211 }
8212 }
8213 } break;
8214
8215 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8216 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8217
8218 boolean nowVisible = msg.arg1 != 0;
8219 boolean nowGone = msg.arg2 != 0;
8220
8221 try {
8222 if (DEBUG_VISIBILITY) Log.v(
8223 TAG, "Reporting visible in " + wtoken
8224 + " visible=" + nowVisible
8225 + " gone=" + nowGone);
8226 if (nowVisible) {
8227 wtoken.appToken.windowsVisible();
8228 } else {
8229 wtoken.appToken.windowsGone();
8230 }
8231 } catch (RemoteException ex) {
8232 }
8233 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 case WINDOW_FREEZE_TIMEOUT: {
8236 synchronized (mWindowMap) {
8237 Log.w(TAG, "Window freeze timeout expired.");
8238 int i = mWindows.size();
8239 while (i > 0) {
8240 i--;
8241 WindowState w = (WindowState)mWindows.get(i);
8242 if (w.mOrientationChanging) {
8243 w.mOrientationChanging = false;
8244 Log.w(TAG, "Force clearing orientation change: " + w);
8245 }
8246 }
8247 performLayoutAndPlaceSurfacesLocked();
8248 }
8249 break;
8250 }
Romain Guy06882f82009-06-10 13:36:04 -07008251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 case HOLD_SCREEN_CHANGED: {
8253 Session oldHold;
8254 Session newHold;
8255 synchronized (mWindowMap) {
8256 oldHold = mLastReportedHold;
8257 newHold = (Session)msg.obj;
8258 mLastReportedHold = newHold;
8259 }
Romain Guy06882f82009-06-10 13:36:04 -07008260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 if (oldHold != newHold) {
8262 try {
8263 if (oldHold != null) {
8264 mBatteryStats.noteStopWakelock(oldHold.mUid,
8265 "window",
8266 BatteryStats.WAKE_TYPE_WINDOW);
8267 }
8268 if (newHold != null) {
8269 mBatteryStats.noteStartWakelock(newHold.mUid,
8270 "window",
8271 BatteryStats.WAKE_TYPE_WINDOW);
8272 }
8273 } catch (RemoteException e) {
8274 }
8275 }
8276 break;
8277 }
Romain Guy06882f82009-06-10 13:36:04 -07008278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 case APP_TRANSITION_TIMEOUT: {
8280 synchronized (mWindowMap) {
8281 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8282 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8283 "*** APP TRANSITION TIMEOUT");
8284 mAppTransitionReady = true;
8285 mAppTransitionTimeout = true;
8286 performLayoutAndPlaceSurfacesLocked();
8287 }
8288 }
8289 break;
8290 }
Romain Guy06882f82009-06-10 13:36:04 -07008291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 case PERSIST_ANIMATION_SCALE: {
8293 Settings.System.putFloat(mContext.getContentResolver(),
8294 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8295 Settings.System.putFloat(mContext.getContentResolver(),
8296 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8297 break;
8298 }
Romain Guy06882f82009-06-10 13:36:04 -07008299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 case FORCE_GC: {
8301 synchronized(mWindowMap) {
8302 if (mAnimationPending) {
8303 // If we are animating, don't do the gc now but
8304 // delay a bit so we don't interrupt the animation.
8305 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8306 2000);
8307 return;
8308 }
8309 // If we are currently rotating the display, it will
8310 // schedule a new message when done.
8311 if (mDisplayFrozen) {
8312 return;
8313 }
8314 mFreezeGcPending = 0;
8315 }
8316 Runtime.getRuntime().gc();
8317 break;
8318 }
Romain Guy06882f82009-06-10 13:36:04 -07008319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 case ENABLE_SCREEN: {
8321 performEnableScreen();
8322 break;
8323 }
Romain Guy06882f82009-06-10 13:36:04 -07008324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 case APP_FREEZE_TIMEOUT: {
8326 synchronized (mWindowMap) {
8327 Log.w(TAG, "App freeze timeout expired.");
8328 int i = mAppTokens.size();
8329 while (i > 0) {
8330 i--;
8331 AppWindowToken tok = mAppTokens.get(i);
8332 if (tok.freezingScreen) {
8333 Log.w(TAG, "Force clearing freeze: " + tok);
8334 unsetAppFreezingScreenLocked(tok, true, true);
8335 }
8336 }
8337 }
8338 break;
8339 }
Romain Guy06882f82009-06-10 13:36:04 -07008340
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008341 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008342 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008343 sendNewConfiguration();
8344 }
8345 break;
8346 }
Romain Guy06882f82009-06-10 13:36:04 -07008347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 }
8349 }
8350 }
8351
8352 // -------------------------------------------------------------
8353 // IWindowManager API
8354 // -------------------------------------------------------------
8355
8356 public IWindowSession openSession(IInputMethodClient client,
8357 IInputContext inputContext) {
8358 if (client == null) throw new IllegalArgumentException("null client");
8359 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8360 return new Session(client, inputContext);
8361 }
8362
8363 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8364 synchronized (mWindowMap) {
8365 // The focus for the client is the window immediately below
8366 // where we would place the input method window.
8367 int idx = findDesiredInputMethodWindowIndexLocked(false);
8368 WindowState imFocus;
8369 if (idx > 0) {
8370 imFocus = (WindowState)mWindows.get(idx-1);
8371 if (imFocus != null) {
8372 if (imFocus.mSession.mClient != null &&
8373 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8374 return true;
8375 }
8376 }
8377 }
8378 }
8379 return false;
8380 }
Romain Guy06882f82009-06-10 13:36:04 -07008381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 // -------------------------------------------------------------
8383 // Internals
8384 // -------------------------------------------------------------
8385
8386 final WindowState windowForClientLocked(Session session, IWindow client) {
8387 return windowForClientLocked(session, client.asBinder());
8388 }
Romain Guy06882f82009-06-10 13:36:04 -07008389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 final WindowState windowForClientLocked(Session session, IBinder client) {
8391 WindowState win = mWindowMap.get(client);
8392 if (localLOGV) Log.v(
8393 TAG, "Looking up client " + client + ": " + win);
8394 if (win == null) {
8395 RuntimeException ex = new RuntimeException();
8396 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8397 return null;
8398 }
8399 if (session != null && win.mSession != session) {
8400 RuntimeException ex = new RuntimeException();
8401 Log.w(TAG, "Requested window " + client + " is in session " +
8402 win.mSession + ", not " + session, ex);
8403 return null;
8404 }
8405
8406 return win;
8407 }
8408
8409 private final void assignLayersLocked() {
8410 int N = mWindows.size();
8411 int curBaseLayer = 0;
8412 int curLayer = 0;
8413 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 for (i=0; i<N; i++) {
8416 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008417 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8418 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 curLayer += WINDOW_LAYER_MULTIPLIER;
8420 w.mLayer = curLayer;
8421 } else {
8422 curBaseLayer = curLayer = w.mBaseLayer;
8423 w.mLayer = curLayer;
8424 }
8425 if (w.mTargetAppToken != null) {
8426 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8427 } else if (w.mAppToken != null) {
8428 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8429 } else {
8430 w.mAnimLayer = w.mLayer;
8431 }
8432 if (w.mIsImWindow) {
8433 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008434 } else if (w.mIsWallpaper) {
8435 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 }
8437 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8438 + w.mAnimLayer);
8439 //System.out.println(
8440 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8441 }
8442 }
8443
8444 private boolean mInLayout = false;
8445 private final void performLayoutAndPlaceSurfacesLocked() {
8446 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008447 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 throw new RuntimeException("Recursive call!");
8449 }
8450 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8451 return;
8452 }
8453
8454 boolean recoveringMemory = false;
8455 if (mForceRemoves != null) {
8456 recoveringMemory = true;
8457 // Wait a little it for things to settle down, and off we go.
8458 for (int i=0; i<mForceRemoves.size(); i++) {
8459 WindowState ws = mForceRemoves.get(i);
8460 Log.i(TAG, "Force removing: " + ws);
8461 removeWindowInnerLocked(ws.mSession, ws);
8462 }
8463 mForceRemoves = null;
8464 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8465 Object tmp = new Object();
8466 synchronized (tmp) {
8467 try {
8468 tmp.wait(250);
8469 } catch (InterruptedException e) {
8470 }
8471 }
8472 }
Romain Guy06882f82009-06-10 13:36:04 -07008473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 mInLayout = true;
8475 try {
8476 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 int i = mPendingRemove.size()-1;
8479 if (i >= 0) {
8480 while (i >= 0) {
8481 WindowState w = mPendingRemove.get(i);
8482 removeWindowInnerLocked(w.mSession, w);
8483 i--;
8484 }
8485 mPendingRemove.clear();
8486
8487 mInLayout = false;
8488 assignLayersLocked();
8489 mLayoutNeeded = true;
8490 performLayoutAndPlaceSurfacesLocked();
8491
8492 } else {
8493 mInLayout = false;
8494 if (mLayoutNeeded) {
8495 requestAnimationLocked(0);
8496 }
8497 }
8498 } catch (RuntimeException e) {
8499 mInLayout = false;
8500 Log.e(TAG, "Unhandled exception while layout out windows", e);
8501 }
8502 }
8503
8504 private final void performLayoutLockedInner() {
8505 final int dw = mDisplay.getWidth();
8506 final int dh = mDisplay.getHeight();
8507
8508 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008509 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008510 int i;
8511
8512 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008513
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008514 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 mPolicy.beginLayoutLw(dw, dh);
8516
8517 // First perform layout of any root windows (not attached
8518 // to another window).
8519 int topAttached = -1;
8520 for (i = N-1; i >= 0; i--) {
8521 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008522
8523 // Don't do layout of a window if it is not visible, or
8524 // soon won't be visible, to avoid wasting time and funky
8525 // changes while a window is animating away.
8526 final AppWindowToken atoken = win.mAppToken;
8527 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008529 || win.mRootToken.hidden
8530 || (atoken != null && atoken.hiddenRequested)
8531 || !win.mPolicyVisibility
8532 || win.mAttachedHidden
8533 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534
8535 // If this view is GONE, then skip it -- keep the current
8536 // frame, and let the caller know so they can ignore it
8537 // if they want. (We do the normal layout for INVISIBLE
8538 // windows, since that means "perform layout as normal,
8539 // just don't display").
8540 if (!gone || !win.mHaveFrame) {
8541 if (!win.mLayoutAttached) {
8542 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8543 } else {
8544 if (topAttached < 0) topAttached = i;
8545 }
8546 }
8547 }
Romain Guy06882f82009-06-10 13:36:04 -07008548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 // Now perform layout of attached windows, which usually
8550 // depend on the position of the window they are attached to.
8551 // XXX does not deal with windows that are attached to windows
8552 // that are themselves attached.
8553 for (i = topAttached; i >= 0; i--) {
8554 WindowState win = (WindowState) mWindows.get(i);
8555
8556 // If this view is GONE, then skip it -- keep the current
8557 // frame, and let the caller know so they can ignore it
8558 // if they want. (We do the normal layout for INVISIBLE
8559 // windows, since that means "perform layout as normal,
8560 // just don't display").
8561 if (win.mLayoutAttached) {
8562 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8563 || !win.mHaveFrame) {
8564 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8565 }
8566 }
8567 }
8568
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008569 if (!mPolicy.finishLayoutLw()) {
8570 mLayoutNeeded = false;
8571 } else if (repeats > 2) {
8572 Log.w(TAG, "Layout repeat aborted after too many iterations");
8573 mLayoutNeeded = false;
8574 } else {
8575 repeats++;
8576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 }
8578 }
Romain Guy06882f82009-06-10 13:36:04 -07008579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008580 private final void performLayoutAndPlaceSurfacesLockedInner(
8581 boolean recoveringMemory) {
8582 final long currentTime = SystemClock.uptimeMillis();
8583 final int dw = mDisplay.getWidth();
8584 final int dh = mDisplay.getHeight();
8585
8586 final int N = mWindows.size();
8587 int i;
8588
8589 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 if (mFxSession == null) {
8593 mFxSession = new SurfaceSession();
8594 }
Romain Guy06882f82009-06-10 13:36:04 -07008595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8597
8598 // Initialize state of exiting tokens.
8599 for (i=mExitingTokens.size()-1; i>=0; i--) {
8600 mExitingTokens.get(i).hasVisible = false;
8601 }
8602
8603 // Initialize state of exiting applications.
8604 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8605 mExitingAppTokens.get(i).hasVisible = false;
8606 }
8607
8608 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 boolean orientationChangeComplete = true;
8610 Session holdScreen = null;
8611 float screenBrightness = -1;
8612 boolean focusDisplayed = false;
8613 boolean animating = false;
8614
8615 Surface.openTransaction();
8616 try {
8617 boolean restart;
8618
8619 do {
8620 final int transactionSequence = ++mTransactionSequence;
8621
8622 // Update animations of all applications, including those
8623 // associated with exiting/removed apps
8624 boolean tokensAnimating = false;
8625 final int NAT = mAppTokens.size();
8626 for (i=0; i<NAT; i++) {
8627 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8628 tokensAnimating = true;
8629 }
8630 }
8631 final int NEAT = mExitingAppTokens.size();
8632 for (i=0; i<NEAT; i++) {
8633 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8634 tokensAnimating = true;
8635 }
8636 }
8637
8638 animating = tokensAnimating;
8639 restart = false;
8640
8641 boolean tokenMayBeDrawn = false;
8642
8643 mPolicy.beginAnimationLw(dw, dh);
8644
8645 for (i=N-1; i>=0; i--) {
8646 WindowState w = (WindowState)mWindows.get(i);
8647
8648 final WindowManager.LayoutParams attrs = w.mAttrs;
8649
8650 if (w.mSurface != null) {
8651 // Execute animation.
8652 w.commitFinishDrawingLocked(currentTime);
8653 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8654 animating = true;
8655 //w.dump(" ");
8656 }
8657
8658 mPolicy.animatingWindowLw(w, attrs);
8659 }
8660
8661 final AppWindowToken atoken = w.mAppToken;
8662 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8663 if (atoken.lastTransactionSequence != transactionSequence) {
8664 atoken.lastTransactionSequence = transactionSequence;
8665 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8666 atoken.startingDisplayed = false;
8667 }
8668 if ((w.isOnScreen() || w.mAttrs.type
8669 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8670 && !w.mExiting && !w.mDestroying) {
8671 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8672 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8673 + w.isDisplayedLw()
8674 + ", isAnimating=" + w.isAnimating());
8675 if (!w.isDisplayedLw()) {
8676 Log.v(TAG, "Not displayed: s=" + w.mSurface
8677 + " pv=" + w.mPolicyVisibility
8678 + " dp=" + w.mDrawPending
8679 + " cdp=" + w.mCommitDrawPending
8680 + " ah=" + w.mAttachedHidden
8681 + " th=" + atoken.hiddenRequested
8682 + " a=" + w.mAnimating);
8683 }
8684 }
8685 if (w != atoken.startingWindow) {
8686 if (!atoken.freezingScreen || !w.mAppFreezing) {
8687 atoken.numInterestingWindows++;
8688 if (w.isDisplayedLw()) {
8689 atoken.numDrawnWindows++;
8690 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8691 "tokenMayBeDrawn: " + atoken
8692 + " freezingScreen=" + atoken.freezingScreen
8693 + " mAppFreezing=" + w.mAppFreezing);
8694 tokenMayBeDrawn = true;
8695 }
8696 }
8697 } else if (w.isDisplayedLw()) {
8698 atoken.startingDisplayed = true;
8699 }
8700 }
8701 } else if (w.mReadyToShow) {
8702 w.performShowLocked();
8703 }
8704 }
8705
8706 if (mPolicy.finishAnimationLw()) {
8707 restart = true;
8708 }
8709
8710 if (tokenMayBeDrawn) {
8711 // See if any windows have been drawn, so they (and others
8712 // associated with them) can now be shown.
8713 final int NT = mTokenList.size();
8714 for (i=0; i<NT; i++) {
8715 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8716 if (wtoken == null) {
8717 continue;
8718 }
8719 if (wtoken.freezingScreen) {
8720 int numInteresting = wtoken.numInterestingWindows;
8721 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8722 if (DEBUG_VISIBILITY) Log.v(TAG,
8723 "allDrawn: " + wtoken
8724 + " interesting=" + numInteresting
8725 + " drawn=" + wtoken.numDrawnWindows);
8726 wtoken.showAllWindowsLocked();
8727 unsetAppFreezingScreenLocked(wtoken, false, true);
8728 orientationChangeComplete = true;
8729 }
8730 } else if (!wtoken.allDrawn) {
8731 int numInteresting = wtoken.numInterestingWindows;
8732 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8733 if (DEBUG_VISIBILITY) Log.v(TAG,
8734 "allDrawn: " + wtoken
8735 + " interesting=" + numInteresting
8736 + " drawn=" + wtoken.numDrawnWindows);
8737 wtoken.allDrawn = true;
8738 restart = true;
8739
8740 // We can now show all of the drawn windows!
8741 if (!mOpeningApps.contains(wtoken)) {
8742 wtoken.showAllWindowsLocked();
8743 }
8744 }
8745 }
8746 }
8747 }
8748
8749 // If we are ready to perform an app transition, check through
8750 // all of the app tokens to be shown and see if they are ready
8751 // to go.
8752 if (mAppTransitionReady) {
8753 int NN = mOpeningApps.size();
8754 boolean goodToGo = true;
8755 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8756 "Checking " + NN + " opening apps (frozen="
8757 + mDisplayFrozen + " timeout="
8758 + mAppTransitionTimeout + ")...");
8759 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8760 // If the display isn't frozen, wait to do anything until
8761 // all of the apps are ready. Otherwise just go because
8762 // we'll unfreeze the display when everyone is ready.
8763 for (i=0; i<NN && goodToGo; i++) {
8764 AppWindowToken wtoken = mOpeningApps.get(i);
8765 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8766 "Check opening app" + wtoken + ": allDrawn="
8767 + wtoken.allDrawn + " startingDisplayed="
8768 + wtoken.startingDisplayed);
8769 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8770 && !wtoken.startingMoved) {
8771 goodToGo = false;
8772 }
8773 }
8774 }
8775 if (goodToGo) {
8776 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8777 int transit = mNextAppTransition;
8778 if (mSkipAppTransitionAnimation) {
8779 transit = WindowManagerPolicy.TRANSIT_NONE;
8780 }
8781 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8782 mAppTransitionReady = false;
8783 mAppTransitionTimeout = false;
8784 mStartingIconInTransition = false;
8785 mSkipAppTransitionAnimation = false;
8786
8787 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8788
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008789 adjustWallpaperWindowsLocked();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008790 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008791 "New wallpaper target=" + mWallpaperTarget
8792 + ", lower target=" + mLowerWallpaperTarget
8793 + ", upper target=" + mUpperWallpaperTarget);
8794 if (mLowerWallpaperTarget != null) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008795 // Need to determine if both the closing and
8796 // opening app token sets are wallpaper targets,
8797 // in which case special animations are needed
8798 // (since the wallpaper needs to stay static
8799 // behind them).
8800 int found = 0;
8801 NN = mOpeningApps.size();
8802 for (i=0; i<NN; i++) {
8803 AppWindowToken wtoken = mOpeningApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008804 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008805 found |= 1;
8806 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008807 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008808 found |= 1;
8809 }
8810 }
8811 NN = mClosingApps.size();
8812 for (i=0; i<NN; i++) {
8813 AppWindowToken wtoken = mClosingApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008814 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008815 found |= 2;
8816 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008817 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008818 found |= 2;
8819 }
8820 }
8821
8822 if (found == 3) {
8823 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8824 "Wallpaper animation!");
8825 switch (transit) {
8826 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
8827 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
8828 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
8829 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN;
8830 break;
8831 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
8832 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
8833 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
8834 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE;
8835 break;
8836 }
8837 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8838 "New transit: " + transit);
8839 }
8840 }
8841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 // We need to figure out which animation to use...
8843 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8844 mOpeningApps, mClosingApps);
8845
8846 NN = mOpeningApps.size();
8847 for (i=0; i<NN; i++) {
8848 AppWindowToken wtoken = mOpeningApps.get(i);
8849 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8850 "Now opening app" + wtoken);
8851 wtoken.reportedVisible = false;
8852 wtoken.inPendingTransaction = false;
8853 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8854 wtoken.updateReportedVisibilityLocked();
8855 wtoken.showAllWindowsLocked();
8856 }
8857 NN = mClosingApps.size();
8858 for (i=0; i<NN; i++) {
8859 AppWindowToken wtoken = mClosingApps.get(i);
8860 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8861 "Now closing app" + wtoken);
8862 wtoken.inPendingTransaction = false;
8863 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8864 wtoken.updateReportedVisibilityLocked();
8865 // Force the allDrawn flag, because we want to start
8866 // this guy's animations regardless of whether it's
8867 // gotten drawn.
8868 wtoken.allDrawn = true;
8869 }
8870
8871 mOpeningApps.clear();
8872 mClosingApps.clear();
8873
8874 // This has changed the visibility of windows, so perform
8875 // a new layout to get them all up-to-date.
8876 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008877 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8878 assignLayersLocked();
8879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 performLayoutLockedInner();
8881 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8882
8883 restart = true;
8884 }
8885 }
8886 } while (restart);
8887
8888 // THIRD LOOP: Update the surfaces of all windows.
8889
8890 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8891
8892 boolean obscured = false;
8893 boolean blurring = false;
8894 boolean dimming = false;
8895 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008896 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008897 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008898
8899 for (i=N-1; i>=0; i--) {
8900 WindowState w = (WindowState)mWindows.get(i);
8901
8902 boolean displayed = false;
8903 final WindowManager.LayoutParams attrs = w.mAttrs;
8904 final int attrFlags = attrs.flags;
8905
8906 if (w.mSurface != null) {
8907 w.computeShownFrameLocked();
8908 if (localLOGV) Log.v(
8909 TAG, "Placing surface #" + i + " " + w.mSurface
8910 + ": new=" + w.mShownFrame + ", old="
8911 + w.mLastShownFrame);
8912
8913 boolean resize;
8914 int width, height;
8915 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8916 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8917 w.mLastRequestedHeight != w.mRequestedHeight;
8918 // for a scaled surface, we just want to use
8919 // the requested size.
8920 width = w.mRequestedWidth;
8921 height = w.mRequestedHeight;
8922 w.mLastRequestedWidth = width;
8923 w.mLastRequestedHeight = height;
8924 w.mLastShownFrame.set(w.mShownFrame);
8925 try {
8926 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8927 } catch (RuntimeException e) {
8928 Log.w(TAG, "Error positioning surface in " + w, e);
8929 if (!recoveringMemory) {
8930 reclaimSomeSurfaceMemoryLocked(w, "position");
8931 }
8932 }
8933 } else {
8934 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8935 width = w.mShownFrame.width();
8936 height = w.mShownFrame.height();
8937 w.mLastShownFrame.set(w.mShownFrame);
8938 if (resize) {
8939 if (SHOW_TRANSACTIONS) Log.i(
8940 TAG, " SURFACE " + w.mSurface + ": ("
8941 + w.mShownFrame.left + ","
8942 + w.mShownFrame.top + ") ("
8943 + w.mShownFrame.width() + "x"
8944 + w.mShownFrame.height() + ")");
8945 }
8946 }
8947
8948 if (resize) {
8949 if (width < 1) width = 1;
8950 if (height < 1) height = 1;
8951 if (w.mSurface != null) {
8952 try {
8953 w.mSurface.setSize(width, height);
8954 w.mSurface.setPosition(w.mShownFrame.left,
8955 w.mShownFrame.top);
8956 } catch (RuntimeException e) {
8957 // If something goes wrong with the surface (such
8958 // as running out of memory), don't take down the
8959 // entire system.
8960 Log.e(TAG, "Failure updating surface of " + w
8961 + "size=(" + width + "x" + height
8962 + "), pos=(" + w.mShownFrame.left
8963 + "," + w.mShownFrame.top + ")", e);
8964 if (!recoveringMemory) {
8965 reclaimSomeSurfaceMemoryLocked(w, "size");
8966 }
8967 }
8968 }
8969 }
8970 if (!w.mAppFreezing) {
8971 w.mContentInsetsChanged =
8972 !w.mLastContentInsets.equals(w.mContentInsets);
8973 w.mVisibleInsetsChanged =
8974 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008975 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 || w.mContentInsetsChanged
8977 || w.mVisibleInsetsChanged) {
8978 w.mLastFrame.set(w.mFrame);
8979 w.mLastContentInsets.set(w.mContentInsets);
8980 w.mLastVisibleInsets.set(w.mVisibleInsets);
8981 // If the orientation is changing, then we need to
8982 // hold off on unfreezing the display until this
8983 // window has been redrawn; to do that, we need
8984 // to go through the process of getting informed
8985 // by the application when it has finished drawing.
8986 if (w.mOrientationChanging) {
8987 if (DEBUG_ORIENTATION) Log.v(TAG,
8988 "Orientation start waiting for draw in "
8989 + w + ", surface " + w.mSurface);
8990 w.mDrawPending = true;
8991 w.mCommitDrawPending = false;
8992 w.mReadyToShow = false;
8993 if (w.mAppToken != null) {
8994 w.mAppToken.allDrawn = false;
8995 }
8996 }
Romain Guy06882f82009-06-10 13:36:04 -07008997 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 "Resizing window " + w + " to " + w.mFrame);
8999 mResizingWindows.add(w);
9000 } else if (w.mOrientationChanging) {
9001 if (!w.mDrawPending && !w.mCommitDrawPending) {
9002 if (DEBUG_ORIENTATION) Log.v(TAG,
9003 "Orientation not waiting for draw in "
9004 + w + ", surface " + w.mSurface);
9005 w.mOrientationChanging = false;
9006 }
9007 }
9008 }
9009
9010 if (w.mAttachedHidden) {
9011 if (!w.mLastHidden) {
9012 //dump();
9013 w.mLastHidden = true;
9014 if (SHOW_TRANSACTIONS) Log.i(
9015 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9016 if (w.mSurface != null) {
9017 try {
9018 w.mSurface.hide();
9019 } catch (RuntimeException e) {
9020 Log.w(TAG, "Exception hiding surface in " + w);
9021 }
9022 }
9023 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9024 }
9025 // If we are waiting for this window to handle an
9026 // orientation change, well, it is hidden, so
9027 // doesn't really matter. Note that this does
9028 // introduce a potential glitch if the window
9029 // becomes unhidden before it has drawn for the
9030 // new orientation.
9031 if (w.mOrientationChanging) {
9032 w.mOrientationChanging = false;
9033 if (DEBUG_ORIENTATION) Log.v(TAG,
9034 "Orientation change skips hidden " + w);
9035 }
9036 } else if (!w.isReadyForDisplay()) {
9037 if (!w.mLastHidden) {
9038 //dump();
9039 w.mLastHidden = true;
9040 if (SHOW_TRANSACTIONS) Log.i(
9041 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9042 if (w.mSurface != null) {
9043 try {
9044 w.mSurface.hide();
9045 } catch (RuntimeException e) {
9046 Log.w(TAG, "Exception exception hiding surface in " + w);
9047 }
9048 }
9049 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9050 }
9051 // If we are waiting for this window to handle an
9052 // orientation change, well, it is hidden, so
9053 // doesn't really matter. Note that this does
9054 // introduce a potential glitch if the window
9055 // becomes unhidden before it has drawn for the
9056 // new orientation.
9057 if (w.mOrientationChanging) {
9058 w.mOrientationChanging = false;
9059 if (DEBUG_ORIENTATION) Log.v(TAG,
9060 "Orientation change skips hidden " + w);
9061 }
9062 } else if (w.mLastLayer != w.mAnimLayer
9063 || w.mLastAlpha != w.mShownAlpha
9064 || w.mLastDsDx != w.mDsDx
9065 || w.mLastDtDx != w.mDtDx
9066 || w.mLastDsDy != w.mDsDy
9067 || w.mLastDtDy != w.mDtDy
9068 || w.mLastHScale != w.mHScale
9069 || w.mLastVScale != w.mVScale
9070 || w.mLastHidden) {
9071 displayed = true;
9072 w.mLastAlpha = w.mShownAlpha;
9073 w.mLastLayer = w.mAnimLayer;
9074 w.mLastDsDx = w.mDsDx;
9075 w.mLastDtDx = w.mDtDx;
9076 w.mLastDsDy = w.mDsDy;
9077 w.mLastDtDy = w.mDtDy;
9078 w.mLastHScale = w.mHScale;
9079 w.mLastVScale = w.mVScale;
9080 if (SHOW_TRANSACTIONS) Log.i(
9081 TAG, " SURFACE " + w.mSurface + ": alpha="
9082 + w.mShownAlpha + " layer=" + w.mAnimLayer);
9083 if (w.mSurface != null) {
9084 try {
9085 w.mSurface.setAlpha(w.mShownAlpha);
9086 w.mSurface.setLayer(w.mAnimLayer);
9087 w.mSurface.setMatrix(
9088 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9089 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9090 } catch (RuntimeException e) {
9091 Log.w(TAG, "Error updating surface in " + w, e);
9092 if (!recoveringMemory) {
9093 reclaimSomeSurfaceMemoryLocked(w, "update");
9094 }
9095 }
9096 }
9097
9098 if (w.mLastHidden && !w.mDrawPending
9099 && !w.mCommitDrawPending
9100 && !w.mReadyToShow) {
9101 if (SHOW_TRANSACTIONS) Log.i(
9102 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9103 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9104 + " during relayout");
9105 if (showSurfaceRobustlyLocked(w)) {
9106 w.mHasDrawn = true;
9107 w.mLastHidden = false;
9108 } else {
9109 w.mOrientationChanging = false;
9110 }
9111 }
9112 if (w.mSurface != null) {
9113 w.mToken.hasVisible = true;
9114 }
9115 } else {
9116 displayed = true;
9117 }
9118
9119 if (displayed) {
9120 if (!covered) {
9121 if (attrs.width == LayoutParams.FILL_PARENT
9122 && attrs.height == LayoutParams.FILL_PARENT) {
9123 covered = true;
9124 }
9125 }
9126 if (w.mOrientationChanging) {
9127 if (w.mDrawPending || w.mCommitDrawPending) {
9128 orientationChangeComplete = false;
9129 if (DEBUG_ORIENTATION) Log.v(TAG,
9130 "Orientation continue waiting for draw in " + w);
9131 } else {
9132 w.mOrientationChanging = false;
9133 if (DEBUG_ORIENTATION) Log.v(TAG,
9134 "Orientation change complete in " + w);
9135 }
9136 }
9137 w.mToken.hasVisible = true;
9138 }
9139 } else if (w.mOrientationChanging) {
9140 if (DEBUG_ORIENTATION) Log.v(TAG,
9141 "Orientation change skips hidden " + w);
9142 w.mOrientationChanging = false;
9143 }
9144
9145 final boolean canBeSeen = w.isDisplayedLw();
9146
9147 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9148 focusDisplayed = true;
9149 }
9150
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009151 final boolean obscuredChanged = w.mObscured != obscured;
9152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009154 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155 if (w.mSurface != null) {
9156 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9157 holdScreen = w.mSession;
9158 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009159 if (!syswin && w.mAttrs.screenBrightness >= 0
9160 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 screenBrightness = w.mAttrs.screenBrightness;
9162 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009163 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9164 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9165 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9166 syswin = true;
9167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009169
9170 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009171 if ((opaqueDrawn && w.isFullscreen(dw, dh))
9172 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 // This window completely covers everything behind it,
9174 // so we want to leave all of them as unblurred (for
9175 // performance reasons).
9176 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009177 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9178 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009179 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009180 obscured = true;
9181 if (mBackgroundFillerSurface == null) {
9182 try {
9183 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9184 0, dw, dh,
9185 PixelFormat.OPAQUE,
9186 Surface.FX_SURFACE_NORMAL);
9187 } catch (Exception e) {
9188 Log.e(TAG, "Exception creating filler surface", e);
9189 }
9190 }
9191 try {
9192 mBackgroundFillerSurface.setPosition(0, 0);
9193 mBackgroundFillerSurface.setSize(dw, dh);
9194 // Using the same layer as Dim because they will never be shown at the
9195 // same time.
9196 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9197 mBackgroundFillerSurface.show();
9198 } catch (RuntimeException e) {
9199 Log.e(TAG, "Exception showing filler surface");
9200 }
9201 backgroundFillerShown = true;
9202 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203 } else if (canBeSeen && !obscured &&
9204 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9205 if (localLOGV) Log.v(TAG, "Win " + w
9206 + ": blurring=" + blurring
9207 + " obscured=" + obscured
9208 + " displayed=" + displayed);
9209 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9210 if (!dimming) {
9211 //Log.i(TAG, "DIM BEHIND: " + w);
9212 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009213 if (mDimAnimator == null) {
9214 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009216 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009218 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 }
9220 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9221 if (!blurring) {
9222 //Log.i(TAG, "BLUR BEHIND: " + w);
9223 blurring = true;
9224 mBlurShown = true;
9225 if (mBlurSurface == null) {
9226 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9227 + mBlurSurface + ": CREATE");
9228 try {
Romain Guy06882f82009-06-10 13:36:04 -07009229 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 -1, 16, 16,
9231 PixelFormat.OPAQUE,
9232 Surface.FX_SURFACE_BLUR);
9233 } catch (Exception e) {
9234 Log.e(TAG, "Exception creating Blur surface", e);
9235 }
9236 }
9237 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9238 + mBlurSurface + ": SHOW pos=(0,0) (" +
9239 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9240 if (mBlurSurface != null) {
9241 mBlurSurface.setPosition(0, 0);
9242 mBlurSurface.setSize(dw, dh);
9243 try {
9244 mBlurSurface.show();
9245 } catch (RuntimeException e) {
9246 Log.w(TAG, "Failure showing blur surface", e);
9247 }
9248 }
9249 }
9250 mBlurSurface.setLayer(w.mAnimLayer-2);
9251 }
9252 }
9253 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009254
9255 if (obscuredChanged && mWallpaperTarget == w) {
9256 // This is the wallpaper target and its obscured state
9257 // changed... make sure the current wallaper's visibility
9258 // has been updated accordingly.
9259 updateWallpaperVisibilityLocked();
9260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009262
9263 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9264 mBackgroundFillerShown = false;
9265 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9266 try {
9267 mBackgroundFillerSurface.hide();
9268 } catch (RuntimeException e) {
9269 Log.e(TAG, "Exception hiding filler surface", e);
9270 }
9271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009273 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9274 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 }
Romain Guy06882f82009-06-10 13:36:04 -07009276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 if (!blurring && mBlurShown) {
9278 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9279 + ": HIDE");
9280 try {
9281 mBlurSurface.hide();
9282 } catch (IllegalArgumentException e) {
9283 Log.w(TAG, "Illegal argument exception hiding blur surface");
9284 }
9285 mBlurShown = false;
9286 }
9287
9288 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9289 } catch (RuntimeException e) {
9290 Log.e(TAG, "Unhandled exception in Window Manager", e);
9291 }
9292
9293 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9296 "With display frozen, orientationChangeComplete="
9297 + orientationChangeComplete);
9298 if (orientationChangeComplete) {
9299 if (mWindowsFreezingScreen) {
9300 mWindowsFreezingScreen = false;
9301 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9302 }
9303 if (mAppsFreezingScreen == 0) {
9304 stopFreezingDisplayLocked();
9305 }
9306 }
Romain Guy06882f82009-06-10 13:36:04 -07009307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009308 i = mResizingWindows.size();
9309 if (i > 0) {
9310 do {
9311 i--;
9312 WindowState win = mResizingWindows.get(i);
9313 try {
9314 win.mClient.resized(win.mFrame.width(),
9315 win.mFrame.height(), win.mLastContentInsets,
9316 win.mLastVisibleInsets, win.mDrawPending);
9317 win.mContentInsetsChanged = false;
9318 win.mVisibleInsetsChanged = false;
9319 } catch (RemoteException e) {
9320 win.mOrientationChanging = false;
9321 }
9322 } while (i > 0);
9323 mResizingWindows.clear();
9324 }
Romain Guy06882f82009-06-10 13:36:04 -07009325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009327 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 i = mDestroySurface.size();
9329 if (i > 0) {
9330 do {
9331 i--;
9332 WindowState win = mDestroySurface.get(i);
9333 win.mDestroying = false;
9334 if (mInputMethodWindow == win) {
9335 mInputMethodWindow = null;
9336 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009337 if (win == mWallpaperTarget) {
9338 wallpaperDestroyed = true;
9339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 win.destroySurfaceLocked();
9341 } while (i > 0);
9342 mDestroySurface.clear();
9343 }
9344
9345 // Time to remove any exiting tokens?
9346 for (i=mExitingTokens.size()-1; i>=0; i--) {
9347 WindowToken token = mExitingTokens.get(i);
9348 if (!token.hasVisible) {
9349 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009350 if (token.windowType == TYPE_WALLPAPER) {
9351 mWallpaperTokens.remove(token);
9352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009353 }
9354 }
9355
9356 // Time to remove any exiting applications?
9357 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9358 AppWindowToken token = mExitingAppTokens.get(i);
9359 if (!token.hasVisible && !mClosingApps.contains(token)) {
9360 mAppTokens.remove(token);
9361 mExitingAppTokens.remove(i);
9362 }
9363 }
9364
9365 if (focusDisplayed) {
9366 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9367 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009368 if (wallpaperDestroyed) {
9369 wallpaperDestroyed = adjustWallpaperWindowsLocked();
9370 }
9371 if (wallpaperDestroyed) {
9372 requestAnimationLocked(0);
9373 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9375 }
9376 mQueue.setHoldScreenLocked(holdScreen != null);
9377 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9378 mPowerManager.setScreenBrightnessOverride(-1);
9379 } else {
9380 mPowerManager.setScreenBrightnessOverride((int)
9381 (screenBrightness * Power.BRIGHTNESS_ON));
9382 }
9383 if (holdScreen != mHoldingScreenOn) {
9384 mHoldingScreenOn = holdScreen;
9385 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9386 mH.sendMessage(m);
9387 }
9388 }
9389
9390 void requestAnimationLocked(long delay) {
9391 if (!mAnimationPending) {
9392 mAnimationPending = true;
9393 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9394 }
9395 }
Romain Guy06882f82009-06-10 13:36:04 -07009396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 /**
9398 * Have the surface flinger show a surface, robustly dealing with
9399 * error conditions. In particular, if there is not enough memory
9400 * to show the surface, then we will try to get rid of other surfaces
9401 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009403 * @return Returns true if the surface was successfully shown.
9404 */
9405 boolean showSurfaceRobustlyLocked(WindowState win) {
9406 try {
9407 if (win.mSurface != null) {
9408 win.mSurface.show();
9409 }
9410 return true;
9411 } catch (RuntimeException e) {
9412 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9413 }
Romain Guy06882f82009-06-10 13:36:04 -07009414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 return false;
9418 }
Romain Guy06882f82009-06-10 13:36:04 -07009419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9421 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009423 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9424 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 if (mForceRemoves == null) {
9427 mForceRemoves = new ArrayList<WindowState>();
9428 }
Romain Guy06882f82009-06-10 13:36:04 -07009429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 long callingIdentity = Binder.clearCallingIdentity();
9431 try {
9432 // There was some problem... first, do a sanity check of the
9433 // window list to make sure we haven't left any dangling surfaces
9434 // around.
9435 int N = mWindows.size();
9436 boolean leakedSurface = false;
9437 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9438 for (int i=0; i<N; i++) {
9439 WindowState ws = (WindowState)mWindows.get(i);
9440 if (ws.mSurface != null) {
9441 if (!mSessions.contains(ws.mSession)) {
9442 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9443 + ws + " surface=" + ws.mSurface
9444 + " token=" + win.mToken
9445 + " pid=" + ws.mSession.mPid
9446 + " uid=" + ws.mSession.mUid);
9447 ws.mSurface.clear();
9448 ws.mSurface = null;
9449 mForceRemoves.add(ws);
9450 i--;
9451 N--;
9452 leakedSurface = true;
9453 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9454 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9455 + ws + " surface=" + ws.mSurface
9456 + " token=" + win.mAppToken);
9457 ws.mSurface.clear();
9458 ws.mSurface = null;
9459 leakedSurface = true;
9460 }
9461 }
9462 }
Romain Guy06882f82009-06-10 13:36:04 -07009463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009464 boolean killedApps = false;
9465 if (!leakedSurface) {
9466 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9467 SparseIntArray pidCandidates = new SparseIntArray();
9468 for (int i=0; i<N; i++) {
9469 WindowState ws = (WindowState)mWindows.get(i);
9470 if (ws.mSurface != null) {
9471 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9472 }
9473 }
9474 if (pidCandidates.size() > 0) {
9475 int[] pids = new int[pidCandidates.size()];
9476 for (int i=0; i<pids.length; i++) {
9477 pids[i] = pidCandidates.keyAt(i);
9478 }
9479 try {
9480 if (mActivityManager.killPidsForMemory(pids)) {
9481 killedApps = true;
9482 }
9483 } catch (RemoteException e) {
9484 }
9485 }
9486 }
Romain Guy06882f82009-06-10 13:36:04 -07009487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009488 if (leakedSurface || killedApps) {
9489 // We managed to reclaim some memory, so get rid of the trouble
9490 // surface and ask the app to request another one.
9491 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9492 if (surface != null) {
9493 surface.clear();
9494 win.mSurface = null;
9495 }
Romain Guy06882f82009-06-10 13:36:04 -07009496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009497 try {
9498 win.mClient.dispatchGetNewSurface();
9499 } catch (RemoteException e) {
9500 }
9501 }
9502 } finally {
9503 Binder.restoreCallingIdentity(callingIdentity);
9504 }
9505 }
Romain Guy06882f82009-06-10 13:36:04 -07009506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 private boolean updateFocusedWindowLocked(int mode) {
9508 WindowState newFocus = computeFocusedWindowLocked();
9509 if (mCurrentFocus != newFocus) {
9510 // This check makes sure that we don't already have the focus
9511 // change message pending.
9512 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9513 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9514 if (localLOGV) Log.v(
9515 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9516 final WindowState oldFocus = mCurrentFocus;
9517 mCurrentFocus = newFocus;
9518 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520 final WindowState imWindow = mInputMethodWindow;
9521 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009522 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009523 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009524 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9525 mLayoutNeeded = true;
9526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9528 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009529 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9530 // Client will do the layout, but we need to assign layers
9531 // for handleNewWindowLocked() below.
9532 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 }
9534 }
Romain Guy06882f82009-06-10 13:36:04 -07009535
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009536 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9537 mKeyWaiter.handleNewWindowLocked(newFocus);
9538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 return true;
9540 }
9541 return false;
9542 }
9543
9544 private WindowState computeFocusedWindowLocked() {
9545 WindowState result = null;
9546 WindowState win;
9547
9548 int i = mWindows.size() - 1;
9549 int nextAppIndex = mAppTokens.size()-1;
9550 WindowToken nextApp = nextAppIndex >= 0
9551 ? mAppTokens.get(nextAppIndex) : null;
9552
9553 while (i >= 0) {
9554 win = (WindowState)mWindows.get(i);
9555
9556 if (localLOGV || DEBUG_FOCUS) Log.v(
9557 TAG, "Looking for focus: " + i
9558 + " = " + win
9559 + ", flags=" + win.mAttrs.flags
9560 + ", canReceive=" + win.canReceiveKeys());
9561
9562 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 // If this window's application has been removed, just skip it.
9565 if (thisApp != null && thisApp.removed) {
9566 i--;
9567 continue;
9568 }
Romain Guy06882f82009-06-10 13:36:04 -07009569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570 // If there is a focused app, don't allow focus to go to any
9571 // windows below it. If this is an application window, step
9572 // through the app tokens until we find its app.
9573 if (thisApp != null && nextApp != null && thisApp != nextApp
9574 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9575 int origAppIndex = nextAppIndex;
9576 while (nextAppIndex > 0) {
9577 if (nextApp == mFocusedApp) {
9578 // Whoops, we are below the focused app... no focus
9579 // for you!
9580 if (localLOGV || DEBUG_FOCUS) Log.v(
9581 TAG, "Reached focused app: " + mFocusedApp);
9582 return null;
9583 }
9584 nextAppIndex--;
9585 nextApp = mAppTokens.get(nextAppIndex);
9586 if (nextApp == thisApp) {
9587 break;
9588 }
9589 }
9590 if (thisApp != nextApp) {
9591 // Uh oh, the app token doesn't exist! This shouldn't
9592 // happen, but if it does we can get totally hosed...
9593 // so restart at the original app.
9594 nextAppIndex = origAppIndex;
9595 nextApp = mAppTokens.get(nextAppIndex);
9596 }
9597 }
9598
9599 // Dispatch to this window if it is wants key events.
9600 if (win.canReceiveKeys()) {
9601 if (DEBUG_FOCUS) Log.v(
9602 TAG, "Found focus @ " + i + " = " + win);
9603 result = win;
9604 break;
9605 }
9606
9607 i--;
9608 }
9609
9610 return result;
9611 }
9612
9613 private void startFreezingDisplayLocked() {
9614 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009615 // Freezing the display also suspends key event delivery, to
9616 // keep events from going astray while the display is reconfigured.
9617 // If someone has changed orientation again while the screen is
9618 // still frozen, the events will continue to be blocked while the
9619 // successive orientation change is processed. To prevent spurious
9620 // ANRs, we reset the event dispatch timeout in this case.
9621 synchronized (mKeyWaiter) {
9622 mKeyWaiter.mWasFrozen = true;
9623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009624 return;
9625 }
Romain Guy06882f82009-06-10 13:36:04 -07009626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 long now = SystemClock.uptimeMillis();
9630 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9631 if (mFreezeGcPending != 0) {
9632 if (now > (mFreezeGcPending+1000)) {
9633 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9634 mH.removeMessages(H.FORCE_GC);
9635 Runtime.getRuntime().gc();
9636 mFreezeGcPending = now;
9637 }
9638 } else {
9639 mFreezeGcPending = now;
9640 }
Romain Guy06882f82009-06-10 13:36:04 -07009641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 mDisplayFrozen = true;
9643 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9644 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9645 mAppTransitionReady = true;
9646 }
Romain Guy06882f82009-06-10 13:36:04 -07009647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 if (PROFILE_ORIENTATION) {
9649 File file = new File("/data/system/frozen");
9650 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9651 }
9652 Surface.freezeDisplay(0);
9653 }
Romain Guy06882f82009-06-10 13:36:04 -07009654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009655 private void stopFreezingDisplayLocked() {
9656 if (!mDisplayFrozen) {
9657 return;
9658 }
Romain Guy06882f82009-06-10 13:36:04 -07009659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 mDisplayFrozen = false;
9661 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9662 if (PROFILE_ORIENTATION) {
9663 Debug.stopMethodTracing();
9664 }
9665 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009666
Chris Tate2ad63a92009-03-25 17:36:48 -07009667 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9668 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 synchronized (mKeyWaiter) {
9670 mKeyWaiter.mWasFrozen = true;
9671 mKeyWaiter.notifyAll();
9672 }
9673
9674 // A little kludge: a lot could have happened while the
9675 // display was frozen, so now that we are coming back we
9676 // do a gc so that any remote references the system
9677 // processes holds on others can be released if they are
9678 // no longer needed.
9679 mH.removeMessages(H.FORCE_GC);
9680 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9681 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 mScreenFrozenLock.release();
9684 }
Romain Guy06882f82009-06-10 13:36:04 -07009685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 @Override
9687 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9688 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9689 != PackageManager.PERMISSION_GRANTED) {
9690 pw.println("Permission Denial: can't dump WindowManager from from pid="
9691 + Binder.getCallingPid()
9692 + ", uid=" + Binder.getCallingUid());
9693 return;
9694 }
Romain Guy06882f82009-06-10 13:36:04 -07009695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 synchronized(mWindowMap) {
9697 pw.println("Current Window Manager state:");
9698 for (int i=mWindows.size()-1; i>=0; i--) {
9699 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009700 pw.print(" Window #"); pw.print(i); pw.print(' ');
9701 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 w.dump(pw, " ");
9703 }
9704 if (mInputMethodDialogs.size() > 0) {
9705 pw.println(" ");
9706 pw.println(" Input method dialogs:");
9707 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9708 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009709 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 }
9711 }
9712 if (mPendingRemove.size() > 0) {
9713 pw.println(" ");
9714 pw.println(" Remove pending for:");
9715 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9716 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009717 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9718 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009719 w.dump(pw, " ");
9720 }
9721 }
9722 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9723 pw.println(" ");
9724 pw.println(" Windows force removing:");
9725 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9726 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009727 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9728 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 w.dump(pw, " ");
9730 }
9731 }
9732 if (mDestroySurface.size() > 0) {
9733 pw.println(" ");
9734 pw.println(" Windows waiting to destroy their surface:");
9735 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9736 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009737 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9738 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 w.dump(pw, " ");
9740 }
9741 }
9742 if (mLosingFocus.size() > 0) {
9743 pw.println(" ");
9744 pw.println(" Windows losing focus:");
9745 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9746 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009747 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9748 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 w.dump(pw, " ");
9750 }
9751 }
9752 if (mSessions.size() > 0) {
9753 pw.println(" ");
9754 pw.println(" All active sessions:");
9755 Iterator<Session> it = mSessions.iterator();
9756 while (it.hasNext()) {
9757 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009758 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009759 s.dump(pw, " ");
9760 }
9761 }
9762 if (mTokenMap.size() > 0) {
9763 pw.println(" ");
9764 pw.println(" All tokens:");
9765 Iterator<WindowToken> it = mTokenMap.values().iterator();
9766 while (it.hasNext()) {
9767 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009768 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 token.dump(pw, " ");
9770 }
9771 }
9772 if (mTokenList.size() > 0) {
9773 pw.println(" ");
9774 pw.println(" Window token list:");
9775 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009776 pw.print(" #"); pw.print(i); pw.print(": ");
9777 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 }
9779 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009780 if (mWallpaperTokens.size() > 0) {
9781 pw.println(" ");
9782 pw.println(" Wallpaper tokens:");
9783 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9784 WindowToken token = mWallpaperTokens.get(i);
9785 pw.print(" Wallpaper #"); pw.print(i);
9786 pw.print(' '); pw.print(token); pw.println(':');
9787 token.dump(pw, " ");
9788 }
9789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 if (mAppTokens.size() > 0) {
9791 pw.println(" ");
9792 pw.println(" Application tokens in Z order:");
9793 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009794 pw.print(" App #"); pw.print(i); pw.print(": ");
9795 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 }
9797 }
9798 if (mFinishedStarting.size() > 0) {
9799 pw.println(" ");
9800 pw.println(" Finishing start of application tokens:");
9801 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9802 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009803 pw.print(" Finished Starting #"); pw.print(i);
9804 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 token.dump(pw, " ");
9806 }
9807 }
9808 if (mExitingTokens.size() > 0) {
9809 pw.println(" ");
9810 pw.println(" Exiting tokens:");
9811 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9812 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009813 pw.print(" Exiting #"); pw.print(i);
9814 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 token.dump(pw, " ");
9816 }
9817 }
9818 if (mExitingAppTokens.size() > 0) {
9819 pw.println(" ");
9820 pw.println(" Exiting application tokens:");
9821 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9822 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009823 pw.print(" Exiting App #"); pw.print(i);
9824 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 token.dump(pw, " ");
9826 }
9827 }
9828 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009829 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9830 pw.print(" mLastFocus="); pw.println(mLastFocus);
9831 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9832 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9833 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -07009834 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009835 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9836 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9837 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9838 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9839 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009840 if (mDimAnimator != null) {
9841 mDimAnimator.printTo(pw);
9842 } else {
9843 pw.print( " no DimAnimator ");
9844 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009845 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009846 pw.print(mInputMethodAnimLayerAdjustment);
9847 pw.print(" mWallpaperAnimLayerAdjustment=");
9848 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009849 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9850 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9851 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9852 pw.print(" mRotation="); pw.print(mRotation);
9853 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9854 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9855 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9856 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9857 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9858 pw.print(" mNextAppTransition=0x");
9859 pw.print(Integer.toHexString(mNextAppTransition));
9860 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9861 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9862 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9863 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9864 if (mOpeningApps.size() > 0) {
9865 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9866 }
9867 if (mClosingApps.size() > 0) {
9868 pw.print(" mClosingApps="); pw.println(mClosingApps);
9869 }
9870 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9871 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009873 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9874 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9875 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9876 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9877 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9878 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 }
9880 }
9881
9882 public void monitor() {
9883 synchronized (mWindowMap) { }
9884 synchronized (mKeyguardDisabled) { }
9885 synchronized (mKeyWaiter) { }
9886 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009887
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009888 public void virtualKeyFeedback(KeyEvent event) {
9889 mPolicy.keyFeedbackFromInput(event);
9890 }
9891
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009892 /**
9893 * DimAnimator class that controls the dim animation. This holds the surface and
9894 * all state used for dim animation.
9895 */
9896 private static class DimAnimator {
9897 Surface mDimSurface;
9898 boolean mDimShown = false;
9899 float mDimCurrentAlpha;
9900 float mDimTargetAlpha;
9901 float mDimDeltaPerMs;
9902 long mLastDimAnimTime;
9903
9904 DimAnimator (SurfaceSession session) {
9905 if (mDimSurface == null) {
9906 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9907 + mDimSurface + ": CREATE");
9908 try {
9909 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9910 Surface.FX_SURFACE_DIM);
9911 } catch (Exception e) {
9912 Log.e(TAG, "Exception creating Dim surface", e);
9913 }
9914 }
9915 }
9916
9917 /**
9918 * Show the dim surface.
9919 */
9920 void show(int dw, int dh) {
9921 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9922 dw + "x" + dh + ")");
9923 mDimShown = true;
9924 try {
9925 mDimSurface.setPosition(0, 0);
9926 mDimSurface.setSize(dw, dh);
9927 mDimSurface.show();
9928 } catch (RuntimeException e) {
9929 Log.w(TAG, "Failure showing dim surface", e);
9930 }
9931 }
9932
9933 /**
9934 * Set's the dim surface's layer and update dim parameters that will be used in
9935 * {@link updateSurface} after all windows are examined.
9936 */
9937 void updateParameters(WindowState w, long currentTime) {
9938 mDimSurface.setLayer(w.mAnimLayer-1);
9939
9940 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9941 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9942 if (mDimTargetAlpha != target) {
9943 // If the desired dim level has changed, then
9944 // start an animation to it.
9945 mLastDimAnimTime = currentTime;
9946 long duration = (w.mAnimating && w.mAnimation != null)
9947 ? w.mAnimation.computeDurationHint()
9948 : DEFAULT_DIM_DURATION;
9949 if (target > mDimTargetAlpha) {
9950 // This is happening behind the activity UI,
9951 // so we can make it run a little longer to
9952 // give a stronger impression without disrupting
9953 // the user.
9954 duration *= DIM_DURATION_MULTIPLIER;
9955 }
9956 if (duration < 1) {
9957 // Don't divide by zero
9958 duration = 1;
9959 }
9960 mDimTargetAlpha = target;
9961 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9962 }
9963 }
9964
9965 /**
9966 * Updating the surface's alpha. Returns true if the animation continues, or returns
9967 * false when the animation is finished and the dim surface is hidden.
9968 */
9969 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9970 if (!dimming) {
9971 if (mDimTargetAlpha != 0) {
9972 mLastDimAnimTime = currentTime;
9973 mDimTargetAlpha = 0;
9974 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9975 }
9976 }
9977
9978 boolean animating = false;
9979 if (mLastDimAnimTime != 0) {
9980 mDimCurrentAlpha += mDimDeltaPerMs
9981 * (currentTime-mLastDimAnimTime);
9982 boolean more = true;
9983 if (displayFrozen) {
9984 // If the display is frozen, there is no reason to animate.
9985 more = false;
9986 } else if (mDimDeltaPerMs > 0) {
9987 if (mDimCurrentAlpha > mDimTargetAlpha) {
9988 more = false;
9989 }
9990 } else if (mDimDeltaPerMs < 0) {
9991 if (mDimCurrentAlpha < mDimTargetAlpha) {
9992 more = false;
9993 }
9994 } else {
9995 more = false;
9996 }
9997
9998 // Do we need to continue animating?
9999 if (more) {
10000 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10001 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10002 mLastDimAnimTime = currentTime;
10003 mDimSurface.setAlpha(mDimCurrentAlpha);
10004 animating = true;
10005 } else {
10006 mDimCurrentAlpha = mDimTargetAlpha;
10007 mLastDimAnimTime = 0;
10008 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10009 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10010 mDimSurface.setAlpha(mDimCurrentAlpha);
10011 if (!dimming) {
10012 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10013 + ": HIDE");
10014 try {
10015 mDimSurface.hide();
10016 } catch (RuntimeException e) {
10017 Log.w(TAG, "Illegal argument exception hiding dim surface");
10018 }
10019 mDimShown = false;
10020 }
10021 }
10022 }
10023 return animating;
10024 }
10025
10026 public void printTo(PrintWriter pw) {
10027 pw.print(" mDimShown="); pw.print(mDimShown);
10028 pw.print(" current="); pw.print(mDimCurrentAlpha);
10029 pw.print(" target="); pw.print(mDimTargetAlpha);
10030 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10031 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10032 }
10033 }
10034
10035 /**
10036 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10037 * This is used for opening/closing transition for apps in compatible mode.
10038 */
10039 private static class FadeInOutAnimation extends Animation {
10040 int mWidth;
10041 boolean mFadeIn;
10042
10043 public FadeInOutAnimation(boolean fadeIn) {
10044 setInterpolator(new AccelerateInterpolator());
10045 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10046 mFadeIn = fadeIn;
10047 }
10048
10049 @Override
10050 protected void applyTransformation(float interpolatedTime, Transformation t) {
10051 float x = interpolatedTime;
10052 if (!mFadeIn) {
10053 x = 1.0f - x; // reverse the interpolation for fade out
10054 }
10055 if (x < 0.5) {
10056 // move the window out of the screen.
10057 t.getMatrix().setTranslate(mWidth, 0);
10058 } else {
10059 t.getMatrix().setTranslate(0, 0);// show
10060 t.setAlpha((x - 0.5f) * 2);
10061 }
10062 }
10063
10064 @Override
10065 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10066 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10067 mWidth = width;
10068 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010069
10070 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010071 public int getZAdjustment() {
10072 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010073 }
10074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075}