blob: 4321b0d4f3888963e1fbebe50b91ea38feb4ed53 [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()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001220 && (mWallpaperTarget == w
1221 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001222 if (DEBUG_WALLPAPER) Log.v(TAG,
1223 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001224 foundW = w;
1225 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001226 if (w == mWallpaperTarget && w.mAppToken != null
1227 && w.mAppToken.animation != null) {
1228 // The current wallpaper target is animating, so we'll
1229 // look behind it for another possible target and figure
1230 // out what is going on below.
1231 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1232 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001233 continue;
1234 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001235 break;
1236 }
1237 }
1238
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001239 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1240 // If we are currently waiting for an app transition, and either
1241 // the current target or the next target are involved with it,
1242 // then hold off on doing anything with the wallpaper.
1243 // Note that we are checking here for just whether the target
1244 // is part of an app token... which is potentially overly aggressive
1245 // (the app token may not be involved in the transition), but good
1246 // enough (we'll just wait until whatever transition is pending
1247 // executes).
1248 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001249 if (DEBUG_WALLPAPER) Log.v(TAG,
1250 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001251 return false;
1252 }
1253 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001254 if (DEBUG_WALLPAPER) Log.v(TAG,
1255 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001256 return false;
1257 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001258 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001259
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001260 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001261 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001262 Log.v(TAG, "New wallpaper target: " + foundW
1263 + " oldTarget: " + mWallpaperTarget);
1264 }
1265
1266 mLowerWallpaperTarget = null;
1267 mUpperWallpaperTarget = null;
1268
1269 WindowState oldW = mWallpaperTarget;
1270 mWallpaperTarget = foundW;
1271
1272 // Now what is happening... if the current and new targets are
1273 // animating, then we are in our super special mode!
1274 if (foundW != null && foundW.mAppToken != null && oldW != null
1275 && oldW.mAppToken != null) {
1276 if (DEBUG_WALLPAPER) {
1277 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1278 + " old animation: " + oldW.mAppToken.animation);
1279 }
1280 if (foundW.mAppToken.animation != null
1281 && oldW.mAppToken.animation != null) {
1282 int oldI = localmWindows.indexOf(oldW);
1283 if (DEBUG_WALLPAPER) {
1284 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1285 }
1286 if (oldI >= 0) {
1287 if (DEBUG_WALLPAPER) {
1288 Log.v(TAG, "Animating wallpapers: old#" + oldI
1289 + "=" + oldW + "; new#" + foundI
1290 + "=" + foundW);
1291 }
1292
1293 // Set the new target correctly.
1294 if (foundW.mAppToken.hiddenRequested) {
1295 if (DEBUG_WALLPAPER) {
1296 Log.v(TAG, "Old wallpaper still the target.");
1297 }
1298 mWallpaperTarget = oldW;
1299 }
1300
1301 // Now set the upper and lower wallpaper targets
1302 // correctly, and make sure that we are positioning
1303 // the wallpaper below the lower.
1304 if (foundI > oldI) {
1305 // The new target is on top of the old one.
1306 if (DEBUG_WALLPAPER) {
1307 Log.v(TAG, "Found target above old target.");
1308 }
1309 mUpperWallpaperTarget = foundW;
1310 mLowerWallpaperTarget = oldW;
1311 foundW = oldW;
1312 foundI = oldI;
1313 } else {
1314 // The new target is below the old one.
1315 if (DEBUG_WALLPAPER) {
1316 Log.v(TAG, "Found target below old target.");
1317 }
1318 mUpperWallpaperTarget = oldW;
1319 mLowerWallpaperTarget = foundW;
1320 }
1321 }
1322 }
1323 }
1324
1325 } else {
1326 // Is it time to stop animating?
1327 if (mLowerWallpaperTarget == null
1328 || mLowerWallpaperTarget.mAppToken.animation == null
1329 || mUpperWallpaperTarget == null
1330 || mUpperWallpaperTarget.mAppToken.animation == null) {
1331 if (DEBUG_WALLPAPER) {
1332 Log.v(TAG, "No longer animating wallpaper targets!");
1333 }
1334 mLowerWallpaperTarget = null;
1335 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001336 }
1337 }
1338
1339 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001340 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001341 // The window is visible to the compositor... but is it visible
1342 // to the user? That is what the wallpaper cares about.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001343 visible = !foundW.mObscured;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001344 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001345
1346 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001347 // its layer adjustment. Only do this if we are not transfering
1348 // between two wallpaper targets.
1349 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001350 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001351 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001352
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001353 // Now w is the window we are supposed to be behind... but we
1354 // need to be sure to also be behind any of its attached windows,
1355 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001356 while (foundI > 0) {
1357 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1358 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001359 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001360 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001361 // This window is not related to the previous one in any
1362 // interesting way, so stop here.
1363 break;
1364 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001365 foundW = wb;
1366 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001367 }
1368 }
1369
1370 // Okay i is the position immediately above the wallpaper. Look at
1371 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001372 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001373
1374 // Start stepping backwards from here, ensuring that our wallpaper windows
1375 // are correctly placed.
1376 int curTokenIndex = mWallpaperTokens.size();
1377 while (curTokenIndex > 0) {
1378 curTokenIndex--;
1379 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1380 int curWallpaperIndex = token.windows.size();
1381 while (curWallpaperIndex > 0) {
1382 curWallpaperIndex--;
1383 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001384
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001385 if (visible) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001386 updateWallpaperOffsetLocked(mWallpaperTarget,
1387 wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001388 }
1389
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001390 // First, make sure the client has the current visibility
1391 // state.
1392 if (wallpaper.mWallpaperVisible != visible) {
1393 wallpaper.mWallpaperVisible = visible;
1394 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001395 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001396 "Setting visibility of wallpaper " + wallpaper
1397 + ": " + visible);
1398 wallpaper.mClient.dispatchAppVisibility(visible);
1399 } catch (RemoteException e) {
1400 }
1401 }
1402
1403 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001404 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1405 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001406
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001407 // First, if this window is at the current index, then all
1408 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001409 if (wallpaper == foundW) {
1410 foundI--;
1411 foundW = foundI > 0
1412 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001413 continue;
1414 }
1415
1416 // The window didn't match... the current wallpaper window,
1417 // wherever it is, is in the wrong place, so make sure it is
1418 // not in the list.
1419 int oldIndex = localmWindows.indexOf(wallpaper);
1420 if (oldIndex >= 0) {
1421 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001422 if (oldIndex < foundI) {
1423 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001424 }
1425 }
1426
1427 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001428 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001429 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001430
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001431 localmWindows.add(foundI, wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001432 changed = true;
1433 }
1434 }
1435
1436 return changed;
1437 }
1438
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001439 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001440 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1441 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001442 mWallpaperAnimLayerAdjustment = adj;
1443 int curTokenIndex = mWallpaperTokens.size();
1444 while (curTokenIndex > 0) {
1445 curTokenIndex--;
1446 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1447 int curWallpaperIndex = token.windows.size();
1448 while (curWallpaperIndex > 0) {
1449 curWallpaperIndex--;
1450 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1451 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001452 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1453 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001454 }
1455 }
1456 }
1457
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001458 boolean updateWallpaperOffsetLocked(WindowState target,
1459 WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001460 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001461 boolean rawChanged = false;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001462 if (target.mWallpaperX >= 0) {
1463 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1464 int offset = availw > 0 ? -(int)(availw*target.mWallpaperX+.5f) : 0;
1465 changed = wallpaperWin.mXOffset != offset;
1466 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001467 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1468 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001469 wallpaperWin.mXOffset = offset;
1470 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001471 if (wallpaperWin.mWallpaperX != target.mWallpaperX) {
1472 wallpaperWin.mWallpaperX = target.mWallpaperX;
1473 rawChanged = true;
1474 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001475 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001476
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001477 if (target.mWallpaperY >= 0) {
1478 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1479 int offset = availh > 0 ? -(int)(availh*target.mWallpaperY+.5f) : 0;
1480 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001481 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1482 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001483 changed = true;
1484 wallpaperWin.mYOffset = offset;
1485 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001486 if (wallpaperWin.mWallpaperY != target.mWallpaperY) {
1487 wallpaperWin.mWallpaperY = target.mWallpaperY;
1488 rawChanged = true;
1489 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001490 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001491
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001492 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001493 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001494 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1495 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1496 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001497 wallpaperWin.mClient.dispatchWallpaperOffsets(
1498 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1499 } catch (RemoteException e) {
1500 }
1501 }
1502
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001503 return changed;
1504 }
1505
1506 boolean updateWallpaperOffsetLocked() {
1507 final int dw = mDisplay.getWidth();
1508 final int dh = mDisplay.getHeight();
1509
1510 boolean changed = false;
1511
1512 WindowState target = mWallpaperTarget;
1513 if (target != null) {
1514 int curTokenIndex = mWallpaperTokens.size();
1515 while (curTokenIndex > 0) {
1516 curTokenIndex--;
1517 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1518 int curWallpaperIndex = token.windows.size();
1519 while (curWallpaperIndex > 0) {
1520 curWallpaperIndex--;
1521 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1522 if (updateWallpaperOffsetLocked(target, wallpaper, dw, dh)) {
1523 wallpaper.computeShownFrameLocked();
1524 changed = true;
1525 }
1526 }
1527 }
1528 }
1529
1530 return changed;
1531 }
1532
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001533 void updateWallpaperVisibilityLocked() {
1534 final boolean visible = mWallpaperTarget != null
1535 && !mWallpaperTarget.mObscured;
1536 final int dw = mDisplay.getWidth();
1537 final int dh = mDisplay.getHeight();
1538
1539 int curTokenIndex = mWallpaperTokens.size();
1540 while (curTokenIndex > 0) {
1541 curTokenIndex--;
1542 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1543 int curWallpaperIndex = token.windows.size();
1544 while (curWallpaperIndex > 0) {
1545 curWallpaperIndex--;
1546 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1547 if (visible) {
1548 updateWallpaperOffsetLocked(mWallpaperTarget,
1549 wallpaper, dw, dh);
1550 }
1551
1552 if (wallpaper.mWallpaperVisible != visible) {
1553 wallpaper.mWallpaperVisible = visible;
1554 try {
1555 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
1556 "Setting visibility of wallpaper " + wallpaper
1557 + ": " + visible);
1558 wallpaper.mClient.dispatchAppVisibility(visible);
1559 } catch (RemoteException e) {
1560 }
1561 }
1562 }
1563 }
1564 }
1565
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001566 void sendPointerToWallpaperLocked(WindowState srcWin,
1567 MotionEvent pointer, long eventTime) {
1568 int curTokenIndex = mWallpaperTokens.size();
1569 while (curTokenIndex > 0) {
1570 curTokenIndex--;
1571 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1572 int curWallpaperIndex = token.windows.size();
1573 while (curWallpaperIndex > 0) {
1574 curWallpaperIndex--;
1575 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1576 if ((wallpaper.mAttrs.flags &
1577 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1578 continue;
1579 }
1580 try {
1581 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1582 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1583 srcWin.mFrame.top-wallpaper.mFrame.top);
1584 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1585 } catch (RemoteException e) {
1586 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1587 }
1588 }
1589 }
1590 }
1591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 public int addWindow(Session session, IWindow client,
1593 WindowManager.LayoutParams attrs, int viewVisibility,
1594 Rect outContentInsets) {
1595 int res = mPolicy.checkAddPermission(attrs);
1596 if (res != WindowManagerImpl.ADD_OKAY) {
1597 return res;
1598 }
Romain Guy06882f82009-06-10 13:36:04 -07001599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 boolean reportNewConfig = false;
1601 WindowState attachedWindow = null;
1602 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 synchronized(mWindowMap) {
1605 // Instantiating a Display requires talking with the simulator,
1606 // so don't do it until we know the system is mostly up and
1607 // running.
1608 if (mDisplay == null) {
1609 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1610 mDisplay = wm.getDefaultDisplay();
1611 mQueue.setDisplay(mDisplay);
1612 reportNewConfig = true;
1613 }
Romain Guy06882f82009-06-10 13:36:04 -07001614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 if (mWindowMap.containsKey(client.asBinder())) {
1616 Log.w(TAG, "Window " + client + " is already added");
1617 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1618 }
1619
1620 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001621 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 if (attachedWindow == null) {
1623 Log.w(TAG, "Attempted to add window with token that is not a window: "
1624 + attrs.token + ". Aborting.");
1625 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1626 }
1627 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1628 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1629 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1630 + attrs.token + ". Aborting.");
1631 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1632 }
1633 }
1634
1635 boolean addToken = false;
1636 WindowToken token = mTokenMap.get(attrs.token);
1637 if (token == null) {
1638 if (attrs.type >= FIRST_APPLICATION_WINDOW
1639 && attrs.type <= LAST_APPLICATION_WINDOW) {
1640 Log.w(TAG, "Attempted to add application window with unknown token "
1641 + attrs.token + ". Aborting.");
1642 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1643 }
1644 if (attrs.type == TYPE_INPUT_METHOD) {
1645 Log.w(TAG, "Attempted to add input method window with unknown token "
1646 + attrs.token + ". Aborting.");
1647 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1648 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001649 if (attrs.type == TYPE_WALLPAPER) {
1650 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1651 + attrs.token + ". Aborting.");
1652 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 token = new WindowToken(attrs.token, -1, false);
1655 addToken = true;
1656 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1657 && attrs.type <= LAST_APPLICATION_WINDOW) {
1658 AppWindowToken atoken = token.appWindowToken;
1659 if (atoken == null) {
1660 Log.w(TAG, "Attempted to add window with non-application token "
1661 + token + ". Aborting.");
1662 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1663 } else if (atoken.removed) {
1664 Log.w(TAG, "Attempted to add window with exiting application token "
1665 + token + ". Aborting.");
1666 return WindowManagerImpl.ADD_APP_EXITING;
1667 }
1668 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1669 // No need for this guy!
1670 if (localLOGV) Log.v(
1671 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1672 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1673 }
1674 } else if (attrs.type == TYPE_INPUT_METHOD) {
1675 if (token.windowType != TYPE_INPUT_METHOD) {
1676 Log.w(TAG, "Attempted to add input method window with bad token "
1677 + attrs.token + ". Aborting.");
1678 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1679 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001680 } else if (attrs.type == TYPE_WALLPAPER) {
1681 if (token.windowType != TYPE_WALLPAPER) {
1682 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1683 + attrs.token + ". Aborting.");
1684 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687
1688 win = new WindowState(session, client, token,
1689 attachedWindow, attrs, viewVisibility);
1690 if (win.mDeathRecipient == null) {
1691 // Client has apparently died, so there is no reason to
1692 // continue.
1693 Log.w(TAG, "Adding window client " + client.asBinder()
1694 + " that is dead, aborting.");
1695 return WindowManagerImpl.ADD_APP_EXITING;
1696 }
1697
1698 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 res = mPolicy.prepareAddWindowLw(win, attrs);
1701 if (res != WindowManagerImpl.ADD_OKAY) {
1702 return res;
1703 }
1704
1705 // From now on, no exceptions or errors allowed!
1706
1707 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if (addToken) {
1712 mTokenMap.put(attrs.token, token);
1713 mTokenList.add(token);
1714 }
1715 win.attach();
1716 mWindowMap.put(client.asBinder(), win);
1717
1718 if (attrs.type == TYPE_APPLICATION_STARTING &&
1719 token.appWindowToken != null) {
1720 token.appWindowToken.startingWindow = win;
1721 }
1722
1723 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 if (attrs.type == TYPE_INPUT_METHOD) {
1726 mInputMethodWindow = win;
1727 addInputMethodWindowToListLocked(win);
1728 imMayMove = false;
1729 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1730 mInputMethodDialogs.add(win);
1731 addWindowToListInOrderLocked(win, true);
1732 adjustInputMethodDialogsLocked();
1733 imMayMove = false;
1734 } else {
1735 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001736 if (attrs.type == TYPE_WALLPAPER ||
1737 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1738 adjustWallpaperWindowsLocked();
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
Romain Guy06882f82009-06-10 13:36:04 -07001741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 if (mInTouchMode) {
1747 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1748 }
1749 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1750 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1751 }
Romain Guy06882f82009-06-10 13:36:04 -07001752
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001753 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001755 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1756 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 imMayMove = false;
1758 }
1759 }
Romain Guy06882f82009-06-10 13:36:04 -07001760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001762 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
Romain Guy06882f82009-06-10 13:36:04 -07001764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 assignLayersLocked();
1766 // Don't do layout here, the window must call
1767 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 //dump();
1770
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001771 if (focusChanged) {
1772 if (mCurrentFocus != null) {
1773 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1774 }
1775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 if (localLOGV) Log.v(
1777 TAG, "New client " + client.asBinder()
1778 + ": window=" + win);
1779 }
1780
1781 // sendNewConfiguration() checks caller permissions so we must call it with
1782 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1783 // identity anyway, so it's safe to just clear & restore around this whole
1784 // block.
1785 final long origId = Binder.clearCallingIdentity();
1786 if (reportNewConfig) {
1787 sendNewConfiguration();
1788 } else {
1789 // Update Orientation after adding a window, only if the window needs to be
1790 // displayed right away
1791 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001792 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 sendNewConfiguration();
1794 }
1795 }
1796 }
1797 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 return res;
1800 }
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 public void removeWindow(Session session, IWindow client) {
1803 synchronized(mWindowMap) {
1804 WindowState win = windowForClientLocked(session, client);
1805 if (win == null) {
1806 return;
1807 }
1808 removeWindowLocked(session, win);
1809 }
1810 }
Romain Guy06882f82009-06-10 13:36:04 -07001811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 public void removeWindowLocked(Session session, WindowState win) {
1813
1814 if (localLOGV || DEBUG_FOCUS) Log.v(
1815 TAG, "Remove " + win + " client="
1816 + Integer.toHexString(System.identityHashCode(
1817 win.mClient.asBinder()))
1818 + ", surface=" + win.mSurface);
1819
1820 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 if (DEBUG_APP_TRANSITIONS) Log.v(
1823 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1824 + " mExiting=" + win.mExiting
1825 + " isAnimating=" + win.isAnimating()
1826 + " app-animation="
1827 + (win.mAppToken != null ? win.mAppToken.animation : null)
1828 + " inPendingTransaction="
1829 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1830 + " mDisplayFrozen=" + mDisplayFrozen);
1831 // Visibility of the removed window. Will be used later to update orientation later on.
1832 boolean wasVisible = false;
1833 // First, see if we need to run an animation. If we do, we have
1834 // to hold off on removing the window until the animation is done.
1835 // If the display is frozen, just remove immediately, since the
1836 // animation wouldn't be seen.
1837 if (win.mSurface != null && !mDisplayFrozen) {
1838 // If we are not currently running the exit animation, we
1839 // need to see about starting one.
1840 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1843 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1844 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1845 }
1846 // Try starting an animation.
1847 if (applyAnimationLocked(win, transit, false)) {
1848 win.mExiting = true;
1849 }
1850 }
1851 if (win.mExiting || win.isAnimating()) {
1852 // The exit animation is running... wait for it!
1853 //Log.i(TAG, "*** Running exit animation...");
1854 win.mExiting = true;
1855 win.mRemoveOnExit = true;
1856 mLayoutNeeded = true;
1857 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1858 performLayoutAndPlaceSurfacesLocked();
1859 if (win.mAppToken != null) {
1860 win.mAppToken.updateReportedVisibilityLocked();
1861 }
1862 //dump();
1863 Binder.restoreCallingIdentity(origId);
1864 return;
1865 }
1866 }
1867
1868 removeWindowInnerLocked(session, win);
1869 // Removing a visible window will effect the computed orientation
1870 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001871 if (wasVisible && computeForcedAppOrientationLocked()
1872 != mForcedAppOrientation) {
1873 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
1875 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1876 Binder.restoreCallingIdentity(origId);
1877 }
Romain Guy06882f82009-06-10 13:36:04 -07001878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001880 mKeyWaiter.finishedKey(session, win.mClient, true,
1881 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1883 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (mInputMethodTarget == win) {
1888 moveInputMethodWindowsIfNeededLocked(false);
1889 }
Romain Guy06882f82009-06-10 13:36:04 -07001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 mPolicy.removeWindowLw(win);
1892 win.removeLocked();
1893
1894 mWindowMap.remove(win.mClient.asBinder());
1895 mWindows.remove(win);
1896
1897 if (mInputMethodWindow == win) {
1898 mInputMethodWindow = null;
1899 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1900 mInputMethodDialogs.remove(win);
1901 }
Romain Guy06882f82009-06-10 13:36:04 -07001902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 final WindowToken token = win.mToken;
1904 final AppWindowToken atoken = win.mAppToken;
1905 token.windows.remove(win);
1906 if (atoken != null) {
1907 atoken.allAppWindows.remove(win);
1908 }
1909 if (localLOGV) Log.v(
1910 TAG, "**** Removing window " + win + ": count="
1911 + token.windows.size());
1912 if (token.windows.size() == 0) {
1913 if (!token.explicit) {
1914 mTokenMap.remove(token.token);
1915 mTokenList.remove(token);
1916 } else if (atoken != null) {
1917 atoken.firstWindowDrawn = false;
1918 }
1919 }
1920
1921 if (atoken != null) {
1922 if (atoken.startingWindow == win) {
1923 atoken.startingWindow = null;
1924 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1925 // If this is the last window and we had requested a starting
1926 // transition window, well there is no point now.
1927 atoken.startingData = null;
1928 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1929 // If this is the last window except for a starting transition
1930 // window, we need to get rid of the starting transition.
1931 if (DEBUG_STARTING_WINDOW) {
1932 Log.v(TAG, "Schedule remove starting " + token
1933 + ": no more real windows");
1934 }
1935 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1936 mH.sendMessage(m);
1937 }
1938 }
Romain Guy06882f82009-06-10 13:36:04 -07001939
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001940 if (win.mAttrs.type == TYPE_WALLPAPER ||
1941 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1942 adjustWallpaperWindowsLocked();
1943 }
1944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (!mInLayout) {
1946 assignLayersLocked();
1947 mLayoutNeeded = true;
1948 performLayoutAndPlaceSurfacesLocked();
1949 if (win.mAppToken != null) {
1950 win.mAppToken.updateReportedVisibilityLocked();
1951 }
1952 }
1953 }
1954
1955 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
1956 long origId = Binder.clearCallingIdentity();
1957 try {
1958 synchronized (mWindowMap) {
1959 WindowState w = windowForClientLocked(session, client);
1960 if ((w != null) && (w.mSurface != null)) {
1961 Surface.openTransaction();
1962 try {
1963 w.mSurface.setTransparentRegionHint(region);
1964 } finally {
1965 Surface.closeTransaction();
1966 }
1967 }
1968 }
1969 } finally {
1970 Binder.restoreCallingIdentity(origId);
1971 }
1972 }
1973
1974 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07001975 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 Rect visibleInsets) {
1977 long origId = Binder.clearCallingIdentity();
1978 try {
1979 synchronized (mWindowMap) {
1980 WindowState w = windowForClientLocked(session, client);
1981 if (w != null) {
1982 w.mGivenInsetsPending = false;
1983 w.mGivenContentInsets.set(contentInsets);
1984 w.mGivenVisibleInsets.set(visibleInsets);
1985 w.mTouchableInsets = touchableInsets;
1986 mLayoutNeeded = true;
1987 performLayoutAndPlaceSurfacesLocked();
1988 }
1989 }
1990 } finally {
1991 Binder.restoreCallingIdentity(origId);
1992 }
1993 }
Romain Guy06882f82009-06-10 13:36:04 -07001994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 public void getWindowDisplayFrame(Session session, IWindow client,
1996 Rect outDisplayFrame) {
1997 synchronized(mWindowMap) {
1998 WindowState win = windowForClientLocked(session, client);
1999 if (win == null) {
2000 outDisplayFrame.setEmpty();
2001 return;
2002 }
2003 outDisplayFrame.set(win.mDisplayFrame);
2004 }
2005 }
2006
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002007 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2008 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2009 window.mWallpaperX = x;
2010 window.mWallpaperY = y;
2011
2012 if (mWallpaperTarget == window) {
2013 if (updateWallpaperOffsetLocked()) {
2014 performLayoutAndPlaceSurfacesLocked();
2015 }
2016 }
2017 }
2018 }
2019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 public int relayoutWindow(Session session, IWindow client,
2021 WindowManager.LayoutParams attrs, int requestedWidth,
2022 int requestedHeight, int viewVisibility, boolean insetsPending,
2023 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2024 Surface outSurface) {
2025 boolean displayed = false;
2026 boolean inTouchMode;
2027 Configuration newConfig = null;
2028 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 synchronized(mWindowMap) {
2031 WindowState win = windowForClientLocked(session, client);
2032 if (win == null) {
2033 return 0;
2034 }
2035 win.mRequestedWidth = requestedWidth;
2036 win.mRequestedHeight = requestedHeight;
2037
2038 if (attrs != null) {
2039 mPolicy.adjustWindowParamsLw(attrs);
2040 }
Romain Guy06882f82009-06-10 13:36:04 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 int attrChanges = 0;
2043 int flagChanges = 0;
2044 if (attrs != null) {
2045 flagChanges = win.mAttrs.flags ^= attrs.flags;
2046 attrChanges = win.mAttrs.copyFrom(attrs);
2047 }
2048
2049 if (localLOGV) Log.v(
2050 TAG, "Relayout given client " + client.asBinder()
2051 + " (" + win.mAttrs.getTitle() + ")");
2052
2053
2054 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2055 win.mAlpha = attrs.alpha;
2056 }
2057
2058 final boolean scaledWindow =
2059 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2060
2061 if (scaledWindow) {
2062 // requested{Width|Height} Surface's physical size
2063 // attrs.{width|height} Size on screen
2064 win.mHScale = (attrs.width != requestedWidth) ?
2065 (attrs.width / (float)requestedWidth) : 1.0f;
2066 win.mVScale = (attrs.height != requestedHeight) ?
2067 (attrs.height / (float)requestedHeight) : 1.0f;
2068 }
2069
2070 boolean imMayMove = (flagChanges&(
2071 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2072 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 boolean focusMayChange = win.mViewVisibility != viewVisibility
2075 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2076 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002077
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002078 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2079 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 win.mRelayoutCalled = true;
2082 final int oldVisibility = win.mViewVisibility;
2083 win.mViewVisibility = viewVisibility;
2084 if (viewVisibility == View.VISIBLE &&
2085 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2086 displayed = !win.isVisibleLw();
2087 if (win.mExiting) {
2088 win.mExiting = false;
2089 win.mAnimation = null;
2090 }
2091 if (win.mDestroying) {
2092 win.mDestroying = false;
2093 mDestroySurface.remove(win);
2094 }
2095 if (oldVisibility == View.GONE) {
2096 win.mEnterAnimationPending = true;
2097 }
2098 if (displayed && win.mSurface != null && !win.mDrawPending
2099 && !win.mCommitDrawPending && !mDisplayFrozen) {
2100 applyEnterAnimationLocked(win);
2101 }
2102 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2103 // To change the format, we need to re-build the surface.
2104 win.destroySurfaceLocked();
2105 displayed = true;
2106 }
2107 try {
2108 Surface surface = win.createSurfaceLocked();
2109 if (surface != null) {
2110 outSurface.copyFrom(surface);
2111 } else {
2112 outSurface.clear();
2113 }
2114 } catch (Exception e) {
2115 Log.w(TAG, "Exception thrown when creating surface for client "
2116 + client + " (" + win.mAttrs.getTitle() + ")",
2117 e);
2118 Binder.restoreCallingIdentity(origId);
2119 return 0;
2120 }
2121 if (displayed) {
2122 focusMayChange = true;
2123 }
2124 if (win.mAttrs.type == TYPE_INPUT_METHOD
2125 && mInputMethodWindow == null) {
2126 mInputMethodWindow = win;
2127 imMayMove = true;
2128 }
2129 } else {
2130 win.mEnterAnimationPending = false;
2131 if (win.mSurface != null) {
2132 // If we are not currently running the exit animation, we
2133 // need to see about starting one.
2134 if (!win.mExiting) {
2135 // Try starting an animation; if there isn't one, we
2136 // can destroy the surface right away.
2137 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2138 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2139 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2140 }
2141 if (win.isWinVisibleLw() &&
2142 applyAnimationLocked(win, transit, false)) {
2143 win.mExiting = true;
2144 mKeyWaiter.finishedKey(session, client, true,
2145 KeyWaiter.RETURN_NOTHING);
2146 } else if (win.isAnimating()) {
2147 // Currently in a hide animation... turn this into
2148 // an exit.
2149 win.mExiting = true;
2150 } else {
2151 if (mInputMethodWindow == win) {
2152 mInputMethodWindow = null;
2153 }
2154 win.destroySurfaceLocked();
2155 }
2156 }
2157 }
2158 outSurface.clear();
2159 }
2160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 if (focusMayChange) {
2162 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2163 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 imMayMove = false;
2165 }
2166 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2167 }
Romain Guy06882f82009-06-10 13:36:04 -07002168
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002169 // updateFocusedWindowLocked() already assigned layers so we only need to
2170 // reassign them at this point if the IM window state gets shuffled
2171 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 if (imMayMove) {
2174 if (moveInputMethodWindowsIfNeededLocked(false)) {
2175 assignLayers = true;
2176 }
2177 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002178 if (wallpaperMayMove) {
2179 if (adjustWallpaperWindowsLocked()) {
2180 assignLayers = true;
2181 }
2182 }
Romain Guy06882f82009-06-10 13:36:04 -07002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 mLayoutNeeded = true;
2185 win.mGivenInsetsPending = insetsPending;
2186 if (assignLayers) {
2187 assignLayersLocked();
2188 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002189 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 performLayoutAndPlaceSurfacesLocked();
2191 if (win.mAppToken != null) {
2192 win.mAppToken.updateReportedVisibilityLocked();
2193 }
2194 outFrame.set(win.mFrame);
2195 outContentInsets.set(win.mContentInsets);
2196 outVisibleInsets.set(win.mVisibleInsets);
2197 if (localLOGV) Log.v(
2198 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002199 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 + ", requestedHeight=" + requestedHeight
2201 + ", viewVisibility=" + viewVisibility
2202 + "\nRelayout returning frame=" + outFrame
2203 + ", surface=" + outSurface);
2204
2205 if (localLOGV || DEBUG_FOCUS) Log.v(
2206 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2207
2208 inTouchMode = mInTouchMode;
2209 }
2210
2211 if (newConfig != null) {
2212 sendNewConfiguration();
2213 }
Romain Guy06882f82009-06-10 13:36:04 -07002214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2218 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2219 }
2220
2221 public void finishDrawingWindow(Session session, IWindow client) {
2222 final long origId = Binder.clearCallingIdentity();
2223 synchronized(mWindowMap) {
2224 WindowState win = windowForClientLocked(session, client);
2225 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002226 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2227 adjustWallpaperWindowsLocked();
2228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 mLayoutNeeded = true;
2230 performLayoutAndPlaceSurfacesLocked();
2231 }
2232 }
2233 Binder.restoreCallingIdentity(origId);
2234 }
2235
2236 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2237 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2238 + (lp != null ? lp.packageName : null)
2239 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2240 if (lp != null && lp.windowAnimations != 0) {
2241 // If this is a system resource, don't try to load it from the
2242 // application resources. It is nice to avoid loading application
2243 // resources if we can.
2244 String packageName = lp.packageName != null ? lp.packageName : "android";
2245 int resId = lp.windowAnimations;
2246 if ((resId&0xFF000000) == 0x01000000) {
2247 packageName = "android";
2248 }
2249 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2250 + packageName);
2251 return AttributeCache.instance().get(packageName, resId,
2252 com.android.internal.R.styleable.WindowAnimation);
2253 }
2254 return null;
2255 }
Romain Guy06882f82009-06-10 13:36:04 -07002256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 private void applyEnterAnimationLocked(WindowState win) {
2258 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2259 if (win.mEnterAnimationPending) {
2260 win.mEnterAnimationPending = false;
2261 transit = WindowManagerPolicy.TRANSIT_ENTER;
2262 }
2263
2264 applyAnimationLocked(win, transit, true);
2265 }
2266
2267 private boolean applyAnimationLocked(WindowState win,
2268 int transit, boolean isEntrance) {
2269 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2270 // If we are trying to apply an animation, but already running
2271 // an animation of the same type, then just leave that one alone.
2272 return true;
2273 }
Romain Guy06882f82009-06-10 13:36:04 -07002274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 // Only apply an animation if the display isn't frozen. If it is
2276 // frozen, there is no reason to animate and it can cause strange
2277 // artifacts when we unfreeze the display if some different animation
2278 // is running.
2279 if (!mDisplayFrozen) {
2280 int anim = mPolicy.selectAnimationLw(win, transit);
2281 int attr = -1;
2282 Animation a = null;
2283 if (anim != 0) {
2284 a = AnimationUtils.loadAnimation(mContext, anim);
2285 } else {
2286 switch (transit) {
2287 case WindowManagerPolicy.TRANSIT_ENTER:
2288 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2289 break;
2290 case WindowManagerPolicy.TRANSIT_EXIT:
2291 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2292 break;
2293 case WindowManagerPolicy.TRANSIT_SHOW:
2294 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2295 break;
2296 case WindowManagerPolicy.TRANSIT_HIDE:
2297 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2298 break;
2299 }
2300 if (attr >= 0) {
2301 a = loadAnimation(win.mAttrs, attr);
2302 }
2303 }
2304 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2305 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2306 + " mAnimation=" + win.mAnimation
2307 + " isEntrance=" + isEntrance);
2308 if (a != null) {
2309 if (DEBUG_ANIM) {
2310 RuntimeException e = new RuntimeException();
2311 e.fillInStackTrace();
2312 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2313 }
2314 win.setAnimation(a);
2315 win.mAnimationIsEntrance = isEntrance;
2316 }
2317 } else {
2318 win.clearAnimation();
2319 }
2320
2321 return win.mAnimation != null;
2322 }
2323
2324 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2325 int anim = 0;
2326 Context context = mContext;
2327 if (animAttr >= 0) {
2328 AttributeCache.Entry ent = getCachedAnimations(lp);
2329 if (ent != null) {
2330 context = ent.context;
2331 anim = ent.array.getResourceId(animAttr, 0);
2332 }
2333 }
2334 if (anim != 0) {
2335 return AnimationUtils.loadAnimation(context, anim);
2336 }
2337 return null;
2338 }
Romain Guy06882f82009-06-10 13:36:04 -07002339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 private boolean applyAnimationLocked(AppWindowToken wtoken,
2341 WindowManager.LayoutParams lp, int transit, boolean enter) {
2342 // Only apply an animation if the display isn't frozen. If it is
2343 // frozen, there is no reason to animate and it can cause strange
2344 // artifacts when we unfreeze the display if some different animation
2345 // is running.
2346 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002347 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002348 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002349 a = new FadeInOutAnimation(enter);
2350 if (DEBUG_ANIM) Log.v(TAG,
2351 "applying FadeInOutAnimation for a window in compatibility mode");
2352 } else {
2353 int animAttr = 0;
2354 switch (transit) {
2355 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2356 animAttr = enter
2357 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2358 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2359 break;
2360 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2361 animAttr = enter
2362 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2363 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2364 break;
2365 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2366 animAttr = enter
2367 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2368 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2369 break;
2370 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2371 animAttr = enter
2372 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2373 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2374 break;
2375 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2376 animAttr = enter
2377 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2378 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2379 break;
2380 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2381 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002382 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002383 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2384 break;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002385 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN:
2386 animAttr = enter
2387 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenEnterAnimation
2388 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityOpenExitAnimation;
2389 break;
2390 case WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE:
2391 animAttr = enter
2392 ? com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseEnterAnimation
2393 : com.android.internal.R.styleable.WindowAnimation_wallpaperActivityCloseExitAnimation;
2394 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002395 }
2396 a = loadAnimation(lp, animAttr);
2397 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2398 + " anim=" + a
2399 + " animAttr=0x" + Integer.toHexString(animAttr)
2400 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 if (a != null) {
2403 if (DEBUG_ANIM) {
2404 RuntimeException e = new RuntimeException();
2405 e.fillInStackTrace();
2406 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2407 }
2408 wtoken.setAnimation(a);
2409 }
2410 } else {
2411 wtoken.clearAnimation();
2412 }
2413
2414 return wtoken.animation != null;
2415 }
2416
2417 // -------------------------------------------------------------
2418 // Application Window Tokens
2419 // -------------------------------------------------------------
2420
2421 public void validateAppTokens(List tokens) {
2422 int v = tokens.size()-1;
2423 int m = mAppTokens.size()-1;
2424 while (v >= 0 && m >= 0) {
2425 AppWindowToken wtoken = mAppTokens.get(m);
2426 if (wtoken.removed) {
2427 m--;
2428 continue;
2429 }
2430 if (tokens.get(v) != wtoken.token) {
2431 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2432 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2433 }
2434 v--;
2435 m--;
2436 }
2437 while (v >= 0) {
2438 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2439 v--;
2440 }
2441 while (m >= 0) {
2442 AppWindowToken wtoken = mAppTokens.get(m);
2443 if (!wtoken.removed) {
2444 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2445 }
2446 m--;
2447 }
2448 }
2449
2450 boolean checkCallingPermission(String permission, String func) {
2451 // Quick check: if the calling permission is me, it's all okay.
2452 if (Binder.getCallingPid() == Process.myPid()) {
2453 return true;
2454 }
Romain Guy06882f82009-06-10 13:36:04 -07002455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 if (mContext.checkCallingPermission(permission)
2457 == PackageManager.PERMISSION_GRANTED) {
2458 return true;
2459 }
2460 String msg = "Permission Denial: " + func + " from pid="
2461 + Binder.getCallingPid()
2462 + ", uid=" + Binder.getCallingUid()
2463 + " requires " + permission;
2464 Log.w(TAG, msg);
2465 return false;
2466 }
Romain Guy06882f82009-06-10 13:36:04 -07002467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 AppWindowToken findAppWindowToken(IBinder token) {
2469 WindowToken wtoken = mTokenMap.get(token);
2470 if (wtoken == null) {
2471 return null;
2472 }
2473 return wtoken.appWindowToken;
2474 }
Romain Guy06882f82009-06-10 13:36:04 -07002475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 public void addWindowToken(IBinder token, int type) {
2477 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2478 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002479 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
Romain Guy06882f82009-06-10 13:36:04 -07002481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 synchronized(mWindowMap) {
2483 WindowToken wtoken = mTokenMap.get(token);
2484 if (wtoken != null) {
2485 Log.w(TAG, "Attempted to add existing input method token: " + token);
2486 return;
2487 }
2488 wtoken = new WindowToken(token, type, true);
2489 mTokenMap.put(token, wtoken);
2490 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002491 if (type == TYPE_WALLPAPER) {
2492 mWallpaperTokens.add(wtoken);
2493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495 }
Romain Guy06882f82009-06-10 13:36:04 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 public void removeWindowToken(IBinder token) {
2498 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2499 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002500 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 }
2502
2503 final long origId = Binder.clearCallingIdentity();
2504 synchronized(mWindowMap) {
2505 WindowToken wtoken = mTokenMap.remove(token);
2506 mTokenList.remove(wtoken);
2507 if (wtoken != null) {
2508 boolean delayed = false;
2509 if (!wtoken.hidden) {
2510 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 final int N = wtoken.windows.size();
2513 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 for (int i=0; i<N; i++) {
2516 WindowState win = wtoken.windows.get(i);
2517
2518 if (win.isAnimating()) {
2519 delayed = true;
2520 }
Romain Guy06882f82009-06-10 13:36:04 -07002521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 if (win.isVisibleNow()) {
2523 applyAnimationLocked(win,
2524 WindowManagerPolicy.TRANSIT_EXIT, false);
2525 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2526 KeyWaiter.RETURN_NOTHING);
2527 changed = true;
2528 }
2529 }
2530
2531 if (changed) {
2532 mLayoutNeeded = true;
2533 performLayoutAndPlaceSurfacesLocked();
2534 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2535 }
Romain Guy06882f82009-06-10 13:36:04 -07002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 if (delayed) {
2538 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002539 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2540 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
2542 }
Romain Guy06882f82009-06-10 13:36:04 -07002543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 } else {
2545 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2546 }
2547 }
2548 Binder.restoreCallingIdentity(origId);
2549 }
2550
2551 public void addAppToken(int addPos, IApplicationToken token,
2552 int groupId, int requestedOrientation, boolean fullscreen) {
2553 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2554 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002555 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
Romain Guy06882f82009-06-10 13:36:04 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 synchronized(mWindowMap) {
2559 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2560 if (wtoken != null) {
2561 Log.w(TAG, "Attempted to add existing app token: " + token);
2562 return;
2563 }
2564 wtoken = new AppWindowToken(token);
2565 wtoken.groupId = groupId;
2566 wtoken.appFullscreen = fullscreen;
2567 wtoken.requestedOrientation = requestedOrientation;
2568 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002569 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 mTokenMap.put(token.asBinder(), wtoken);
2571 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 // Application tokens start out hidden.
2574 wtoken.hidden = true;
2575 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 //dump();
2578 }
2579 }
Romain Guy06882f82009-06-10 13:36:04 -07002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 public void setAppGroupId(IBinder token, int groupId) {
2582 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2583 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002584 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
2586
2587 synchronized(mWindowMap) {
2588 AppWindowToken wtoken = findAppWindowToken(token);
2589 if (wtoken == null) {
2590 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2591 return;
2592 }
2593 wtoken.groupId = groupId;
2594 }
2595 }
Romain Guy06882f82009-06-10 13:36:04 -07002596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 public int getOrientationFromWindowsLocked() {
2598 int pos = mWindows.size() - 1;
2599 while (pos >= 0) {
2600 WindowState wtoken = (WindowState) mWindows.get(pos);
2601 pos--;
2602 if (wtoken.mAppToken != null) {
2603 // We hit an application window. so the orientation will be determined by the
2604 // app window. No point in continuing further.
2605 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2606 }
2607 if (!wtoken.isVisibleLw()) {
2608 continue;
2609 }
2610 int req = wtoken.mAttrs.screenOrientation;
2611 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2612 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2613 continue;
2614 } else {
2615 return req;
2616 }
2617 }
2618 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2619 }
Romain Guy06882f82009-06-10 13:36:04 -07002620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 public int getOrientationFromAppTokensLocked() {
2622 int pos = mAppTokens.size() - 1;
2623 int curGroup = 0;
2624 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002625 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002627 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 while (pos >= 0) {
2629 AppWindowToken wtoken = mAppTokens.get(pos);
2630 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002631 // if we're about to tear down this window and not seek for
2632 // the behind activity, don't use it for orientation
2633 if (!findingBehind
2634 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002635 continue;
2636 }
2637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 if (!haveGroup) {
2639 // We ignore any hidden applications on the top.
2640 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2641 continue;
2642 }
2643 haveGroup = true;
2644 curGroup = wtoken.groupId;
2645 lastOrientation = wtoken.requestedOrientation;
2646 } else if (curGroup != wtoken.groupId) {
2647 // If we have hit a new application group, and the bottom
2648 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002649 // the orientation behind it, and the last app was
2650 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002652 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2653 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 return lastOrientation;
2655 }
2656 }
2657 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002658 // If this application is fullscreen, and didn't explicitly say
2659 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002661 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002662 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002663 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 return or;
2665 }
2666 // If this application has requested an explicit orientation,
2667 // then use it.
2668 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2669 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2670 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2671 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2672 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2673 return or;
2674 }
Owen Lin3413b892009-05-01 17:12:32 -07002675 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
2677 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2678 }
Romain Guy06882f82009-06-10 13:36:04 -07002679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002681 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002682 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2683 "updateOrientationFromAppTokens()")) {
2684 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2685 }
2686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 Configuration config;
2688 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002689 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2690 freezeThisOneIfNeeded);
2691 Binder.restoreCallingIdentity(ident);
2692 return config;
2693 }
2694
2695 Configuration updateOrientationFromAppTokensUnchecked(
2696 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2697 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002699 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 }
2701 if (config != null) {
2702 mLayoutNeeded = true;
2703 performLayoutAndPlaceSurfacesLocked();
2704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 return config;
2706 }
Romain Guy06882f82009-06-10 13:36:04 -07002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 /*
2709 * The orientation is computed from non-application windows first. If none of
2710 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002711 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2713 * android.os.IBinder)
2714 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002715 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002716 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 long ident = Binder.clearCallingIdentity();
2719 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002720 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 if (req != mForcedAppOrientation) {
2723 changed = true;
2724 mForcedAppOrientation = req;
2725 //send a message to Policy indicating orientation change to take
2726 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002727 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 }
Romain Guy06882f82009-06-10 13:36:04 -07002729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 if (changed) {
2731 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002732 WindowManagerPolicy.USE_LAST_ROTATION,
2733 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 if (changed) {
2735 if (freezeThisOneIfNeeded != null) {
2736 AppWindowToken wtoken = findAppWindowToken(
2737 freezeThisOneIfNeeded);
2738 if (wtoken != null) {
2739 startAppFreezingScreenLocked(wtoken,
2740 ActivityInfo.CONFIG_ORIENTATION);
2741 }
2742 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002743 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002746
2747 // No obvious action we need to take, but if our current
2748 // state mismatches the activity maanager's, update it
2749 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002750 mTempConfiguration.setToDefaults();
2751 if (computeNewConfigurationLocked(mTempConfiguration)) {
2752 if (appConfig.diff(mTempConfiguration) != 0) {
2753 Log.i(TAG, "Config changed: " + mTempConfiguration);
2754 return new Configuration(mTempConfiguration);
2755 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002756 }
2757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 } finally {
2759 Binder.restoreCallingIdentity(ident);
2760 }
Romain Guy06882f82009-06-10 13:36:04 -07002761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 return null;
2763 }
Romain Guy06882f82009-06-10 13:36:04 -07002764
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002765 int computeForcedAppOrientationLocked() {
2766 int req = getOrientationFromWindowsLocked();
2767 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2768 req = getOrientationFromAppTokensLocked();
2769 }
2770 return req;
2771 }
Romain Guy06882f82009-06-10 13:36:04 -07002772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2774 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2775 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002776 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
Romain Guy06882f82009-06-10 13:36:04 -07002778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 synchronized(mWindowMap) {
2780 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2781 if (wtoken == null) {
2782 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2783 return;
2784 }
Romain Guy06882f82009-06-10 13:36:04 -07002785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 wtoken.requestedOrientation = requestedOrientation;
2787 }
2788 }
Romain Guy06882f82009-06-10 13:36:04 -07002789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 public int getAppOrientation(IApplicationToken token) {
2791 synchronized(mWindowMap) {
2792 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2793 if (wtoken == null) {
2794 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2795 }
Romain Guy06882f82009-06-10 13:36:04 -07002796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 return wtoken.requestedOrientation;
2798 }
2799 }
Romain Guy06882f82009-06-10 13:36:04 -07002800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2802 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2803 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002804 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 }
2806
2807 synchronized(mWindowMap) {
2808 boolean changed = false;
2809 if (token == null) {
2810 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2811 changed = mFocusedApp != null;
2812 mFocusedApp = null;
2813 mKeyWaiter.tickle();
2814 } else {
2815 AppWindowToken newFocus = findAppWindowToken(token);
2816 if (newFocus == null) {
2817 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2818 return;
2819 }
2820 changed = mFocusedApp != newFocus;
2821 mFocusedApp = newFocus;
2822 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2823 mKeyWaiter.tickle();
2824 }
2825
2826 if (moveFocusNow && changed) {
2827 final long origId = Binder.clearCallingIdentity();
2828 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2829 Binder.restoreCallingIdentity(origId);
2830 }
2831 }
2832 }
2833
2834 public void prepareAppTransition(int transit) {
2835 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2836 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002837 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 }
Romain Guy06882f82009-06-10 13:36:04 -07002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 synchronized(mWindowMap) {
2841 if (DEBUG_APP_TRANSITIONS) Log.v(
2842 TAG, "Prepare app transition: transit=" + transit
2843 + " mNextAppTransition=" + mNextAppTransition);
2844 if (!mDisplayFrozen) {
2845 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2846 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002847 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2848 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2849 // Opening a new task always supersedes a close for the anim.
2850 mNextAppTransition = transit;
2851 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2852 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2853 // Opening a new activity always supersedes a close for the anim.
2854 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
2856 mAppTransitionReady = false;
2857 mAppTransitionTimeout = false;
2858 mStartingIconInTransition = false;
2859 mSkipAppTransitionAnimation = false;
2860 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2861 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2862 5000);
2863 }
2864 }
2865 }
2866
2867 public int getPendingAppTransition() {
2868 return mNextAppTransition;
2869 }
Romain Guy06882f82009-06-10 13:36:04 -07002870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 public void executeAppTransition() {
2872 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2873 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002874 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 }
Romain Guy06882f82009-06-10 13:36:04 -07002876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 synchronized(mWindowMap) {
2878 if (DEBUG_APP_TRANSITIONS) Log.v(
2879 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2880 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2881 mAppTransitionReady = true;
2882 final long origId = Binder.clearCallingIdentity();
2883 performLayoutAndPlaceSurfacesLocked();
2884 Binder.restoreCallingIdentity(origId);
2885 }
2886 }
2887 }
2888
2889 public void setAppStartingWindow(IBinder token, String pkg,
2890 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2891 IBinder transferFrom, boolean createIfNeeded) {
2892 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2893 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002894 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 }
2896
2897 synchronized(mWindowMap) {
2898 if (DEBUG_STARTING_WINDOW) Log.v(
2899 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2900 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 AppWindowToken wtoken = findAppWindowToken(token);
2903 if (wtoken == null) {
2904 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2905 return;
2906 }
2907
2908 // If the display is frozen, we won't do anything until the
2909 // actual window is displayed so there is no reason to put in
2910 // the starting window.
2911 if (mDisplayFrozen) {
2912 return;
2913 }
Romain Guy06882f82009-06-10 13:36:04 -07002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 if (wtoken.startingData != null) {
2916 return;
2917 }
Romain Guy06882f82009-06-10 13:36:04 -07002918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 if (transferFrom != null) {
2920 AppWindowToken ttoken = findAppWindowToken(transferFrom);
2921 if (ttoken != null) {
2922 WindowState startingWindow = ttoken.startingWindow;
2923 if (startingWindow != null) {
2924 if (mStartingIconInTransition) {
2925 // In this case, the starting icon has already
2926 // been displayed, so start letting windows get
2927 // shown immediately without any more transitions.
2928 mSkipAppTransitionAnimation = true;
2929 }
2930 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2931 "Moving existing starting from " + ttoken
2932 + " to " + wtoken);
2933 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 // Transfer the starting window over to the new
2936 // token.
2937 wtoken.startingData = ttoken.startingData;
2938 wtoken.startingView = ttoken.startingView;
2939 wtoken.startingWindow = startingWindow;
2940 ttoken.startingData = null;
2941 ttoken.startingView = null;
2942 ttoken.startingWindow = null;
2943 ttoken.startingMoved = true;
2944 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07002945 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 startingWindow.mAppToken = wtoken;
2947 mWindows.remove(startingWindow);
2948 ttoken.windows.remove(startingWindow);
2949 ttoken.allAppWindows.remove(startingWindow);
2950 addWindowToListInOrderLocked(startingWindow, true);
2951 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07002952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 // Propagate other interesting state between the
2954 // tokens. If the old token is displayed, we should
2955 // immediately force the new one to be displayed. If
2956 // it is animating, we need to move that animation to
2957 // the new one.
2958 if (ttoken.allDrawn) {
2959 wtoken.allDrawn = true;
2960 }
2961 if (ttoken.firstWindowDrawn) {
2962 wtoken.firstWindowDrawn = true;
2963 }
2964 if (!ttoken.hidden) {
2965 wtoken.hidden = false;
2966 wtoken.hiddenRequested = false;
2967 wtoken.willBeHidden = false;
2968 }
2969 if (wtoken.clientHidden != ttoken.clientHidden) {
2970 wtoken.clientHidden = ttoken.clientHidden;
2971 wtoken.sendAppVisibilityToClients();
2972 }
2973 if (ttoken.animation != null) {
2974 wtoken.animation = ttoken.animation;
2975 wtoken.animating = ttoken.animating;
2976 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
2977 ttoken.animation = null;
2978 ttoken.animLayerAdjustment = 0;
2979 wtoken.updateLayers();
2980 ttoken.updateLayers();
2981 }
Romain Guy06882f82009-06-10 13:36:04 -07002982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 mLayoutNeeded = true;
2985 performLayoutAndPlaceSurfacesLocked();
2986 Binder.restoreCallingIdentity(origId);
2987 return;
2988 } else if (ttoken.startingData != null) {
2989 // The previous app was getting ready to show a
2990 // starting window, but hasn't yet done so. Steal it!
2991 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
2992 "Moving pending starting from " + ttoken
2993 + " to " + wtoken);
2994 wtoken.startingData = ttoken.startingData;
2995 ttoken.startingData = null;
2996 ttoken.startingMoved = true;
2997 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
2998 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2999 // want to process the message ASAP, before any other queued
3000 // messages.
3001 mH.sendMessageAtFrontOfQueue(m);
3002 return;
3003 }
3004 }
3005 }
3006
3007 // There is no existing starting window, and the caller doesn't
3008 // want us to create one, so that's it!
3009 if (!createIfNeeded) {
3010 return;
3011 }
Romain Guy06882f82009-06-10 13:36:04 -07003012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 mStartingIconInTransition = true;
3014 wtoken.startingData = new StartingData(
3015 pkg, theme, nonLocalizedLabel,
3016 labelRes, icon);
3017 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3018 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3019 // want to process the message ASAP, before any other queued
3020 // messages.
3021 mH.sendMessageAtFrontOfQueue(m);
3022 }
3023 }
3024
3025 public void setAppWillBeHidden(IBinder token) {
3026 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3027 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003028 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 }
3030
3031 AppWindowToken wtoken;
3032
3033 synchronized(mWindowMap) {
3034 wtoken = findAppWindowToken(token);
3035 if (wtoken == null) {
3036 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3037 return;
3038 }
3039 wtoken.willBeHidden = true;
3040 }
3041 }
Romain Guy06882f82009-06-10 13:36:04 -07003042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3044 boolean visible, int transit, boolean performLayout) {
3045 boolean delayed = false;
3046
3047 if (wtoken.clientHidden == visible) {
3048 wtoken.clientHidden = !visible;
3049 wtoken.sendAppVisibilityToClients();
3050 }
Romain Guy06882f82009-06-10 13:36:04 -07003051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 wtoken.willBeHidden = false;
3053 if (wtoken.hidden == visible) {
3054 final int N = wtoken.allAppWindows.size();
3055 boolean changed = false;
3056 if (DEBUG_APP_TRANSITIONS) Log.v(
3057 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3058 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3063 if (wtoken.animation == sDummyAnimation) {
3064 wtoken.animation = null;
3065 }
3066 applyAnimationLocked(wtoken, lp, transit, visible);
3067 changed = true;
3068 if (wtoken.animation != null) {
3069 delayed = runningAppAnimation = true;
3070 }
3071 }
Romain Guy06882f82009-06-10 13:36:04 -07003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 for (int i=0; i<N; i++) {
3074 WindowState win = wtoken.allAppWindows.get(i);
3075 if (win == wtoken.startingWindow) {
3076 continue;
3077 }
3078
3079 if (win.isAnimating()) {
3080 delayed = true;
3081 }
Romain Guy06882f82009-06-10 13:36:04 -07003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3084 //win.dump(" ");
3085 if (visible) {
3086 if (!win.isVisibleNow()) {
3087 if (!runningAppAnimation) {
3088 applyAnimationLocked(win,
3089 WindowManagerPolicy.TRANSIT_ENTER, true);
3090 }
3091 changed = true;
3092 }
3093 } else if (win.isVisibleNow()) {
3094 if (!runningAppAnimation) {
3095 applyAnimationLocked(win,
3096 WindowManagerPolicy.TRANSIT_EXIT, false);
3097 }
3098 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3099 KeyWaiter.RETURN_NOTHING);
3100 changed = true;
3101 }
3102 }
3103
3104 wtoken.hidden = wtoken.hiddenRequested = !visible;
3105 if (!visible) {
3106 unsetAppFreezingScreenLocked(wtoken, true, true);
3107 } else {
3108 // If we are being set visible, and the starting window is
3109 // not yet displayed, then make sure it doesn't get displayed.
3110 WindowState swin = wtoken.startingWindow;
3111 if (swin != null && (swin.mDrawPending
3112 || swin.mCommitDrawPending)) {
3113 swin.mPolicyVisibility = false;
3114 swin.mPolicyVisibilityAfterAnim = false;
3115 }
3116 }
Romain Guy06882f82009-06-10 13:36:04 -07003117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3119 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3120 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 if (changed && performLayout) {
3123 mLayoutNeeded = true;
3124 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 performLayoutAndPlaceSurfacesLocked();
3126 }
3127 }
3128
3129 if (wtoken.animation != null) {
3130 delayed = true;
3131 }
Romain Guy06882f82009-06-10 13:36:04 -07003132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 return delayed;
3134 }
3135
3136 public void setAppVisibility(IBinder token, boolean visible) {
3137 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3138 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003139 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 }
3141
3142 AppWindowToken wtoken;
3143
3144 synchronized(mWindowMap) {
3145 wtoken = findAppWindowToken(token);
3146 if (wtoken == null) {
3147 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3148 return;
3149 }
3150
3151 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3152 RuntimeException e = new RuntimeException();
3153 e.fillInStackTrace();
3154 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3155 + "): mNextAppTransition=" + mNextAppTransition
3156 + " hidden=" + wtoken.hidden
3157 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3158 }
Romain Guy06882f82009-06-10 13:36:04 -07003159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 // If we are preparing an app transition, then delay changing
3161 // the visibility of this token until we execute that transition.
3162 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3163 // Already in requested state, don't do anything more.
3164 if (wtoken.hiddenRequested != visible) {
3165 return;
3166 }
3167 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 if (DEBUG_APP_TRANSITIONS) Log.v(
3170 TAG, "Setting dummy animation on: " + wtoken);
3171 wtoken.setDummyAnimation();
3172 mOpeningApps.remove(wtoken);
3173 mClosingApps.remove(wtoken);
3174 wtoken.inPendingTransaction = true;
3175 if (visible) {
3176 mOpeningApps.add(wtoken);
3177 wtoken.allDrawn = false;
3178 wtoken.startingDisplayed = false;
3179 wtoken.startingMoved = false;
Romain Guy06882f82009-06-10 13:36:04 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 if (wtoken.clientHidden) {
3182 // In the case where we are making an app visible
3183 // but holding off for a transition, we still need
3184 // to tell the client to make its windows visible so
3185 // they get drawn. Otherwise, we will wait on
3186 // performing the transition until all windows have
3187 // been drawn, they never will be, and we are sad.
3188 wtoken.clientHidden = false;
3189 wtoken.sendAppVisibilityToClients();
3190 }
3191 } else {
3192 mClosingApps.add(wtoken);
3193 }
3194 return;
3195 }
Romain Guy06882f82009-06-10 13:36:04 -07003196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 final long origId = Binder.clearCallingIdentity();
3198 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3199 wtoken.updateReportedVisibilityLocked();
3200 Binder.restoreCallingIdentity(origId);
3201 }
3202 }
3203
3204 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3205 boolean unfreezeSurfaceNow, boolean force) {
3206 if (wtoken.freezingScreen) {
3207 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3208 + " force=" + force);
3209 final int N = wtoken.allAppWindows.size();
3210 boolean unfrozeWindows = false;
3211 for (int i=0; i<N; i++) {
3212 WindowState w = wtoken.allAppWindows.get(i);
3213 if (w.mAppFreezing) {
3214 w.mAppFreezing = false;
3215 if (w.mSurface != null && !w.mOrientationChanging) {
3216 w.mOrientationChanging = true;
3217 }
3218 unfrozeWindows = true;
3219 }
3220 }
3221 if (force || unfrozeWindows) {
3222 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3223 wtoken.freezingScreen = false;
3224 mAppsFreezingScreen--;
3225 }
3226 if (unfreezeSurfaceNow) {
3227 if (unfrozeWindows) {
3228 mLayoutNeeded = true;
3229 performLayoutAndPlaceSurfacesLocked();
3230 }
3231 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3232 stopFreezingDisplayLocked();
3233 }
3234 }
3235 }
3236 }
Romain Guy06882f82009-06-10 13:36:04 -07003237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3239 int configChanges) {
3240 if (DEBUG_ORIENTATION) {
3241 RuntimeException e = new RuntimeException();
3242 e.fillInStackTrace();
3243 Log.i(TAG, "Set freezing of " + wtoken.appToken
3244 + ": hidden=" + wtoken.hidden + " freezing="
3245 + wtoken.freezingScreen, e);
3246 }
3247 if (!wtoken.hiddenRequested) {
3248 if (!wtoken.freezingScreen) {
3249 wtoken.freezingScreen = true;
3250 mAppsFreezingScreen++;
3251 if (mAppsFreezingScreen == 1) {
3252 startFreezingDisplayLocked();
3253 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3254 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3255 5000);
3256 }
3257 }
3258 final int N = wtoken.allAppWindows.size();
3259 for (int i=0; i<N; i++) {
3260 WindowState w = wtoken.allAppWindows.get(i);
3261 w.mAppFreezing = true;
3262 }
3263 }
3264 }
Romain Guy06882f82009-06-10 13:36:04 -07003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 public void startAppFreezingScreen(IBinder token, int configChanges) {
3267 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3268 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003269 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 }
3271
3272 synchronized(mWindowMap) {
3273 if (configChanges == 0 && !mDisplayFrozen) {
3274 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3275 return;
3276 }
Romain Guy06882f82009-06-10 13:36:04 -07003277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 AppWindowToken wtoken = findAppWindowToken(token);
3279 if (wtoken == null || wtoken.appToken == null) {
3280 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3281 return;
3282 }
3283 final long origId = Binder.clearCallingIdentity();
3284 startAppFreezingScreenLocked(wtoken, configChanges);
3285 Binder.restoreCallingIdentity(origId);
3286 }
3287 }
Romain Guy06882f82009-06-10 13:36:04 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 public void stopAppFreezingScreen(IBinder token, boolean force) {
3290 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3291 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003292 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 }
3294
3295 synchronized(mWindowMap) {
3296 AppWindowToken wtoken = findAppWindowToken(token);
3297 if (wtoken == null || wtoken.appToken == null) {
3298 return;
3299 }
3300 final long origId = Binder.clearCallingIdentity();
3301 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3302 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3303 unsetAppFreezingScreenLocked(wtoken, true, force);
3304 Binder.restoreCallingIdentity(origId);
3305 }
3306 }
Romain Guy06882f82009-06-10 13:36:04 -07003307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 public void removeAppToken(IBinder token) {
3309 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3310 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003311 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 }
3313
3314 AppWindowToken wtoken = null;
3315 AppWindowToken startingToken = null;
3316 boolean delayed = false;
3317
3318 final long origId = Binder.clearCallingIdentity();
3319 synchronized(mWindowMap) {
3320 WindowToken basewtoken = mTokenMap.remove(token);
3321 mTokenList.remove(basewtoken);
3322 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3323 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3324 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3325 wtoken.inPendingTransaction = false;
3326 mOpeningApps.remove(wtoken);
3327 if (mClosingApps.contains(wtoken)) {
3328 delayed = true;
3329 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3330 mClosingApps.add(wtoken);
3331 delayed = true;
3332 }
3333 if (DEBUG_APP_TRANSITIONS) Log.v(
3334 TAG, "Removing app " + wtoken + " delayed=" + delayed
3335 + " animation=" + wtoken.animation
3336 + " animating=" + wtoken.animating);
3337 if (delayed) {
3338 // set the token aside because it has an active animation to be finished
3339 mExitingAppTokens.add(wtoken);
3340 }
3341 mAppTokens.remove(wtoken);
3342 wtoken.removed = true;
3343 if (wtoken.startingData != null) {
3344 startingToken = wtoken;
3345 }
3346 unsetAppFreezingScreenLocked(wtoken, true, true);
3347 if (mFocusedApp == wtoken) {
3348 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3349 mFocusedApp = null;
3350 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3351 mKeyWaiter.tickle();
3352 }
3353 } else {
3354 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3355 }
Romain Guy06882f82009-06-10 13:36:04 -07003356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 if (!delayed && wtoken != null) {
3358 wtoken.updateReportedVisibilityLocked();
3359 }
3360 }
3361 Binder.restoreCallingIdentity(origId);
3362
3363 if (startingToken != null) {
3364 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3365 + startingToken + ": app token removed");
3366 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3367 mH.sendMessage(m);
3368 }
3369 }
3370
3371 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3372 final int NW = token.windows.size();
3373 for (int i=0; i<NW; i++) {
3374 WindowState win = token.windows.get(i);
3375 mWindows.remove(win);
3376 int j = win.mChildWindows.size();
3377 while (j > 0) {
3378 j--;
3379 mWindows.remove(win.mChildWindows.get(j));
3380 }
3381 }
3382 return NW > 0;
3383 }
3384
3385 void dumpAppTokensLocked() {
3386 for (int i=mAppTokens.size()-1; i>=0; i--) {
3387 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3388 }
3389 }
Romain Guy06882f82009-06-10 13:36:04 -07003390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 void dumpWindowsLocked() {
3392 for (int i=mWindows.size()-1; i>=0; i--) {
3393 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3394 }
3395 }
Romain Guy06882f82009-06-10 13:36:04 -07003396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 private int findWindowOffsetLocked(int tokenPos) {
3398 final int NW = mWindows.size();
3399
3400 if (tokenPos >= mAppTokens.size()) {
3401 int i = NW;
3402 while (i > 0) {
3403 i--;
3404 WindowState win = (WindowState)mWindows.get(i);
3405 if (win.getAppToken() != null) {
3406 return i+1;
3407 }
3408 }
3409 }
3410
3411 while (tokenPos > 0) {
3412 // Find the first app token below the new position that has
3413 // a window displayed.
3414 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3415 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3416 + tokenPos + " -- " + wtoken.token);
3417 int i = wtoken.windows.size();
3418 while (i > 0) {
3419 i--;
3420 WindowState win = wtoken.windows.get(i);
3421 int j = win.mChildWindows.size();
3422 while (j > 0) {
3423 j--;
3424 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3425 if (cwin.mSubLayer >= 0 ) {
3426 for (int pos=NW-1; pos>=0; pos--) {
3427 if (mWindows.get(pos) == cwin) {
3428 if (DEBUG_REORDER) Log.v(TAG,
3429 "Found child win @" + (pos+1));
3430 return pos+1;
3431 }
3432 }
3433 }
3434 }
3435 for (int pos=NW-1; pos>=0; pos--) {
3436 if (mWindows.get(pos) == win) {
3437 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3438 return pos+1;
3439 }
3440 }
3441 }
3442 tokenPos--;
3443 }
3444
3445 return 0;
3446 }
3447
3448 private final int reAddWindowLocked(int index, WindowState win) {
3449 final int NCW = win.mChildWindows.size();
3450 boolean added = false;
3451 for (int j=0; j<NCW; j++) {
3452 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3453 if (!added && cwin.mSubLayer >= 0) {
3454 mWindows.add(index, win);
3455 index++;
3456 added = true;
3457 }
3458 mWindows.add(index, cwin);
3459 index++;
3460 }
3461 if (!added) {
3462 mWindows.add(index, win);
3463 index++;
3464 }
3465 return index;
3466 }
Romain Guy06882f82009-06-10 13:36:04 -07003467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3469 final int NW = token.windows.size();
3470 for (int i=0; i<NW; i++) {
3471 index = reAddWindowLocked(index, token.windows.get(i));
3472 }
3473 return index;
3474 }
3475
3476 public void moveAppToken(int index, IBinder token) {
3477 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3478 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003479 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 }
3481
3482 synchronized(mWindowMap) {
3483 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3484 if (DEBUG_REORDER) dumpAppTokensLocked();
3485 final AppWindowToken wtoken = findAppWindowToken(token);
3486 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3487 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3488 + token + " (" + wtoken + ")");
3489 return;
3490 }
3491 mAppTokens.add(index, wtoken);
3492 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3493 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 final long origId = Binder.clearCallingIdentity();
3496 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3497 if (DEBUG_REORDER) dumpWindowsLocked();
3498 if (tmpRemoveAppWindowsLocked(wtoken)) {
3499 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3500 if (DEBUG_REORDER) dumpWindowsLocked();
3501 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3502 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3503 if (DEBUG_REORDER) dumpWindowsLocked();
3504 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 mLayoutNeeded = true;
3506 performLayoutAndPlaceSurfacesLocked();
3507 }
3508 Binder.restoreCallingIdentity(origId);
3509 }
3510 }
3511
3512 private void removeAppTokensLocked(List<IBinder> tokens) {
3513 // XXX This should be done more efficiently!
3514 // (take advantage of the fact that both lists should be
3515 // ordered in the same way.)
3516 int N = tokens.size();
3517 for (int i=0; i<N; i++) {
3518 IBinder token = tokens.get(i);
3519 final AppWindowToken wtoken = findAppWindowToken(token);
3520 if (!mAppTokens.remove(wtoken)) {
3521 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3522 + token + " (" + wtoken + ")");
3523 i--;
3524 N--;
3525 }
3526 }
3527 }
3528
3529 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3530 // First remove all of the windows from the list.
3531 final int N = tokens.size();
3532 int i;
3533 for (i=0; i<N; i++) {
3534 WindowToken token = mTokenMap.get(tokens.get(i));
3535 if (token != null) {
3536 tmpRemoveAppWindowsLocked(token);
3537 }
3538 }
3539
3540 // Where to start adding?
3541 int pos = findWindowOffsetLocked(tokenPos);
3542
3543 // And now add them back at the correct place.
3544 for (i=0; i<N; i++) {
3545 WindowToken token = mTokenMap.get(tokens.get(i));
3546 if (token != null) {
3547 pos = reAddAppWindowsLocked(pos, token);
3548 }
3549 }
3550
3551 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 mLayoutNeeded = true;
3553 performLayoutAndPlaceSurfacesLocked();
3554
3555 //dump();
3556 }
3557
3558 public void moveAppTokensToTop(List<IBinder> tokens) {
3559 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3560 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003561 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563
3564 final long origId = Binder.clearCallingIdentity();
3565 synchronized(mWindowMap) {
3566 removeAppTokensLocked(tokens);
3567 final int N = tokens.size();
3568 for (int i=0; i<N; i++) {
3569 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3570 if (wt != null) {
3571 mAppTokens.add(wt);
3572 }
3573 }
3574 moveAppWindowsLocked(tokens, mAppTokens.size());
3575 }
3576 Binder.restoreCallingIdentity(origId);
3577 }
3578
3579 public void moveAppTokensToBottom(List<IBinder> tokens) {
3580 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3581 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003582 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 }
3584
3585 final long origId = Binder.clearCallingIdentity();
3586 synchronized(mWindowMap) {
3587 removeAppTokensLocked(tokens);
3588 final int N = tokens.size();
3589 int pos = 0;
3590 for (int i=0; i<N; i++) {
3591 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3592 if (wt != null) {
3593 mAppTokens.add(pos, wt);
3594 pos++;
3595 }
3596 }
3597 moveAppWindowsLocked(tokens, 0);
3598 }
3599 Binder.restoreCallingIdentity(origId);
3600 }
3601
3602 // -------------------------------------------------------------
3603 // Misc IWindowSession methods
3604 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 public void disableKeyguard(IBinder token, String tag) {
3607 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3608 != PackageManager.PERMISSION_GRANTED) {
3609 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3610 }
3611 mKeyguardDisabled.acquire(token, tag);
3612 }
3613
3614 public void reenableKeyguard(IBinder token) {
3615 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3616 != PackageManager.PERMISSION_GRANTED) {
3617 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3618 }
3619 synchronized (mKeyguardDisabled) {
3620 mKeyguardDisabled.release(token);
3621
3622 if (!mKeyguardDisabled.isAcquired()) {
3623 // if we are the last one to reenable the keyguard wait until
3624 // we have actaully finished reenabling until returning
3625 mWaitingUntilKeyguardReenabled = true;
3626 while (mWaitingUntilKeyguardReenabled) {
3627 try {
3628 mKeyguardDisabled.wait();
3629 } catch (InterruptedException e) {
3630 Thread.currentThread().interrupt();
3631 }
3632 }
3633 }
3634 }
3635 }
3636
3637 /**
3638 * @see android.app.KeyguardManager#exitKeyguardSecurely
3639 */
3640 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3641 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3642 != PackageManager.PERMISSION_GRANTED) {
3643 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3644 }
3645 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3646 public void onKeyguardExitResult(boolean success) {
3647 try {
3648 callback.onKeyguardExitResult(success);
3649 } catch (RemoteException e) {
3650 // Client has died, we don't care.
3651 }
3652 }
3653 });
3654 }
3655
3656 public boolean inKeyguardRestrictedInputMode() {
3657 return mPolicy.inKeyguardRestrictedKeyInputMode();
3658 }
Romain Guy06882f82009-06-10 13:36:04 -07003659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 static float fixScale(float scale) {
3661 if (scale < 0) scale = 0;
3662 else if (scale > 20) scale = 20;
3663 return Math.abs(scale);
3664 }
Romain Guy06882f82009-06-10 13:36:04 -07003665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 public void setAnimationScale(int which, float scale) {
3667 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3668 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003669 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 }
3671
3672 if (scale < 0) scale = 0;
3673 else if (scale > 20) scale = 20;
3674 scale = Math.abs(scale);
3675 switch (which) {
3676 case 0: mWindowAnimationScale = fixScale(scale); break;
3677 case 1: mTransitionAnimationScale = fixScale(scale); break;
3678 }
Romain Guy06882f82009-06-10 13:36:04 -07003679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 // Persist setting
3681 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3682 }
Romain Guy06882f82009-06-10 13:36:04 -07003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 public void setAnimationScales(float[] scales) {
3685 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3686 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003687 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 }
3689
3690 if (scales != null) {
3691 if (scales.length >= 1) {
3692 mWindowAnimationScale = fixScale(scales[0]);
3693 }
3694 if (scales.length >= 2) {
3695 mTransitionAnimationScale = fixScale(scales[1]);
3696 }
3697 }
Romain Guy06882f82009-06-10 13:36:04 -07003698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 // Persist setting
3700 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3701 }
Romain Guy06882f82009-06-10 13:36:04 -07003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 public float getAnimationScale(int which) {
3704 switch (which) {
3705 case 0: return mWindowAnimationScale;
3706 case 1: return mTransitionAnimationScale;
3707 }
3708 return 0;
3709 }
Romain Guy06882f82009-06-10 13:36:04 -07003710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 public float[] getAnimationScales() {
3712 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3713 }
Romain Guy06882f82009-06-10 13:36:04 -07003714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 public int getSwitchState(int sw) {
3716 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3717 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003718 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 }
3720 return KeyInputQueue.getSwitchState(sw);
3721 }
Romain Guy06882f82009-06-10 13:36:04 -07003722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 public int getSwitchStateForDevice(int devid, int sw) {
3724 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3725 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003726 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
3728 return KeyInputQueue.getSwitchState(devid, sw);
3729 }
Romain Guy06882f82009-06-10 13:36:04 -07003730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 public int getScancodeState(int sw) {
3732 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3733 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003734 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 }
3736 return KeyInputQueue.getScancodeState(sw);
3737 }
Romain Guy06882f82009-06-10 13:36:04 -07003738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 public int getScancodeStateForDevice(int devid, int sw) {
3740 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3741 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003742 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 }
3744 return KeyInputQueue.getScancodeState(devid, sw);
3745 }
Romain Guy06882f82009-06-10 13:36:04 -07003746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 public int getKeycodeState(int sw) {
3748 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3749 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003750 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 }
3752 return KeyInputQueue.getKeycodeState(sw);
3753 }
Romain Guy06882f82009-06-10 13:36:04 -07003754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 public int getKeycodeStateForDevice(int devid, int sw) {
3756 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3757 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003758 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
3760 return KeyInputQueue.getKeycodeState(devid, sw);
3761 }
Romain Guy06882f82009-06-10 13:36:04 -07003762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3764 return KeyInputQueue.hasKeys(keycodes, keyExists);
3765 }
Romain Guy06882f82009-06-10 13:36:04 -07003766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 public void enableScreenAfterBoot() {
3768 synchronized(mWindowMap) {
3769 if (mSystemBooted) {
3770 return;
3771 }
3772 mSystemBooted = true;
3773 }
Romain Guy06882f82009-06-10 13:36:04 -07003774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 performEnableScreen();
3776 }
Romain Guy06882f82009-06-10 13:36:04 -07003777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 public void enableScreenIfNeededLocked() {
3779 if (mDisplayEnabled) {
3780 return;
3781 }
3782 if (!mSystemBooted) {
3783 return;
3784 }
3785 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3786 }
Romain Guy06882f82009-06-10 13:36:04 -07003787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 public void performEnableScreen() {
3789 synchronized(mWindowMap) {
3790 if (mDisplayEnabled) {
3791 return;
3792 }
3793 if (!mSystemBooted) {
3794 return;
3795 }
Romain Guy06882f82009-06-10 13:36:04 -07003796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 // Don't enable the screen until all existing windows
3798 // have been drawn.
3799 final int N = mWindows.size();
3800 for (int i=0; i<N; i++) {
3801 WindowState w = (WindowState)mWindows.get(i);
3802 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3803 return;
3804 }
3805 }
Romain Guy06882f82009-06-10 13:36:04 -07003806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 mDisplayEnabled = true;
3808 if (false) {
3809 Log.i(TAG, "ENABLING SCREEN!");
3810 StringWriter sw = new StringWriter();
3811 PrintWriter pw = new PrintWriter(sw);
3812 this.dump(null, pw, null);
3813 Log.i(TAG, sw.toString());
3814 }
3815 try {
3816 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3817 if (surfaceFlinger != null) {
3818 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3819 Parcel data = Parcel.obtain();
3820 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3821 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3822 data, null, 0);
3823 data.recycle();
3824 }
3825 } catch (RemoteException ex) {
3826 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3827 }
3828 }
Romain Guy06882f82009-06-10 13:36:04 -07003829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003833 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3834 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 }
Romain Guy06882f82009-06-10 13:36:04 -07003836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 public void setInTouchMode(boolean mode) {
3838 synchronized(mWindowMap) {
3839 mInTouchMode = mode;
3840 }
3841 }
3842
Romain Guy06882f82009-06-10 13:36:04 -07003843 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003844 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003846 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003847 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 }
3849
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003850 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
Romain Guy06882f82009-06-10 13:36:04 -07003852
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003853 public void setRotationUnchecked(int rotation,
3854 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 if(DEBUG_ORIENTATION) Log.v(TAG,
3856 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07003857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 long origId = Binder.clearCallingIdentity();
3859 boolean changed;
3860 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003861 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 }
Romain Guy06882f82009-06-10 13:36:04 -07003863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 if (changed) {
3865 sendNewConfiguration();
3866 synchronized(mWindowMap) {
3867 mLayoutNeeded = true;
3868 performLayoutAndPlaceSurfacesLocked();
3869 }
3870 } else if (alwaysSendConfiguration) {
3871 //update configuration ignoring orientation change
3872 sendNewConfiguration();
3873 }
Romain Guy06882f82009-06-10 13:36:04 -07003874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 Binder.restoreCallingIdentity(origId);
3876 }
Romain Guy06882f82009-06-10 13:36:04 -07003877
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003878 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 boolean changed;
3880 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
3881 rotation = mRequestedRotation;
3882 } else {
3883 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07003884 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 }
3886 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003887 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 mRotation, mDisplayEnabled);
3889 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
3890 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07003891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07003893 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 "Rotation changed to " + rotation
3895 + " from " + mRotation
3896 + " (forceApp=" + mForcedAppOrientation
3897 + ", req=" + mRequestedRotation + ")");
3898 mRotation = rotation;
3899 mWindowsFreezingScreen = true;
3900 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
3901 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
3902 2000);
3903 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07003904 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 mQueue.setOrientation(rotation);
3906 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07003907 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 }
3909 for (int i=mWindows.size()-1; i>=0; i--) {
3910 WindowState w = (WindowState)mWindows.get(i);
3911 if (w.mSurface != null) {
3912 w.mOrientationChanging = true;
3913 }
3914 }
3915 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
3916 try {
3917 mRotationWatchers.get(i).onRotationChanged(rotation);
3918 } catch (RemoteException e) {
3919 }
3920 }
3921 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07003922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 return changed;
3924 }
Romain Guy06882f82009-06-10 13:36:04 -07003925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 public int getRotation() {
3927 return mRotation;
3928 }
3929
3930 public int watchRotation(IRotationWatcher watcher) {
3931 final IBinder watcherBinder = watcher.asBinder();
3932 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
3933 public void binderDied() {
3934 synchronized (mWindowMap) {
3935 for (int i=0; i<mRotationWatchers.size(); i++) {
3936 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003937 IRotationWatcher removed = mRotationWatchers.remove(i);
3938 if (removed != null) {
3939 removed.asBinder().unlinkToDeath(this, 0);
3940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 i--;
3942 }
3943 }
3944 }
3945 }
3946 };
Romain Guy06882f82009-06-10 13:36:04 -07003947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 synchronized (mWindowMap) {
3949 try {
3950 watcher.asBinder().linkToDeath(dr, 0);
3951 mRotationWatchers.add(watcher);
3952 } catch (RemoteException e) {
3953 // Client died, no cleanup needed.
3954 }
Romain Guy06882f82009-06-10 13:36:04 -07003955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 return mRotation;
3957 }
3958 }
3959
3960 /**
3961 * Starts the view server on the specified port.
3962 *
3963 * @param port The port to listener to.
3964 *
3965 * @return True if the server was successfully started, false otherwise.
3966 *
3967 * @see com.android.server.ViewServer
3968 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
3969 */
3970 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07003971 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 return false;
3973 }
3974
3975 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
3976 return false;
3977 }
3978
3979 if (port < 1024) {
3980 return false;
3981 }
3982
3983 if (mViewServer != null) {
3984 if (!mViewServer.isRunning()) {
3985 try {
3986 return mViewServer.start();
3987 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07003988 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 }
3991 return false;
3992 }
3993
3994 try {
3995 mViewServer = new ViewServer(this, port);
3996 return mViewServer.start();
3997 } catch (IOException e) {
3998 Log.w(TAG, "View server did not start");
3999 }
4000 return false;
4001 }
4002
Romain Guy06882f82009-06-10 13:36:04 -07004003 private boolean isSystemSecure() {
4004 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4005 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4006 }
4007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 /**
4009 * Stops the view server if it exists.
4010 *
4011 * @return True if the server stopped, false if it wasn't started or
4012 * couldn't be stopped.
4013 *
4014 * @see com.android.server.ViewServer
4015 */
4016 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004017 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 return false;
4019 }
4020
4021 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4022 return false;
4023 }
4024
4025 if (mViewServer != null) {
4026 return mViewServer.stop();
4027 }
4028 return false;
4029 }
4030
4031 /**
4032 * Indicates whether the view server is running.
4033 *
4034 * @return True if the server is running, false otherwise.
4035 *
4036 * @see com.android.server.ViewServer
4037 */
4038 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004039 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 return false;
4041 }
4042
4043 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4044 return false;
4045 }
4046
4047 return mViewServer != null && mViewServer.isRunning();
4048 }
4049
4050 /**
4051 * Lists all availble windows in the system. The listing is written in the
4052 * specified Socket's output stream with the following syntax:
4053 * windowHashCodeInHexadecimal windowName
4054 * Each line of the ouput represents a different window.
4055 *
4056 * @param client The remote client to send the listing to.
4057 * @return False if an error occured, true otherwise.
4058 */
4059 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004060 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 return false;
4062 }
4063
4064 boolean result = true;
4065
4066 Object[] windows;
4067 synchronized (mWindowMap) {
4068 windows = new Object[mWindows.size()];
4069 //noinspection unchecked
4070 windows = mWindows.toArray(windows);
4071 }
4072
4073 BufferedWriter out = null;
4074
4075 // Any uncaught exception will crash the system process
4076 try {
4077 OutputStream clientStream = client.getOutputStream();
4078 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4079
4080 final int count = windows.length;
4081 for (int i = 0; i < count; i++) {
4082 final WindowState w = (WindowState) windows[i];
4083 out.write(Integer.toHexString(System.identityHashCode(w)));
4084 out.write(' ');
4085 out.append(w.mAttrs.getTitle());
4086 out.write('\n');
4087 }
4088
4089 out.write("DONE.\n");
4090 out.flush();
4091 } catch (Exception e) {
4092 result = false;
4093 } finally {
4094 if (out != null) {
4095 try {
4096 out.close();
4097 } catch (IOException e) {
4098 result = false;
4099 }
4100 }
4101 }
4102
4103 return result;
4104 }
4105
4106 /**
4107 * Sends a command to a target window. The result of the command, if any, will be
4108 * written in the output stream of the specified socket.
4109 *
4110 * The parameters must follow this syntax:
4111 * windowHashcode extra
4112 *
4113 * Where XX is the length in characeters of the windowTitle.
4114 *
4115 * The first parameter is the target window. The window with the specified hashcode
4116 * will be the target. If no target can be found, nothing happens. The extra parameters
4117 * will be delivered to the target window and as parameters to the command itself.
4118 *
4119 * @param client The remote client to sent the result, if any, to.
4120 * @param command The command to execute.
4121 * @param parameters The command parameters.
4122 *
4123 * @return True if the command was successfully delivered, false otherwise. This does
4124 * not indicate whether the command itself was successful.
4125 */
4126 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004127 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 return false;
4129 }
4130
4131 boolean success = true;
4132 Parcel data = null;
4133 Parcel reply = null;
4134
4135 // Any uncaught exception will crash the system process
4136 try {
4137 // Find the hashcode of the window
4138 int index = parameters.indexOf(' ');
4139 if (index == -1) {
4140 index = parameters.length();
4141 }
4142 final String code = parameters.substring(0, index);
4143 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4144
4145 // Extract the command's parameter after the window description
4146 if (index < parameters.length()) {
4147 parameters = parameters.substring(index + 1);
4148 } else {
4149 parameters = "";
4150 }
4151
4152 final WindowManagerService.WindowState window = findWindow(hashCode);
4153 if (window == null) {
4154 return false;
4155 }
4156
4157 data = Parcel.obtain();
4158 data.writeInterfaceToken("android.view.IWindow");
4159 data.writeString(command);
4160 data.writeString(parameters);
4161 data.writeInt(1);
4162 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4163
4164 reply = Parcel.obtain();
4165
4166 final IBinder binder = window.mClient.asBinder();
4167 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4168 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4169
4170 reply.readException();
4171
4172 } catch (Exception e) {
4173 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4174 success = false;
4175 } finally {
4176 if (data != null) {
4177 data.recycle();
4178 }
4179 if (reply != null) {
4180 reply.recycle();
4181 }
4182 }
4183
4184 return success;
4185 }
4186
4187 private WindowState findWindow(int hashCode) {
4188 if (hashCode == -1) {
4189 return getFocusedWindow();
4190 }
4191
4192 synchronized (mWindowMap) {
4193 final ArrayList windows = mWindows;
4194 final int count = windows.size();
4195
4196 for (int i = 0; i < count; i++) {
4197 WindowState w = (WindowState) windows.get(i);
4198 if (System.identityHashCode(w) == hashCode) {
4199 return w;
4200 }
4201 }
4202 }
4203
4204 return null;
4205 }
4206
4207 /*
4208 * Instruct the Activity Manager to fetch the current configuration and broadcast
4209 * that to config-changed listeners if appropriate.
4210 */
4211 void sendNewConfiguration() {
4212 try {
4213 mActivityManager.updateConfiguration(null);
4214 } catch (RemoteException e) {
4215 }
4216 }
Romain Guy06882f82009-06-10 13:36:04 -07004217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 public Configuration computeNewConfiguration() {
4219 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004220 return computeNewConfigurationLocked();
4221 }
4222 }
Romain Guy06882f82009-06-10 13:36:04 -07004223
Dianne Hackbornc485a602009-03-24 22:39:49 -07004224 Configuration computeNewConfigurationLocked() {
4225 Configuration config = new Configuration();
4226 if (!computeNewConfigurationLocked(config)) {
4227 return null;
4228 }
4229 Log.i(TAG, "Config changed: " + config);
4230 long now = SystemClock.uptimeMillis();
4231 //Log.i(TAG, "Config changing, gc pending: " + mFreezeGcPending + ", now " + now);
4232 if (mFreezeGcPending != 0) {
4233 if (now > (mFreezeGcPending+1000)) {
4234 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
4235 mH.removeMessages(H.FORCE_GC);
4236 Runtime.getRuntime().gc();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 mFreezeGcPending = now;
4238 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004239 } else {
4240 mFreezeGcPending = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004242 return config;
4243 }
Romain Guy06882f82009-06-10 13:36:04 -07004244
Dianne Hackbornc485a602009-03-24 22:39:49 -07004245 boolean computeNewConfigurationLocked(Configuration config) {
4246 if (mDisplay == null) {
4247 return false;
4248 }
4249 mQueue.getInputConfiguration(config);
4250 final int dw = mDisplay.getWidth();
4251 final int dh = mDisplay.getHeight();
4252 int orientation = Configuration.ORIENTATION_SQUARE;
4253 if (dw < dh) {
4254 orientation = Configuration.ORIENTATION_PORTRAIT;
4255 } else if (dw > dh) {
4256 orientation = Configuration.ORIENTATION_LANDSCAPE;
4257 }
4258 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004259
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004260 DisplayMetrics dm = new DisplayMetrics();
4261 mDisplay.getMetrics(dm);
4262 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4263
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004264 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004265 // Note we only do this once because at this point we don't
4266 // expect the screen to change in this way at runtime, and want
4267 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004268 int longSize = dw;
4269 int shortSize = dh;
4270 if (longSize < shortSize) {
4271 int tmp = longSize;
4272 longSize = shortSize;
4273 shortSize = tmp;
4274 }
4275 longSize = (int)(longSize/dm.density);
4276 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004277
Dianne Hackborn723738c2009-06-25 19:48:04 -07004278 // These semi-magic numbers define our compatibility modes for
4279 // applications with different screens. Don't change unless you
4280 // make sure to test lots and lots of apps!
4281 if (longSize < 470) {
4282 // This is shorter than an HVGA normal density screen (which
4283 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004284 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4285 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004286 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004287 // Is this a large screen?
4288 if (longSize > 640 && shortSize >= 480) {
4289 // VGA or larger screens at medium density are the point
4290 // at which we consider it to be a large screen.
4291 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4292 } else {
4293 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4294
4295 // If this screen is wider than normal HVGA, or taller
4296 // than FWVGA, then for old apps we want to run in size
4297 // compatibility mode.
4298 if (shortSize > 321 || longSize > 570) {
4299 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4300 }
4301 }
4302
4303 // Is this a long screen?
4304 if (((longSize*3)/5) >= (shortSize-1)) {
4305 // Anything wider than WVGA (5:3) is considering to be long.
4306 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4307 } else {
4308 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4309 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004310 }
4311 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004312 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004313
Dianne Hackbornc485a602009-03-24 22:39:49 -07004314 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4315 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4316 mPolicy.adjustConfigurationLw(config);
4317 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 }
Romain Guy06882f82009-06-10 13:36:04 -07004319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 // -------------------------------------------------------------
4321 // Input Events and Focus Management
4322 // -------------------------------------------------------------
4323
4324 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004325 long curTime = SystemClock.uptimeMillis();
4326
Michael Chane10de972009-05-18 11:24:50 -07004327 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004328 if (mLastTouchEventType == eventType &&
4329 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4330 return;
4331 }
4332 mLastUserActivityCallTime = curTime;
4333 mLastTouchEventType = eventType;
4334 }
4335
4336 if (targetWin == null
4337 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4338 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 }
4340 }
4341
4342 // tells if it's a cheek event or not -- this function is stateful
4343 private static final int EVENT_NONE = 0;
4344 private static final int EVENT_UNKNOWN = 0;
4345 private static final int EVENT_CHEEK = 0;
4346 private static final int EVENT_IGNORE_DURATION = 300; // ms
4347 private static final float CHEEK_THRESHOLD = 0.6f;
4348 private int mEventState = EVENT_NONE;
4349 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 private int eventType(MotionEvent ev) {
4352 float size = ev.getSize();
4353 switch (ev.getAction()) {
4354 case MotionEvent.ACTION_DOWN:
4355 mEventSize = size;
4356 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4357 case MotionEvent.ACTION_UP:
4358 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004359 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 case MotionEvent.ACTION_MOVE:
4361 final int N = ev.getHistorySize();
4362 if (size > mEventSize) mEventSize = size;
4363 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4364 for (int i=0; i<N; i++) {
4365 size = ev.getHistoricalSize(i);
4366 if (size > mEventSize) mEventSize = size;
4367 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4368 }
4369 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4370 return TOUCH_EVENT;
4371 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004372 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 }
4374 default:
4375 // not good
4376 return OTHER_EVENT;
4377 }
4378 }
4379
4380 /**
4381 * @return Returns true if event was dispatched, false if it was dropped for any reason
4382 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004383 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4385 "dispatchPointer " + ev);
4386
Michael Chan53071d62009-05-13 17:29:48 -07004387 if (MEASURE_LATENCY) {
4388 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4389 }
4390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004392 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393
Michael Chan53071d62009-05-13 17:29:48 -07004394 if (MEASURE_LATENCY) {
4395 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4396 }
4397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 if (action == MotionEvent.ACTION_UP) {
4401 // let go of our target
4402 mKeyWaiter.mMotionTarget = null;
4403 mPowerManager.logPointerUpEvent();
4404 } else if (action == MotionEvent.ACTION_DOWN) {
4405 mPowerManager.logPointerDownEvent();
4406 }
4407
4408 if (targetObj == null) {
4409 // In this case we are either dropping the event, or have received
4410 // a move or up without a down. It is common to receive move
4411 // events in such a way, since this means the user is moving the
4412 // pointer without actually pressing down. All other cases should
4413 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004414 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4416 }
4417 if (qev != null) {
4418 mQueue.recycleEvent(qev);
4419 }
4420 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004421 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
4423 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4424 if (qev != null) {
4425 mQueue.recycleEvent(qev);
4426 }
4427 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004428 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 }
Romain Guy06882f82009-06-10 13:36:04 -07004430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004434 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435
4436 //Log.i(TAG, "Sending " + ev + " to " + target);
4437
4438 if (uid != 0 && uid != target.mSession.mUid) {
4439 if (mContext.checkPermission(
4440 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4441 != PackageManager.PERMISSION_GRANTED) {
4442 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4443 + pid + " uid " + uid + " to window " + target
4444 + " owned by uid " + target.mSession.mUid);
4445 if (qev != null) {
4446 mQueue.recycleEvent(qev);
4447 }
4448 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004449 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 }
4451 }
4452
Michael Chan53071d62009-05-13 17:29:48 -07004453 if (MEASURE_LATENCY) {
4454 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4455 }
4456
Romain Guy06882f82009-06-10 13:36:04 -07004457 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4459 //target wants to ignore fat touch events
4460 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4461 //explicit flag to return without processing event further
4462 boolean returnFlag = false;
4463 if((action == MotionEvent.ACTION_DOWN)) {
4464 mFatTouch = false;
4465 if(cheekPress) {
4466 mFatTouch = true;
4467 returnFlag = true;
4468 }
4469 } else {
4470 if(action == MotionEvent.ACTION_UP) {
4471 if(mFatTouch) {
4472 //earlier even was invalid doesnt matter if current up is cheekpress or not
4473 mFatTouch = false;
4474 returnFlag = true;
4475 } else if(cheekPress) {
4476 //cancel the earlier event
4477 ev.setAction(MotionEvent.ACTION_CANCEL);
4478 action = MotionEvent.ACTION_CANCEL;
4479 }
4480 } else if(action == MotionEvent.ACTION_MOVE) {
4481 if(mFatTouch) {
4482 //two cases here
4483 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004484 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 returnFlag = true;
4486 } else if(cheekPress) {
4487 //valid down followed by invalid moves
4488 //an invalid move have to cancel earlier action
4489 ev.setAction(MotionEvent.ACTION_CANCEL);
4490 action = MotionEvent.ACTION_CANCEL;
4491 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4492 //note that the subsequent invalid moves will not get here
4493 mFatTouch = true;
4494 }
4495 }
4496 } //else if action
4497 if(returnFlag) {
4498 //recycle que, ev
4499 if (qev != null) {
4500 mQueue.recycleEvent(qev);
4501 }
4502 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004503 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 }
4505 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004506
Michael Chan9f028e62009-08-04 17:37:46 -07004507 // Enable this for testing the "right" value
4508 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004509 int max_events_per_sec = 35;
4510 try {
4511 max_events_per_sec = Integer.parseInt(SystemProperties
4512 .get("windowsmgr.max_events_per_sec"));
4513 if (max_events_per_sec < 1) {
4514 max_events_per_sec = 35;
4515 }
4516 } catch (NumberFormatException e) {
4517 }
4518 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4519 }
4520
4521 /*
4522 * Throttle events to minimize CPU usage when there's a flood of events
4523 * e.g. constant contact with the screen
4524 */
4525 if (action == MotionEvent.ACTION_MOVE) {
4526 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4527 long now = SystemClock.uptimeMillis();
4528 if (now < nextEventTime) {
4529 try {
4530 Thread.sleep(nextEventTime - now);
4531 } catch (InterruptedException e) {
4532 }
4533 mLastTouchEventTime = nextEventTime;
4534 } else {
4535 mLastTouchEventTime = now;
4536 }
4537 }
4538
Michael Chan53071d62009-05-13 17:29:48 -07004539 if (MEASURE_LATENCY) {
4540 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4541 }
4542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 synchronized(mWindowMap) {
4544 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4545 mKeyWaiter.bindTargetWindowLocked(target,
4546 KeyWaiter.RETURN_PENDING_POINTER, qev);
4547 ev = null;
4548 } else {
4549 if (action == MotionEvent.ACTION_DOWN) {
4550 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4551 if (out != null) {
4552 MotionEvent oev = MotionEvent.obtain(ev);
4553 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4554 do {
4555 final Rect frame = out.mFrame;
4556 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4557 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004558 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 } catch (android.os.RemoteException e) {
4560 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4561 }
4562 oev.offsetLocation((float)frame.left, (float)frame.top);
4563 out = out.mNextOutsideTouch;
4564 } while (out != null);
4565 mKeyWaiter.mOutsideTouchTargets = null;
4566 }
4567 }
4568 final Rect frame = target.mFrame;
4569 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4570 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004571
4572 // If we are on top of the wallpaper, then the wallpaper also
4573 // gets to see this movement.
4574 if (mWallpaperTarget == target) {
4575 sendPointerToWallpaperLocked(target, ev, eventTime);
4576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 }
4578 }
Romain Guy06882f82009-06-10 13:36:04 -07004579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 // finally offset the event to the target's coordinate system and
4581 // dispatch the event.
4582 try {
4583 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4584 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4585 }
Michael Chan53071d62009-05-13 17:29:48 -07004586
4587 if (MEASURE_LATENCY) {
4588 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4589 }
4590
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004591 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004592
4593 if (MEASURE_LATENCY) {
4594 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4595 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004596 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 } catch (android.os.RemoteException e) {
4598 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4599 mKeyWaiter.mMotionTarget = null;
4600 try {
4601 removeWindow(target.mSession, target.mClient);
4602 } catch (java.util.NoSuchElementException ex) {
4603 // This will happen if the window has already been
4604 // removed.
4605 }
4606 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004607 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 }
Romain Guy06882f82009-06-10 13:36:04 -07004609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 /**
4611 * @return Returns true if event was dispatched, false if it was dropped for any reason
4612 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004613 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 if (DEBUG_INPUT) Log.v(
4615 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004618 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 if (focusObj == null) {
4620 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4621 if (qev != null) {
4622 mQueue.recycleEvent(qev);
4623 }
4624 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004625 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 }
4627 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4628 if (qev != null) {
4629 mQueue.recycleEvent(qev);
4630 }
4631 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004632 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 }
Romain Guy06882f82009-06-10 13:36:04 -07004634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 if (uid != 0 && uid != focus.mSession.mUid) {
4638 if (mContext.checkPermission(
4639 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4640 != PackageManager.PERMISSION_GRANTED) {
4641 Log.w(TAG, "Permission denied: injecting key event from pid "
4642 + pid + " uid " + uid + " to window " + focus
4643 + " owned by uid " + focus.mSession.mUid);
4644 if (qev != null) {
4645 mQueue.recycleEvent(qev);
4646 }
4647 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004648 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 }
4650 }
Romain Guy06882f82009-06-10 13:36:04 -07004651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 synchronized(mWindowMap) {
4655 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4656 mKeyWaiter.bindTargetWindowLocked(focus,
4657 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4658 // We don't deliver movement events to the client, we hold
4659 // them and wait for them to call back.
4660 ev = null;
4661 } else {
4662 mKeyWaiter.bindTargetWindowLocked(focus);
4663 }
4664 }
Romain Guy06882f82009-06-10 13:36:04 -07004665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004667 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004668 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 } catch (android.os.RemoteException e) {
4670 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4671 try {
4672 removeWindow(focus.mSession, focus.mClient);
4673 } catch (java.util.NoSuchElementException ex) {
4674 // This will happen if the window has already been
4675 // removed.
4676 }
4677 }
Romain Guy06882f82009-06-10 13:36:04 -07004678
Dianne Hackborncfaef692009-06-15 14:24:44 -07004679 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 }
Romain Guy06882f82009-06-10 13:36:04 -07004681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 /**
4683 * @return Returns true if event was dispatched, false if it was dropped for any reason
4684 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004685 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4687
4688 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004689 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 if (focusObj == null) {
4691 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004692 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 }
4694 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004695 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 }
Romain Guy06882f82009-06-10 13:36:04 -07004697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 if (DEBUG_INPUT) Log.v(
4701 TAG, "Dispatching to " + focus + ": " + event);
4702
4703 if (uid != 0 && uid != focus.mSession.mUid) {
4704 if (mContext.checkPermission(
4705 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4706 != PackageManager.PERMISSION_GRANTED) {
4707 Log.w(TAG, "Permission denied: injecting key event from pid "
4708 + pid + " uid " + uid + " to window " + focus
4709 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004710 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 }
4712 }
Romain Guy06882f82009-06-10 13:36:04 -07004713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 synchronized(mWindowMap) {
4715 mKeyWaiter.bindTargetWindowLocked(focus);
4716 }
4717
4718 // NOSHIP extra state logging
4719 mKeyWaiter.recordDispatchState(event, focus);
4720 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 try {
4723 if (DEBUG_INPUT || DEBUG_FOCUS) {
4724 Log.v(TAG, "Delivering key " + event.getKeyCode()
4725 + " to " + focus);
4726 }
4727 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004728 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 } catch (android.os.RemoteException e) {
4730 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4731 try {
4732 removeWindow(focus.mSession, focus.mClient);
4733 } catch (java.util.NoSuchElementException ex) {
4734 // This will happen if the window has already been
4735 // removed.
4736 }
4737 }
Romain Guy06882f82009-06-10 13:36:04 -07004738
Dianne Hackborncfaef692009-06-15 14:24:44 -07004739 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 }
Romain Guy06882f82009-06-10 13:36:04 -07004741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 public void pauseKeyDispatching(IBinder _token) {
4743 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4744 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004745 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 }
4747
4748 synchronized (mWindowMap) {
4749 WindowToken token = mTokenMap.get(_token);
4750 if (token != null) {
4751 mKeyWaiter.pauseDispatchingLocked(token);
4752 }
4753 }
4754 }
4755
4756 public void resumeKeyDispatching(IBinder _token) {
4757 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4758 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004759 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 }
4761
4762 synchronized (mWindowMap) {
4763 WindowToken token = mTokenMap.get(_token);
4764 if (token != null) {
4765 mKeyWaiter.resumeDispatchingLocked(token);
4766 }
4767 }
4768 }
4769
4770 public void setEventDispatching(boolean enabled) {
4771 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4772 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004773 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 }
4775
4776 synchronized (mWindowMap) {
4777 mKeyWaiter.setEventDispatchingLocked(enabled);
4778 }
4779 }
Romain Guy06882f82009-06-10 13:36:04 -07004780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 /**
4782 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004783 *
4784 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 * {@link SystemClock#uptimeMillis()} as the timebase.)
4786 * @param sync If true, wait for the event to be completed before returning to the caller.
4787 * @return Returns true if event was dispatched, false if it was dropped for any reason
4788 */
4789 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4790 long downTime = ev.getDownTime();
4791 long eventTime = ev.getEventTime();
4792
4793 int action = ev.getAction();
4794 int code = ev.getKeyCode();
4795 int repeatCount = ev.getRepeatCount();
4796 int metaState = ev.getMetaState();
4797 int deviceId = ev.getDeviceId();
4798 int scancode = ev.getScanCode();
4799
4800 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4801 if (downTime == 0) downTime = eventTime;
4802
4803 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004804 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004806 final int pid = Binder.getCallingPid();
4807 final int uid = Binder.getCallingUid();
4808 final long ident = Binder.clearCallingIdentity();
4809 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004811 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004813 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004814 switch (result) {
4815 case INJECT_NO_PERMISSION:
4816 throw new SecurityException(
4817 "Injecting to another application requires INJECT_EVENT permission");
4818 case INJECT_SUCCEEDED:
4819 return true;
4820 }
4821 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 }
4823
4824 /**
4825 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004826 *
4827 * @param ev A motion event describing the pointer (touch) action. (As noted in
4828 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 * {@link SystemClock#uptimeMillis()} as the timebase.)
4830 * @param sync If true, wait for the event to be completed before returning to the caller.
4831 * @return Returns true if event was dispatched, false if it was dropped for any reason
4832 */
4833 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004834 final int pid = Binder.getCallingPid();
4835 final int uid = Binder.getCallingUid();
4836 final long ident = Binder.clearCallingIdentity();
4837 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004839 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004841 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004842 switch (result) {
4843 case INJECT_NO_PERMISSION:
4844 throw new SecurityException(
4845 "Injecting to another application requires INJECT_EVENT permission");
4846 case INJECT_SUCCEEDED:
4847 return true;
4848 }
4849 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 }
Romain Guy06882f82009-06-10 13:36:04 -07004851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 /**
4853 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004854 *
4855 * @param ev A motion event describing the trackball action. (As noted in
4856 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 * {@link SystemClock#uptimeMillis()} as the timebase.)
4858 * @param sync If true, wait for the event to be completed before returning to the caller.
4859 * @return Returns true if event was dispatched, false if it was dropped for any reason
4860 */
4861 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004862 final int pid = Binder.getCallingPid();
4863 final int uid = Binder.getCallingUid();
4864 final long ident = Binder.clearCallingIdentity();
4865 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004867 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004869 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004870 switch (result) {
4871 case INJECT_NO_PERMISSION:
4872 throw new SecurityException(
4873 "Injecting to another application requires INJECT_EVENT permission");
4874 case INJECT_SUCCEEDED:
4875 return true;
4876 }
4877 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
Romain Guy06882f82009-06-10 13:36:04 -07004879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 private WindowState getFocusedWindow() {
4881 synchronized (mWindowMap) {
4882 return getFocusedWindowLocked();
4883 }
4884 }
4885
4886 private WindowState getFocusedWindowLocked() {
4887 return mCurrentFocus;
4888 }
Romain Guy06882f82009-06-10 13:36:04 -07004889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 /**
4891 * This class holds the state for dispatching key events. This state
4892 * is protected by the KeyWaiter instance, NOT by the window lock. You
4893 * can be holding the main window lock while acquire the KeyWaiter lock,
4894 * but not the other way around.
4895 */
4896 final class KeyWaiter {
4897 // NOSHIP debugging
4898 public class DispatchState {
4899 private KeyEvent event;
4900 private WindowState focus;
4901 private long time;
4902 private WindowState lastWin;
4903 private IBinder lastBinder;
4904 private boolean finished;
4905 private boolean gotFirstWindow;
4906 private boolean eventDispatching;
4907 private long timeToSwitch;
4908 private boolean wasFrozen;
4909 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004910 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07004911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 DispatchState(KeyEvent theEvent, WindowState theFocus) {
4913 focus = theFocus;
4914 event = theEvent;
4915 time = System.currentTimeMillis();
4916 // snapshot KeyWaiter state
4917 lastWin = mLastWin;
4918 lastBinder = mLastBinder;
4919 finished = mFinished;
4920 gotFirstWindow = mGotFirstWindow;
4921 eventDispatching = mEventDispatching;
4922 timeToSwitch = mTimeToSwitch;
4923 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004924 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004925 // cache the paused state at ctor time as well
4926 if (theFocus == null || theFocus.mToken == null) {
4927 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
4928 focusPaused = false;
4929 } else {
4930 focusPaused = theFocus.mToken.paused;
4931 }
4932 }
Romain Guy06882f82009-06-10 13:36:04 -07004933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 public String toString() {
4935 return "{{" + event + " to " + focus + " @ " + time
4936 + " lw=" + lastWin + " lb=" + lastBinder
4937 + " fin=" + finished + " gfw=" + gotFirstWindow
4938 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08004939 + " wf=" + wasFrozen + " fp=" + focusPaused
4940 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942 };
4943 private DispatchState mDispatchState = null;
4944 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
4945 mDispatchState = new DispatchState(theEvent, theFocus);
4946 }
4947 // END NOSHIP
4948
4949 public static final int RETURN_NOTHING = 0;
4950 public static final int RETURN_PENDING_POINTER = 1;
4951 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07004952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 final Object SKIP_TARGET_TOKEN = new Object();
4954 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07004955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 private WindowState mLastWin = null;
4957 private IBinder mLastBinder = null;
4958 private boolean mFinished = true;
4959 private boolean mGotFirstWindow = false;
4960 private boolean mEventDispatching = true;
4961 private long mTimeToSwitch = 0;
4962 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07004963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 // Target of Motion events
4965 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07004966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 // Windows above the target who would like to receive an "outside"
4968 // touch event for any down events outside of them.
4969 WindowState mOutsideTouchTargets;
4970
4971 /**
4972 * Wait for the last event dispatch to complete, then find the next
4973 * target that should receive the given event and wait for that one
4974 * to be ready to receive it.
4975 */
4976 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
4977 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004978 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 long startTime = SystemClock.uptimeMillis();
4980 long keyDispatchingTimeout = 5 * 1000;
4981 long waitedFor = 0;
4982
4983 while (true) {
4984 // Figure out which window we care about. It is either the
4985 // last window we are waiting to have process the event or,
4986 // if none, then the next window we think the event should go
4987 // to. Note: we retrieve mLastWin outside of the lock, so
4988 // it may change before we lock. Thus we must check it again.
4989 WindowState targetWin = mLastWin;
4990 boolean targetIsNew = targetWin == null;
4991 if (DEBUG_INPUT) Log.v(
4992 TAG, "waitForLastKey: mFinished=" + mFinished +
4993 ", mLastWin=" + mLastWin);
4994 if (targetIsNew) {
4995 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004996 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 if (target == SKIP_TARGET_TOKEN) {
4998 // The user has pressed a special key, and we are
4999 // dropping all pending events before it.
5000 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5001 + " " + nextMotion);
5002 return null;
5003 }
5004 if (target == CONSUMED_EVENT_TOKEN) {
5005 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5006 + " " + nextMotion);
5007 return target;
5008 }
5009 targetWin = (WindowState)target;
5010 }
Romain Guy06882f82009-06-10 13:36:04 -07005011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 // Now: is it okay to send the next event to this window?
5015 synchronized (this) {
5016 // First: did we come here based on the last window not
5017 // being null, but it changed by the time we got here?
5018 // If so, try again.
5019 if (!targetIsNew && mLastWin == null) {
5020 continue;
5021 }
Romain Guy06882f82009-06-10 13:36:04 -07005022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 // We never dispatch events if not finished with the
5024 // last one, or the display is frozen.
5025 if (mFinished && !mDisplayFrozen) {
5026 // If event dispatching is disabled, then we
5027 // just consume the events.
5028 if (!mEventDispatching) {
5029 if (DEBUG_INPUT) Log.v(TAG,
5030 "Skipping event; dispatching disabled: "
5031 + nextKey + " " + nextMotion);
5032 return null;
5033 }
5034 if (targetWin != null) {
5035 // If this is a new target, and that target is not
5036 // paused or unresponsive, then all looks good to
5037 // handle the event.
5038 if (targetIsNew && !targetWin.mToken.paused) {
5039 return targetWin;
5040 }
Romain Guy06882f82009-06-10 13:36:04 -07005041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 // If we didn't find a target window, and there is no
5043 // focused app window, then just eat the events.
5044 } else if (mFocusedApp == null) {
5045 if (DEBUG_INPUT) Log.v(TAG,
5046 "Skipping event; no focused app: "
5047 + nextKey + " " + nextMotion);
5048 return null;
5049 }
5050 }
Romain Guy06882f82009-06-10 13:36:04 -07005051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 if (DEBUG_INPUT) Log.v(
5053 TAG, "Waiting for last key in " + mLastBinder
5054 + " target=" + targetWin
5055 + " mFinished=" + mFinished
5056 + " mDisplayFrozen=" + mDisplayFrozen
5057 + " targetIsNew=" + targetIsNew
5058 + " paused="
5059 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005060 + " mFocusedApp=" + mFocusedApp
5061 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 targetApp = targetWin != null
5064 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 long curTimeout = keyDispatchingTimeout;
5067 if (mTimeToSwitch != 0) {
5068 long now = SystemClock.uptimeMillis();
5069 if (mTimeToSwitch <= now) {
5070 // If an app switch key has been pressed, and we have
5071 // waited too long for the current app to finish
5072 // processing keys, then wait no more!
5073 doFinishedKeyLocked(true);
5074 continue;
5075 }
5076 long switchTimeout = mTimeToSwitch - now;
5077 if (curTimeout > switchTimeout) {
5078 curTimeout = switchTimeout;
5079 }
5080 }
Romain Guy06882f82009-06-10 13:36:04 -07005081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 try {
5083 // after that continue
5084 // processing keys, so we don't get stuck.
5085 if (DEBUG_INPUT) Log.v(
5086 TAG, "Waiting for key dispatch: " + curTimeout);
5087 wait(curTimeout);
5088 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5089 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005090 + startTime + " switchTime=" + mTimeToSwitch
5091 + " target=" + targetWin + " mLW=" + mLastWin
5092 + " mLB=" + mLastBinder + " fin=" + mFinished
5093 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 } catch (InterruptedException e) {
5095 }
5096 }
5097
5098 // If we were frozen during configuration change, restart the
5099 // timeout checks from now; otherwise look at whether we timed
5100 // out before awakening.
5101 if (mWasFrozen) {
5102 waitedFor = 0;
5103 mWasFrozen = false;
5104 } else {
5105 waitedFor = SystemClock.uptimeMillis() - startTime;
5106 }
5107
5108 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5109 IApplicationToken at = null;
5110 synchronized (this) {
5111 Log.w(TAG, "Key dispatching timed out sending to " +
5112 (targetWin != null ? targetWin.mAttrs.getTitle()
5113 : "<null>"));
5114 // NOSHIP debugging
5115 Log.w(TAG, "Dispatch state: " + mDispatchState);
5116 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5117 // END NOSHIP
5118 //dump();
5119 if (targetWin != null) {
5120 at = targetWin.getAppToken();
5121 } else if (targetApp != null) {
5122 at = targetApp.appToken;
5123 }
5124 }
5125
5126 boolean abort = true;
5127 if (at != null) {
5128 try {
5129 long timeout = at.getKeyDispatchingTimeout();
5130 if (timeout > waitedFor) {
5131 // we did not wait the proper amount of time for this application.
5132 // set the timeout to be the real timeout and wait again.
5133 keyDispatchingTimeout = timeout - waitedFor;
5134 continue;
5135 } else {
5136 abort = at.keyDispatchingTimedOut();
5137 }
5138 } catch (RemoteException ex) {
5139 }
5140 }
5141
5142 synchronized (this) {
5143 if (abort && (mLastWin == targetWin || targetWin == null)) {
5144 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005145 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 if (DEBUG_INPUT) Log.v(TAG,
5147 "Window " + mLastWin +
5148 " timed out on key input");
5149 if (mLastWin.mToken.paused) {
5150 Log.w(TAG, "Un-pausing dispatching to this window");
5151 mLastWin.mToken.paused = false;
5152 }
5153 }
5154 if (mMotionTarget == targetWin) {
5155 mMotionTarget = null;
5156 }
5157 mLastWin = null;
5158 mLastBinder = null;
5159 if (failIfTimeout || targetWin == null) {
5160 return null;
5161 }
5162 } else {
5163 Log.w(TAG, "Continuing to wait for key to be dispatched");
5164 startTime = SystemClock.uptimeMillis();
5165 }
5166 }
5167 }
5168 }
5169 }
Romain Guy06882f82009-06-10 13:36:04 -07005170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005172 MotionEvent nextMotion, boolean isPointerEvent,
5173 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 if (nextKey != null) {
5177 // Find the target window for a normal key event.
5178 final int keycode = nextKey.getKeyCode();
5179 final int repeatCount = nextKey.getRepeatCount();
5180 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5181 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005184 if (callingUid == 0 ||
5185 mContext.checkPermission(
5186 android.Manifest.permission.INJECT_EVENTS,
5187 callingPid, callingUid)
5188 == PackageManager.PERMISSION_GRANTED) {
5189 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005190 nextKey.getMetaState(), down, repeatCount,
5191 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 Log.w(TAG, "Event timeout during app switch: dropping "
5194 + nextKey);
5195 return SKIP_TARGET_TOKEN;
5196 }
Romain Guy06882f82009-06-10 13:36:04 -07005197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 WindowState focus = null;
5201 synchronized(mWindowMap) {
5202 focus = getFocusedWindowLocked();
5203 }
Romain Guy06882f82009-06-10 13:36:04 -07005204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005206
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005207 if (callingUid == 0 ||
5208 (focus != null && callingUid == focus.mSession.mUid) ||
5209 mContext.checkPermission(
5210 android.Manifest.permission.INJECT_EVENTS,
5211 callingPid, callingUid)
5212 == PackageManager.PERMISSION_GRANTED) {
5213 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005214 keycode, nextKey.getMetaState(), down, repeatCount,
5215 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005216 return CONSUMED_EVENT_TOKEN;
5217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 }
Romain Guy06882f82009-06-10 13:36:04 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 } else if (!isPointerEvent) {
5223 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5224 if (!dispatch) {
5225 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5226 + nextMotion);
5227 return SKIP_TARGET_TOKEN;
5228 }
Romain Guy06882f82009-06-10 13:36:04 -07005229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 WindowState focus = null;
5231 synchronized(mWindowMap) {
5232 focus = getFocusedWindowLocked();
5233 }
Romain Guy06882f82009-06-10 13:36:04 -07005234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5236 return focus;
5237 }
Romain Guy06882f82009-06-10 13:36:04 -07005238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 if (nextMotion == null) {
5240 return SKIP_TARGET_TOKEN;
5241 }
Romain Guy06882f82009-06-10 13:36:04 -07005242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5244 KeyEvent.KEYCODE_UNKNOWN);
5245 if (!dispatch) {
5246 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5247 + nextMotion);
5248 return SKIP_TARGET_TOKEN;
5249 }
Romain Guy06882f82009-06-10 13:36:04 -07005250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 // Find the target window for a pointer event.
5252 int action = nextMotion.getAction();
5253 final float xf = nextMotion.getX();
5254 final float yf = nextMotion.getY();
5255 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 final boolean screenWasOff = qev != null
5258 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 synchronized(mWindowMap) {
5263 synchronized (this) {
5264 if (action == MotionEvent.ACTION_DOWN) {
5265 if (mMotionTarget != null) {
5266 // this is weird, we got a pen down, but we thought it was
5267 // already down!
5268 // XXX: We should probably send an ACTION_UP to the current
5269 // target.
5270 Log.w(TAG, "Pointer down received while already down in: "
5271 + mMotionTarget);
5272 mMotionTarget = null;
5273 }
Romain Guy06882f82009-06-10 13:36:04 -07005274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 // ACTION_DOWN is special, because we need to lock next events to
5276 // the window we'll land onto.
5277 final int x = (int)xf;
5278 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 final ArrayList windows = mWindows;
5281 final int N = windows.size();
5282 WindowState topErrWindow = null;
5283 final Rect tmpRect = mTempRect;
5284 for (int i=N-1; i>=0; i--) {
5285 WindowState child = (WindowState)windows.get(i);
5286 //Log.i(TAG, "Checking dispatch to: " + child);
5287 final int flags = child.mAttrs.flags;
5288 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5289 if (topErrWindow == null) {
5290 topErrWindow = child;
5291 }
5292 }
5293 if (!child.isVisibleLw()) {
5294 //Log.i(TAG, "Not visible!");
5295 continue;
5296 }
5297 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5298 //Log.i(TAG, "Not touchable!");
5299 if ((flags & WindowManager.LayoutParams
5300 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5301 child.mNextOutsideTouch = mOutsideTouchTargets;
5302 mOutsideTouchTargets = child;
5303 }
5304 continue;
5305 }
5306 tmpRect.set(child.mFrame);
5307 if (child.mTouchableInsets == ViewTreeObserver
5308 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5309 // The touch is inside of the window if it is
5310 // inside the frame, AND the content part of that
5311 // frame that was given by the application.
5312 tmpRect.left += child.mGivenContentInsets.left;
5313 tmpRect.top += child.mGivenContentInsets.top;
5314 tmpRect.right -= child.mGivenContentInsets.right;
5315 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5316 } else if (child.mTouchableInsets == ViewTreeObserver
5317 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5318 // The touch is inside of the window if it is
5319 // inside the frame, AND the visible part of that
5320 // frame that was given by the application.
5321 tmpRect.left += child.mGivenVisibleInsets.left;
5322 tmpRect.top += child.mGivenVisibleInsets.top;
5323 tmpRect.right -= child.mGivenVisibleInsets.right;
5324 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5325 }
5326 final int touchFlags = flags &
5327 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5328 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5329 if (tmpRect.contains(x, y) || touchFlags == 0) {
5330 //Log.i(TAG, "Using this target!");
5331 if (!screenWasOff || (flags &
5332 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5333 mMotionTarget = child;
5334 } else {
5335 //Log.i(TAG, "Waking, skip!");
5336 mMotionTarget = null;
5337 }
5338 break;
5339 }
Romain Guy06882f82009-06-10 13:36:04 -07005340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 if ((flags & WindowManager.LayoutParams
5342 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5343 child.mNextOutsideTouch = mOutsideTouchTargets;
5344 mOutsideTouchTargets = child;
5345 //Log.i(TAG, "Adding to outside target list: " + child);
5346 }
5347 }
5348
5349 // if there's an error window but it's not accepting
5350 // focus (typically because it is not yet visible) just
5351 // wait for it -- any other focused window may in fact
5352 // be in ANR state.
5353 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5354 mMotionTarget = null;
5355 }
5356 }
Romain Guy06882f82009-06-10 13:36:04 -07005357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 target = mMotionTarget;
5359 }
5360 }
Romain Guy06882f82009-06-10 13:36:04 -07005361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 // Pointer events are a little different -- if there isn't a
5365 // target found for any event, then just drop it.
5366 return target != null ? target : SKIP_TARGET_TOKEN;
5367 }
Romain Guy06882f82009-06-10 13:36:04 -07005368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 boolean checkShouldDispatchKey(int keycode) {
5370 synchronized (this) {
5371 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5372 mTimeToSwitch = 0;
5373 return true;
5374 }
5375 if (mTimeToSwitch != 0
5376 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5377 return false;
5378 }
5379 return true;
5380 }
5381 }
Romain Guy06882f82009-06-10 13:36:04 -07005382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 void bindTargetWindowLocked(WindowState win,
5384 int pendingWhat, QueuedEvent pendingMotion) {
5385 synchronized (this) {
5386 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5387 }
5388 }
Romain Guy06882f82009-06-10 13:36:04 -07005389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 void bindTargetWindowLocked(WindowState win) {
5391 synchronized (this) {
5392 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5393 }
5394 }
5395
5396 void bindTargetWindowLockedLocked(WindowState win,
5397 int pendingWhat, QueuedEvent pendingMotion) {
5398 mLastWin = win;
5399 mLastBinder = win.mClient.asBinder();
5400 mFinished = false;
5401 if (pendingMotion != null) {
5402 final Session s = win.mSession;
5403 if (pendingWhat == RETURN_PENDING_POINTER) {
5404 releasePendingPointerLocked(s);
5405 s.mPendingPointerMove = pendingMotion;
5406 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005407 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 "bindTargetToWindow " + s.mPendingPointerMove);
5409 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5410 releasePendingTrackballLocked(s);
5411 s.mPendingTrackballMove = pendingMotion;
5412 s.mPendingTrackballWindow = win;
5413 }
5414 }
5415 }
Romain Guy06882f82009-06-10 13:36:04 -07005416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 void releasePendingPointerLocked(Session s) {
5418 if (DEBUG_INPUT) Log.v(TAG,
5419 "releasePendingPointer " + s.mPendingPointerMove);
5420 if (s.mPendingPointerMove != null) {
5421 mQueue.recycleEvent(s.mPendingPointerMove);
5422 s.mPendingPointerMove = null;
5423 }
5424 }
Romain Guy06882f82009-06-10 13:36:04 -07005425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 void releasePendingTrackballLocked(Session s) {
5427 if (s.mPendingTrackballMove != null) {
5428 mQueue.recycleEvent(s.mPendingTrackballMove);
5429 s.mPendingTrackballMove = null;
5430 }
5431 }
Romain Guy06882f82009-06-10 13:36:04 -07005432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5434 int returnWhat) {
5435 if (DEBUG_INPUT) Log.v(
5436 TAG, "finishedKey: client=" + client + ", force=" + force);
5437
5438 if (client == null) {
5439 return null;
5440 }
5441
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005442 MotionEvent res = null;
5443 QueuedEvent qev = null;
5444 WindowState win = null;
5445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 synchronized (this) {
5447 if (DEBUG_INPUT) Log.v(
5448 TAG, "finishedKey: client=" + client.asBinder()
5449 + ", force=" + force + ", last=" + mLastBinder
5450 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 if (returnWhat == RETURN_PENDING_POINTER) {
5453 qev = session.mPendingPointerMove;
5454 win = session.mPendingPointerWindow;
5455 session.mPendingPointerMove = null;
5456 session.mPendingPointerWindow = null;
5457 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5458 qev = session.mPendingTrackballMove;
5459 win = session.mPendingTrackballWindow;
5460 session.mPendingTrackballMove = null;
5461 session.mPendingTrackballWindow = null;
5462 }
Romain Guy06882f82009-06-10 13:36:04 -07005463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 if (mLastBinder == client.asBinder()) {
5465 if (DEBUG_INPUT) Log.v(
5466 TAG, "finishedKey: last paused="
5467 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5468 if (mLastWin != null && (!mLastWin.mToken.paused || force
5469 || !mEventDispatching)) {
5470 doFinishedKeyLocked(false);
5471 } else {
5472 // Make sure to wake up anyone currently waiting to
5473 // dispatch a key, so they can re-evaluate their
5474 // current situation.
5475 mFinished = true;
5476 notifyAll();
5477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 }
Romain Guy06882f82009-06-10 13:36:04 -07005479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005481 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 if (DEBUG_INPUT) Log.v(TAG,
5483 "Returning pending motion: " + res);
5484 mQueue.recycleEvent(qev);
5485 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5486 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005490
5491 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5492 synchronized (mWindowMap) {
5493 if (mWallpaperTarget == win) {
5494 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5495 }
5496 }
5497 }
5498
5499 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 }
5501
5502 void tickle() {
5503 synchronized (this) {
5504 notifyAll();
5505 }
5506 }
Romain Guy06882f82009-06-10 13:36:04 -07005507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 void handleNewWindowLocked(WindowState newWindow) {
5509 if (!newWindow.canReceiveKeys()) {
5510 return;
5511 }
5512 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005513 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 TAG, "New key dispatch window: win="
5515 + newWindow.mClient.asBinder()
5516 + ", last=" + mLastBinder
5517 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5518 + "), finished=" + mFinished + ", paused="
5519 + newWindow.mToken.paused);
5520
5521 // Displaying a window implicitly causes dispatching to
5522 // be unpaused. (This is to protect against bugs if someone
5523 // pauses dispatching but forgets to resume.)
5524 newWindow.mToken.paused = false;
5525
5526 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527
5528 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5529 if (DEBUG_INPUT) Log.v(TAG,
5530 "New SYSTEM_ERROR window; resetting state");
5531 mLastWin = null;
5532 mLastBinder = null;
5533 mMotionTarget = null;
5534 mFinished = true;
5535 } else if (mLastWin != null) {
5536 // If the new window is above the window we are
5537 // waiting on, then stop waiting and let key dispatching
5538 // start on the new guy.
5539 if (DEBUG_INPUT) Log.v(
5540 TAG, "Last win layer=" + mLastWin.mLayer
5541 + ", new win layer=" + newWindow.mLayer);
5542 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005543 // The new window is above the old; finish pending input to the last
5544 // window and start directing it to the new one.
5545 mLastWin.mToken.paused = false;
5546 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005548 // Either the new window is lower, so there is no need to wake key waiters,
5549 // or we just finished key input to the previous window, which implicitly
5550 // notified the key waiters. In both cases, we don't need to issue the
5551 // notification here.
5552 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 }
5554
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005555 // Now that we've put a new window state in place, make the event waiter
5556 // take notice and retarget its attentions.
5557 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 }
5559 }
5560
5561 void pauseDispatchingLocked(WindowToken token) {
5562 synchronized (this)
5563 {
5564 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5565 token.paused = true;
5566
5567 /*
5568 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5569 mPaused = true;
5570 } else {
5571 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005572 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005574 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005576 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 }
5578 }
5579 */
5580 }
5581 }
5582
5583 void resumeDispatchingLocked(WindowToken token) {
5584 synchronized (this) {
5585 if (token.paused) {
5586 if (DEBUG_INPUT) Log.v(
5587 TAG, "Resuming WindowToken " + token
5588 + ", last=" + mLastBinder
5589 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5590 + "), finished=" + mFinished + ", paused="
5591 + token.paused);
5592 token.paused = false;
5593 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5594 doFinishedKeyLocked(true);
5595 } else {
5596 notifyAll();
5597 }
5598 }
5599 }
5600 }
5601
5602 void setEventDispatchingLocked(boolean enabled) {
5603 synchronized (this) {
5604 mEventDispatching = enabled;
5605 notifyAll();
5606 }
5607 }
Romain Guy06882f82009-06-10 13:36:04 -07005608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 void appSwitchComing() {
5610 synchronized (this) {
5611 // Don't wait for more than .5 seconds for app to finish
5612 // processing the pending events.
5613 long now = SystemClock.uptimeMillis() + 500;
5614 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5615 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5616 mTimeToSwitch = now;
5617 }
5618 notifyAll();
5619 }
5620 }
Romain Guy06882f82009-06-10 13:36:04 -07005621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 private final void doFinishedKeyLocked(boolean doRecycle) {
5623 if (mLastWin != null) {
5624 releasePendingPointerLocked(mLastWin.mSession);
5625 releasePendingTrackballLocked(mLastWin.mSession);
5626 }
Romain Guy06882f82009-06-10 13:36:04 -07005627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 if (mLastWin == null || !mLastWin.mToken.paused
5629 || !mLastWin.isVisibleLw()) {
5630 // If the current window has been paused, we aren't -really-
5631 // finished... so let the waiters still wait.
5632 mLastWin = null;
5633 mLastBinder = null;
5634 }
5635 mFinished = true;
5636 notifyAll();
5637 }
5638 }
5639
5640 private class KeyQ extends KeyInputQueue
5641 implements KeyInputQueue.FilterCallback {
5642 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005645 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5647 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5648 "KEEP_SCREEN_ON_FLAG");
5649 mHoldingScreen.setReferenceCounted(false);
5650 }
5651
5652 @Override
5653 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5654 if (mPolicy.preprocessInputEventTq(event)) {
5655 return true;
5656 }
Romain Guy06882f82009-06-10 13:36:04 -07005657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 switch (event.type) {
5659 case RawInputEvent.EV_KEY: {
5660 // XXX begin hack
5661 if (DEBUG) {
5662 if (event.keycode == KeyEvent.KEYCODE_G) {
5663 if (event.value != 0) {
5664 // G down
5665 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5666 }
5667 return false;
5668 }
5669 if (event.keycode == KeyEvent.KEYCODE_D) {
5670 if (event.value != 0) {
5671 //dump();
5672 }
5673 return false;
5674 }
5675 }
5676 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 boolean screenIsOff = !mPowerManager.screenIsOn();
5679 boolean screenIsDim = !mPowerManager.screenIsBright();
5680 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5683 mPowerManager.goToSleep(event.when);
5684 }
5685
5686 if (screenIsOff) {
5687 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5688 }
5689 if (screenIsDim) {
5690 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5691 }
5692 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5693 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005694 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 }
Romain Guy06882f82009-06-10 13:36:04 -07005696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5698 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5699 filterQueue(this);
5700 mKeyWaiter.appSwitchComing();
5701 }
5702 return true;
5703 } else {
5704 return false;
5705 }
5706 }
Romain Guy06882f82009-06-10 13:36:04 -07005707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 case RawInputEvent.EV_REL: {
5709 boolean screenIsOff = !mPowerManager.screenIsOn();
5710 boolean screenIsDim = !mPowerManager.screenIsBright();
5711 if (screenIsOff) {
5712 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5713 device.classes, event)) {
5714 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5715 return false;
5716 }
5717 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5718 }
5719 if (screenIsDim) {
5720 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5721 }
5722 return true;
5723 }
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 case RawInputEvent.EV_ABS: {
5726 boolean screenIsOff = !mPowerManager.screenIsOn();
5727 boolean screenIsDim = !mPowerManager.screenIsBright();
5728 if (screenIsOff) {
5729 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5730 device.classes, event)) {
5731 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5732 return false;
5733 }
5734 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5735 }
5736 if (screenIsDim) {
5737 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5738 }
5739 return true;
5740 }
Romain Guy06882f82009-06-10 13:36:04 -07005741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 default:
5743 return true;
5744 }
5745 }
5746
5747 public int filterEvent(QueuedEvent ev) {
5748 switch (ev.classType) {
5749 case RawInputEvent.CLASS_KEYBOARD:
5750 KeyEvent ke = (KeyEvent)ev.event;
5751 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5752 Log.w(TAG, "Dropping movement key during app switch: "
5753 + ke.getKeyCode() + ", action=" + ke.getAction());
5754 return FILTER_REMOVE;
5755 }
5756 return FILTER_ABORT;
5757 default:
5758 return FILTER_KEEP;
5759 }
5760 }
Romain Guy06882f82009-06-10 13:36:04 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 /**
5763 * Must be called with the main window manager lock held.
5764 */
5765 void setHoldScreenLocked(boolean holding) {
5766 boolean state = mHoldingScreen.isHeld();
5767 if (holding != state) {
5768 if (holding) {
5769 mHoldingScreen.acquire();
5770 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005771 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 mHoldingScreen.release();
5773 }
5774 }
5775 }
Michael Chan53071d62009-05-13 17:29:48 -07005776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777
5778 public boolean detectSafeMode() {
5779 mSafeMode = mPolicy.detectSafeMode();
5780 return mSafeMode;
5781 }
Romain Guy06882f82009-06-10 13:36:04 -07005782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 public void systemReady() {
5784 mPolicy.systemReady();
5785 }
Romain Guy06882f82009-06-10 13:36:04 -07005786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 private final class InputDispatcherThread extends Thread {
5788 // Time to wait when there is nothing to do: 9999 seconds.
5789 static final int LONG_WAIT=9999*1000;
5790
5791 public InputDispatcherThread() {
5792 super("InputDispatcher");
5793 }
Romain Guy06882f82009-06-10 13:36:04 -07005794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 @Override
5796 public void run() {
5797 while (true) {
5798 try {
5799 process();
5800 } catch (Exception e) {
5801 Log.e(TAG, "Exception in input dispatcher", e);
5802 }
5803 }
5804 }
Romain Guy06882f82009-06-10 13:36:04 -07005805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 private void process() {
5807 android.os.Process.setThreadPriority(
5808 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 // The last key event we saw
5811 KeyEvent lastKey = null;
5812
5813 // Last keydown time for auto-repeating keys
5814 long lastKeyTime = SystemClock.uptimeMillis();
5815 long nextKeyTime = lastKeyTime+LONG_WAIT;
5816
Romain Guy06882f82009-06-10 13:36:04 -07005817 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 int keyRepeatCount = 0;
5819
5820 // Need to report that configuration has changed?
5821 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 while (true) {
5824 long curTime = SystemClock.uptimeMillis();
5825
5826 if (DEBUG_INPUT) Log.v(
5827 TAG, "Waiting for next key: now=" + curTime
5828 + ", repeat @ " + nextKeyTime);
5829
5830 // Retrieve next event, waiting only as long as the next
5831 // repeat timeout. If the configuration has changed, then
5832 // don't wait at all -- we'll report the change as soon as
5833 // we have processed all events.
5834 QueuedEvent ev = mQueue.getEvent(
5835 (int)((!configChanged && curTime < nextKeyTime)
5836 ? (nextKeyTime-curTime) : 0));
5837
5838 if (DEBUG_INPUT && ev != null) Log.v(
5839 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5840
Michael Chan53071d62009-05-13 17:29:48 -07005841 if (MEASURE_LATENCY) {
5842 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5843 }
5844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 try {
5846 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005847 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 int eventType;
5849 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5850 eventType = eventType((MotionEvent)ev.event);
5851 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
5852 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
5853 eventType = LocalPowerManager.BUTTON_EVENT;
5854 } else {
5855 eventType = LocalPowerManager.OTHER_EVENT;
5856 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005857 try {
Michael Chan53071d62009-05-13 17:29:48 -07005858 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07005859 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07005860 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07005861 mBatteryStats.noteInputEvent();
5862 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005863 } catch (RemoteException e) {
5864 // Ignore
5865 }
Michael Chane10de972009-05-18 11:24:50 -07005866
5867 if (eventType != TOUCH_EVENT
5868 && eventType != LONG_TOUCH_EVENT
5869 && eventType != CHEEK_EVENT) {
5870 mPowerManager.userActivity(curTime, false,
5871 eventType, false);
5872 } else if (mLastTouchEventType != eventType
5873 || (curTime - mLastUserActivityCallTime)
5874 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
5875 mLastUserActivityCallTime = curTime;
5876 mLastTouchEventType = eventType;
5877 mPowerManager.userActivity(curTime, false,
5878 eventType, false);
5879 }
5880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 switch (ev.classType) {
5882 case RawInputEvent.CLASS_KEYBOARD:
5883 KeyEvent ke = (KeyEvent)ev.event;
5884 if (ke.isDown()) {
5885 lastKey = ke;
5886 keyRepeatCount = 0;
5887 lastKeyTime = curTime;
5888 nextKeyTime = lastKeyTime
5889 + KEY_REPEAT_FIRST_DELAY;
5890 if (DEBUG_INPUT) Log.v(
5891 TAG, "Received key down: first repeat @ "
5892 + nextKeyTime);
5893 } else {
5894 lastKey = null;
5895 // Arbitrary long timeout.
5896 lastKeyTime = curTime;
5897 nextKeyTime = curTime + LONG_WAIT;
5898 if (DEBUG_INPUT) Log.v(
5899 TAG, "Received key up: ignore repeat @ "
5900 + nextKeyTime);
5901 }
5902 dispatchKey((KeyEvent)ev.event, 0, 0);
5903 mQueue.recycleEvent(ev);
5904 break;
5905 case RawInputEvent.CLASS_TOUCHSCREEN:
5906 //Log.i(TAG, "Read next event " + ev);
5907 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
5908 break;
5909 case RawInputEvent.CLASS_TRACKBALL:
5910 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
5911 break;
5912 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
5913 configChanged = true;
5914 break;
5915 default:
5916 mQueue.recycleEvent(ev);
5917 break;
5918 }
Romain Guy06882f82009-06-10 13:36:04 -07005919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920 } else if (configChanged) {
5921 configChanged = false;
5922 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07005923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 } else if (lastKey != null) {
5925 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 // Timeout occurred while key was down. If it is at or
5928 // past the key repeat time, dispatch the repeat.
5929 if (DEBUG_INPUT) Log.v(
5930 TAG, "Key timeout: repeat=" + nextKeyTime
5931 + ", now=" + curTime);
5932 if (curTime < nextKeyTime) {
5933 continue;
5934 }
Romain Guy06882f82009-06-10 13:36:04 -07005935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 lastKeyTime = nextKeyTime;
5937 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
5938 keyRepeatCount++;
5939 if (DEBUG_INPUT) Log.v(
5940 TAG, "Key repeat: count=" + keyRepeatCount
5941 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005942 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07005943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 } else {
5945 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07005946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 lastKeyTime = curTime;
5948 nextKeyTime = curTime + LONG_WAIT;
5949 }
Romain Guy06882f82009-06-10 13:36:04 -07005950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 } catch (Exception e) {
5952 Log.e(TAG,
5953 "Input thread received uncaught exception: " + e, e);
5954 }
5955 }
5956 }
5957 }
5958
5959 // -------------------------------------------------------------
5960 // Client Session State
5961 // -------------------------------------------------------------
5962
5963 private final class Session extends IWindowSession.Stub
5964 implements IBinder.DeathRecipient {
5965 final IInputMethodClient mClient;
5966 final IInputContext mInputContext;
5967 final int mUid;
5968 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005969 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 SurfaceSession mSurfaceSession;
5971 int mNumWindow = 0;
5972 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07005973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 /**
5975 * Current pointer move event being dispatched to client window... must
5976 * hold key lock to access.
5977 */
5978 QueuedEvent mPendingPointerMove;
5979 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07005980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005981 /**
5982 * Current trackball move event being dispatched to client window... must
5983 * hold key lock to access.
5984 */
5985 QueuedEvent mPendingTrackballMove;
5986 WindowState mPendingTrackballWindow;
5987
5988 public Session(IInputMethodClient client, IInputContext inputContext) {
5989 mClient = client;
5990 mInputContext = inputContext;
5991 mUid = Binder.getCallingUid();
5992 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005993 StringBuilder sb = new StringBuilder();
5994 sb.append("Session{");
5995 sb.append(Integer.toHexString(System.identityHashCode(this)));
5996 sb.append(" uid ");
5997 sb.append(mUid);
5998 sb.append("}");
5999 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 synchronized (mWindowMap) {
6002 if (mInputMethodManager == null && mHaveInputMethods) {
6003 IBinder b = ServiceManager.getService(
6004 Context.INPUT_METHOD_SERVICE);
6005 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6006 }
6007 }
6008 long ident = Binder.clearCallingIdentity();
6009 try {
6010 // Note: it is safe to call in to the input method manager
6011 // here because we are not holding our lock.
6012 if (mInputMethodManager != null) {
6013 mInputMethodManager.addClient(client, inputContext,
6014 mUid, mPid);
6015 } else {
6016 client.setUsingInputMethod(false);
6017 }
6018 client.asBinder().linkToDeath(this, 0);
6019 } catch (RemoteException e) {
6020 // The caller has died, so we can just forget about this.
6021 try {
6022 if (mInputMethodManager != null) {
6023 mInputMethodManager.removeClient(client);
6024 }
6025 } catch (RemoteException ee) {
6026 }
6027 } finally {
6028 Binder.restoreCallingIdentity(ident);
6029 }
6030 }
Romain Guy06882f82009-06-10 13:36:04 -07006031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 @Override
6033 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6034 throws RemoteException {
6035 try {
6036 return super.onTransact(code, data, reply, flags);
6037 } catch (RuntimeException e) {
6038 // Log all 'real' exceptions thrown to the caller
6039 if (!(e instanceof SecurityException)) {
6040 Log.e(TAG, "Window Session Crash", e);
6041 }
6042 throw e;
6043 }
6044 }
6045
6046 public void binderDied() {
6047 // Note: it is safe to call in to the input method manager
6048 // here because we are not holding our lock.
6049 try {
6050 if (mInputMethodManager != null) {
6051 mInputMethodManager.removeClient(mClient);
6052 }
6053 } catch (RemoteException e) {
6054 }
6055 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006056 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 mClientDead = true;
6058 killSessionLocked();
6059 }
6060 }
6061
6062 public int add(IWindow window, WindowManager.LayoutParams attrs,
6063 int viewVisibility, Rect outContentInsets) {
6064 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6065 }
Romain Guy06882f82009-06-10 13:36:04 -07006066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 public void remove(IWindow window) {
6068 removeWindow(this, window);
6069 }
Romain Guy06882f82009-06-10 13:36:04 -07006070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6072 int requestedWidth, int requestedHeight, int viewFlags,
6073 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6074 Rect outVisibleInsets, Surface outSurface) {
6075 return relayoutWindow(this, window, attrs,
6076 requestedWidth, requestedHeight, viewFlags, insetsPending,
6077 outFrame, outContentInsets, outVisibleInsets, outSurface);
6078 }
Romain Guy06882f82009-06-10 13:36:04 -07006079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 public void setTransparentRegion(IWindow window, Region region) {
6081 setTransparentRegionWindow(this, window, region);
6082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 public void setInsets(IWindow window, int touchableInsets,
6085 Rect contentInsets, Rect visibleInsets) {
6086 setInsetsWindow(this, window, touchableInsets, contentInsets,
6087 visibleInsets);
6088 }
Romain Guy06882f82009-06-10 13:36:04 -07006089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6091 getWindowDisplayFrame(this, window, outDisplayFrame);
6092 }
Romain Guy06882f82009-06-10 13:36:04 -07006093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 public void finishDrawing(IWindow window) {
6095 if (localLOGV) Log.v(
6096 TAG, "IWindow finishDrawing called for " + window);
6097 finishDrawingWindow(this, window);
6098 }
6099
6100 public void finishKey(IWindow window) {
6101 if (localLOGV) Log.v(
6102 TAG, "IWindow finishKey called for " + window);
6103 mKeyWaiter.finishedKey(this, window, false,
6104 KeyWaiter.RETURN_NOTHING);
6105 }
6106
6107 public MotionEvent getPendingPointerMove(IWindow window) {
6108 if (localLOGV) Log.v(
6109 TAG, "IWindow getPendingMotionEvent called for " + window);
6110 return mKeyWaiter.finishedKey(this, window, false,
6111 KeyWaiter.RETURN_PENDING_POINTER);
6112 }
Romain Guy06882f82009-06-10 13:36:04 -07006113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 public MotionEvent getPendingTrackballMove(IWindow window) {
6115 if (localLOGV) Log.v(
6116 TAG, "IWindow getPendingMotionEvent called for " + window);
6117 return mKeyWaiter.finishedKey(this, window, false,
6118 KeyWaiter.RETURN_PENDING_TRACKBALL);
6119 }
6120
6121 public void setInTouchMode(boolean mode) {
6122 synchronized(mWindowMap) {
6123 mInTouchMode = mode;
6124 }
6125 }
6126
6127 public boolean getInTouchMode() {
6128 synchronized(mWindowMap) {
6129 return mInTouchMode;
6130 }
6131 }
6132
6133 public boolean performHapticFeedback(IWindow window, int effectId,
6134 boolean always) {
6135 synchronized(mWindowMap) {
6136 long ident = Binder.clearCallingIdentity();
6137 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006138 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 windowForClientLocked(this, window), effectId, always);
6140 } finally {
6141 Binder.restoreCallingIdentity(ident);
6142 }
6143 }
6144 }
Romain Guy06882f82009-06-10 13:36:04 -07006145
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006146 public void setWallpaperPosition(IBinder window, float x, float y) {
6147 synchronized(mWindowMap) {
6148 long ident = Binder.clearCallingIdentity();
6149 try {
6150 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6151 x, y);
6152 } finally {
6153 Binder.restoreCallingIdentity(ident);
6154 }
6155 }
6156 }
6157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 void windowAddedLocked() {
6159 if (mSurfaceSession == null) {
6160 if (localLOGV) Log.v(
6161 TAG, "First window added to " + this + ", creating SurfaceSession");
6162 mSurfaceSession = new SurfaceSession();
6163 mSessions.add(this);
6164 }
6165 mNumWindow++;
6166 }
6167
6168 void windowRemovedLocked() {
6169 mNumWindow--;
6170 killSessionLocked();
6171 }
Romain Guy06882f82009-06-10 13:36:04 -07006172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 void killSessionLocked() {
6174 if (mNumWindow <= 0 && mClientDead) {
6175 mSessions.remove(this);
6176 if (mSurfaceSession != null) {
6177 if (localLOGV) Log.v(
6178 TAG, "Last window removed from " + this
6179 + ", destroying " + mSurfaceSession);
6180 try {
6181 mSurfaceSession.kill();
6182 } catch (Exception e) {
6183 Log.w(TAG, "Exception thrown when killing surface session "
6184 + mSurfaceSession + " in session " + this
6185 + ": " + e.toString());
6186 }
6187 mSurfaceSession = null;
6188 }
6189 }
6190 }
Romain Guy06882f82009-06-10 13:36:04 -07006191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006193 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6194 pw.print(" mClientDead="); pw.print(mClientDead);
6195 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6196 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6197 pw.print(prefix);
6198 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6199 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6200 }
6201 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6202 pw.print(prefix);
6203 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6204 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 }
6207
6208 @Override
6209 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006210 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 }
6212 }
6213
6214 // -------------------------------------------------------------
6215 // Client Window State
6216 // -------------------------------------------------------------
6217
6218 private final class WindowState implements WindowManagerPolicy.WindowState {
6219 final Session mSession;
6220 final IWindow mClient;
6221 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006222 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 AppWindowToken mAppToken;
6224 AppWindowToken mTargetAppToken;
6225 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6226 final DeathRecipient mDeathRecipient;
6227 final WindowState mAttachedWindow;
6228 final ArrayList mChildWindows = new ArrayList();
6229 final int mBaseLayer;
6230 final int mSubLayer;
6231 final boolean mLayoutAttached;
6232 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006233 final boolean mIsWallpaper;
6234 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 int mViewVisibility;
6236 boolean mPolicyVisibility = true;
6237 boolean mPolicyVisibilityAfterAnim = true;
6238 boolean mAppFreezing;
6239 Surface mSurface;
6240 boolean mAttachedHidden; // is our parent window hidden?
6241 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006242 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 int mRequestedWidth;
6244 int mRequestedHeight;
6245 int mLastRequestedWidth;
6246 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 int mLayer;
6248 int mAnimLayer;
6249 int mLastLayer;
6250 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006251 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252
6253 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 // Actual frame shown on-screen (may be modified by animation)
6256 final Rect mShownFrame = new Rect();
6257 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 /**
6260 * Insets that determine the actually visible area
6261 */
6262 final Rect mVisibleInsets = new Rect();
6263 final Rect mLastVisibleInsets = new Rect();
6264 boolean mVisibleInsetsChanged;
6265
6266 /**
6267 * Insets that are covered by system windows
6268 */
6269 final Rect mContentInsets = new Rect();
6270 final Rect mLastContentInsets = new Rect();
6271 boolean mContentInsetsChanged;
6272
6273 /**
6274 * Set to true if we are waiting for this window to receive its
6275 * given internal insets before laying out other windows based on it.
6276 */
6277 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 /**
6280 * These are the content insets that were given during layout for
6281 * this window, to be applied to windows behind it.
6282 */
6283 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 /**
6286 * These are the visible insets that were given during layout for
6287 * this window, to be applied to windows behind it.
6288 */
6289 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006291 /**
6292 * Flag indicating whether the touchable region should be adjusted by
6293 * the visible insets; if false the area outside the visible insets is
6294 * NOT touchable, so we must use those to adjust the frame during hit
6295 * tests.
6296 */
6297 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 // Current transformation being applied.
6300 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6301 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6302 float mHScale=1, mVScale=1;
6303 float mLastHScale=1, mLastVScale=1;
6304 final Matrix mTmpMatrix = new Matrix();
6305
6306 // "Real" frame that the application sees.
6307 final Rect mFrame = new Rect();
6308 final Rect mLastFrame = new Rect();
6309
6310 final Rect mContainingFrame = new Rect();
6311 final Rect mDisplayFrame = new Rect();
6312 final Rect mContentFrame = new Rect();
6313 final Rect mVisibleFrame = new Rect();
6314
6315 float mShownAlpha = 1;
6316 float mAlpha = 1;
6317 float mLastAlpha = 1;
6318
6319 // Set to true if, when the window gets displayed, it should perform
6320 // an enter animation.
6321 boolean mEnterAnimationPending;
6322
6323 // Currently running animation.
6324 boolean mAnimating;
6325 boolean mLocalAnimating;
6326 Animation mAnimation;
6327 boolean mAnimationIsEntrance;
6328 boolean mHasTransformation;
6329 boolean mHasLocalTransformation;
6330 final Transformation mTransformation = new Transformation();
6331
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006332 // If a window showing a wallpaper: the requested offset for the
6333 // wallpaper; if a wallpaper window: the currently applied offset.
6334 float mWallpaperX = -1;
6335 float mWallpaperY = -1;
6336
6337 // Wallpaper windows: pixels offset based on above variables.
6338 int mXOffset;
6339 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 // This is set after IWindowSession.relayout() has been called at
6342 // least once for the window. It allows us to detect the situation
6343 // where we don't yet have a surface, but should have one soon, so
6344 // we can give the window focus before waiting for the relayout.
6345 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 // This is set after the Surface has been created but before the
6348 // window has been drawn. During this time the surface is hidden.
6349 boolean mDrawPending;
6350
6351 // This is set after the window has finished drawing for the first
6352 // time but before its surface is shown. The surface will be
6353 // displayed when the next layout is run.
6354 boolean mCommitDrawPending;
6355
6356 // This is set during the time after the window's drawing has been
6357 // committed, and before its surface is actually shown. It is used
6358 // to delay showing the surface until all windows in a token are ready
6359 // to be shown.
6360 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 // Set when the window has been shown in the screen the first time.
6363 boolean mHasDrawn;
6364
6365 // Currently running an exit animation?
6366 boolean mExiting;
6367
6368 // Currently on the mDestroySurface list?
6369 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 // Completely remove from window manager after exit animation?
6372 boolean mRemoveOnExit;
6373
6374 // Set when the orientation is changing and this window has not yet
6375 // been updated for the new orientation.
6376 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 // Is this window now (or just being) removed?
6379 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 WindowState(Session s, IWindow c, WindowToken token,
6382 WindowState attachedWindow, WindowManager.LayoutParams a,
6383 int viewVisibility) {
6384 mSession = s;
6385 mClient = c;
6386 mToken = token;
6387 mAttrs.copyFrom(a);
6388 mViewVisibility = viewVisibility;
6389 DeathRecipient deathRecipient = new DeathRecipient();
6390 mAlpha = a.alpha;
6391 if (localLOGV) Log.v(
6392 TAG, "Window " + this + " client=" + c.asBinder()
6393 + " token=" + token + " (" + mAttrs.token + ")");
6394 try {
6395 c.asBinder().linkToDeath(deathRecipient, 0);
6396 } catch (RemoteException e) {
6397 mDeathRecipient = null;
6398 mAttachedWindow = null;
6399 mLayoutAttached = false;
6400 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006401 mIsWallpaper = false;
6402 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 mBaseLayer = 0;
6404 mSubLayer = 0;
6405 return;
6406 }
6407 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6410 mAttrs.type <= LAST_SUB_WINDOW)) {
6411 // The multiplier here is to reserve space for multiple
6412 // windows in the same type layer.
6413 mBaseLayer = mPolicy.windowTypeToLayerLw(
6414 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6415 + TYPE_LAYER_OFFSET;
6416 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6417 mAttachedWindow = attachedWindow;
6418 mAttachedWindow.mChildWindows.add(this);
6419 mLayoutAttached = mAttrs.type !=
6420 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6421 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6422 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006423 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6424 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 } else {
6426 // The multiplier here is to reserve space for multiple
6427 // windows in the same type layer.
6428 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6429 * TYPE_LAYER_MULTIPLIER
6430 + TYPE_LAYER_OFFSET;
6431 mSubLayer = 0;
6432 mAttachedWindow = null;
6433 mLayoutAttached = false;
6434 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6435 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006436 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6437 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 }
6439
6440 WindowState appWin = this;
6441 while (appWin.mAttachedWindow != null) {
6442 appWin = mAttachedWindow;
6443 }
6444 WindowToken appToken = appWin.mToken;
6445 while (appToken.appWindowToken == null) {
6446 WindowToken parent = mTokenMap.get(appToken.token);
6447 if (parent == null || appToken == parent) {
6448 break;
6449 }
6450 appToken = parent;
6451 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006452 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 mAppToken = appToken.appWindowToken;
6454
6455 mSurface = null;
6456 mRequestedWidth = 0;
6457 mRequestedHeight = 0;
6458 mLastRequestedWidth = 0;
6459 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006460 mXOffset = 0;
6461 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 mLayer = 0;
6463 mAnimLayer = 0;
6464 mLastLayer = 0;
6465 }
6466
6467 void attach() {
6468 if (localLOGV) Log.v(
6469 TAG, "Attaching " + this + " token=" + mToken
6470 + ", list=" + mToken.windows);
6471 mSession.windowAddedLocked();
6472 }
6473
6474 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6475 mHaveFrame = true;
6476
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006477 final Rect container = mContainingFrame;
6478 container.set(pf);
6479
6480 final Rect display = mDisplayFrame;
6481 display.set(df);
6482
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006483 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006484 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006485 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6486 display.intersect(mCompatibleScreenFrame);
6487 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006488 }
6489
6490 final int pw = container.right - container.left;
6491 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492
6493 int w,h;
6494 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6495 w = mAttrs.width < 0 ? pw : mAttrs.width;
6496 h = mAttrs.height< 0 ? ph : mAttrs.height;
6497 } else {
6498 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6499 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6500 }
Romain Guy06882f82009-06-10 13:36:04 -07006501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 final Rect content = mContentFrame;
6503 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 final Rect visible = mVisibleFrame;
6506 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006509 final int fw = frame.width();
6510 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6513 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6514
6515 Gravity.apply(mAttrs.gravity, w, h, container,
6516 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6517 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6518
6519 //System.out.println("Out: " + mFrame);
6520
6521 // Now make sure the window fits in the overall display.
6522 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 // Make sure the content and visible frames are inside of the
6525 // final window frame.
6526 if (content.left < frame.left) content.left = frame.left;
6527 if (content.top < frame.top) content.top = frame.top;
6528 if (content.right > frame.right) content.right = frame.right;
6529 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6530 if (visible.left < frame.left) visible.left = frame.left;
6531 if (visible.top < frame.top) visible.top = frame.top;
6532 if (visible.right > frame.right) visible.right = frame.right;
6533 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 final Rect contentInsets = mContentInsets;
6536 contentInsets.left = content.left-frame.left;
6537 contentInsets.top = content.top-frame.top;
6538 contentInsets.right = frame.right-content.right;
6539 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 final Rect visibleInsets = mVisibleInsets;
6542 visibleInsets.left = visible.left-frame.left;
6543 visibleInsets.top = visible.top-frame.top;
6544 visibleInsets.right = frame.right-visible.right;
6545 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006546
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006547 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())
6548 && mWallpaperTarget != null) {
6549 updateWallpaperOffsetLocked(mWallpaperTarget, this,
6550 mDisplay.getWidth(), mDisplay.getHeight());
6551 }
6552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 if (localLOGV) {
6554 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6555 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6556 Log.v(TAG, "Resolving (mRequestedWidth="
6557 + mRequestedWidth + ", mRequestedheight="
6558 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6559 + "): frame=" + mFrame.toShortString()
6560 + " ci=" + contentInsets.toShortString()
6561 + " vi=" + visibleInsets.toShortString());
6562 //}
6563 }
6564 }
Romain Guy06882f82009-06-10 13:36:04 -07006565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 public Rect getFrameLw() {
6567 return mFrame;
6568 }
6569
6570 public Rect getShownFrameLw() {
6571 return mShownFrame;
6572 }
6573
6574 public Rect getDisplayFrameLw() {
6575 return mDisplayFrame;
6576 }
6577
6578 public Rect getContentFrameLw() {
6579 return mContentFrame;
6580 }
6581
6582 public Rect getVisibleFrameLw() {
6583 return mVisibleFrame;
6584 }
6585
6586 public boolean getGivenInsetsPendingLw() {
6587 return mGivenInsetsPending;
6588 }
6589
6590 public Rect getGivenContentInsetsLw() {
6591 return mGivenContentInsets;
6592 }
Romain Guy06882f82009-06-10 13:36:04 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 public Rect getGivenVisibleInsetsLw() {
6595 return mGivenVisibleInsets;
6596 }
Romain Guy06882f82009-06-10 13:36:04 -07006597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 public WindowManager.LayoutParams getAttrs() {
6599 return mAttrs;
6600 }
6601
6602 public int getSurfaceLayer() {
6603 return mLayer;
6604 }
Romain Guy06882f82009-06-10 13:36:04 -07006605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 public IApplicationToken getAppToken() {
6607 return mAppToken != null ? mAppToken.appToken : null;
6608 }
6609
6610 public boolean hasAppShownWindows() {
6611 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6612 }
6613
6614 public boolean hasAppStartingIcon() {
6615 return mAppToken != null ? (mAppToken.startingData != null) : false;
6616 }
6617
6618 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6619 return mAppToken != null ? mAppToken.startingWindow : null;
6620 }
6621
6622 public void setAnimation(Animation anim) {
6623 if (localLOGV) Log.v(
6624 TAG, "Setting animation in " + this + ": " + anim);
6625 mAnimating = false;
6626 mLocalAnimating = false;
6627 mAnimation = anim;
6628 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6629 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6630 }
6631
6632 public void clearAnimation() {
6633 if (mAnimation != null) {
6634 mAnimating = true;
6635 mLocalAnimating = false;
6636 mAnimation = null;
6637 }
6638 }
Romain Guy06882f82009-06-10 13:36:04 -07006639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 Surface createSurfaceLocked() {
6641 if (mSurface == null) {
6642 mDrawPending = true;
6643 mCommitDrawPending = false;
6644 mReadyToShow = false;
6645 if (mAppToken != null) {
6646 mAppToken.allDrawn = false;
6647 }
6648
6649 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006650 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 flags |= Surface.PUSH_BUFFERS;
6652 }
6653
6654 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6655 flags |= Surface.SECURE;
6656 }
6657 if (DEBUG_VISIBILITY) Log.v(
6658 TAG, "Creating surface in session "
6659 + mSession.mSurfaceSession + " window " + this
6660 + " w=" + mFrame.width()
6661 + " h=" + mFrame.height() + " format="
6662 + mAttrs.format + " flags=" + flags);
6663
6664 int w = mFrame.width();
6665 int h = mFrame.height();
6666 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6667 // for a scaled surface, we always want the requested
6668 // size.
6669 w = mRequestedWidth;
6670 h = mRequestedHeight;
6671 }
6672
6673 try {
6674 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006675 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 0, w, h, mAttrs.format, flags);
6677 } catch (Surface.OutOfResourcesException e) {
6678 Log.w(TAG, "OutOfResourcesException creating surface");
6679 reclaimSomeSurfaceMemoryLocked(this, "create");
6680 return null;
6681 } catch (Exception e) {
6682 Log.e(TAG, "Exception creating surface", e);
6683 return null;
6684 }
Romain Guy06882f82009-06-10 13:36:04 -07006685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 if (localLOGV) Log.v(
6687 TAG, "Got surface: " + mSurface
6688 + ", set left=" + mFrame.left + " top=" + mFrame.top
6689 + ", animLayer=" + mAnimLayer);
6690 if (SHOW_TRANSACTIONS) {
6691 Log.i(TAG, ">>> OPEN TRANSACTION");
6692 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6693 + mAttrs.getTitle() + ") pos=(" +
6694 mFrame.left + "," + mFrame.top + ") (" +
6695 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6696 mAnimLayer + " HIDE");
6697 }
6698 Surface.openTransaction();
6699 try {
6700 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006701 mSurface.setPosition(mFrame.left + mXOffset,
6702 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 mSurface.setLayer(mAnimLayer);
6704 mSurface.hide();
6705 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
6706 mSurface.setFlags(Surface.SURFACE_DITHER,
6707 Surface.SURFACE_DITHER);
6708 }
6709 } catch (RuntimeException e) {
6710 Log.w(TAG, "Error creating surface in " + w, e);
6711 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6712 }
6713 mLastHidden = true;
6714 } finally {
6715 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6716 Surface.closeTransaction();
6717 }
6718 if (localLOGV) Log.v(
6719 TAG, "Created surface " + this);
6720 }
6721 return mSurface;
6722 }
Romain Guy06882f82009-06-10 13:36:04 -07006723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 void destroySurfaceLocked() {
6725 // Window is no longer on-screen, so can no longer receive
6726 // key events... if we were waiting for it to finish
6727 // handling a key event, the wait is over!
6728 mKeyWaiter.finishedKey(mSession, mClient, true,
6729 KeyWaiter.RETURN_NOTHING);
6730 mKeyWaiter.releasePendingPointerLocked(mSession);
6731 mKeyWaiter.releasePendingTrackballLocked(mSession);
6732
6733 if (mAppToken != null && this == mAppToken.startingWindow) {
6734 mAppToken.startingDisplayed = false;
6735 }
Romain Guy06882f82009-06-10 13:36:04 -07006736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 if (mSurface != null) {
6738 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006739 if (DEBUG_VISIBILITY) {
6740 RuntimeException e = new RuntimeException();
6741 e.fillInStackTrace();
6742 Log.w(TAG, "Window " + this + " destroying surface "
6743 + mSurface + ", session " + mSession, e);
6744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 if (SHOW_TRANSACTIONS) {
6746 RuntimeException ex = new RuntimeException();
6747 ex.fillInStackTrace();
6748 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6749 + mAttrs.getTitle() + ")", ex);
6750 }
6751 mSurface.clear();
6752 } catch (RuntimeException e) {
6753 Log.w(TAG, "Exception thrown when destroying Window " + this
6754 + " surface " + mSurface + " session " + mSession
6755 + ": " + e.toString());
6756 }
6757 mSurface = null;
6758 mDrawPending = false;
6759 mCommitDrawPending = false;
6760 mReadyToShow = false;
6761
6762 int i = mChildWindows.size();
6763 while (i > 0) {
6764 i--;
6765 WindowState c = (WindowState)mChildWindows.get(i);
6766 c.mAttachedHidden = true;
6767 }
6768 }
6769 }
6770
6771 boolean finishDrawingLocked() {
6772 if (mDrawPending) {
6773 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6774 TAG, "finishDrawingLocked: " + mSurface);
6775 mCommitDrawPending = true;
6776 mDrawPending = false;
6777 return true;
6778 }
6779 return false;
6780 }
6781
6782 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006783 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006784 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6785 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006786 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006787 }
6788 mCommitDrawPending = false;
6789 mReadyToShow = true;
6790 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6791 final AppWindowToken atoken = mAppToken;
6792 if (atoken == null || atoken.allDrawn || starting) {
6793 performShowLocked();
6794 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006795 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 }
6797
6798 // This must be called while inside a transaction.
6799 boolean performShowLocked() {
6800 if (DEBUG_VISIBILITY) {
6801 RuntimeException e = new RuntimeException();
6802 e.fillInStackTrace();
6803 Log.v(TAG, "performShow on " + this
6804 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6805 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6806 }
6807 if (mReadyToShow && isReadyForDisplay()) {
6808 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6809 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6810 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6811 + " during animation: policyVis=" + mPolicyVisibility
6812 + " attHidden=" + mAttachedHidden
6813 + " tok.hiddenRequested="
6814 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6815 + " tok.idden="
6816 + (mAppToken != null ? mAppToken.hidden : false)
6817 + " animating=" + mAnimating
6818 + " tok animating="
6819 + (mAppToken != null ? mAppToken.animating : false));
6820 if (!showSurfaceRobustlyLocked(this)) {
6821 return false;
6822 }
6823 mLastAlpha = -1;
6824 mHasDrawn = true;
6825 mLastHidden = false;
6826 mReadyToShow = false;
6827 enableScreenIfNeededLocked();
6828
6829 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006831 int i = mChildWindows.size();
6832 while (i > 0) {
6833 i--;
6834 WindowState c = (WindowState)mChildWindows.get(i);
6835 if (c.mSurface != null && c.mAttachedHidden) {
6836 c.mAttachedHidden = false;
6837 c.performShowLocked();
6838 }
6839 }
Romain Guy06882f82009-06-10 13:36:04 -07006840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 if (mAttrs.type != TYPE_APPLICATION_STARTING
6842 && mAppToken != null) {
6843 mAppToken.firstWindowDrawn = true;
6844 if (mAnimation == null && mAppToken.startingData != null) {
6845 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6846 + mToken
6847 + ": first real window is shown, no animation");
6848 mFinishedStarting.add(mAppToken);
6849 mH.sendEmptyMessage(H.FINISHED_STARTING);
6850 }
6851 mAppToken.updateReportedVisibilityLocked();
6852 }
6853 }
6854 return true;
6855 }
Romain Guy06882f82009-06-10 13:36:04 -07006856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 // This must be called while inside a transaction. Returns true if
6858 // there is more animation to run.
6859 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
6860 if (!mDisplayFrozen) {
6861 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07006862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
6864 mHasTransformation = true;
6865 mHasLocalTransformation = true;
6866 if (!mLocalAnimating) {
6867 if (DEBUG_ANIM) Log.v(
6868 TAG, "Starting animation in " + this +
6869 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
6870 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
6871 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
6872 mAnimation.setStartTime(currentTime);
6873 mLocalAnimating = true;
6874 mAnimating = true;
6875 }
6876 mTransformation.clear();
6877 final boolean more = mAnimation.getTransformation(
6878 currentTime, mTransformation);
6879 if (DEBUG_ANIM) Log.v(
6880 TAG, "Stepped animation in " + this +
6881 ": more=" + more + ", xform=" + mTransformation);
6882 if (more) {
6883 // we're not done!
6884 return true;
6885 }
6886 if (DEBUG_ANIM) Log.v(
6887 TAG, "Finished animation in " + this +
6888 " @ " + currentTime);
6889 mAnimation = null;
6890 //WindowManagerService.this.dump();
6891 }
6892 mHasLocalTransformation = false;
6893 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006894 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 // When our app token is animating, we kind-of pretend like
6896 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
6897 // part of this check means that we will only do this if
6898 // our window is not currently exiting, or it is not
6899 // locally animating itself. The idea being that one that
6900 // is exiting and doing a local animation should be removed
6901 // once that animation is done.
6902 mAnimating = true;
6903 mHasTransformation = true;
6904 mTransformation.clear();
6905 return false;
6906 } else if (mHasTransformation) {
6907 // Little trick to get through the path below to act like
6908 // we have finished an animation.
6909 mAnimating = true;
6910 } else if (isAnimating()) {
6911 mAnimating = true;
6912 }
6913 } else if (mAnimation != null) {
6914 // If the display is frozen, and there is a pending animation,
6915 // clear it and make sure we run the cleanup code.
6916 mAnimating = true;
6917 mLocalAnimating = true;
6918 mAnimation = null;
6919 }
Romain Guy06882f82009-06-10 13:36:04 -07006920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 if (!mAnimating && !mLocalAnimating) {
6922 return false;
6923 }
6924
6925 if (DEBUG_ANIM) Log.v(
6926 TAG, "Animation done in " + this + ": exiting=" + mExiting
6927 + ", reportedVisible="
6928 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07006929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 mAnimating = false;
6931 mLocalAnimating = false;
6932 mAnimation = null;
6933 mAnimLayer = mLayer;
6934 if (mIsImWindow) {
6935 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006936 } else if (mIsWallpaper) {
6937 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 }
6939 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
6940 + " anim layer: " + mAnimLayer);
6941 mHasTransformation = false;
6942 mHasLocalTransformation = false;
6943 mPolicyVisibility = mPolicyVisibilityAfterAnim;
6944 mTransformation.clear();
6945 if (mHasDrawn
6946 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
6947 && mAppToken != null
6948 && mAppToken.firstWindowDrawn
6949 && mAppToken.startingData != null) {
6950 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
6951 + mToken + ": first real window done animating");
6952 mFinishedStarting.add(mAppToken);
6953 mH.sendEmptyMessage(H.FINISHED_STARTING);
6954 }
Romain Guy06882f82009-06-10 13:36:04 -07006955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 finishExit();
6957
6958 if (mAppToken != null) {
6959 mAppToken.updateReportedVisibilityLocked();
6960 }
6961
6962 return false;
6963 }
6964
6965 void finishExit() {
6966 if (DEBUG_ANIM) Log.v(
6967 TAG, "finishExit in " + this
6968 + ": exiting=" + mExiting
6969 + " remove=" + mRemoveOnExit
6970 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07006971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 final int N = mChildWindows.size();
6973 for (int i=0; i<N; i++) {
6974 ((WindowState)mChildWindows.get(i)).finishExit();
6975 }
Romain Guy06882f82009-06-10 13:36:04 -07006976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 if (!mExiting) {
6978 return;
6979 }
Romain Guy06882f82009-06-10 13:36:04 -07006980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 if (isWindowAnimating()) {
6982 return;
6983 }
6984
6985 if (localLOGV) Log.v(
6986 TAG, "Exit animation finished in " + this
6987 + ": remove=" + mRemoveOnExit);
6988 if (mSurface != null) {
6989 mDestroySurface.add(this);
6990 mDestroying = true;
6991 if (SHOW_TRANSACTIONS) Log.i(
6992 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
6993 try {
6994 mSurface.hide();
6995 } catch (RuntimeException e) {
6996 Log.w(TAG, "Error hiding surface in " + this, e);
6997 }
6998 mLastHidden = true;
6999 mKeyWaiter.releasePendingPointerLocked(mSession);
7000 }
7001 mExiting = false;
7002 if (mRemoveOnExit) {
7003 mPendingRemove.add(this);
7004 mRemoveOnExit = false;
7005 }
7006 }
Romain Guy06882f82009-06-10 13:36:04 -07007007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7009 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7010 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7011 if (dtdx < -.000001f || dtdx > .000001f) return false;
7012 if (dsdy < -.000001f || dsdy > .000001f) return false;
7013 return true;
7014 }
Romain Guy06882f82009-06-10 13:36:04 -07007015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016 void computeShownFrameLocked() {
7017 final boolean selfTransformation = mHasLocalTransformation;
7018 Transformation attachedTransformation =
7019 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7020 ? mAttachedWindow.mTransformation : null;
7021 Transformation appTransformation =
7022 (mAppToken != null && mAppToken.hasTransformation)
7023 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007024
7025 // Wallpapers are animated based on the "real" window they
7026 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007027 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007028 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007029 if (mWallpaperTarget.mHasLocalTransformation) {
7030 attachedTransformation = mWallpaperTarget.mTransformation;
7031 }
7032 if (mWallpaperTarget.mAppToken != null &&
7033 mWallpaperTarget.mAppToken.hasTransformation) {
7034 appTransformation = mWallpaperTarget.mAppToken.transformation;
7035 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007036 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7037 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7038 }
7039 if (DEBUG_WALLPAPER && appTransformation != null) {
7040 Log.v(TAG, "WP target app xform: " + appTransformation);
7041 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007042 }
7043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 if (selfTransformation || attachedTransformation != null
7045 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007046 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 final Rect frame = mFrame;
7048 final float tmpFloats[] = mTmpFloats;
7049 final Matrix tmpMatrix = mTmpMatrix;
7050
7051 // Compute the desired transformation.
7052 tmpMatrix.setTranslate(frame.left, frame.top);
7053 if (selfTransformation) {
7054 tmpMatrix.preConcat(mTransformation.getMatrix());
7055 }
7056 if (attachedTransformation != null) {
7057 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7058 }
7059 if (appTransformation != null) {
7060 tmpMatrix.preConcat(appTransformation.getMatrix());
7061 }
7062
7063 // "convert" it into SurfaceFlinger's format
7064 // (a 2x2 matrix + an offset)
7065 // Here we must not transform the position of the surface
7066 // since it is already included in the transformation.
7067 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 tmpMatrix.getValues(tmpFloats);
7070 mDsDx = tmpFloats[Matrix.MSCALE_X];
7071 mDtDx = tmpFloats[Matrix.MSKEW_X];
7072 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7073 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007074 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7075 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 int w = frame.width();
7077 int h = frame.height();
7078 mShownFrame.set(x, y, x+w, y+h);
7079
7080 // Now set the alpha... but because our current hardware
7081 // can't do alpha transformation on a non-opaque surface,
7082 // turn it off if we are running an animation that is also
7083 // transforming since it is more important to have that
7084 // animation be smooth.
7085 mShownAlpha = mAlpha;
7086 if (!mLimitedAlphaCompositing
7087 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7088 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7089 && x == frame.left && y == frame.top))) {
7090 //Log.i(TAG, "Applying alpha transform");
7091 if (selfTransformation) {
7092 mShownAlpha *= mTransformation.getAlpha();
7093 }
7094 if (attachedTransformation != null) {
7095 mShownAlpha *= attachedTransformation.getAlpha();
7096 }
7097 if (appTransformation != null) {
7098 mShownAlpha *= appTransformation.getAlpha();
7099 }
7100 } else {
7101 //Log.i(TAG, "Not applying alpha transform");
7102 }
Romain Guy06882f82009-06-10 13:36:04 -07007103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 if (localLOGV) Log.v(
7105 TAG, "Continuing animation in " + this +
7106 ": " + mShownFrame +
7107 ", alpha=" + mTransformation.getAlpha());
7108 return;
7109 }
Romain Guy06882f82009-06-10 13:36:04 -07007110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007112 if (mXOffset != 0 || mYOffset != 0) {
7113 mShownFrame.offset(mXOffset, mYOffset);
7114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 mShownAlpha = mAlpha;
7116 mDsDx = 1;
7117 mDtDx = 0;
7118 mDsDy = 0;
7119 mDtDy = 1;
7120 }
Romain Guy06882f82009-06-10 13:36:04 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 /**
7123 * Is this window visible? It is not visible if there is no
7124 * surface, or we are in the process of running an exit animation
7125 * that will remove the surface, or its app token has been hidden.
7126 */
7127 public boolean isVisibleLw() {
7128 final AppWindowToken atoken = mAppToken;
7129 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7130 && (atoken == null || !atoken.hiddenRequested)
7131 && !mExiting && !mDestroying;
7132 }
7133
7134 /**
7135 * Is this window visible, ignoring its app token? It is not visible
7136 * if there is no surface, or we are in the process of running an exit animation
7137 * that will remove the surface.
7138 */
7139 public boolean isWinVisibleLw() {
7140 final AppWindowToken atoken = mAppToken;
7141 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7142 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7143 && !mExiting && !mDestroying;
7144 }
7145
7146 /**
7147 * The same as isVisible(), but follows the current hidden state of
7148 * the associated app token, not the pending requested hidden state.
7149 */
7150 boolean isVisibleNow() {
7151 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007152 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 }
7154
7155 /**
7156 * Same as isVisible(), but we also count it as visible between the
7157 * call to IWindowSession.add() and the first relayout().
7158 */
7159 boolean isVisibleOrAdding() {
7160 final AppWindowToken atoken = mAppToken;
7161 return (mSurface != null
7162 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7163 && mPolicyVisibility && !mAttachedHidden
7164 && (atoken == null || !atoken.hiddenRequested)
7165 && !mExiting && !mDestroying;
7166 }
7167
7168 /**
7169 * Is this window currently on-screen? It is on-screen either if it
7170 * is visible or it is currently running an animation before no longer
7171 * being visible.
7172 */
7173 boolean isOnScreen() {
7174 final AppWindowToken atoken = mAppToken;
7175 if (atoken != null) {
7176 return mSurface != null && mPolicyVisibility && !mDestroying
7177 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007178 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 } else {
7180 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007181 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183 }
Romain Guy06882f82009-06-10 13:36:04 -07007184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 /**
7186 * Like isOnScreen(), but we don't return true if the window is part
7187 * of a transition that has not yet been started.
7188 */
7189 boolean isReadyForDisplay() {
7190 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007191 final boolean animating = atoken != null
7192 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007194 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7195 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007196 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 }
7198
7199 /** Is the window or its container currently animating? */
7200 boolean isAnimating() {
7201 final WindowState attached = mAttachedWindow;
7202 final AppWindowToken atoken = mAppToken;
7203 return mAnimation != null
7204 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007205 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 (atoken.animation != null
7207 || atoken.inPendingTransaction));
7208 }
7209
7210 /** Is this window currently animating? */
7211 boolean isWindowAnimating() {
7212 return mAnimation != null;
7213 }
7214
7215 /**
7216 * Like isOnScreen, but returns false if the surface hasn't yet
7217 * been drawn.
7218 */
7219 public boolean isDisplayedLw() {
7220 final AppWindowToken atoken = mAppToken;
7221 return mSurface != null && mPolicyVisibility && !mDestroying
7222 && !mDrawPending && !mCommitDrawPending
7223 && ((!mAttachedHidden &&
7224 (atoken == null || !atoken.hiddenRequested))
7225 || mAnimating);
7226 }
7227
7228 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7229 boolean shownFrame, boolean onlyOpaque) {
7230 if (mSurface == null) {
7231 return false;
7232 }
7233 if (mAppToken != null && !mAppToken.appFullscreen) {
7234 return false;
7235 }
7236 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7237 return false;
7238 }
7239 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007240
7241 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7242 return frame.left <= mCompatibleScreenFrame.left &&
7243 frame.top <= mCompatibleScreenFrame.top &&
7244 frame.right >= mCompatibleScreenFrame.right &&
7245 frame.bottom >= mCompatibleScreenFrame.bottom;
7246 } else {
7247 return frame.left <= 0 && frame.top <= 0
7248 && frame.right >= screenWidth
7249 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 }
Romain Guy06882f82009-06-10 13:36:04 -07007252
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007253 /**
7254 * Return true if the window is opaque and fully drawn.
7255 */
7256 boolean isOpaqueDrawn() {
7257 return mAttrs.format == PixelFormat.OPAQUE && mSurface != null
7258 && mAnimation == null && !mDrawPending && !mCommitDrawPending;
7259 }
7260
7261 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7262 return
7263 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007264 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7265 // only if it's visible
7266 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007267 // and only if the application fills the compatible screen
7268 mFrame.left <= mCompatibleScreenFrame.left &&
7269 mFrame.top <= mCompatibleScreenFrame.top &&
7270 mFrame.right >= mCompatibleScreenFrame.right &&
7271 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007272 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007273 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007274 }
7275
7276 boolean isFullscreen(int screenWidth, int screenHeight) {
7277 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007278 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 }
7280
7281 void removeLocked() {
7282 if (mAttachedWindow != null) {
7283 mAttachedWindow.mChildWindows.remove(this);
7284 }
7285 destroySurfaceLocked();
7286 mSession.windowRemovedLocked();
7287 try {
7288 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7289 } catch (RuntimeException e) {
7290 // Ignore if it has already been removed (usually because
7291 // we are doing this as part of processing a death note.)
7292 }
7293 }
7294
7295 private class DeathRecipient implements IBinder.DeathRecipient {
7296 public void binderDied() {
7297 try {
7298 synchronized(mWindowMap) {
7299 WindowState win = windowForClientLocked(mSession, mClient);
7300 Log.i(TAG, "WIN DEATH: " + win);
7301 if (win != null) {
7302 removeWindowLocked(mSession, win);
7303 }
7304 }
7305 } catch (IllegalArgumentException ex) {
7306 // This will happen if the window has already been
7307 // removed.
7308 }
7309 }
7310 }
7311
7312 /** Returns true if this window desires key events. */
7313 public final boolean canReceiveKeys() {
7314 return isVisibleOrAdding()
7315 && (mViewVisibility == View.VISIBLE)
7316 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7317 }
7318
7319 public boolean hasDrawnLw() {
7320 return mHasDrawn;
7321 }
7322
7323 public boolean showLw(boolean doAnimation) {
7324 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7325 mPolicyVisibility = true;
7326 mPolicyVisibilityAfterAnim = true;
7327 if (doAnimation) {
7328 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7329 }
7330 requestAnimationLocked(0);
7331 return true;
7332 }
7333 return false;
7334 }
7335
7336 public boolean hideLw(boolean doAnimation) {
7337 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7338 : mPolicyVisibility;
7339 if (current) {
7340 if (doAnimation) {
7341 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7342 if (mAnimation == null) {
7343 doAnimation = false;
7344 }
7345 }
7346 if (doAnimation) {
7347 mPolicyVisibilityAfterAnim = false;
7348 } else {
7349 mPolicyVisibilityAfterAnim = false;
7350 mPolicyVisibility = false;
7351 }
7352 requestAnimationLocked(0);
7353 return true;
7354 }
7355 return false;
7356 }
7357
7358 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007359 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007360
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007361 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7362 pw.print(" mClient="); pw.println(mClient.asBinder());
7363 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7364 if (mAttachedWindow != null || mLayoutAttached) {
7365 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7366 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7367 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007368 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7369 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7370 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007371 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7372 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007373 }
7374 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7375 pw.print(" mSubLayer="); pw.print(mSubLayer);
7376 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7377 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7378 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7379 pw.print("="); pw.print(mAnimLayer);
7380 pw.print(" mLastLayer="); pw.println(mLastLayer);
7381 if (mSurface != null) {
7382 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7383 }
7384 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7385 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7386 if (mAppToken != null) {
7387 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7388 }
7389 if (mTargetAppToken != null) {
7390 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7391 }
7392 pw.print(prefix); pw.print("mViewVisibility=0x");
7393 pw.print(Integer.toHexString(mViewVisibility));
7394 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007395 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7396 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007397 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7398 pw.print(prefix); pw.print("mPolicyVisibility=");
7399 pw.print(mPolicyVisibility);
7400 pw.print(" mPolicyVisibilityAfterAnim=");
7401 pw.print(mPolicyVisibilityAfterAnim);
7402 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7403 }
7404 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007405 pw.print(" h="); pw.println(mRequestedHeight);
7406 if (mXOffset != 0 || mYOffset != 0) {
7407 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7408 pw.print(" y="); pw.println(mYOffset);
7409 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007410 pw.print(prefix); pw.print("mGivenContentInsets=");
7411 mGivenContentInsets.printShortString(pw);
7412 pw.print(" mGivenVisibleInsets=");
7413 mGivenVisibleInsets.printShortString(pw);
7414 pw.println();
7415 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7416 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7417 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7418 }
7419 pw.print(prefix); pw.print("mShownFrame=");
7420 mShownFrame.printShortString(pw);
7421 pw.print(" last="); mLastShownFrame.printShortString(pw);
7422 pw.println();
7423 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7424 pw.print(" last="); mLastFrame.printShortString(pw);
7425 pw.println();
7426 pw.print(prefix); pw.print("mContainingFrame=");
7427 mContainingFrame.printShortString(pw);
7428 pw.print(" mDisplayFrame=");
7429 mDisplayFrame.printShortString(pw);
7430 pw.println();
7431 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7432 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7433 pw.println();
7434 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7435 pw.print(" last="); mLastContentInsets.printShortString(pw);
7436 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7437 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7438 pw.println();
7439 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7440 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7441 pw.print(" mAlpha="); pw.print(mAlpha);
7442 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7443 }
7444 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7445 || mAnimation != null) {
7446 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7447 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7448 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7449 pw.print(" mAnimation="); pw.println(mAnimation);
7450 }
7451 if (mHasTransformation || mHasLocalTransformation) {
7452 pw.print(prefix); pw.print("XForm: has=");
7453 pw.print(mHasTransformation);
7454 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7455 pw.print(" "); mTransformation.printShortString(pw);
7456 pw.println();
7457 }
7458 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7459 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7460 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7461 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7462 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7463 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7464 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7465 pw.print(" mDestroying="); pw.print(mDestroying);
7466 pw.print(" mRemoved="); pw.println(mRemoved);
7467 }
7468 if (mOrientationChanging || mAppFreezing) {
7469 pw.print(prefix); pw.print("mOrientationChanging=");
7470 pw.print(mOrientationChanging);
7471 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7472 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007473 if (mHScale != 1 || mVScale != 1) {
7474 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7475 pw.print(" mVScale="); pw.println(mVScale);
7476 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007477 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007478 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7479 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 }
7482
7483 @Override
7484 public String toString() {
7485 return "Window{"
7486 + Integer.toHexString(System.identityHashCode(this))
7487 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7488 }
7489 }
Romain Guy06882f82009-06-10 13:36:04 -07007490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007491 // -------------------------------------------------------------
7492 // Window Token State
7493 // -------------------------------------------------------------
7494
7495 class WindowToken {
7496 // The actual token.
7497 final IBinder token;
7498
7499 // The type of window this token is for, as per WindowManager.LayoutParams.
7500 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 // Set if this token was explicitly added by a client, so should
7503 // not be removed when all windows are removed.
7504 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007505
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007506 // For printing.
7507 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 // If this is an AppWindowToken, this is non-null.
7510 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 // All of the windows associated with this token.
7513 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7514
7515 // Is key dispatching paused for this token?
7516 boolean paused = false;
7517
7518 // Should this token's windows be hidden?
7519 boolean hidden;
7520
7521 // Temporary for finding which tokens no longer have visible windows.
7522 boolean hasVisible;
7523
7524 WindowToken(IBinder _token, int type, boolean _explicit) {
7525 token = _token;
7526 windowType = type;
7527 explicit = _explicit;
7528 }
7529
7530 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007531 pw.print(prefix); pw.print("token="); pw.println(token);
7532 pw.print(prefix); pw.print("windows="); pw.println(windows);
7533 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7534 pw.print(" hidden="); pw.print(hidden);
7535 pw.print(" hasVisible="); pw.println(hasVisible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 }
7537
7538 @Override
7539 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007540 if (stringName == null) {
7541 StringBuilder sb = new StringBuilder();
7542 sb.append("WindowToken{");
7543 sb.append(Integer.toHexString(System.identityHashCode(this)));
7544 sb.append(" token="); sb.append(token); sb.append('}');
7545 stringName = sb.toString();
7546 }
7547 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 }
7549 };
7550
7551 class AppWindowToken extends WindowToken {
7552 // Non-null only for application tokens.
7553 final IApplicationToken appToken;
7554
7555 // All of the windows and child windows that are included in this
7556 // application token. Note this list is NOT sorted!
7557 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7558
7559 int groupId = -1;
7560 boolean appFullscreen;
7561 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 // These are used for determining when all windows associated with
7564 // an activity have been drawn, so they can be made visible together
7565 // at the same time.
7566 int lastTransactionSequence = mTransactionSequence-1;
7567 int numInterestingWindows;
7568 int numDrawnWindows;
7569 boolean inPendingTransaction;
7570 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 // Is this token going to be hidden in a little while? If so, it
7573 // won't be taken into account for setting the screen orientation.
7574 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007576 // Is this window's surface needed? This is almost like hidden, except
7577 // it will sometimes be true a little earlier: when the token has
7578 // been shown, but is still waiting for its app transition to execute
7579 // before making its windows shown.
7580 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 // Have we told the window clients to hide themselves?
7583 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 // Last visibility state we reported to the app token.
7586 boolean reportedVisible;
7587
7588 // Set to true when the token has been removed from the window mgr.
7589 boolean removed;
7590
7591 // Have we been asked to have this token keep the screen frozen?
7592 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 boolean animating;
7595 Animation animation;
7596 boolean hasTransformation;
7597 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 // Offset to the window of all layers in the token, for use by
7600 // AppWindowToken animations.
7601 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007603 // Information about an application starting window if displayed.
7604 StartingData startingData;
7605 WindowState startingWindow;
7606 View startingView;
7607 boolean startingDisplayed;
7608 boolean startingMoved;
7609 boolean firstWindowDrawn;
7610
7611 AppWindowToken(IApplicationToken _token) {
7612 super(_token.asBinder(),
7613 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7614 appWindowToken = this;
7615 appToken = _token;
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 public void setAnimation(Animation anim) {
7619 if (localLOGV) Log.v(
7620 TAG, "Setting animation in " + this + ": " + anim);
7621 animation = anim;
7622 animating = false;
7623 anim.restrictDuration(MAX_ANIMATION_DURATION);
7624 anim.scaleCurrentDuration(mTransitionAnimationScale);
7625 int zorder = anim.getZAdjustment();
7626 int adj = 0;
7627 if (zorder == Animation.ZORDER_TOP) {
7628 adj = TYPE_LAYER_OFFSET;
7629 } else if (zorder == Animation.ZORDER_BOTTOM) {
7630 adj = -TYPE_LAYER_OFFSET;
7631 }
Romain Guy06882f82009-06-10 13:36:04 -07007632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 if (animLayerAdjustment != adj) {
7634 animLayerAdjustment = adj;
7635 updateLayers();
7636 }
7637 }
Romain Guy06882f82009-06-10 13:36:04 -07007638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 public void setDummyAnimation() {
7640 if (animation == null) {
7641 if (localLOGV) Log.v(
7642 TAG, "Setting dummy animation in " + this);
7643 animation = sDummyAnimation;
7644 }
7645 }
7646
7647 public void clearAnimation() {
7648 if (animation != null) {
7649 animation = null;
7650 animating = true;
7651 }
7652 }
Romain Guy06882f82009-06-10 13:36:04 -07007653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 void updateLayers() {
7655 final int N = allAppWindows.size();
7656 final int adj = animLayerAdjustment;
7657 for (int i=0; i<N; i++) {
7658 WindowState w = allAppWindows.get(i);
7659 w.mAnimLayer = w.mLayer + adj;
7660 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7661 + w.mAnimLayer);
7662 if (w == mInputMethodTarget) {
7663 setInputMethodAnimLayerAdjustment(adj);
7664 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007665 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007666 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 }
7669 }
Romain Guy06882f82009-06-10 13:36:04 -07007670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 void sendAppVisibilityToClients() {
7672 final int N = allAppWindows.size();
7673 for (int i=0; i<N; i++) {
7674 WindowState win = allAppWindows.get(i);
7675 if (win == startingWindow && clientHidden) {
7676 // Don't hide the starting window.
7677 continue;
7678 }
7679 try {
7680 if (DEBUG_VISIBILITY) Log.v(TAG,
7681 "Setting visibility of " + win + ": " + (!clientHidden));
7682 win.mClient.dispatchAppVisibility(!clientHidden);
7683 } catch (RemoteException e) {
7684 }
7685 }
7686 }
Romain Guy06882f82009-06-10 13:36:04 -07007687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 void showAllWindowsLocked() {
7689 final int NW = allAppWindows.size();
7690 for (int i=0; i<NW; i++) {
7691 WindowState w = allAppWindows.get(i);
7692 if (DEBUG_VISIBILITY) Log.v(TAG,
7693 "performing show on: " + w);
7694 w.performShowLocked();
7695 }
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 // This must be called while inside a transaction.
7699 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7700 if (!mDisplayFrozen) {
7701 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 if (animation == sDummyAnimation) {
7704 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007705 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 // when it is really time to animate, this will be set to
7707 // a real animation and the next call will execute normally.
7708 return false;
7709 }
Romain Guy06882f82009-06-10 13:36:04 -07007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7712 if (!animating) {
7713 if (DEBUG_ANIM) Log.v(
7714 TAG, "Starting animation in " + this +
7715 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7716 + " scale=" + mTransitionAnimationScale
7717 + " allDrawn=" + allDrawn + " animating=" + animating);
7718 animation.initialize(dw, dh, dw, dh);
7719 animation.setStartTime(currentTime);
7720 animating = true;
7721 }
7722 transformation.clear();
7723 final boolean more = animation.getTransformation(
7724 currentTime, transformation);
7725 if (DEBUG_ANIM) Log.v(
7726 TAG, "Stepped animation in " + this +
7727 ": more=" + more + ", xform=" + transformation);
7728 if (more) {
7729 // we're done!
7730 hasTransformation = true;
7731 return true;
7732 }
7733 if (DEBUG_ANIM) Log.v(
7734 TAG, "Finished animation in " + this +
7735 " @ " + currentTime);
7736 animation = null;
7737 }
7738 } else if (animation != null) {
7739 // If the display is frozen, and there is a pending animation,
7740 // clear it and make sure we run the cleanup code.
7741 animating = true;
7742 animation = null;
7743 }
7744
7745 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747 if (!animating) {
7748 return false;
7749 }
7750
7751 clearAnimation();
7752 animating = false;
7753 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7754 moveInputMethodWindowsIfNeededLocked(true);
7755 }
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 if (DEBUG_ANIM) Log.v(
7758 TAG, "Animation done in " + this
7759 + ": reportedVisible=" + reportedVisible);
7760
7761 transformation.clear();
7762 if (animLayerAdjustment != 0) {
7763 animLayerAdjustment = 0;
7764 updateLayers();
7765 }
Romain Guy06882f82009-06-10 13:36:04 -07007766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 final int N = windows.size();
7768 for (int i=0; i<N; i++) {
7769 ((WindowState)windows.get(i)).finishExit();
7770 }
7771 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 return false;
7774 }
7775
7776 void updateReportedVisibilityLocked() {
7777 if (appToken == null) {
7778 return;
7779 }
Romain Guy06882f82009-06-10 13:36:04 -07007780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 int numInteresting = 0;
7782 int numVisible = 0;
7783 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7786 final int N = allAppWindows.size();
7787 for (int i=0; i<N; i++) {
7788 WindowState win = allAppWindows.get(i);
7789 if (win == startingWindow || win.mAppFreezing) {
7790 continue;
7791 }
7792 if (DEBUG_VISIBILITY) {
7793 Log.v(TAG, "Win " + win + ": isDisplayed="
7794 + win.isDisplayedLw()
7795 + ", isAnimating=" + win.isAnimating());
7796 if (!win.isDisplayedLw()) {
7797 Log.v(TAG, "Not displayed: s=" + win.mSurface
7798 + " pv=" + win.mPolicyVisibility
7799 + " dp=" + win.mDrawPending
7800 + " cdp=" + win.mCommitDrawPending
7801 + " ah=" + win.mAttachedHidden
7802 + " th="
7803 + (win.mAppToken != null
7804 ? win.mAppToken.hiddenRequested : false)
7805 + " a=" + win.mAnimating);
7806 }
7807 }
7808 numInteresting++;
7809 if (win.isDisplayedLw()) {
7810 if (!win.isAnimating()) {
7811 numVisible++;
7812 }
7813 nowGone = false;
7814 } else if (win.isAnimating()) {
7815 nowGone = false;
7816 }
7817 }
Romain Guy06882f82009-06-10 13:36:04 -07007818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
7820 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
7821 + numInteresting + " visible=" + numVisible);
7822 if (nowVisible != reportedVisible) {
7823 if (DEBUG_VISIBILITY) Log.v(
7824 TAG, "Visibility changed in " + this
7825 + ": vis=" + nowVisible);
7826 reportedVisible = nowVisible;
7827 Message m = mH.obtainMessage(
7828 H.REPORT_APPLICATION_TOKEN_WINDOWS,
7829 nowVisible ? 1 : 0,
7830 nowGone ? 1 : 0,
7831 this);
7832 mH.sendMessage(m);
7833 }
7834 }
Romain Guy06882f82009-06-10 13:36:04 -07007835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 void dump(PrintWriter pw, String prefix) {
7837 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007838 if (appToken != null) {
7839 pw.print(prefix); pw.println("app=true");
7840 }
7841 if (allAppWindows.size() > 0) {
7842 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
7843 }
7844 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007845 pw.print(" appFullscreen="); pw.println(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007846 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
7847 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
7848 pw.print(" clientHidden="); pw.print(clientHidden);
7849 pw.print(" willBeHidden="); pw.print(willBeHidden);
7850 pw.print(" reportedVisible="); pw.println(reportedVisible);
7851 if (paused || freezingScreen) {
7852 pw.print(prefix); pw.print("paused="); pw.print(paused);
7853 pw.print(" freezingScreen="); pw.println(freezingScreen);
7854 }
7855 if (numInterestingWindows != 0 || numDrawnWindows != 0
7856 || inPendingTransaction || allDrawn) {
7857 pw.print(prefix); pw.print("numInterestingWindows=");
7858 pw.print(numInterestingWindows);
7859 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
7860 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
7861 pw.print(" allDrawn="); pw.println(allDrawn);
7862 }
7863 if (animating || animation != null) {
7864 pw.print(prefix); pw.print("animating="); pw.print(animating);
7865 pw.print(" animation="); pw.println(animation);
7866 }
7867 if (animLayerAdjustment != 0) {
7868 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
7869 }
7870 if (hasTransformation) {
7871 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
7872 pw.print(" transformation="); transformation.printShortString(pw);
7873 pw.println();
7874 }
7875 if (startingData != null || removed || firstWindowDrawn) {
7876 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
7877 pw.print(" removed="); pw.print(removed);
7878 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
7879 }
7880 if (startingWindow != null || startingView != null
7881 || startingDisplayed || startingMoved) {
7882 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
7883 pw.print(" startingView="); pw.print(startingView);
7884 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
7885 pw.print(" startingMoved"); pw.println(startingMoved);
7886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 }
7888
7889 @Override
7890 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007891 if (stringName == null) {
7892 StringBuilder sb = new StringBuilder();
7893 sb.append("AppWindowToken{");
7894 sb.append(Integer.toHexString(System.identityHashCode(this)));
7895 sb.append(" token="); sb.append(token); sb.append('}');
7896 stringName = sb.toString();
7897 }
7898 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 }
7900 }
Romain Guy06882f82009-06-10 13:36:04 -07007901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 public static WindowManager.LayoutParams findAnimations(
7903 ArrayList<AppWindowToken> order,
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007904 ArrayList<AppWindowToken> openingTokenList1,
7905 ArrayList<AppWindowToken> closingTokenList2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 // We need to figure out which animation to use...
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007907
7908 // First, check if there is a compatible window in opening/closing
7909 // apps, and use it if exists.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 WindowManager.LayoutParams animParams = null;
7911 int animSrc = 0;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007912 animParams = findCompatibleWindowParams(openingTokenList1);
7913 if (animParams == null) {
7914 animParams = findCompatibleWindowParams(closingTokenList2);
7915 }
7916 if (animParams != null) {
7917 return animParams;
7918 }
7919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 //Log.i(TAG, "Looking for animations...");
7921 for (int i=order.size()-1; i>=0; i--) {
7922 AppWindowToken wtoken = order.get(i);
7923 //Log.i(TAG, "Token " + wtoken + " with " + wtoken.windows.size() + " windows");
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007924 if (openingTokenList1.contains(wtoken) || closingTokenList2.contains(wtoken)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 int j = wtoken.windows.size();
7926 while (j > 0) {
7927 j--;
7928 WindowState win = wtoken.windows.get(j);
7929 //Log.i(TAG, "Window " + win + ": type=" + win.mAttrs.type);
7930 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
7931 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
7932 //Log.i(TAG, "Found base or application window, done!");
7933 if (wtoken.appFullscreen) {
7934 return win.mAttrs;
7935 }
7936 if (animSrc < 2) {
7937 animParams = win.mAttrs;
7938 animSrc = 2;
7939 }
7940 } else if (animSrc < 1 && win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION) {
7941 //Log.i(TAG, "Found normal window, we may use this...");
7942 animParams = win.mAttrs;
7943 animSrc = 1;
7944 }
7945 }
7946 }
7947 }
Romain Guy06882f82009-06-10 13:36:04 -07007948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949 return animParams;
7950 }
Romain Guy06882f82009-06-10 13:36:04 -07007951
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07007952 private static LayoutParams findCompatibleWindowParams(ArrayList<AppWindowToken> tokenList) {
7953 for (int appCount = tokenList.size() - 1; appCount >= 0; appCount--) {
7954 AppWindowToken wtoken = tokenList.get(appCount);
7955 // Just checking one window is sufficient as all windows have the compatible flag
7956 // if the application is in compatibility mode.
7957 if (wtoken.windows.size() > 0) {
7958 WindowManager.LayoutParams params = wtoken.windows.get(0).mAttrs;
7959 if ((params.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7960 return params;
7961 }
7962 }
7963 }
7964 return null;
7965 }
7966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 // -------------------------------------------------------------
7968 // DummyAnimation
7969 // -------------------------------------------------------------
7970
7971 // This is an animation that does nothing: it just immediately finishes
7972 // itself every time it is called. It is used as a stub animation in cases
7973 // where we want to synchronize multiple things that may be animating.
7974 static final class DummyAnimation extends Animation {
7975 public boolean getTransformation(long currentTime, Transformation outTransformation) {
7976 return false;
7977 }
7978 }
7979 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07007980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 // -------------------------------------------------------------
7982 // Async Handler
7983 // -------------------------------------------------------------
7984
7985 static final class StartingData {
7986 final String pkg;
7987 final int theme;
7988 final CharSequence nonLocalizedLabel;
7989 final int labelRes;
7990 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07007991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
7993 int _labelRes, int _icon) {
7994 pkg = _pkg;
7995 theme = _theme;
7996 nonLocalizedLabel = _nonLocalizedLabel;
7997 labelRes = _labelRes;
7998 icon = _icon;
7999 }
8000 }
8001
8002 private final class H extends Handler {
8003 public static final int REPORT_FOCUS_CHANGE = 2;
8004 public static final int REPORT_LOSING_FOCUS = 3;
8005 public static final int ANIMATE = 4;
8006 public static final int ADD_STARTING = 5;
8007 public static final int REMOVE_STARTING = 6;
8008 public static final int FINISHED_STARTING = 7;
8009 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8011 public static final int HOLD_SCREEN_CHANGED = 12;
8012 public static final int APP_TRANSITION_TIMEOUT = 13;
8013 public static final int PERSIST_ANIMATION_SCALE = 14;
8014 public static final int FORCE_GC = 15;
8015 public static final int ENABLE_SCREEN = 16;
8016 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008017 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 public H() {
8022 }
Romain Guy06882f82009-06-10 13:36:04 -07008023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 @Override
8025 public void handleMessage(Message msg) {
8026 switch (msg.what) {
8027 case REPORT_FOCUS_CHANGE: {
8028 WindowState lastFocus;
8029 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 synchronized(mWindowMap) {
8032 lastFocus = mLastFocus;
8033 newFocus = mCurrentFocus;
8034 if (lastFocus == newFocus) {
8035 // Focus is not changing, so nothing to do.
8036 return;
8037 }
8038 mLastFocus = newFocus;
8039 //Log.i(TAG, "Focus moving from " + lastFocus
8040 // + " to " + newFocus);
8041 if (newFocus != null && lastFocus != null
8042 && !newFocus.isDisplayedLw()) {
8043 //Log.i(TAG, "Delaying loss of focus...");
8044 mLosingFocus.add(lastFocus);
8045 lastFocus = null;
8046 }
8047 }
8048
8049 if (lastFocus != newFocus) {
8050 //System.out.println("Changing focus from " + lastFocus
8051 // + " to " + newFocus);
8052 if (newFocus != null) {
8053 try {
8054 //Log.i(TAG, "Gaining focus: " + newFocus);
8055 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8056 } catch (RemoteException e) {
8057 // Ignore if process has died.
8058 }
8059 }
8060
8061 if (lastFocus != null) {
8062 try {
8063 //Log.i(TAG, "Losing focus: " + lastFocus);
8064 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8065 } catch (RemoteException e) {
8066 // Ignore if process has died.
8067 }
8068 }
8069 }
8070 } break;
8071
8072 case REPORT_LOSING_FOCUS: {
8073 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 synchronized(mWindowMap) {
8076 losers = mLosingFocus;
8077 mLosingFocus = new ArrayList<WindowState>();
8078 }
8079
8080 final int N = losers.size();
8081 for (int i=0; i<N; i++) {
8082 try {
8083 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8084 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8085 } catch (RemoteException e) {
8086 // Ignore if process has died.
8087 }
8088 }
8089 } break;
8090
8091 case ANIMATE: {
8092 synchronized(mWindowMap) {
8093 mAnimationPending = false;
8094 performLayoutAndPlaceSurfacesLocked();
8095 }
8096 } break;
8097
8098 case ADD_STARTING: {
8099 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8100 final StartingData sd = wtoken.startingData;
8101
8102 if (sd == null) {
8103 // Animation has been canceled... do nothing.
8104 return;
8105 }
Romain Guy06882f82009-06-10 13:36:04 -07008106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8108 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 View view = null;
8111 try {
8112 view = mPolicy.addStartingWindow(
8113 wtoken.token, sd.pkg,
8114 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8115 sd.icon);
8116 } catch (Exception e) {
8117 Log.w(TAG, "Exception when adding starting window", e);
8118 }
8119
8120 if (view != null) {
8121 boolean abort = false;
8122
8123 synchronized(mWindowMap) {
8124 if (wtoken.removed || wtoken.startingData == null) {
8125 // If the window was successfully added, then
8126 // we need to remove it.
8127 if (wtoken.startingWindow != null) {
8128 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8129 "Aborted starting " + wtoken
8130 + ": removed=" + wtoken.removed
8131 + " startingData=" + wtoken.startingData);
8132 wtoken.startingWindow = null;
8133 wtoken.startingData = null;
8134 abort = true;
8135 }
8136 } else {
8137 wtoken.startingView = view;
8138 }
8139 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8140 "Added starting " + wtoken
8141 + ": startingWindow="
8142 + wtoken.startingWindow + " startingView="
8143 + wtoken.startingView);
8144 }
8145
8146 if (abort) {
8147 try {
8148 mPolicy.removeStartingWindow(wtoken.token, view);
8149 } catch (Exception e) {
8150 Log.w(TAG, "Exception when removing starting window", e);
8151 }
8152 }
8153 }
8154 } break;
8155
8156 case REMOVE_STARTING: {
8157 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8158 IBinder token = null;
8159 View view = null;
8160 synchronized (mWindowMap) {
8161 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8162 + wtoken + ": startingWindow="
8163 + wtoken.startingWindow + " startingView="
8164 + wtoken.startingView);
8165 if (wtoken.startingWindow != null) {
8166 view = wtoken.startingView;
8167 token = wtoken.token;
8168 wtoken.startingData = null;
8169 wtoken.startingView = null;
8170 wtoken.startingWindow = null;
8171 }
8172 }
8173 if (view != null) {
8174 try {
8175 mPolicy.removeStartingWindow(token, view);
8176 } catch (Exception e) {
8177 Log.w(TAG, "Exception when removing starting window", e);
8178 }
8179 }
8180 } break;
8181
8182 case FINISHED_STARTING: {
8183 IBinder token = null;
8184 View view = null;
8185 while (true) {
8186 synchronized (mWindowMap) {
8187 final int N = mFinishedStarting.size();
8188 if (N <= 0) {
8189 break;
8190 }
8191 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8192
8193 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8194 "Finished starting " + wtoken
8195 + ": startingWindow=" + wtoken.startingWindow
8196 + " startingView=" + wtoken.startingView);
8197
8198 if (wtoken.startingWindow == null) {
8199 continue;
8200 }
8201
8202 view = wtoken.startingView;
8203 token = wtoken.token;
8204 wtoken.startingData = null;
8205 wtoken.startingView = null;
8206 wtoken.startingWindow = null;
8207 }
8208
8209 try {
8210 mPolicy.removeStartingWindow(token, view);
8211 } catch (Exception e) {
8212 Log.w(TAG, "Exception when removing starting window", e);
8213 }
8214 }
8215 } break;
8216
8217 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8218 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8219
8220 boolean nowVisible = msg.arg1 != 0;
8221 boolean nowGone = msg.arg2 != 0;
8222
8223 try {
8224 if (DEBUG_VISIBILITY) Log.v(
8225 TAG, "Reporting visible in " + wtoken
8226 + " visible=" + nowVisible
8227 + " gone=" + nowGone);
8228 if (nowVisible) {
8229 wtoken.appToken.windowsVisible();
8230 } else {
8231 wtoken.appToken.windowsGone();
8232 }
8233 } catch (RemoteException ex) {
8234 }
8235 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 case WINDOW_FREEZE_TIMEOUT: {
8238 synchronized (mWindowMap) {
8239 Log.w(TAG, "Window freeze timeout expired.");
8240 int i = mWindows.size();
8241 while (i > 0) {
8242 i--;
8243 WindowState w = (WindowState)mWindows.get(i);
8244 if (w.mOrientationChanging) {
8245 w.mOrientationChanging = false;
8246 Log.w(TAG, "Force clearing orientation change: " + w);
8247 }
8248 }
8249 performLayoutAndPlaceSurfacesLocked();
8250 }
8251 break;
8252 }
Romain Guy06882f82009-06-10 13:36:04 -07008253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 case HOLD_SCREEN_CHANGED: {
8255 Session oldHold;
8256 Session newHold;
8257 synchronized (mWindowMap) {
8258 oldHold = mLastReportedHold;
8259 newHold = (Session)msg.obj;
8260 mLastReportedHold = newHold;
8261 }
Romain Guy06882f82009-06-10 13:36:04 -07008262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 if (oldHold != newHold) {
8264 try {
8265 if (oldHold != null) {
8266 mBatteryStats.noteStopWakelock(oldHold.mUid,
8267 "window",
8268 BatteryStats.WAKE_TYPE_WINDOW);
8269 }
8270 if (newHold != null) {
8271 mBatteryStats.noteStartWakelock(newHold.mUid,
8272 "window",
8273 BatteryStats.WAKE_TYPE_WINDOW);
8274 }
8275 } catch (RemoteException e) {
8276 }
8277 }
8278 break;
8279 }
Romain Guy06882f82009-06-10 13:36:04 -07008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 case APP_TRANSITION_TIMEOUT: {
8282 synchronized (mWindowMap) {
8283 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8284 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8285 "*** APP TRANSITION TIMEOUT");
8286 mAppTransitionReady = true;
8287 mAppTransitionTimeout = true;
8288 performLayoutAndPlaceSurfacesLocked();
8289 }
8290 }
8291 break;
8292 }
Romain Guy06882f82009-06-10 13:36:04 -07008293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 case PERSIST_ANIMATION_SCALE: {
8295 Settings.System.putFloat(mContext.getContentResolver(),
8296 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8297 Settings.System.putFloat(mContext.getContentResolver(),
8298 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8299 break;
8300 }
Romain Guy06882f82009-06-10 13:36:04 -07008301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 case FORCE_GC: {
8303 synchronized(mWindowMap) {
8304 if (mAnimationPending) {
8305 // If we are animating, don't do the gc now but
8306 // delay a bit so we don't interrupt the animation.
8307 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8308 2000);
8309 return;
8310 }
8311 // If we are currently rotating the display, it will
8312 // schedule a new message when done.
8313 if (mDisplayFrozen) {
8314 return;
8315 }
8316 mFreezeGcPending = 0;
8317 }
8318 Runtime.getRuntime().gc();
8319 break;
8320 }
Romain Guy06882f82009-06-10 13:36:04 -07008321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 case ENABLE_SCREEN: {
8323 performEnableScreen();
8324 break;
8325 }
Romain Guy06882f82009-06-10 13:36:04 -07008326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 case APP_FREEZE_TIMEOUT: {
8328 synchronized (mWindowMap) {
8329 Log.w(TAG, "App freeze timeout expired.");
8330 int i = mAppTokens.size();
8331 while (i > 0) {
8332 i--;
8333 AppWindowToken tok = mAppTokens.get(i);
8334 if (tok.freezingScreen) {
8335 Log.w(TAG, "Force clearing freeze: " + tok);
8336 unsetAppFreezingScreenLocked(tok, true, true);
8337 }
8338 }
8339 }
8340 break;
8341 }
Romain Guy06882f82009-06-10 13:36:04 -07008342
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008343 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008344 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008345 sendNewConfiguration();
8346 }
8347 break;
8348 }
Romain Guy06882f82009-06-10 13:36:04 -07008349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 }
8351 }
8352 }
8353
8354 // -------------------------------------------------------------
8355 // IWindowManager API
8356 // -------------------------------------------------------------
8357
8358 public IWindowSession openSession(IInputMethodClient client,
8359 IInputContext inputContext) {
8360 if (client == null) throw new IllegalArgumentException("null client");
8361 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8362 return new Session(client, inputContext);
8363 }
8364
8365 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8366 synchronized (mWindowMap) {
8367 // The focus for the client is the window immediately below
8368 // where we would place the input method window.
8369 int idx = findDesiredInputMethodWindowIndexLocked(false);
8370 WindowState imFocus;
8371 if (idx > 0) {
8372 imFocus = (WindowState)mWindows.get(idx-1);
8373 if (imFocus != null) {
8374 if (imFocus.mSession.mClient != null &&
8375 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8376 return true;
8377 }
8378 }
8379 }
8380 }
8381 return false;
8382 }
Romain Guy06882f82009-06-10 13:36:04 -07008383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 // -------------------------------------------------------------
8385 // Internals
8386 // -------------------------------------------------------------
8387
8388 final WindowState windowForClientLocked(Session session, IWindow client) {
8389 return windowForClientLocked(session, client.asBinder());
8390 }
Romain Guy06882f82009-06-10 13:36:04 -07008391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 final WindowState windowForClientLocked(Session session, IBinder client) {
8393 WindowState win = mWindowMap.get(client);
8394 if (localLOGV) Log.v(
8395 TAG, "Looking up client " + client + ": " + win);
8396 if (win == null) {
8397 RuntimeException ex = new RuntimeException();
8398 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8399 return null;
8400 }
8401 if (session != null && win.mSession != session) {
8402 RuntimeException ex = new RuntimeException();
8403 Log.w(TAG, "Requested window " + client + " is in session " +
8404 win.mSession + ", not " + session, ex);
8405 return null;
8406 }
8407
8408 return win;
8409 }
8410
8411 private final void assignLayersLocked() {
8412 int N = mWindows.size();
8413 int curBaseLayer = 0;
8414 int curLayer = 0;
8415 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 for (i=0; i<N; i++) {
8418 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008419 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8420 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421 curLayer += WINDOW_LAYER_MULTIPLIER;
8422 w.mLayer = curLayer;
8423 } else {
8424 curBaseLayer = curLayer = w.mBaseLayer;
8425 w.mLayer = curLayer;
8426 }
8427 if (w.mTargetAppToken != null) {
8428 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8429 } else if (w.mAppToken != null) {
8430 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8431 } else {
8432 w.mAnimLayer = w.mLayer;
8433 }
8434 if (w.mIsImWindow) {
8435 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008436 } else if (w.mIsWallpaper) {
8437 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 }
8439 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8440 + w.mAnimLayer);
8441 //System.out.println(
8442 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8443 }
8444 }
8445
8446 private boolean mInLayout = false;
8447 private final void performLayoutAndPlaceSurfacesLocked() {
8448 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008449 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 throw new RuntimeException("Recursive call!");
8451 }
8452 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8453 return;
8454 }
8455
8456 boolean recoveringMemory = false;
8457 if (mForceRemoves != null) {
8458 recoveringMemory = true;
8459 // Wait a little it for things to settle down, and off we go.
8460 for (int i=0; i<mForceRemoves.size(); i++) {
8461 WindowState ws = mForceRemoves.get(i);
8462 Log.i(TAG, "Force removing: " + ws);
8463 removeWindowInnerLocked(ws.mSession, ws);
8464 }
8465 mForceRemoves = null;
8466 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8467 Object tmp = new Object();
8468 synchronized (tmp) {
8469 try {
8470 tmp.wait(250);
8471 } catch (InterruptedException e) {
8472 }
8473 }
8474 }
Romain Guy06882f82009-06-10 13:36:04 -07008475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 mInLayout = true;
8477 try {
8478 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 int i = mPendingRemove.size()-1;
8481 if (i >= 0) {
8482 while (i >= 0) {
8483 WindowState w = mPendingRemove.get(i);
8484 removeWindowInnerLocked(w.mSession, w);
8485 i--;
8486 }
8487 mPendingRemove.clear();
8488
8489 mInLayout = false;
8490 assignLayersLocked();
8491 mLayoutNeeded = true;
8492 performLayoutAndPlaceSurfacesLocked();
8493
8494 } else {
8495 mInLayout = false;
8496 if (mLayoutNeeded) {
8497 requestAnimationLocked(0);
8498 }
8499 }
8500 } catch (RuntimeException e) {
8501 mInLayout = false;
8502 Log.e(TAG, "Unhandled exception while layout out windows", e);
8503 }
8504 }
8505
8506 private final void performLayoutLockedInner() {
8507 final int dw = mDisplay.getWidth();
8508 final int dh = mDisplay.getHeight();
8509
8510 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008511 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 int i;
8513
8514 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008515
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008516 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 mPolicy.beginLayoutLw(dw, dh);
8518
8519 // First perform layout of any root windows (not attached
8520 // to another window).
8521 int topAttached = -1;
8522 for (i = N-1; i >= 0; i--) {
8523 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008524
8525 // Don't do layout of a window if it is not visible, or
8526 // soon won't be visible, to avoid wasting time and funky
8527 // changes while a window is animating away.
8528 final AppWindowToken atoken = win.mAppToken;
8529 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008531 || win.mRootToken.hidden
8532 || (atoken != null && atoken.hiddenRequested)
8533 || !win.mPolicyVisibility
8534 || win.mAttachedHidden
8535 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536
8537 // If this view is GONE, then skip it -- keep the current
8538 // frame, and let the caller know so they can ignore it
8539 // if they want. (We do the normal layout for INVISIBLE
8540 // windows, since that means "perform layout as normal,
8541 // just don't display").
8542 if (!gone || !win.mHaveFrame) {
8543 if (!win.mLayoutAttached) {
8544 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8545 } else {
8546 if (topAttached < 0) topAttached = i;
8547 }
8548 }
8549 }
Romain Guy06882f82009-06-10 13:36:04 -07008550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 // Now perform layout of attached windows, which usually
8552 // depend on the position of the window they are attached to.
8553 // XXX does not deal with windows that are attached to windows
8554 // that are themselves attached.
8555 for (i = topAttached; i >= 0; i--) {
8556 WindowState win = (WindowState) mWindows.get(i);
8557
8558 // If this view is GONE, then skip it -- keep the current
8559 // frame, and let the caller know so they can ignore it
8560 // if they want. (We do the normal layout for INVISIBLE
8561 // windows, since that means "perform layout as normal,
8562 // just don't display").
8563 if (win.mLayoutAttached) {
8564 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8565 || !win.mHaveFrame) {
8566 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8567 }
8568 }
8569 }
8570
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008571 if (!mPolicy.finishLayoutLw()) {
8572 mLayoutNeeded = false;
8573 } else if (repeats > 2) {
8574 Log.w(TAG, "Layout repeat aborted after too many iterations");
8575 mLayoutNeeded = false;
8576 } else {
8577 repeats++;
8578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580 }
Romain Guy06882f82009-06-10 13:36:04 -07008581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 private final void performLayoutAndPlaceSurfacesLockedInner(
8583 boolean recoveringMemory) {
8584 final long currentTime = SystemClock.uptimeMillis();
8585 final int dw = mDisplay.getWidth();
8586 final int dh = mDisplay.getHeight();
8587
8588 final int N = mWindows.size();
8589 int i;
8590
8591 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 if (mFxSession == null) {
8595 mFxSession = new SurfaceSession();
8596 }
Romain Guy06882f82009-06-10 13:36:04 -07008597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008598 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8599
8600 // Initialize state of exiting tokens.
8601 for (i=mExitingTokens.size()-1; i>=0; i--) {
8602 mExitingTokens.get(i).hasVisible = false;
8603 }
8604
8605 // Initialize state of exiting applications.
8606 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8607 mExitingAppTokens.get(i).hasVisible = false;
8608 }
8609
8610 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 boolean orientationChangeComplete = true;
8612 Session holdScreen = null;
8613 float screenBrightness = -1;
8614 boolean focusDisplayed = false;
8615 boolean animating = false;
8616
8617 Surface.openTransaction();
8618 try {
8619 boolean restart;
8620
8621 do {
8622 final int transactionSequence = ++mTransactionSequence;
8623
8624 // Update animations of all applications, including those
8625 // associated with exiting/removed apps
8626 boolean tokensAnimating = false;
8627 final int NAT = mAppTokens.size();
8628 for (i=0; i<NAT; i++) {
8629 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8630 tokensAnimating = true;
8631 }
8632 }
8633 final int NEAT = mExitingAppTokens.size();
8634 for (i=0; i<NEAT; i++) {
8635 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8636 tokensAnimating = true;
8637 }
8638 }
8639
8640 animating = tokensAnimating;
8641 restart = false;
8642
8643 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008644 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645
8646 mPolicy.beginAnimationLw(dw, dh);
8647
8648 for (i=N-1; i>=0; i--) {
8649 WindowState w = (WindowState)mWindows.get(i);
8650
8651 final WindowManager.LayoutParams attrs = w.mAttrs;
8652
8653 if (w.mSurface != null) {
8654 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008655 if (w.commitFinishDrawingLocked(currentTime)) {
8656 if ((w.mAttrs.flags
8657 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
8658 wallpaperMayChange = true;
8659 }
8660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8662 animating = true;
8663 //w.dump(" ");
8664 }
8665
8666 mPolicy.animatingWindowLw(w, attrs);
8667 }
8668
8669 final AppWindowToken atoken = w.mAppToken;
8670 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8671 if (atoken.lastTransactionSequence != transactionSequence) {
8672 atoken.lastTransactionSequence = transactionSequence;
8673 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8674 atoken.startingDisplayed = false;
8675 }
8676 if ((w.isOnScreen() || w.mAttrs.type
8677 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8678 && !w.mExiting && !w.mDestroying) {
8679 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8680 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8681 + w.isDisplayedLw()
8682 + ", isAnimating=" + w.isAnimating());
8683 if (!w.isDisplayedLw()) {
8684 Log.v(TAG, "Not displayed: s=" + w.mSurface
8685 + " pv=" + w.mPolicyVisibility
8686 + " dp=" + w.mDrawPending
8687 + " cdp=" + w.mCommitDrawPending
8688 + " ah=" + w.mAttachedHidden
8689 + " th=" + atoken.hiddenRequested
8690 + " a=" + w.mAnimating);
8691 }
8692 }
8693 if (w != atoken.startingWindow) {
8694 if (!atoken.freezingScreen || !w.mAppFreezing) {
8695 atoken.numInterestingWindows++;
8696 if (w.isDisplayedLw()) {
8697 atoken.numDrawnWindows++;
8698 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8699 "tokenMayBeDrawn: " + atoken
8700 + " freezingScreen=" + atoken.freezingScreen
8701 + " mAppFreezing=" + w.mAppFreezing);
8702 tokenMayBeDrawn = true;
8703 }
8704 }
8705 } else if (w.isDisplayedLw()) {
8706 atoken.startingDisplayed = true;
8707 }
8708 }
8709 } else if (w.mReadyToShow) {
8710 w.performShowLocked();
8711 }
8712 }
8713
8714 if (mPolicy.finishAnimationLw()) {
8715 restart = true;
8716 }
8717
8718 if (tokenMayBeDrawn) {
8719 // See if any windows have been drawn, so they (and others
8720 // associated with them) can now be shown.
8721 final int NT = mTokenList.size();
8722 for (i=0; i<NT; i++) {
8723 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8724 if (wtoken == null) {
8725 continue;
8726 }
8727 if (wtoken.freezingScreen) {
8728 int numInteresting = wtoken.numInterestingWindows;
8729 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8730 if (DEBUG_VISIBILITY) Log.v(TAG,
8731 "allDrawn: " + wtoken
8732 + " interesting=" + numInteresting
8733 + " drawn=" + wtoken.numDrawnWindows);
8734 wtoken.showAllWindowsLocked();
8735 unsetAppFreezingScreenLocked(wtoken, false, true);
8736 orientationChangeComplete = true;
8737 }
8738 } else if (!wtoken.allDrawn) {
8739 int numInteresting = wtoken.numInterestingWindows;
8740 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8741 if (DEBUG_VISIBILITY) Log.v(TAG,
8742 "allDrawn: " + wtoken
8743 + " interesting=" + numInteresting
8744 + " drawn=" + wtoken.numDrawnWindows);
8745 wtoken.allDrawn = true;
8746 restart = true;
8747
8748 // We can now show all of the drawn windows!
8749 if (!mOpeningApps.contains(wtoken)) {
8750 wtoken.showAllWindowsLocked();
8751 }
8752 }
8753 }
8754 }
8755 }
8756
8757 // If we are ready to perform an app transition, check through
8758 // all of the app tokens to be shown and see if they are ready
8759 // to go.
8760 if (mAppTransitionReady) {
8761 int NN = mOpeningApps.size();
8762 boolean goodToGo = true;
8763 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8764 "Checking " + NN + " opening apps (frozen="
8765 + mDisplayFrozen + " timeout="
8766 + mAppTransitionTimeout + ")...");
8767 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8768 // If the display isn't frozen, wait to do anything until
8769 // all of the apps are ready. Otherwise just go because
8770 // we'll unfreeze the display when everyone is ready.
8771 for (i=0; i<NN && goodToGo; i++) {
8772 AppWindowToken wtoken = mOpeningApps.get(i);
8773 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8774 "Check opening app" + wtoken + ": allDrawn="
8775 + wtoken.allDrawn + " startingDisplayed="
8776 + wtoken.startingDisplayed);
8777 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8778 && !wtoken.startingMoved) {
8779 goodToGo = false;
8780 }
8781 }
8782 }
8783 if (goodToGo) {
8784 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8785 int transit = mNextAppTransition;
8786 if (mSkipAppTransitionAnimation) {
8787 transit = WindowManagerPolicy.TRANSIT_NONE;
8788 }
8789 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8790 mAppTransitionReady = false;
8791 mAppTransitionTimeout = false;
8792 mStartingIconInTransition = false;
8793 mSkipAppTransitionAnimation = false;
8794
8795 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8796
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008797 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008798 wallpaperMayChange = false;
8799
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008800 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008801 "New wallpaper target=" + mWallpaperTarget
8802 + ", lower target=" + mLowerWallpaperTarget
8803 + ", upper target=" + mUpperWallpaperTarget);
8804 if (mLowerWallpaperTarget != null) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008805 // Need to determine if both the closing and
8806 // opening app token sets are wallpaper targets,
8807 // in which case special animations are needed
8808 // (since the wallpaper needs to stay static
8809 // behind them).
8810 int found = 0;
8811 NN = mOpeningApps.size();
8812 for (i=0; i<NN; i++) {
8813 AppWindowToken wtoken = mOpeningApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008814 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008815 found |= 1;
8816 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008817 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008818 found |= 1;
8819 }
8820 }
8821 NN = mClosingApps.size();
8822 for (i=0; i<NN; i++) {
8823 AppWindowToken wtoken = mClosingApps.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008824 if (mLowerWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008825 found |= 2;
8826 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008827 if (mUpperWallpaperTarget.mAppToken == wtoken) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008828 found |= 2;
8829 }
8830 }
8831
8832 if (found == 3) {
8833 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8834 "Wallpaper animation!");
8835 switch (transit) {
8836 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
8837 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
8838 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
8839 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_OPEN;
8840 break;
8841 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
8842 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
8843 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
8844 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_ACTIVITY_CLOSE;
8845 break;
8846 }
8847 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8848 "New transit: " + transit);
8849 }
8850 }
8851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 // We need to figure out which animation to use...
8853 WindowManager.LayoutParams lp = findAnimations(mAppTokens,
8854 mOpeningApps, mClosingApps);
8855
8856 NN = mOpeningApps.size();
8857 for (i=0; i<NN; i++) {
8858 AppWindowToken wtoken = mOpeningApps.get(i);
8859 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8860 "Now opening app" + wtoken);
8861 wtoken.reportedVisible = false;
8862 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07008863 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 setTokenVisibilityLocked(wtoken, lp, true, transit, false);
8865 wtoken.updateReportedVisibilityLocked();
8866 wtoken.showAllWindowsLocked();
8867 }
8868 NN = mClosingApps.size();
8869 for (i=0; i<NN; i++) {
8870 AppWindowToken wtoken = mClosingApps.get(i);
8871 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8872 "Now closing app" + wtoken);
8873 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07008874 wtoken.animation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 setTokenVisibilityLocked(wtoken, lp, false, transit, false);
8876 wtoken.updateReportedVisibilityLocked();
8877 // Force the allDrawn flag, because we want to start
8878 // this guy's animations regardless of whether it's
8879 // gotten drawn.
8880 wtoken.allDrawn = true;
8881 }
8882
8883 mOpeningApps.clear();
8884 mClosingApps.clear();
8885
8886 // This has changed the visibility of windows, so perform
8887 // a new layout to get them all up-to-date.
8888 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07008889 if (!moveInputMethodWindowsIfNeededLocked(true)) {
8890 assignLayersLocked();
8891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008892 performLayoutLockedInner();
8893 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
8894
8895 restart = true;
8896 }
8897 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008898
8899 if (wallpaperMayChange) {
8900 if (adjustWallpaperWindowsLocked()) {
8901 assignLayersLocked();
8902 }
8903 }
8904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 } while (restart);
8906
8907 // THIRD LOOP: Update the surfaces of all windows.
8908
8909 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
8910
8911 boolean obscured = false;
8912 boolean blurring = false;
8913 boolean dimming = false;
8914 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07008915 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008916 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008917
8918 for (i=N-1; i>=0; i--) {
8919 WindowState w = (WindowState)mWindows.get(i);
8920
8921 boolean displayed = false;
8922 final WindowManager.LayoutParams attrs = w.mAttrs;
8923 final int attrFlags = attrs.flags;
8924
8925 if (w.mSurface != null) {
8926 w.computeShownFrameLocked();
8927 if (localLOGV) Log.v(
8928 TAG, "Placing surface #" + i + " " + w.mSurface
8929 + ": new=" + w.mShownFrame + ", old="
8930 + w.mLastShownFrame);
8931
8932 boolean resize;
8933 int width, height;
8934 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
8935 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
8936 w.mLastRequestedHeight != w.mRequestedHeight;
8937 // for a scaled surface, we just want to use
8938 // the requested size.
8939 width = w.mRequestedWidth;
8940 height = w.mRequestedHeight;
8941 w.mLastRequestedWidth = width;
8942 w.mLastRequestedHeight = height;
8943 w.mLastShownFrame.set(w.mShownFrame);
8944 try {
8945 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
8946 } catch (RuntimeException e) {
8947 Log.w(TAG, "Error positioning surface in " + w, e);
8948 if (!recoveringMemory) {
8949 reclaimSomeSurfaceMemoryLocked(w, "position");
8950 }
8951 }
8952 } else {
8953 resize = !w.mLastShownFrame.equals(w.mShownFrame);
8954 width = w.mShownFrame.width();
8955 height = w.mShownFrame.height();
8956 w.mLastShownFrame.set(w.mShownFrame);
8957 if (resize) {
8958 if (SHOW_TRANSACTIONS) Log.i(
8959 TAG, " SURFACE " + w.mSurface + ": ("
8960 + w.mShownFrame.left + ","
8961 + w.mShownFrame.top + ") ("
8962 + w.mShownFrame.width() + "x"
8963 + w.mShownFrame.height() + ")");
8964 }
8965 }
8966
8967 if (resize) {
8968 if (width < 1) width = 1;
8969 if (height < 1) height = 1;
8970 if (w.mSurface != null) {
8971 try {
8972 w.mSurface.setSize(width, height);
8973 w.mSurface.setPosition(w.mShownFrame.left,
8974 w.mShownFrame.top);
8975 } catch (RuntimeException e) {
8976 // If something goes wrong with the surface (such
8977 // as running out of memory), don't take down the
8978 // entire system.
8979 Log.e(TAG, "Failure updating surface of " + w
8980 + "size=(" + width + "x" + height
8981 + "), pos=(" + w.mShownFrame.left
8982 + "," + w.mShownFrame.top + ")", e);
8983 if (!recoveringMemory) {
8984 reclaimSomeSurfaceMemoryLocked(w, "size");
8985 }
8986 }
8987 }
8988 }
8989 if (!w.mAppFreezing) {
8990 w.mContentInsetsChanged =
8991 !w.mLastContentInsets.equals(w.mContentInsets);
8992 w.mVisibleInsetsChanged =
8993 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07008994 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 || w.mContentInsetsChanged
8996 || w.mVisibleInsetsChanged) {
8997 w.mLastFrame.set(w.mFrame);
8998 w.mLastContentInsets.set(w.mContentInsets);
8999 w.mLastVisibleInsets.set(w.mVisibleInsets);
9000 // If the orientation is changing, then we need to
9001 // hold off on unfreezing the display until this
9002 // window has been redrawn; to do that, we need
9003 // to go through the process of getting informed
9004 // by the application when it has finished drawing.
9005 if (w.mOrientationChanging) {
9006 if (DEBUG_ORIENTATION) Log.v(TAG,
9007 "Orientation start waiting for draw in "
9008 + w + ", surface " + w.mSurface);
9009 w.mDrawPending = true;
9010 w.mCommitDrawPending = false;
9011 w.mReadyToShow = false;
9012 if (w.mAppToken != null) {
9013 w.mAppToken.allDrawn = false;
9014 }
9015 }
Romain Guy06882f82009-06-10 13:36:04 -07009016 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009017 "Resizing window " + w + " to " + w.mFrame);
9018 mResizingWindows.add(w);
9019 } else if (w.mOrientationChanging) {
9020 if (!w.mDrawPending && !w.mCommitDrawPending) {
9021 if (DEBUG_ORIENTATION) Log.v(TAG,
9022 "Orientation not waiting for draw in "
9023 + w + ", surface " + w.mSurface);
9024 w.mOrientationChanging = false;
9025 }
9026 }
9027 }
9028
9029 if (w.mAttachedHidden) {
9030 if (!w.mLastHidden) {
9031 //dump();
9032 w.mLastHidden = true;
9033 if (SHOW_TRANSACTIONS) Log.i(
9034 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9035 if (w.mSurface != null) {
9036 try {
9037 w.mSurface.hide();
9038 } catch (RuntimeException e) {
9039 Log.w(TAG, "Exception hiding surface in " + w);
9040 }
9041 }
9042 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9043 }
9044 // If we are waiting for this window to handle an
9045 // orientation change, well, it is hidden, so
9046 // doesn't really matter. Note that this does
9047 // introduce a potential glitch if the window
9048 // becomes unhidden before it has drawn for the
9049 // new orientation.
9050 if (w.mOrientationChanging) {
9051 w.mOrientationChanging = false;
9052 if (DEBUG_ORIENTATION) Log.v(TAG,
9053 "Orientation change skips hidden " + w);
9054 }
9055 } else if (!w.isReadyForDisplay()) {
9056 if (!w.mLastHidden) {
9057 //dump();
9058 w.mLastHidden = true;
9059 if (SHOW_TRANSACTIONS) Log.i(
9060 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9061 if (w.mSurface != null) {
9062 try {
9063 w.mSurface.hide();
9064 } catch (RuntimeException e) {
9065 Log.w(TAG, "Exception exception hiding surface in " + w);
9066 }
9067 }
9068 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9069 }
9070 // If we are waiting for this window to handle an
9071 // orientation change, well, it is hidden, so
9072 // doesn't really matter. Note that this does
9073 // introduce a potential glitch if the window
9074 // becomes unhidden before it has drawn for the
9075 // new orientation.
9076 if (w.mOrientationChanging) {
9077 w.mOrientationChanging = false;
9078 if (DEBUG_ORIENTATION) Log.v(TAG,
9079 "Orientation change skips hidden " + w);
9080 }
9081 } else if (w.mLastLayer != w.mAnimLayer
9082 || w.mLastAlpha != w.mShownAlpha
9083 || w.mLastDsDx != w.mDsDx
9084 || w.mLastDtDx != w.mDtDx
9085 || w.mLastDsDy != w.mDsDy
9086 || w.mLastDtDy != w.mDtDy
9087 || w.mLastHScale != w.mHScale
9088 || w.mLastVScale != w.mVScale
9089 || w.mLastHidden) {
9090 displayed = true;
9091 w.mLastAlpha = w.mShownAlpha;
9092 w.mLastLayer = w.mAnimLayer;
9093 w.mLastDsDx = w.mDsDx;
9094 w.mLastDtDx = w.mDtDx;
9095 w.mLastDsDy = w.mDsDy;
9096 w.mLastDtDy = w.mDtDy;
9097 w.mLastHScale = w.mHScale;
9098 w.mLastVScale = w.mVScale;
9099 if (SHOW_TRANSACTIONS) Log.i(
9100 TAG, " SURFACE " + w.mSurface + ": alpha="
9101 + w.mShownAlpha + " layer=" + w.mAnimLayer);
9102 if (w.mSurface != null) {
9103 try {
9104 w.mSurface.setAlpha(w.mShownAlpha);
9105 w.mSurface.setLayer(w.mAnimLayer);
9106 w.mSurface.setMatrix(
9107 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9108 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9109 } catch (RuntimeException e) {
9110 Log.w(TAG, "Error updating surface in " + w, e);
9111 if (!recoveringMemory) {
9112 reclaimSomeSurfaceMemoryLocked(w, "update");
9113 }
9114 }
9115 }
9116
9117 if (w.mLastHidden && !w.mDrawPending
9118 && !w.mCommitDrawPending
9119 && !w.mReadyToShow) {
9120 if (SHOW_TRANSACTIONS) Log.i(
9121 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9122 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9123 + " during relayout");
9124 if (showSurfaceRobustlyLocked(w)) {
9125 w.mHasDrawn = true;
9126 w.mLastHidden = false;
9127 } else {
9128 w.mOrientationChanging = false;
9129 }
9130 }
9131 if (w.mSurface != null) {
9132 w.mToken.hasVisible = true;
9133 }
9134 } else {
9135 displayed = true;
9136 }
9137
9138 if (displayed) {
9139 if (!covered) {
9140 if (attrs.width == LayoutParams.FILL_PARENT
9141 && attrs.height == LayoutParams.FILL_PARENT) {
9142 covered = true;
9143 }
9144 }
9145 if (w.mOrientationChanging) {
9146 if (w.mDrawPending || w.mCommitDrawPending) {
9147 orientationChangeComplete = false;
9148 if (DEBUG_ORIENTATION) Log.v(TAG,
9149 "Orientation continue waiting for draw in " + w);
9150 } else {
9151 w.mOrientationChanging = false;
9152 if (DEBUG_ORIENTATION) Log.v(TAG,
9153 "Orientation change complete in " + w);
9154 }
9155 }
9156 w.mToken.hasVisible = true;
9157 }
9158 } else if (w.mOrientationChanging) {
9159 if (DEBUG_ORIENTATION) Log.v(TAG,
9160 "Orientation change skips hidden " + w);
9161 w.mOrientationChanging = false;
9162 }
9163
9164 final boolean canBeSeen = w.isDisplayedLw();
9165
9166 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9167 focusDisplayed = true;
9168 }
9169
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009170 final boolean obscuredChanged = w.mObscured != obscured;
9171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009172 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009173 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 if (w.mSurface != null) {
9175 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9176 holdScreen = w.mSession;
9177 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009178 if (!syswin && w.mAttrs.screenBrightness >= 0
9179 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 screenBrightness = w.mAttrs.screenBrightness;
9181 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009182 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9183 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9184 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9185 syswin = true;
9186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009188
9189 boolean opaqueDrawn = w.isOpaqueDrawn();
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009190 if ((opaqueDrawn && w.isFullscreen(dw, dh))
9191 || attrs.type == TYPE_WALLPAPER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 // This window completely covers everything behind it,
9193 // so we want to leave all of them as unblurred (for
9194 // performance reasons).
9195 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009196 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9197 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009198 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009199 obscured = true;
9200 if (mBackgroundFillerSurface == null) {
9201 try {
9202 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9203 0, dw, dh,
9204 PixelFormat.OPAQUE,
9205 Surface.FX_SURFACE_NORMAL);
9206 } catch (Exception e) {
9207 Log.e(TAG, "Exception creating filler surface", e);
9208 }
9209 }
9210 try {
9211 mBackgroundFillerSurface.setPosition(0, 0);
9212 mBackgroundFillerSurface.setSize(dw, dh);
9213 // Using the same layer as Dim because they will never be shown at the
9214 // same time.
9215 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9216 mBackgroundFillerSurface.show();
9217 } catch (RuntimeException e) {
9218 Log.e(TAG, "Exception showing filler surface");
9219 }
9220 backgroundFillerShown = true;
9221 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 } else if (canBeSeen && !obscured &&
9223 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9224 if (localLOGV) Log.v(TAG, "Win " + w
9225 + ": blurring=" + blurring
9226 + " obscured=" + obscured
9227 + " displayed=" + displayed);
9228 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9229 if (!dimming) {
9230 //Log.i(TAG, "DIM BEHIND: " + w);
9231 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009232 if (mDimAnimator == null) {
9233 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009235 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009237 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009238 }
9239 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9240 if (!blurring) {
9241 //Log.i(TAG, "BLUR BEHIND: " + w);
9242 blurring = true;
9243 mBlurShown = true;
9244 if (mBlurSurface == null) {
9245 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9246 + mBlurSurface + ": CREATE");
9247 try {
Romain Guy06882f82009-06-10 13:36:04 -07009248 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009249 -1, 16, 16,
9250 PixelFormat.OPAQUE,
9251 Surface.FX_SURFACE_BLUR);
9252 } catch (Exception e) {
9253 Log.e(TAG, "Exception creating Blur surface", e);
9254 }
9255 }
9256 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9257 + mBlurSurface + ": SHOW pos=(0,0) (" +
9258 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9259 if (mBlurSurface != null) {
9260 mBlurSurface.setPosition(0, 0);
9261 mBlurSurface.setSize(dw, dh);
9262 try {
9263 mBlurSurface.show();
9264 } catch (RuntimeException e) {
9265 Log.w(TAG, "Failure showing blur surface", e);
9266 }
9267 }
9268 }
9269 mBlurSurface.setLayer(w.mAnimLayer-2);
9270 }
9271 }
9272 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009273
9274 if (obscuredChanged && mWallpaperTarget == w) {
9275 // This is the wallpaper target and its obscured state
9276 // changed... make sure the current wallaper's visibility
9277 // has been updated accordingly.
9278 updateWallpaperVisibilityLocked();
9279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009281
9282 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9283 mBackgroundFillerShown = false;
9284 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9285 try {
9286 mBackgroundFillerSurface.hide();
9287 } catch (RuntimeException e) {
9288 Log.e(TAG, "Exception hiding filler surface", e);
9289 }
9290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009291
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009292 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9293 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 }
Romain Guy06882f82009-06-10 13:36:04 -07009295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 if (!blurring && mBlurShown) {
9297 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9298 + ": HIDE");
9299 try {
9300 mBlurSurface.hide();
9301 } catch (IllegalArgumentException e) {
9302 Log.w(TAG, "Illegal argument exception hiding blur surface");
9303 }
9304 mBlurShown = false;
9305 }
9306
9307 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9308 } catch (RuntimeException e) {
9309 Log.e(TAG, "Unhandled exception in Window Manager", e);
9310 }
9311
9312 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9315 "With display frozen, orientationChangeComplete="
9316 + orientationChangeComplete);
9317 if (orientationChangeComplete) {
9318 if (mWindowsFreezingScreen) {
9319 mWindowsFreezingScreen = false;
9320 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9321 }
9322 if (mAppsFreezingScreen == 0) {
9323 stopFreezingDisplayLocked();
9324 }
9325 }
Romain Guy06882f82009-06-10 13:36:04 -07009326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 i = mResizingWindows.size();
9328 if (i > 0) {
9329 do {
9330 i--;
9331 WindowState win = mResizingWindows.get(i);
9332 try {
9333 win.mClient.resized(win.mFrame.width(),
9334 win.mFrame.height(), win.mLastContentInsets,
9335 win.mLastVisibleInsets, win.mDrawPending);
9336 win.mContentInsetsChanged = false;
9337 win.mVisibleInsetsChanged = false;
9338 } catch (RemoteException e) {
9339 win.mOrientationChanging = false;
9340 }
9341 } while (i > 0);
9342 mResizingWindows.clear();
9343 }
Romain Guy06882f82009-06-10 13:36:04 -07009344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009346 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 i = mDestroySurface.size();
9348 if (i > 0) {
9349 do {
9350 i--;
9351 WindowState win = mDestroySurface.get(i);
9352 win.mDestroying = false;
9353 if (mInputMethodWindow == win) {
9354 mInputMethodWindow = null;
9355 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009356 if (win == mWallpaperTarget) {
9357 wallpaperDestroyed = true;
9358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 win.destroySurfaceLocked();
9360 } while (i > 0);
9361 mDestroySurface.clear();
9362 }
9363
9364 // Time to remove any exiting tokens?
9365 for (i=mExitingTokens.size()-1; i>=0; i--) {
9366 WindowToken token = mExitingTokens.get(i);
9367 if (!token.hasVisible) {
9368 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009369 if (token.windowType == TYPE_WALLPAPER) {
9370 mWallpaperTokens.remove(token);
9371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009372 }
9373 }
9374
9375 // Time to remove any exiting applications?
9376 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9377 AppWindowToken token = mExitingAppTokens.get(i);
9378 if (!token.hasVisible && !mClosingApps.contains(token)) {
9379 mAppTokens.remove(token);
9380 mExitingAppTokens.remove(i);
9381 }
9382 }
9383
9384 if (focusDisplayed) {
9385 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9386 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009387 if (wallpaperDestroyed) {
9388 wallpaperDestroyed = adjustWallpaperWindowsLocked();
9389 }
9390 if (wallpaperDestroyed) {
9391 requestAnimationLocked(0);
9392 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9394 }
9395 mQueue.setHoldScreenLocked(holdScreen != null);
9396 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9397 mPowerManager.setScreenBrightnessOverride(-1);
9398 } else {
9399 mPowerManager.setScreenBrightnessOverride((int)
9400 (screenBrightness * Power.BRIGHTNESS_ON));
9401 }
9402 if (holdScreen != mHoldingScreenOn) {
9403 mHoldingScreenOn = holdScreen;
9404 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9405 mH.sendMessage(m);
9406 }
9407 }
9408
9409 void requestAnimationLocked(long delay) {
9410 if (!mAnimationPending) {
9411 mAnimationPending = true;
9412 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9413 }
9414 }
Romain Guy06882f82009-06-10 13:36:04 -07009415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009416 /**
9417 * Have the surface flinger show a surface, robustly dealing with
9418 * error conditions. In particular, if there is not enough memory
9419 * to show the surface, then we will try to get rid of other surfaces
9420 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 * @return Returns true if the surface was successfully shown.
9423 */
9424 boolean showSurfaceRobustlyLocked(WindowState win) {
9425 try {
9426 if (win.mSurface != null) {
9427 win.mSurface.show();
9428 }
9429 return true;
9430 } catch (RuntimeException e) {
9431 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9432 }
Romain Guy06882f82009-06-10 13:36:04 -07009433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009436 return false;
9437 }
Romain Guy06882f82009-06-10 13:36:04 -07009438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009439 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9440 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9443 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 if (mForceRemoves == null) {
9446 mForceRemoves = new ArrayList<WindowState>();
9447 }
Romain Guy06882f82009-06-10 13:36:04 -07009448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009449 long callingIdentity = Binder.clearCallingIdentity();
9450 try {
9451 // There was some problem... first, do a sanity check of the
9452 // window list to make sure we haven't left any dangling surfaces
9453 // around.
9454 int N = mWindows.size();
9455 boolean leakedSurface = false;
9456 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9457 for (int i=0; i<N; i++) {
9458 WindowState ws = (WindowState)mWindows.get(i);
9459 if (ws.mSurface != null) {
9460 if (!mSessions.contains(ws.mSession)) {
9461 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9462 + ws + " surface=" + ws.mSurface
9463 + " token=" + win.mToken
9464 + " pid=" + ws.mSession.mPid
9465 + " uid=" + ws.mSession.mUid);
9466 ws.mSurface.clear();
9467 ws.mSurface = null;
9468 mForceRemoves.add(ws);
9469 i--;
9470 N--;
9471 leakedSurface = true;
9472 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9473 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9474 + ws + " surface=" + ws.mSurface
9475 + " token=" + win.mAppToken);
9476 ws.mSurface.clear();
9477 ws.mSurface = null;
9478 leakedSurface = true;
9479 }
9480 }
9481 }
Romain Guy06882f82009-06-10 13:36:04 -07009482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 boolean killedApps = false;
9484 if (!leakedSurface) {
9485 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9486 SparseIntArray pidCandidates = new SparseIntArray();
9487 for (int i=0; i<N; i++) {
9488 WindowState ws = (WindowState)mWindows.get(i);
9489 if (ws.mSurface != null) {
9490 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9491 }
9492 }
9493 if (pidCandidates.size() > 0) {
9494 int[] pids = new int[pidCandidates.size()];
9495 for (int i=0; i<pids.length; i++) {
9496 pids[i] = pidCandidates.keyAt(i);
9497 }
9498 try {
9499 if (mActivityManager.killPidsForMemory(pids)) {
9500 killedApps = true;
9501 }
9502 } catch (RemoteException e) {
9503 }
9504 }
9505 }
Romain Guy06882f82009-06-10 13:36:04 -07009506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 if (leakedSurface || killedApps) {
9508 // We managed to reclaim some memory, so get rid of the trouble
9509 // surface and ask the app to request another one.
9510 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9511 if (surface != null) {
9512 surface.clear();
9513 win.mSurface = null;
9514 }
Romain Guy06882f82009-06-10 13:36:04 -07009515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 try {
9517 win.mClient.dispatchGetNewSurface();
9518 } catch (RemoteException e) {
9519 }
9520 }
9521 } finally {
9522 Binder.restoreCallingIdentity(callingIdentity);
9523 }
9524 }
Romain Guy06882f82009-06-10 13:36:04 -07009525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526 private boolean updateFocusedWindowLocked(int mode) {
9527 WindowState newFocus = computeFocusedWindowLocked();
9528 if (mCurrentFocus != newFocus) {
9529 // This check makes sure that we don't already have the focus
9530 // change message pending.
9531 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9532 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9533 if (localLOGV) Log.v(
9534 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9535 final WindowState oldFocus = mCurrentFocus;
9536 mCurrentFocus = newFocus;
9537 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 final WindowState imWindow = mInputMethodWindow;
9540 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009541 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009543 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9544 mLayoutNeeded = true;
9545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9547 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009548 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9549 // Client will do the layout, but we need to assign layers
9550 // for handleNewWindowLocked() below.
9551 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009552 }
9553 }
Romain Guy06882f82009-06-10 13:36:04 -07009554
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009555 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9556 mKeyWaiter.handleNewWindowLocked(newFocus);
9557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 return true;
9559 }
9560 return false;
9561 }
9562
9563 private WindowState computeFocusedWindowLocked() {
9564 WindowState result = null;
9565 WindowState win;
9566
9567 int i = mWindows.size() - 1;
9568 int nextAppIndex = mAppTokens.size()-1;
9569 WindowToken nextApp = nextAppIndex >= 0
9570 ? mAppTokens.get(nextAppIndex) : null;
9571
9572 while (i >= 0) {
9573 win = (WindowState)mWindows.get(i);
9574
9575 if (localLOGV || DEBUG_FOCUS) Log.v(
9576 TAG, "Looking for focus: " + i
9577 + " = " + win
9578 + ", flags=" + win.mAttrs.flags
9579 + ", canReceive=" + win.canReceiveKeys());
9580
9581 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009583 // If this window's application has been removed, just skip it.
9584 if (thisApp != null && thisApp.removed) {
9585 i--;
9586 continue;
9587 }
Romain Guy06882f82009-06-10 13:36:04 -07009588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 // If there is a focused app, don't allow focus to go to any
9590 // windows below it. If this is an application window, step
9591 // through the app tokens until we find its app.
9592 if (thisApp != null && nextApp != null && thisApp != nextApp
9593 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9594 int origAppIndex = nextAppIndex;
9595 while (nextAppIndex > 0) {
9596 if (nextApp == mFocusedApp) {
9597 // Whoops, we are below the focused app... no focus
9598 // for you!
9599 if (localLOGV || DEBUG_FOCUS) Log.v(
9600 TAG, "Reached focused app: " + mFocusedApp);
9601 return null;
9602 }
9603 nextAppIndex--;
9604 nextApp = mAppTokens.get(nextAppIndex);
9605 if (nextApp == thisApp) {
9606 break;
9607 }
9608 }
9609 if (thisApp != nextApp) {
9610 // Uh oh, the app token doesn't exist! This shouldn't
9611 // happen, but if it does we can get totally hosed...
9612 // so restart at the original app.
9613 nextAppIndex = origAppIndex;
9614 nextApp = mAppTokens.get(nextAppIndex);
9615 }
9616 }
9617
9618 // Dispatch to this window if it is wants key events.
9619 if (win.canReceiveKeys()) {
9620 if (DEBUG_FOCUS) Log.v(
9621 TAG, "Found focus @ " + i + " = " + win);
9622 result = win;
9623 break;
9624 }
9625
9626 i--;
9627 }
9628
9629 return result;
9630 }
9631
9632 private void startFreezingDisplayLocked() {
9633 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009634 // Freezing the display also suspends key event delivery, to
9635 // keep events from going astray while the display is reconfigured.
9636 // If someone has changed orientation again while the screen is
9637 // still frozen, the events will continue to be blocked while the
9638 // successive orientation change is processed. To prevent spurious
9639 // ANRs, we reset the event dispatch timeout in this case.
9640 synchronized (mKeyWaiter) {
9641 mKeyWaiter.mWasFrozen = true;
9642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 return;
9644 }
Romain Guy06882f82009-06-10 13:36:04 -07009645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 long now = SystemClock.uptimeMillis();
9649 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9650 if (mFreezeGcPending != 0) {
9651 if (now > (mFreezeGcPending+1000)) {
9652 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9653 mH.removeMessages(H.FORCE_GC);
9654 Runtime.getRuntime().gc();
9655 mFreezeGcPending = now;
9656 }
9657 } else {
9658 mFreezeGcPending = now;
9659 }
Romain Guy06882f82009-06-10 13:36:04 -07009660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 mDisplayFrozen = true;
9662 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9663 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9664 mAppTransitionReady = true;
9665 }
Romain Guy06882f82009-06-10 13:36:04 -07009666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009667 if (PROFILE_ORIENTATION) {
9668 File file = new File("/data/system/frozen");
9669 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9670 }
9671 Surface.freezeDisplay(0);
9672 }
Romain Guy06882f82009-06-10 13:36:04 -07009673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 private void stopFreezingDisplayLocked() {
9675 if (!mDisplayFrozen) {
9676 return;
9677 }
Romain Guy06882f82009-06-10 13:36:04 -07009678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 mDisplayFrozen = false;
9680 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9681 if (PROFILE_ORIENTATION) {
9682 Debug.stopMethodTracing();
9683 }
9684 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009685
Chris Tate2ad63a92009-03-25 17:36:48 -07009686 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9687 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009688 synchronized (mKeyWaiter) {
9689 mKeyWaiter.mWasFrozen = true;
9690 mKeyWaiter.notifyAll();
9691 }
9692
9693 // A little kludge: a lot could have happened while the
9694 // display was frozen, so now that we are coming back we
9695 // do a gc so that any remote references the system
9696 // processes holds on others can be released if they are
9697 // no longer needed.
9698 mH.removeMessages(H.FORCE_GC);
9699 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9700 2000);
Romain Guy06882f82009-06-10 13:36:04 -07009701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 mScreenFrozenLock.release();
9703 }
Romain Guy06882f82009-06-10 13:36:04 -07009704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 @Override
9706 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
9707 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
9708 != PackageManager.PERMISSION_GRANTED) {
9709 pw.println("Permission Denial: can't dump WindowManager from from pid="
9710 + Binder.getCallingPid()
9711 + ", uid=" + Binder.getCallingUid());
9712 return;
9713 }
Romain Guy06882f82009-06-10 13:36:04 -07009714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 synchronized(mWindowMap) {
9716 pw.println("Current Window Manager state:");
9717 for (int i=mWindows.size()-1; i>=0; i--) {
9718 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009719 pw.print(" Window #"); pw.print(i); pw.print(' ');
9720 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 w.dump(pw, " ");
9722 }
9723 if (mInputMethodDialogs.size() > 0) {
9724 pw.println(" ");
9725 pw.println(" Input method dialogs:");
9726 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
9727 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009728 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009729 }
9730 }
9731 if (mPendingRemove.size() > 0) {
9732 pw.println(" ");
9733 pw.println(" Remove pending for:");
9734 for (int i=mPendingRemove.size()-1; i>=0; i--) {
9735 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009736 pw.print(" Remove #"); pw.print(i); pw.print(' ');
9737 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 w.dump(pw, " ");
9739 }
9740 }
9741 if (mForceRemoves != null && mForceRemoves.size() > 0) {
9742 pw.println(" ");
9743 pw.println(" Windows force removing:");
9744 for (int i=mForceRemoves.size()-1; i>=0; i--) {
9745 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009746 pw.print(" Removing #"); pw.print(i); pw.print(' ');
9747 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 w.dump(pw, " ");
9749 }
9750 }
9751 if (mDestroySurface.size() > 0) {
9752 pw.println(" ");
9753 pw.println(" Windows waiting to destroy their surface:");
9754 for (int i=mDestroySurface.size()-1; i>=0; i--) {
9755 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009756 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
9757 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 w.dump(pw, " ");
9759 }
9760 }
9761 if (mLosingFocus.size() > 0) {
9762 pw.println(" ");
9763 pw.println(" Windows losing focus:");
9764 for (int i=mLosingFocus.size()-1; i>=0; i--) {
9765 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009766 pw.print(" Losing #"); pw.print(i); pw.print(' ');
9767 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 w.dump(pw, " ");
9769 }
9770 }
9771 if (mSessions.size() > 0) {
9772 pw.println(" ");
9773 pw.println(" All active sessions:");
9774 Iterator<Session> it = mSessions.iterator();
9775 while (it.hasNext()) {
9776 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009777 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 s.dump(pw, " ");
9779 }
9780 }
9781 if (mTokenMap.size() > 0) {
9782 pw.println(" ");
9783 pw.println(" All tokens:");
9784 Iterator<WindowToken> it = mTokenMap.values().iterator();
9785 while (it.hasNext()) {
9786 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009787 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 token.dump(pw, " ");
9789 }
9790 }
9791 if (mTokenList.size() > 0) {
9792 pw.println(" ");
9793 pw.println(" Window token list:");
9794 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009795 pw.print(" #"); pw.print(i); pw.print(": ");
9796 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 }
9798 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009799 if (mWallpaperTokens.size() > 0) {
9800 pw.println(" ");
9801 pw.println(" Wallpaper tokens:");
9802 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
9803 WindowToken token = mWallpaperTokens.get(i);
9804 pw.print(" Wallpaper #"); pw.print(i);
9805 pw.print(' '); pw.print(token); pw.println(':');
9806 token.dump(pw, " ");
9807 }
9808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 if (mAppTokens.size() > 0) {
9810 pw.println(" ");
9811 pw.println(" Application tokens in Z order:");
9812 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009813 pw.print(" App #"); pw.print(i); pw.print(": ");
9814 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 }
9816 }
9817 if (mFinishedStarting.size() > 0) {
9818 pw.println(" ");
9819 pw.println(" Finishing start of application tokens:");
9820 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
9821 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009822 pw.print(" Finished Starting #"); pw.print(i);
9823 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009824 token.dump(pw, " ");
9825 }
9826 }
9827 if (mExitingTokens.size() > 0) {
9828 pw.println(" ");
9829 pw.println(" Exiting tokens:");
9830 for (int i=mExitingTokens.size()-1; i>=0; i--) {
9831 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009832 pw.print(" Exiting #"); pw.print(i);
9833 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 token.dump(pw, " ");
9835 }
9836 }
9837 if (mExitingAppTokens.size() > 0) {
9838 pw.println(" ");
9839 pw.println(" Exiting application tokens:");
9840 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
9841 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009842 pw.print(" Exiting App #"); pw.print(i);
9843 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844 token.dump(pw, " ");
9845 }
9846 }
9847 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009848 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
9849 pw.print(" mLastFocus="); pw.println(mLastFocus);
9850 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
9851 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
9852 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -07009853 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009854 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
9855 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
9856 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
9857 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
9858 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009859 if (mDimAnimator != null) {
9860 mDimAnimator.printTo(pw);
9861 } else {
9862 pw.print( " no DimAnimator ");
9863 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009864 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009865 pw.print(mInputMethodAnimLayerAdjustment);
9866 pw.print(" mWallpaperAnimLayerAdjustment=");
9867 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009868 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
9869 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
9870 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
9871 pw.print(" mRotation="); pw.print(mRotation);
9872 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
9873 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
9874 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
9875 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
9876 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
9877 pw.print(" mNextAppTransition=0x");
9878 pw.print(Integer.toHexString(mNextAppTransition));
9879 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
9880 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
9881 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
9882 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
9883 if (mOpeningApps.size() > 0) {
9884 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
9885 }
9886 if (mClosingApps.size() > 0) {
9887 pw.print(" mClosingApps="); pw.println(mClosingApps);
9888 }
9889 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
9890 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009892 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
9893 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
9894 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
9895 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
9896 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
9897 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 }
9899 }
9900
9901 public void monitor() {
9902 synchronized (mWindowMap) { }
9903 synchronized (mKeyguardDisabled) { }
9904 synchronized (mKeyWaiter) { }
9905 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009906
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07009907 public void virtualKeyFeedback(KeyEvent event) {
9908 mPolicy.keyFeedbackFromInput(event);
9909 }
9910
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009911 /**
9912 * DimAnimator class that controls the dim animation. This holds the surface and
9913 * all state used for dim animation.
9914 */
9915 private static class DimAnimator {
9916 Surface mDimSurface;
9917 boolean mDimShown = false;
9918 float mDimCurrentAlpha;
9919 float mDimTargetAlpha;
9920 float mDimDeltaPerMs;
9921 long mLastDimAnimTime;
9922
9923 DimAnimator (SurfaceSession session) {
9924 if (mDimSurface == null) {
9925 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
9926 + mDimSurface + ": CREATE");
9927 try {
9928 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
9929 Surface.FX_SURFACE_DIM);
9930 } catch (Exception e) {
9931 Log.e(TAG, "Exception creating Dim surface", e);
9932 }
9933 }
9934 }
9935
9936 /**
9937 * Show the dim surface.
9938 */
9939 void show(int dw, int dh) {
9940 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
9941 dw + "x" + dh + ")");
9942 mDimShown = true;
9943 try {
9944 mDimSurface.setPosition(0, 0);
9945 mDimSurface.setSize(dw, dh);
9946 mDimSurface.show();
9947 } catch (RuntimeException e) {
9948 Log.w(TAG, "Failure showing dim surface", e);
9949 }
9950 }
9951
9952 /**
9953 * Set's the dim surface's layer and update dim parameters that will be used in
9954 * {@link updateSurface} after all windows are examined.
9955 */
9956 void updateParameters(WindowState w, long currentTime) {
9957 mDimSurface.setLayer(w.mAnimLayer-1);
9958
9959 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
9960 if (SHOW_TRANSACTIONS) Log.i(TAG, "layer=" + (w.mAnimLayer-1) + ", target=" + target);
9961 if (mDimTargetAlpha != target) {
9962 // If the desired dim level has changed, then
9963 // start an animation to it.
9964 mLastDimAnimTime = currentTime;
9965 long duration = (w.mAnimating && w.mAnimation != null)
9966 ? w.mAnimation.computeDurationHint()
9967 : DEFAULT_DIM_DURATION;
9968 if (target > mDimTargetAlpha) {
9969 // This is happening behind the activity UI,
9970 // so we can make it run a little longer to
9971 // give a stronger impression without disrupting
9972 // the user.
9973 duration *= DIM_DURATION_MULTIPLIER;
9974 }
9975 if (duration < 1) {
9976 // Don't divide by zero
9977 duration = 1;
9978 }
9979 mDimTargetAlpha = target;
9980 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
9981 }
9982 }
9983
9984 /**
9985 * Updating the surface's alpha. Returns true if the animation continues, or returns
9986 * false when the animation is finished and the dim surface is hidden.
9987 */
9988 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
9989 if (!dimming) {
9990 if (mDimTargetAlpha != 0) {
9991 mLastDimAnimTime = currentTime;
9992 mDimTargetAlpha = 0;
9993 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
9994 }
9995 }
9996
9997 boolean animating = false;
9998 if (mLastDimAnimTime != 0) {
9999 mDimCurrentAlpha += mDimDeltaPerMs
10000 * (currentTime-mLastDimAnimTime);
10001 boolean more = true;
10002 if (displayFrozen) {
10003 // If the display is frozen, there is no reason to animate.
10004 more = false;
10005 } else if (mDimDeltaPerMs > 0) {
10006 if (mDimCurrentAlpha > mDimTargetAlpha) {
10007 more = false;
10008 }
10009 } else if (mDimDeltaPerMs < 0) {
10010 if (mDimCurrentAlpha < mDimTargetAlpha) {
10011 more = false;
10012 }
10013 } else {
10014 more = false;
10015 }
10016
10017 // Do we need to continue animating?
10018 if (more) {
10019 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10020 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10021 mLastDimAnimTime = currentTime;
10022 mDimSurface.setAlpha(mDimCurrentAlpha);
10023 animating = true;
10024 } else {
10025 mDimCurrentAlpha = mDimTargetAlpha;
10026 mLastDimAnimTime = 0;
10027 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10028 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10029 mDimSurface.setAlpha(mDimCurrentAlpha);
10030 if (!dimming) {
10031 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10032 + ": HIDE");
10033 try {
10034 mDimSurface.hide();
10035 } catch (RuntimeException e) {
10036 Log.w(TAG, "Illegal argument exception hiding dim surface");
10037 }
10038 mDimShown = false;
10039 }
10040 }
10041 }
10042 return animating;
10043 }
10044
10045 public void printTo(PrintWriter pw) {
10046 pw.print(" mDimShown="); pw.print(mDimShown);
10047 pw.print(" current="); pw.print(mDimCurrentAlpha);
10048 pw.print(" target="); pw.print(mDimTargetAlpha);
10049 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10050 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10051 }
10052 }
10053
10054 /**
10055 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10056 * This is used for opening/closing transition for apps in compatible mode.
10057 */
10058 private static class FadeInOutAnimation extends Animation {
10059 int mWidth;
10060 boolean mFadeIn;
10061
10062 public FadeInOutAnimation(boolean fadeIn) {
10063 setInterpolator(new AccelerateInterpolator());
10064 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10065 mFadeIn = fadeIn;
10066 }
10067
10068 @Override
10069 protected void applyTransformation(float interpolatedTime, Transformation t) {
10070 float x = interpolatedTime;
10071 if (!mFadeIn) {
10072 x = 1.0f - x; // reverse the interpolation for fade out
10073 }
10074 if (x < 0.5) {
10075 // move the window out of the screen.
10076 t.getMatrix().setTranslate(mWidth, 0);
10077 } else {
10078 t.getMatrix().setTranslate(0, 0);// show
10079 t.setAlpha((x - 0.5f) * 2);
10080 }
10081 }
10082
10083 @Override
10084 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10085 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10086 mWidth = width;
10087 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010088
10089 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010090 public int getZAdjustment() {
10091 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010092 }
10093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094}