blob: b0e40380e62f7bb42ac179412174320412be64fd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.LocalPowerManager.CHEEK_EVENT;
20import static android.os.LocalPowerManager.OTHER_EVENT;
21import static android.os.LocalPowerManager.TOUCH_EVENT;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -070022import static android.os.LocalPowerManager.LONG_TOUCH_EVENT;
23import static android.os.LocalPowerManager.TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
25import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
26import static android.view.WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070027import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
29import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -070030import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import static android.view.WindowManager.LayoutParams.FLAG_SYSTEM_ERROR;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
33import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070034import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
36import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import static android.view.WindowManager.LayoutParams.MEMORY_TYPE_PUSH_BUFFERS;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
39import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
41import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44import com.android.internal.app.IBatteryStats;
45import com.android.internal.policy.PolicyManager;
46import com.android.internal.view.IInputContext;
47import com.android.internal.view.IInputMethodClient;
48import com.android.internal.view.IInputMethodManager;
49import com.android.server.KeyInputQueue.QueuedEvent;
50import com.android.server.am.BatteryStatsService;
51
52import android.Manifest;
53import android.app.ActivityManagerNative;
54import android.app.IActivityManager;
55import android.content.Context;
56import android.content.pm.ActivityInfo;
57import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070058import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.res.Configuration;
60import android.graphics.Matrix;
61import android.graphics.PixelFormat;
62import android.graphics.Rect;
63import android.graphics.Region;
64import android.os.BatteryStats;
65import android.os.Binder;
66import android.os.Debug;
67import android.os.Handler;
68import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070069import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.LocalPowerManager;
71import android.os.Looper;
72import android.os.Message;
73import android.os.Parcel;
74import android.os.ParcelFileDescriptor;
75import android.os.Power;
76import android.os.PowerManager;
77import android.os.Process;
78import android.os.RemoteException;
79import android.os.ServiceManager;
80import android.os.SystemClock;
81import android.os.SystemProperties;
82import android.os.TokenWatcher;
83import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070084import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
86import android.util.Log;
87import android.util.SparseIntArray;
88import android.view.Display;
89import android.view.Gravity;
90import android.view.IApplicationToken;
91import android.view.IOnKeyguardExitResult;
92import android.view.IRotationWatcher;
93import android.view.IWindow;
94import android.view.IWindowManager;
95import android.view.IWindowSession;
96import android.view.KeyEvent;
97import android.view.MotionEvent;
98import android.view.RawInputEvent;
99import android.view.Surface;
100import android.view.SurfaceSession;
101import android.view.View;
102import android.view.ViewTreeObserver;
103import android.view.WindowManager;
104import android.view.WindowManagerImpl;
105import android.view.WindowManagerPolicy;
106import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700107import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.animation.Animation;
109import android.view.animation.AnimationUtils;
110import android.view.animation.Transformation;
111
112import java.io.BufferedWriter;
113import java.io.File;
114import java.io.FileDescriptor;
115import java.io.IOException;
116import java.io.OutputStream;
117import java.io.OutputStreamWriter;
118import java.io.PrintWriter;
119import java.io.StringWriter;
120import java.net.Socket;
121import java.util.ArrayList;
122import java.util.HashMap;
123import java.util.HashSet;
124import java.util.Iterator;
125import java.util.List;
126
127/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700128public class WindowManagerService extends IWindowManager.Stub
129 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final String TAG = "WindowManager";
131 static final boolean DEBUG = false;
132 static final boolean DEBUG_FOCUS = false;
133 static final boolean DEBUG_ANIM = false;
134 static final boolean DEBUG_LAYERS = false;
135 static final boolean DEBUG_INPUT = false;
136 static final boolean DEBUG_INPUT_METHOD = false;
137 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700138 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 static final boolean DEBUG_ORIENTATION = false;
140 static final boolean DEBUG_APP_TRANSITIONS = false;
141 static final boolean DEBUG_STARTING_WINDOW = false;
142 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700143 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final boolean SHOW_TRANSACTIONS = false;
Michael Chan53071d62009-05-13 17:29:48 -0700145 static final boolean MEASURE_LATENCY = false;
146 static private LatencyTimer lt;
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final boolean PROFILE_ORIENTATION = false;
149 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700150 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final int LOG_WM_NO_SURFACE_MEMORY = 31000;
Romain Guy06882f82009-06-10 13:36:04 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /** How long to wait for first key repeat, in milliseconds */
155 static final int KEY_REPEAT_FIRST_DELAY = 750;
Romain Guy06882f82009-06-10 13:36:04 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /** How long to wait for subsequent key repeats, in milliseconds */
158 static final int KEY_REPEAT_DELAY = 50;
159
160 /** How much to multiply the policy's type layer, to reserve room
161 * for multiple windows of the same type and Z-ordering adjustment
162 * with TYPE_LAYER_OFFSET. */
163 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
166 * or below others in the same layer. */
167 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** How much to increment the layer for each window, to reserve room
170 * for effect surfaces between them.
171 */
172 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /** The maximum length we will accept for a loaded animation duration:
175 * this is 10 seconds.
176 */
177 static final int MAX_ANIMATION_DURATION = 10*1000;
178
179 /** Amount of time (in milliseconds) to animate the dim surface from one
180 * value to another, when no window animation is driving it.
181 */
182 static final int DEFAULT_DIM_DURATION = 200;
183
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700184 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
185 * compatible windows.
186 */
187 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /** Adjustment to time to perform a dim, to make it more dramatic.
190 */
191 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700192
Dianne Hackborncfaef692009-06-15 14:24:44 -0700193 static final int INJECT_FAILED = 0;
194 static final int INJECT_SUCCEEDED = 1;
195 static final int INJECT_NO_PERMISSION = -1;
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 static final int UPDATE_FOCUS_NORMAL = 0;
198 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
199 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
200 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700201
Michael Chane96440f2009-05-06 10:27:36 -0700202 /** The minimum time between dispatching touch events. */
203 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
204
205 // Last touch event time
206 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700207
Michael Chane96440f2009-05-06 10:27:36 -0700208 // Last touch event type
209 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Time to wait before calling useractivity again. This saves CPU usage
212 // when we get a flood of touch events.
213 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
214
215 // Last time we call user activity
216 long mLastUserActivityCallTime = 0;
217
Romain Guy06882f82009-06-10 13:36:04 -0700218 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700219 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700222 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 /**
225 * Condition waited on by {@link #reenableKeyguard} to know the call to
226 * the window policy has finished.
227 */
228 private boolean mWaitingUntilKeyguardReenabled = false;
229
230
231 final TokenWatcher mKeyguardDisabled = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardDisabled") {
233 public void acquired() {
234 mPolicy.enableKeyguard(false);
235 }
236 public void released() {
237 synchronized (mKeyguardDisabled) {
238 mPolicy.enableKeyguard(true);
239 mWaitingUntilKeyguardReenabled = false;
240 mKeyguardDisabled.notifyAll();
241 }
242 }
243 };
244
245 final Context mContext;
246
247 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
252
253 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * All currently active sessions with clients.
259 */
260 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * Mapping from an IWindow IBinder to the server's Window object.
264 * This is also used as the lock for all of our state.
265 */
266 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
267
268 /**
269 * Mapping from a token IBinder to a WindowToken object.
270 */
271 final HashMap<IBinder, WindowToken> mTokenMap =
272 new HashMap<IBinder, WindowToken>();
273
274 /**
275 * The same tokens as mTokenMap, stored in a list for efficient iteration
276 * over them.
277 */
278 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Window tokens that are in the process of exiting, but still
282 * on screen for animations.
283 */
284 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
285
286 /**
287 * Z-ordered (bottom-most first) list of all application tokens, for
288 * controlling the ordering of windows in different applications. This
289 * contains WindowToken objects.
290 */
291 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
292
293 /**
294 * Application tokens that are in the process of exiting, but still
295 * on screen for animations.
296 */
297 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * List of window tokens that have finished starting their application,
301 * and now need to have the policy remove their windows.
302 */
303 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
304
305 /**
306 * Z-ordered (bottom-most first) list of all Window objects.
307 */
308 final ArrayList mWindows = new ArrayList();
309
310 /**
311 * Windows that are being resized. Used so we can tell the client about
312 * the resize after closing the transaction in which we resized the
313 * underlying surface.
314 */
315 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
316
317 /**
318 * Windows whose animations have ended and now must be removed.
319 */
320 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
321
322 /**
323 * Windows whose surface should be destroyed.
324 */
325 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
326
327 /**
328 * Windows that have lost input focus and are waiting for the new
329 * focus window to be displayed before they are told about this.
330 */
331 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
332
333 /**
334 * This is set when we have run out of memory, and will either be an empty
335 * list or contain windows that need to be force removed.
336 */
337 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700342 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Surface mBlurSurface;
344 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 final float[] mTmpFloats = new float[9];
349
350 boolean mSafeMode;
351 boolean mDisplayEnabled = false;
352 boolean mSystemBooted = false;
353 int mRotation = 0;
354 int mRequestedRotation = 0;
355 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700356 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 ArrayList<IRotationWatcher> mRotationWatchers
358 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 boolean mLayoutNeeded = true;
361 boolean mAnimationPending = false;
362 boolean mDisplayFrozen = false;
363 boolean mWindowsFreezingScreen = false;
364 long mFreezeGcPending = 0;
365 int mAppsFreezingScreen = 0;
366
367 // This is held as long as we have the screen frozen, to give us time to
368 // perform a rotation animation when turning off shows the lock screen which
369 // changes the orientation.
370 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // State management of app transitions. When we are preparing for a
373 // transition, mNextAppTransition will be the kind of transition to
374 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
375 // mOpeningApps and mClosingApps are the lists of tokens that will be
376 // made visible or hidden at the next transition.
377 int mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
378 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700379 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 boolean mAppTransitionTimeout = false;
381 boolean mStartingIconInTransition = false;
382 boolean mSkipAppTransitionAnimation = false;
383 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
384 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700385 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
386 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 //flag to detect fat touch events
389 boolean mFatTouch = false;
390 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 H mH = new H();
393
394 WindowState mCurrentFocus = null;
395 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 // This just indicates the window the input method is on top of, not
398 // necessarily the window its input is going to.
399 WindowState mInputMethodTarget = null;
400 WindowState mUpcomingInputMethodTarget = null;
401 boolean mInputMethodTargetWaitingAnim;
402 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 WindowState mInputMethodWindow = null;
405 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
406
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700407 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
408
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700409 // If non-null, this is the currently visible window that is associated
410 // with the wallpaper.
411 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700412 // If non-null, we are in the middle of animating from one wallpaper target
413 // to another, and this is the lower one in Z-order.
414 WindowState mLowerWallpaperTarget = null;
415 // If non-null, we are in the middle of animating from one wallpaper target
416 // to another, and this is the higher one in Z-order.
417 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700418 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700419 float mLastWallpaperX;
420 float mLastWallpaperY;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 AppWindowToken mFocusedApp = null;
423
424 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 float mWindowAnimationScale = 1.0f;
427 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 final KeyWaiter mKeyWaiter = new KeyWaiter();
430 final KeyQ mQueue;
431 final InputDispatcherThread mInputThread;
432
433 // Who is holding the screen on.
434 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Whether the UI is currently running in touch mode (not showing
438 * navigational focus because the user is directly pressing the screen).
439 */
440 boolean mInTouchMode = false;
441
442 private ViewServer mViewServer;
443
444 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700445
Dianne Hackbornc485a602009-03-24 22:39:49 -0700446 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700447 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700448
449 // The frame use to limit the size of the app running in compatibility mode.
450 Rect mCompatibleScreenFrame = new Rect();
451 // The surface used to fill the outer rim of the app running in compatibility mode.
452 Surface mBackgroundFillerSurface = null;
453 boolean mBackgroundFillerShown = false;
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public static WindowManagerService main(Context context,
456 PowerManagerService pm, boolean haveInputMethods) {
457 WMThread thr = new WMThread(context, pm, haveInputMethods);
458 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (thr) {
461 while (thr.mService == null) {
462 try {
463 thr.wait();
464 } catch (InterruptedException e) {
465 }
466 }
467 }
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 return thr.mService;
470 }
Romain Guy06882f82009-06-10 13:36:04 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 static class WMThread extends Thread {
473 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 private final Context mContext;
476 private final PowerManagerService mPM;
477 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 public WMThread(Context context, PowerManagerService pm,
480 boolean haveInputMethods) {
481 super("WindowManager");
482 mContext = context;
483 mPM = pm;
484 mHaveInputMethods = haveInputMethods;
485 }
Romain Guy06882f82009-06-10 13:36:04 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public void run() {
488 Looper.prepare();
489 WindowManagerService s = new WindowManagerService(mContext, mPM,
490 mHaveInputMethods);
491 android.os.Process.setThreadPriority(
492 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 synchronized (this) {
495 mService = s;
496 notifyAll();
497 }
Romain Guy06882f82009-06-10 13:36:04 -0700498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 Looper.loop();
500 }
501 }
502
503 static class PolicyThread extends Thread {
504 private final WindowManagerPolicy mPolicy;
505 private final WindowManagerService mService;
506 private final Context mContext;
507 private final PowerManagerService mPM;
508 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public PolicyThread(WindowManagerPolicy policy,
511 WindowManagerService service, Context context,
512 PowerManagerService pm) {
513 super("WindowManagerPolicy");
514 mPolicy = policy;
515 mService = service;
516 mContext = context;
517 mPM = pm;
518 }
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public void run() {
521 Looper.prepare();
522 //Looper.myLooper().setMessageLogging(new LogPrinter(
523 // Log.VERBOSE, "WindowManagerPolicy"));
524 android.os.Process.setThreadPriority(
525 android.os.Process.THREAD_PRIORITY_FOREGROUND);
526 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 synchronized (this) {
529 mRunning = true;
530 notifyAll();
531 }
Romain Guy06882f82009-06-10 13:36:04 -0700532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 Looper.loop();
534 }
535 }
536
537 private WindowManagerService(Context context, PowerManagerService pm,
538 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700539 if (MEASURE_LATENCY) {
540 lt = new LatencyTimer(100, 1000);
541 }
542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 mContext = context;
544 mHaveInputMethods = haveInputMethods;
545 mLimitedAlphaCompositing = context.getResources().getBoolean(
546 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 mPowerManager = pm;
549 mPowerManager.setPolicy(mPolicy);
550 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
551 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
552 "SCREEN_FROZEN");
553 mScreenFrozenLock.setReferenceCounted(false);
554
555 mActivityManager = ActivityManagerNative.getDefault();
556 mBatteryStats = BatteryStatsService.getService();
557
558 // Get persisted window scale setting
559 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
560 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
561 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
562 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700563
Michael Chan9f028e62009-08-04 17:37:46 -0700564 int max_events_per_sec = 35;
565 try {
566 max_events_per_sec = Integer.parseInt(SystemProperties
567 .get("windowsmgr.max_events_per_sec"));
568 if (max_events_per_sec < 1) {
569 max_events_per_sec = 35;
570 }
571 } catch (NumberFormatException e) {
572 }
573 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mQueue = new KeyQ();
576
577 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
580 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 synchronized (thr) {
583 while (!thr.mRunning) {
584 try {
585 thr.wait();
586 } catch (InterruptedException e) {
587 }
588 }
589 }
Romain Guy06882f82009-06-10 13:36:04 -0700590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 // Add ourself to the Watchdog monitors.
594 Watchdog.getInstance().addMonitor(this);
595 }
596
597 @Override
598 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
599 throws RemoteException {
600 try {
601 return super.onTransact(code, data, reply, flags);
602 } catch (RuntimeException e) {
603 // The window manager only throws security exceptions, so let's
604 // log all others.
605 if (!(e instanceof SecurityException)) {
606 Log.e(TAG, "Window Manager Crash", e);
607 }
608 throw e;
609 }
610 }
611
612 private void placeWindowAfter(Object pos, WindowState window) {
613 final int i = mWindows.indexOf(pos);
614 if (localLOGV || DEBUG_FOCUS) Log.v(
615 TAG, "Adding window " + window + " at "
616 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
617 mWindows.add(i+1, window);
618 }
619
620 private void placeWindowBefore(Object pos, WindowState window) {
621 final int i = mWindows.indexOf(pos);
622 if (localLOGV || DEBUG_FOCUS) Log.v(
623 TAG, "Adding window " + window + " at "
624 + i + " of " + mWindows.size() + " (before " + pos + ")");
625 mWindows.add(i, window);
626 }
627
628 //This method finds out the index of a window that has the same app token as
629 //win. used for z ordering the windows in mWindows
630 private int findIdxBasedOnAppTokens(WindowState win) {
631 //use a local variable to cache mWindows
632 ArrayList localmWindows = mWindows;
633 int jmax = localmWindows.size();
634 if(jmax == 0) {
635 return -1;
636 }
637 for(int j = (jmax-1); j >= 0; j--) {
638 WindowState wentry = (WindowState)localmWindows.get(j);
639 if(wentry.mAppToken == win.mAppToken) {
640 return j;
641 }
642 }
643 return -1;
644 }
Romain Guy06882f82009-06-10 13:36:04 -0700645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
647 final IWindow client = win.mClient;
648 final WindowToken token = win.mToken;
649 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 final int N = localmWindows.size();
652 final WindowState attached = win.mAttachedWindow;
653 int i;
654 if (attached == null) {
655 int tokenWindowsPos = token.windows.size();
656 if (token.appWindowToken != null) {
657 int index = tokenWindowsPos-1;
658 if (index >= 0) {
659 // If this application has existing windows, we
660 // simply place the new window on top of them... but
661 // keep the starting window on top.
662 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
663 // Base windows go behind everything else.
664 placeWindowBefore(token.windows.get(0), win);
665 tokenWindowsPos = 0;
666 } else {
667 AppWindowToken atoken = win.mAppToken;
668 if (atoken != null &&
669 token.windows.get(index) == atoken.startingWindow) {
670 placeWindowBefore(token.windows.get(index), win);
671 tokenWindowsPos--;
672 } else {
673 int newIdx = findIdxBasedOnAppTokens(win);
674 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700675 //there is a window above this one associated with the same
676 //apptoken note that the window could be a floating window
677 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 //windows associated with this token.
679 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
683 } else {
684 if (localLOGV) Log.v(
685 TAG, "Figuring out where to add app window "
686 + client.asBinder() + " (token=" + token + ")");
687 // Figure out where the window should go, based on the
688 // order of applications.
689 final int NA = mAppTokens.size();
690 Object pos = null;
691 for (i=NA-1; i>=0; i--) {
692 AppWindowToken t = mAppTokens.get(i);
693 if (t == token) {
694 i--;
695 break;
696 }
Dianne Hackborna8f60182009-09-01 19:01:50 -0700697
698 // We haven't reached the token yet; if this token
699 // is not going to the bottom and has windows, we can
700 // use it as an anchor for when we do reach the token.
701 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 pos = t.windows.get(0);
703 }
704 }
705 // We now know the index into the apps. If we found
706 // an app window above, that gives us the position; else
707 // we need to look some more.
708 if (pos != null) {
709 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700710 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 mTokenMap.get(((WindowState)pos).mClient.asBinder());
712 if (atoken != null) {
713 final int NC = atoken.windows.size();
714 if (NC > 0) {
715 WindowState bottom = atoken.windows.get(0);
716 if (bottom.mSubLayer < 0) {
717 pos = bottom;
718 }
719 }
720 }
721 placeWindowBefore(pos, win);
722 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700723 // Continue looking down until we find the first
724 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 while (i >= 0) {
726 AppWindowToken t = mAppTokens.get(i);
727 final int NW = t.windows.size();
728 if (NW > 0) {
729 pos = t.windows.get(NW-1);
730 break;
731 }
732 i--;
733 }
734 if (pos != null) {
735 // Move in front of any windows attached to this
736 // one.
737 WindowToken atoken =
738 mTokenMap.get(((WindowState)pos).mClient.asBinder());
739 if (atoken != null) {
740 final int NC = atoken.windows.size();
741 if (NC > 0) {
742 WindowState top = atoken.windows.get(NC-1);
743 if (top.mSubLayer >= 0) {
744 pos = top;
745 }
746 }
747 }
748 placeWindowAfter(pos, win);
749 } else {
750 // Just search for the start of this layer.
751 final int myLayer = win.mBaseLayer;
752 for (i=0; i<N; i++) {
753 WindowState w = (WindowState)localmWindows.get(i);
754 if (w.mBaseLayer > myLayer) {
755 break;
756 }
757 }
758 if (localLOGV || DEBUG_FOCUS) Log.v(
759 TAG, "Adding window " + win + " at "
760 + i + " of " + N);
761 localmWindows.add(i, win);
762 }
763 }
764 }
765 } else {
766 // Figure out where window should go, based on layer.
767 final int myLayer = win.mBaseLayer;
768 for (i=N-1; i>=0; i--) {
769 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
770 i++;
771 break;
772 }
773 }
774 if (i < 0) i = 0;
775 if (localLOGV || DEBUG_FOCUS) Log.v(
776 TAG, "Adding window " + win + " at "
777 + i + " of " + N);
778 localmWindows.add(i, win);
779 }
780 if (addToToken) {
781 token.windows.add(tokenWindowsPos, win);
782 }
783
784 } else {
785 // Figure out this window's ordering relative to the window
786 // it is attached to.
787 final int NA = token.windows.size();
788 final int sublayer = win.mSubLayer;
789 int largestSublayer = Integer.MIN_VALUE;
790 WindowState windowWithLargestSublayer = null;
791 for (i=0; i<NA; i++) {
792 WindowState w = token.windows.get(i);
793 final int wSublayer = w.mSubLayer;
794 if (wSublayer >= largestSublayer) {
795 largestSublayer = wSublayer;
796 windowWithLargestSublayer = w;
797 }
798 if (sublayer < 0) {
799 // For negative sublayers, we go below all windows
800 // in the same sublayer.
801 if (wSublayer >= sublayer) {
802 if (addToToken) {
803 token.windows.add(i, win);
804 }
805 placeWindowBefore(
806 wSublayer >= 0 ? attached : w, win);
807 break;
808 }
809 } else {
810 // For positive sublayers, we go above all windows
811 // in the same sublayer.
812 if (wSublayer > sublayer) {
813 if (addToToken) {
814 token.windows.add(i, win);
815 }
816 placeWindowBefore(w, win);
817 break;
818 }
819 }
820 }
821 if (i >= NA) {
822 if (addToToken) {
823 token.windows.add(win);
824 }
825 if (sublayer < 0) {
826 placeWindowBefore(attached, win);
827 } else {
828 placeWindowAfter(largestSublayer >= 0
829 ? windowWithLargestSublayer
830 : attached,
831 win);
832 }
833 }
834 }
Romain Guy06882f82009-06-10 13:36:04 -0700835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 if (win.mAppToken != null && addToToken) {
837 win.mAppToken.allAppWindows.add(win);
838 }
839 }
Romain Guy06882f82009-06-10 13:36:04 -0700840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 static boolean canBeImeTarget(WindowState w) {
842 final int fl = w.mAttrs.flags
843 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
844 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
845 return w.isVisibleOrAdding();
846 }
847 return false;
848 }
Romain Guy06882f82009-06-10 13:36:04 -0700849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
851 final ArrayList localmWindows = mWindows;
852 final int N = localmWindows.size();
853 WindowState w = null;
854 int i = N;
855 while (i > 0) {
856 i--;
857 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 //Log.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
860 // + Integer.toHexString(w.mAttrs.flags));
861 if (canBeImeTarget(w)) {
862 //Log.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 // Yet more tricksyness! If this window is a "starting"
865 // window, we do actually want to be on top of it, but
866 // it is not -really- where input will go. So if the caller
867 // is not actually looking to move the IME, look down below
868 // for a real window to target...
869 if (!willMove
870 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
871 && i > 0) {
872 WindowState wb = (WindowState)localmWindows.get(i-1);
873 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
874 i--;
875 w = wb;
876 }
877 }
878 break;
879 }
880 }
Romain Guy06882f82009-06-10 13:36:04 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Desired input method target="
885 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 if (willMove && w != null) {
888 final WindowState curTarget = mInputMethodTarget;
889 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 // Now some fun for dealing with window animations that
892 // modify the Z order. We need to look at all windows below
893 // the current target that are in this app, finding the highest
894 // visible one in layering.
895 AppWindowToken token = curTarget.mAppToken;
896 WindowState highestTarget = null;
897 int highestPos = 0;
898 if (token.animating || token.animation != null) {
899 int pos = 0;
900 pos = localmWindows.indexOf(curTarget);
901 while (pos >= 0) {
902 WindowState win = (WindowState)localmWindows.get(pos);
903 if (win.mAppToken != token) {
904 break;
905 }
906 if (!win.mRemoved) {
907 if (highestTarget == null || win.mAnimLayer >
908 highestTarget.mAnimLayer) {
909 highestTarget = win;
910 highestPos = pos;
911 }
912 }
913 pos--;
914 }
915 }
Romain Guy06882f82009-06-10 13:36:04 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (highestTarget != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700918 if (DEBUG_INPUT_METHOD) Log.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 + mNextAppTransition + " " + highestTarget
920 + " animating=" + highestTarget.isAnimating()
921 + " layer=" + highestTarget.mAnimLayer
922 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
925 // If we are currently setting up for an animation,
926 // hold everything until we can find out what will happen.
927 mInputMethodTargetWaitingAnim = true;
928 mInputMethodTarget = highestTarget;
929 return highestPos + 1;
930 } else if (highestTarget.isAnimating() &&
931 highestTarget.mAnimLayer > w.mAnimLayer) {
932 // If the window we are currently targeting is involved
933 // with an animation, and it is on top of the next target
934 // we will be over, then hold off on moving until
935 // that is done.
936 mInputMethodTarget = highestTarget;
937 return highestPos + 1;
938 }
939 }
940 }
941 }
Romain Guy06882f82009-06-10 13:36:04 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 //Log.i(TAG, "Placing input method @" + (i+1));
944 if (w != null) {
945 if (willMove) {
946 RuntimeException e = new RuntimeException();
947 e.fillInStackTrace();
948 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
949 + mInputMethodTarget + " to " + w, e);
950 mInputMethodTarget = w;
951 if (w.mAppToken != null) {
952 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
953 } else {
954 setInputMethodAnimLayerAdjustment(0);
955 }
956 }
957 return i+1;
958 }
959 if (willMove) {
960 RuntimeException e = new RuntimeException();
961 e.fillInStackTrace();
962 if (DEBUG_INPUT_METHOD) Log.w(TAG, "Moving IM target from "
963 + mInputMethodTarget + " to null", e);
964 mInputMethodTarget = null;
965 setInputMethodAnimLayerAdjustment(0);
966 }
967 return -1;
968 }
Romain Guy06882f82009-06-10 13:36:04 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 void addInputMethodWindowToListLocked(WindowState win) {
971 int pos = findDesiredInputMethodWindowIndexLocked(true);
972 if (pos >= 0) {
973 win.mTargetAppToken = mInputMethodTarget.mAppToken;
974 mWindows.add(pos, win);
975 moveInputMethodDialogsLocked(pos+1);
976 return;
977 }
978 win.mTargetAppToken = null;
979 addWindowToListInOrderLocked(win, true);
980 moveInputMethodDialogsLocked(pos);
981 }
Romain Guy06882f82009-06-10 13:36:04 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 void setInputMethodAnimLayerAdjustment(int adj) {
984 if (DEBUG_LAYERS) Log.v(TAG, "Setting im layer adj to " + adj);
985 mInputMethodAnimLayerAdjustment = adj;
986 WindowState imw = mInputMethodWindow;
987 if (imw != null) {
988 imw.mAnimLayer = imw.mLayer + adj;
989 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
990 + " anim layer: " + imw.mAnimLayer);
991 int wi = imw.mChildWindows.size();
992 while (wi > 0) {
993 wi--;
994 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
995 cw.mAnimLayer = cw.mLayer + adj;
996 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + cw
997 + " anim layer: " + cw.mAnimLayer);
998 }
999 }
1000 int di = mInputMethodDialogs.size();
1001 while (di > 0) {
1002 di --;
1003 imw = mInputMethodDialogs.get(di);
1004 imw.mAnimLayer = imw.mLayer + adj;
1005 if (DEBUG_LAYERS) Log.v(TAG, "IM win " + imw
1006 + " anim layer: " + imw.mAnimLayer);
1007 }
1008 }
Romain Guy06882f82009-06-10 13:36:04 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1011 int wpos = mWindows.indexOf(win);
1012 if (wpos >= 0) {
1013 if (wpos < interestingPos) interestingPos--;
1014 mWindows.remove(wpos);
1015 int NC = win.mChildWindows.size();
1016 while (NC > 0) {
1017 NC--;
1018 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1019 int cpos = mWindows.indexOf(cw);
1020 if (cpos >= 0) {
1021 if (cpos < interestingPos) interestingPos--;
1022 mWindows.remove(cpos);
1023 }
1024 }
1025 }
1026 return interestingPos;
1027 }
Romain Guy06882f82009-06-10 13:36:04 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 private void reAddWindowToListInOrderLocked(WindowState win) {
1030 addWindowToListInOrderLocked(win, false);
1031 // This is a hack to get all of the child windows added as well
1032 // at the right position. Child windows should be rare and
1033 // this case should be rare, so it shouldn't be that big a deal.
1034 int wpos = mWindows.indexOf(win);
1035 if (wpos >= 0) {
1036 mWindows.remove(wpos);
1037 reAddWindowLocked(wpos, win);
1038 }
1039 }
Romain Guy06882f82009-06-10 13:36:04 -07001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 void logWindowList(String prefix) {
1042 int N = mWindows.size();
1043 while (N > 0) {
1044 N--;
1045 Log.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
1046 }
1047 }
Romain Guy06882f82009-06-10 13:36:04 -07001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 void moveInputMethodDialogsLocked(int pos) {
1050 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 final int N = dialogs.size();
1053 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
1054 for (int i=0; i<N; i++) {
1055 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1056 }
1057 if (DEBUG_INPUT_METHOD) {
1058 Log.v(TAG, "Window list w/pos=" + pos);
1059 logWindowList(" ");
1060 }
Romain Guy06882f82009-06-10 13:36:04 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 if (pos >= 0) {
1063 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1064 if (pos < mWindows.size()) {
1065 WindowState wp = (WindowState)mWindows.get(pos);
1066 if (wp == mInputMethodWindow) {
1067 pos++;
1068 }
1069 }
1070 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
1071 for (int i=0; i<N; i++) {
1072 WindowState win = dialogs.get(i);
1073 win.mTargetAppToken = targetAppToken;
1074 pos = reAddWindowLocked(pos, win);
1075 }
1076 if (DEBUG_INPUT_METHOD) {
1077 Log.v(TAG, "Final window list:");
1078 logWindowList(" ");
1079 }
1080 return;
1081 }
1082 for (int i=0; i<N; i++) {
1083 WindowState win = dialogs.get(i);
1084 win.mTargetAppToken = null;
1085 reAddWindowToListInOrderLocked(win);
1086 if (DEBUG_INPUT_METHOD) {
1087 Log.v(TAG, "No IM target, final list:");
1088 logWindowList(" ");
1089 }
1090 }
1091 }
Romain Guy06882f82009-06-10 13:36:04 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1094 final WindowState imWin = mInputMethodWindow;
1095 final int DN = mInputMethodDialogs.size();
1096 if (imWin == null && DN == 0) {
1097 return false;
1098 }
Romain Guy06882f82009-06-10 13:36:04 -07001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1101 if (imPos >= 0) {
1102 // In this case, the input method windows are to be placed
1103 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 // First check to see if the input method windows are already
1106 // located here, and contiguous.
1107 final int N = mWindows.size();
1108 WindowState firstImWin = imPos < N
1109 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 // Figure out the actual input method window that should be
1112 // at the bottom of their stack.
1113 WindowState baseImWin = imWin != null
1114 ? imWin : mInputMethodDialogs.get(0);
1115 if (baseImWin.mChildWindows.size() > 0) {
1116 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1117 if (cw.mSubLayer < 0) baseImWin = cw;
1118 }
Romain Guy06882f82009-06-10 13:36:04 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (firstImWin == baseImWin) {
1121 // The windows haven't moved... but are they still contiguous?
1122 // First find the top IM window.
1123 int pos = imPos+1;
1124 while (pos < N) {
1125 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1126 break;
1127 }
1128 pos++;
1129 }
1130 pos++;
1131 // Now there should be no more input method windows above.
1132 while (pos < N) {
1133 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1134 break;
1135 }
1136 pos++;
1137 }
1138 if (pos >= N) {
1139 // All is good!
1140 return false;
1141 }
1142 }
Romain Guy06882f82009-06-10 13:36:04 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 if (imWin != null) {
1145 if (DEBUG_INPUT_METHOD) {
1146 Log.v(TAG, "Moving IM from " + imPos);
1147 logWindowList(" ");
1148 }
1149 imPos = tmpRemoveWindowLocked(imPos, imWin);
1150 if (DEBUG_INPUT_METHOD) {
1151 Log.v(TAG, "List after moving with new pos " + imPos + ":");
1152 logWindowList(" ");
1153 }
1154 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1155 reAddWindowLocked(imPos, imWin);
1156 if (DEBUG_INPUT_METHOD) {
1157 Log.v(TAG, "List after moving IM to " + imPos + ":");
1158 logWindowList(" ");
1159 }
1160 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1161 } else {
1162 moveInputMethodDialogsLocked(imPos);
1163 }
Romain Guy06882f82009-06-10 13:36:04 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 } else {
1166 // In this case, the input method windows go in a fixed layer,
1167 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 if (imWin != null) {
1170 if (DEBUG_INPUT_METHOD) Log.v(TAG, "Moving IM from " + imPos);
1171 tmpRemoveWindowLocked(0, imWin);
1172 imWin.mTargetAppToken = null;
1173 reAddWindowToListInOrderLocked(imWin);
1174 if (DEBUG_INPUT_METHOD) {
1175 Log.v(TAG, "List with no IM target:");
1176 logWindowList(" ");
1177 }
1178 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1179 } else {
1180 moveInputMethodDialogsLocked(-1);;
1181 }
Romain Guy06882f82009-06-10 13:36:04 -07001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
Romain Guy06882f82009-06-10 13:36:04 -07001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (needAssignLayers) {
1186 assignLayersLocked();
1187 }
Romain Guy06882f82009-06-10 13:36:04 -07001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 return true;
1190 }
Romain Guy06882f82009-06-10 13:36:04 -07001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 void adjustInputMethodDialogsLocked() {
1193 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1194 }
Romain Guy06882f82009-06-10 13:36:04 -07001195
Dianne Hackborn25994b42009-09-04 14:21:19 -07001196 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
1197 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper vis: target obscured="
1198 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1199 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1200 ? wallpaperTarget.mAppToken.animation : null)
1201 + " upper=" + mUpperWallpaperTarget
1202 + " lower=" + mLowerWallpaperTarget);
1203 return (wallpaperTarget != null
1204 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1205 && wallpaperTarget.mAppToken.animation != null)))
1206 || mUpperWallpaperTarget != null
1207 || mLowerWallpaperTarget != null;
1208 }
1209
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001210 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1211 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
1212
1213 int adjustWallpaperWindowsLocked() {
1214 int changed = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001215
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001216 final int dw = mDisplay.getWidth();
1217 final int dh = mDisplay.getHeight();
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001218
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001219 // First find top-most window that has asked to be on top of the
1220 // wallpaper; all wallpapers go behind it.
1221 final ArrayList localmWindows = mWindows;
1222 int N = localmWindows.size();
1223 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001224 WindowState foundW = null;
1225 int foundI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001226 int i = N;
1227 while (i > 0) {
1228 i--;
1229 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001230 if (w.mAppToken != null) {
1231 // If this window's app token is hidden and not animating,
1232 // it is of no interest to us.
1233 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
1234 if (DEBUG_WALLPAPER) Log.v(TAG,
1235 "Skipping hidden or animating token: " + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001236 continue;
1237 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001238 // If this window's app token is ot fullscreen, also irrelevant.
1239 if (!w.mAppToken.appFullscreen) {
1240 if (DEBUG_WALLPAPER) Log.v(TAG,
1241 "Skipping non-fullscreen token: " + w);
1242 continue;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001243 }
1244 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001245 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w + ": readyfordisplay="
1246 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1247 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001248 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001249 && (mWallpaperTarget == w
1250 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001251 if (DEBUG_WALLPAPER) Log.v(TAG,
1252 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001253 foundW = w;
1254 foundI = i;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001255 if (w == mWallpaperTarget && w.mAppToken != null
1256 && w.mAppToken.animation != null) {
1257 // The current wallpaper target is animating, so we'll
1258 // look behind it for another possible target and figure
1259 // out what is going on below.
1260 if (DEBUG_WALLPAPER) Log.v(TAG, "Win " + w
1261 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001262 continue;
1263 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001264 break;
1265 }
1266 }
1267
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001268 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
1269 // If we are currently waiting for an app transition, and either
1270 // the current target or the next target are involved with it,
1271 // then hold off on doing anything with the wallpaper.
1272 // Note that we are checking here for just whether the target
1273 // is part of an app token... which is potentially overly aggressive
1274 // (the app token may not be involved in the transition), but good
1275 // enough (we'll just wait until whatever transition is pending
1276 // executes).
1277 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001278 if (DEBUG_WALLPAPER) Log.v(TAG,
1279 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001280 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001281 }
1282 if (foundW != null && foundW.mAppToken != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001283 if (DEBUG_WALLPAPER) Log.v(TAG,
1284 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001285 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001286 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001287 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001288
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001289 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001290 if (DEBUG_WALLPAPER) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001291 Log.v(TAG, "New wallpaper target: " + foundW
1292 + " oldTarget: " + mWallpaperTarget);
1293 }
1294
1295 mLowerWallpaperTarget = null;
1296 mUpperWallpaperTarget = null;
1297
1298 WindowState oldW = mWallpaperTarget;
1299 mWallpaperTarget = foundW;
1300
1301 // Now what is happening... if the current and new targets are
1302 // animating, then we are in our super special mode!
1303 if (foundW != null && foundW.mAppToken != null && oldW != null
1304 && oldW.mAppToken != null) {
1305 if (DEBUG_WALLPAPER) {
1306 Log.v(TAG, "New animation: " + foundW.mAppToken.animation
1307 + " old animation: " + oldW.mAppToken.animation);
1308 }
1309 if (foundW.mAppToken.animation != null
1310 && oldW.mAppToken.animation != null) {
1311 int oldI = localmWindows.indexOf(oldW);
1312 if (DEBUG_WALLPAPER) {
1313 Log.v(TAG, "New i: " + foundI + " old i: " + oldI);
1314 }
1315 if (oldI >= 0) {
1316 if (DEBUG_WALLPAPER) {
1317 Log.v(TAG, "Animating wallpapers: old#" + oldI
1318 + "=" + oldW + "; new#" + foundI
1319 + "=" + foundW);
1320 }
1321
1322 // Set the new target correctly.
1323 if (foundW.mAppToken.hiddenRequested) {
1324 if (DEBUG_WALLPAPER) {
1325 Log.v(TAG, "Old wallpaper still the target.");
1326 }
1327 mWallpaperTarget = oldW;
1328 }
1329
1330 // Now set the upper and lower wallpaper targets
1331 // correctly, and make sure that we are positioning
1332 // the wallpaper below the lower.
1333 if (foundI > oldI) {
1334 // The new target is on top of the old one.
1335 if (DEBUG_WALLPAPER) {
1336 Log.v(TAG, "Found target above old target.");
1337 }
1338 mUpperWallpaperTarget = foundW;
1339 mLowerWallpaperTarget = oldW;
1340 foundW = oldW;
1341 foundI = oldI;
1342 } else {
1343 // The new target is below the old one.
1344 if (DEBUG_WALLPAPER) {
1345 Log.v(TAG, "Found target below old target.");
1346 }
1347 mUpperWallpaperTarget = oldW;
1348 mLowerWallpaperTarget = foundW;
1349 }
1350 }
1351 }
1352 }
1353
1354 } else {
1355 // Is it time to stop animating?
1356 if (mLowerWallpaperTarget == null
1357 || mLowerWallpaperTarget.mAppToken.animation == null
1358 || mUpperWallpaperTarget == null
1359 || mUpperWallpaperTarget.mAppToken.animation == null) {
1360 if (DEBUG_WALLPAPER) {
1361 Log.v(TAG, "No longer animating wallpaper targets!");
1362 }
1363 mLowerWallpaperTarget = null;
1364 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001365 }
1366 }
1367
1368 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001369 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001370 // The window is visible to the compositor... but is it visible
1371 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001372 visible = isWallpaperVisible(foundW);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001373 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper visibility: " + visible);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001374
1375 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001376 // its layer adjustment. Only do this if we are not transfering
1377 // between two wallpaper targets.
1378 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001379 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001380 ? foundW.mAppToken.animLayerAdjustment : 0;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001381
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001382 // Now w is the window we are supposed to be behind... but we
1383 // need to be sure to also be behind any of its attached windows,
1384 // AND any starting window associated with it.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001385 while (foundI > 0) {
1386 WindowState wb = (WindowState)localmWindows.get(foundI-1);
1387 if (wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001388 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001389 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001390 // This window is not related to the previous one in any
1391 // interesting way, so stop here.
1392 break;
1393 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001394 foundW = wb;
1395 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001396 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001397 } else {
1398 if (DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper not visible");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001399 }
1400
1401 // Okay i is the position immediately above the wallpaper. Look at
1402 // what is below it for later.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001403 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001404
Dianne Hackborn284ac932009-08-28 10:34:25 -07001405 if (visible) {
1406 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
1407 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
1408 }
1409
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001410 // Start stepping backwards from here, ensuring that our wallpaper windows
1411 // are correctly placed.
1412 int curTokenIndex = mWallpaperTokens.size();
1413 while (curTokenIndex > 0) {
1414 curTokenIndex--;
1415 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001416 if (token.hidden == visible) {
1417 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1418 token.hidden = !visible;
1419 // Need to do a layout to ensure the wallpaper now has the
1420 // correct size.
1421 mLayoutNeeded = true;
1422 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001423
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001424 int curWallpaperIndex = token.windows.size();
1425 while (curWallpaperIndex > 0) {
1426 curWallpaperIndex--;
1427 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001428
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001429 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001430 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001431 }
1432
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001433 // First, make sure the client has the current visibility
1434 // state.
1435 if (wallpaper.mWallpaperVisible != visible) {
1436 wallpaper.mWallpaperVisible = visible;
1437 try {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001438 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001439 "Setting visibility of wallpaper " + wallpaper
1440 + ": " + visible);
1441 wallpaper.mClient.dispatchAppVisibility(visible);
1442 } catch (RemoteException e) {
1443 }
1444 }
1445
1446 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001447 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1448 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001449
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001450 // First, if this window is at the current index, then all
1451 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001452 if (wallpaper == foundW) {
1453 foundI--;
1454 foundW = foundI > 0
1455 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001456 continue;
1457 }
1458
1459 // The window didn't match... the current wallpaper window,
1460 // wherever it is, is in the wrong place, so make sure it is
1461 // not in the list.
1462 int oldIndex = localmWindows.indexOf(wallpaper);
1463 if (oldIndex >= 0) {
1464 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001465 if (oldIndex < foundI) {
1466 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001467 }
1468 }
1469
1470 // Now stick it in.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001471 if (DEBUG_WALLPAPER) Log.v(TAG, "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001472 + " from " + oldIndex + " to " + foundI);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001473
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001474 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001475 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001476 }
1477 }
1478
1479 return changed;
1480 }
1481
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001482 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001483 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG,
1484 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001485 mWallpaperAnimLayerAdjustment = adj;
1486 int curTokenIndex = mWallpaperTokens.size();
1487 while (curTokenIndex > 0) {
1488 curTokenIndex--;
1489 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1490 int curWallpaperIndex = token.windows.size();
1491 while (curWallpaperIndex > 0) {
1492 curWallpaperIndex--;
1493 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1494 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001495 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Log.v(TAG, "Wallpaper win "
1496 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001497 }
1498 }
1499 }
1500
Dianne Hackborn284ac932009-08-28 10:34:25 -07001501 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001502 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001503 boolean rawChanged = false;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001504 if (mLastWallpaperX >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001505 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001506 int offset = availw > 0 ? -(int)(availw*mLastWallpaperX+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001507 changed = wallpaperWin.mXOffset != offset;
1508 if (changed) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001509 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1510 + wallpaperWin + " x: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001511 wallpaperWin.mXOffset = offset;
1512 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001513 if (wallpaperWin.mWallpaperX != mLastWallpaperX) {
1514 wallpaperWin.mWallpaperX = mLastWallpaperX;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001515 rawChanged = true;
1516 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001517 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001518
Dianne Hackborn284ac932009-08-28 10:34:25 -07001519 if (mLastWallpaperY >= 0) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001520 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
Dianne Hackborn284ac932009-08-28 10:34:25 -07001521 int offset = availh > 0 ? -(int)(availh*mLastWallpaperY+.5f) : 0;
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001522 if (wallpaperWin.mYOffset != offset) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001523 if (DEBUG_WALLPAPER) Log.v(TAG, "Update wallpaper "
1524 + wallpaperWin + " y: " + offset);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001525 changed = true;
1526 wallpaperWin.mYOffset = offset;
1527 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001528 if (wallpaperWin.mWallpaperY != mLastWallpaperY) {
1529 wallpaperWin.mWallpaperY = mLastWallpaperY;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001530 rawChanged = true;
1531 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001532 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001533
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001534 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001535 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001536 if (DEBUG_WALLPAPER) Log.v(TAG, "Report new wp offset "
1537 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1538 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001539 wallpaperWin.mClient.dispatchWallpaperOffsets(
1540 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY);
1541 } catch (RemoteException e) {
1542 }
1543 }
1544
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001545 return changed;
1546 }
1547
1548 boolean updateWallpaperOffsetLocked() {
1549 final int dw = mDisplay.getWidth();
1550 final int dh = mDisplay.getHeight();
1551
1552 boolean changed = false;
1553
1554 WindowState target = mWallpaperTarget;
1555 if (target != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001556 mLastWallpaperX = target.mWallpaperX;
1557 mLastWallpaperY = target.mWallpaperY;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001558 int curTokenIndex = mWallpaperTokens.size();
1559 while (curTokenIndex > 0) {
1560 curTokenIndex--;
1561 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1562 int curWallpaperIndex = token.windows.size();
1563 while (curWallpaperIndex > 0) {
1564 curWallpaperIndex--;
1565 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Dianne Hackborn284ac932009-08-28 10:34:25 -07001566 if (updateWallpaperOffsetLocked(wallpaper, dw, dh)) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001567 wallpaper.computeShownFrameLocked();
1568 changed = true;
1569 }
1570 }
1571 }
1572 }
1573
1574 return changed;
1575 }
1576
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001577 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001578 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001579 final int dw = mDisplay.getWidth();
1580 final int dh = mDisplay.getHeight();
1581
1582 int curTokenIndex = mWallpaperTokens.size();
1583 while (curTokenIndex > 0) {
1584 curTokenIndex--;
1585 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001586 if (token.hidden == visible) {
1587 token.hidden = !visible;
1588 // Need to do a layout to ensure the wallpaper now has the
1589 // correct size.
1590 mLayoutNeeded = true;
1591 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001592
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001593 int curWallpaperIndex = token.windows.size();
1594 while (curWallpaperIndex > 0) {
1595 curWallpaperIndex--;
1596 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1597 if (visible) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07001598 updateWallpaperOffsetLocked(wallpaper, dw, dh);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001599 }
1600
1601 if (wallpaper.mWallpaperVisible != visible) {
1602 wallpaper.mWallpaperVisible = visible;
1603 try {
1604 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Log.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001605 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001606 + ": " + visible);
1607 wallpaper.mClient.dispatchAppVisibility(visible);
1608 } catch (RemoteException e) {
1609 }
1610 }
1611 }
1612 }
1613 }
1614
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001615 void sendPointerToWallpaperLocked(WindowState srcWin,
1616 MotionEvent pointer, long eventTime) {
1617 int curTokenIndex = mWallpaperTokens.size();
1618 while (curTokenIndex > 0) {
1619 curTokenIndex--;
1620 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1621 int curWallpaperIndex = token.windows.size();
1622 while (curWallpaperIndex > 0) {
1623 curWallpaperIndex--;
1624 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1625 if ((wallpaper.mAttrs.flags &
1626 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1627 continue;
1628 }
1629 try {
1630 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
1631 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1632 srcWin.mFrame.top-wallpaper.mFrame.top);
1633 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1634 } catch (RemoteException e) {
1635 Log.w(TAG, "Failure sending pointer to wallpaper", e);
1636 }
1637 }
1638 }
1639 }
1640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 public int addWindow(Session session, IWindow client,
1642 WindowManager.LayoutParams attrs, int viewVisibility,
1643 Rect outContentInsets) {
1644 int res = mPolicy.checkAddPermission(attrs);
1645 if (res != WindowManagerImpl.ADD_OKAY) {
1646 return res;
1647 }
Romain Guy06882f82009-06-10 13:36:04 -07001648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 boolean reportNewConfig = false;
1650 WindowState attachedWindow = null;
1651 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 synchronized(mWindowMap) {
1654 // Instantiating a Display requires talking with the simulator,
1655 // so don't do it until we know the system is mostly up and
1656 // running.
1657 if (mDisplay == null) {
1658 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1659 mDisplay = wm.getDefaultDisplay();
1660 mQueue.setDisplay(mDisplay);
1661 reportNewConfig = true;
1662 }
Romain Guy06882f82009-06-10 13:36:04 -07001663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 if (mWindowMap.containsKey(client.asBinder())) {
1665 Log.w(TAG, "Window " + client + " is already added");
1666 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1667 }
1668
1669 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Romain Guy06882f82009-06-10 13:36:04 -07001670 attachedWindow = windowForClientLocked(null, attrs.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 if (attachedWindow == null) {
1672 Log.w(TAG, "Attempted to add window with token that is not a window: "
1673 + attrs.token + ". Aborting.");
1674 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1675 }
1676 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1677 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
1678 Log.w(TAG, "Attempted to add window with token that is a sub-window: "
1679 + attrs.token + ". Aborting.");
1680 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1681 }
1682 }
1683
1684 boolean addToken = false;
1685 WindowToken token = mTokenMap.get(attrs.token);
1686 if (token == null) {
1687 if (attrs.type >= FIRST_APPLICATION_WINDOW
1688 && attrs.type <= LAST_APPLICATION_WINDOW) {
1689 Log.w(TAG, "Attempted to add application window with unknown token "
1690 + attrs.token + ". Aborting.");
1691 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1692 }
1693 if (attrs.type == TYPE_INPUT_METHOD) {
1694 Log.w(TAG, "Attempted to add input method window with unknown token "
1695 + attrs.token + ". Aborting.");
1696 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1697 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001698 if (attrs.type == TYPE_WALLPAPER) {
1699 Log.w(TAG, "Attempted to add wallpaper window with unknown token "
1700 + attrs.token + ". Aborting.");
1701 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 token = new WindowToken(attrs.token, -1, false);
1704 addToken = true;
1705 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1706 && attrs.type <= LAST_APPLICATION_WINDOW) {
1707 AppWindowToken atoken = token.appWindowToken;
1708 if (atoken == null) {
1709 Log.w(TAG, "Attempted to add window with non-application token "
1710 + token + ". Aborting.");
1711 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1712 } else if (atoken.removed) {
1713 Log.w(TAG, "Attempted to add window with exiting application token "
1714 + token + ". Aborting.");
1715 return WindowManagerImpl.ADD_APP_EXITING;
1716 }
1717 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1718 // No need for this guy!
1719 if (localLOGV) Log.v(
1720 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1721 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1722 }
1723 } else if (attrs.type == TYPE_INPUT_METHOD) {
1724 if (token.windowType != TYPE_INPUT_METHOD) {
1725 Log.w(TAG, "Attempted to add input method window with bad token "
1726 + attrs.token + ". Aborting.");
1727 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1728 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001729 } else if (attrs.type == TYPE_WALLPAPER) {
1730 if (token.windowType != TYPE_WALLPAPER) {
1731 Log.w(TAG, "Attempted to add wallpaper window with bad token "
1732 + attrs.token + ". Aborting.");
1733 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
1737 win = new WindowState(session, client, token,
1738 attachedWindow, attrs, viewVisibility);
1739 if (win.mDeathRecipient == null) {
1740 // Client has apparently died, so there is no reason to
1741 // continue.
1742 Log.w(TAG, "Adding window client " + client.asBinder()
1743 + " that is dead, aborting.");
1744 return WindowManagerImpl.ADD_APP_EXITING;
1745 }
1746
1747 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 res = mPolicy.prepareAddWindowLw(win, attrs);
1750 if (res != WindowManagerImpl.ADD_OKAY) {
1751 return res;
1752 }
1753
1754 // From now on, no exceptions or errors allowed!
1755
1756 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 if (addToken) {
1761 mTokenMap.put(attrs.token, token);
1762 mTokenList.add(token);
1763 }
1764 win.attach();
1765 mWindowMap.put(client.asBinder(), win);
1766
1767 if (attrs.type == TYPE_APPLICATION_STARTING &&
1768 token.appWindowToken != null) {
1769 token.appWindowToken.startingWindow = win;
1770 }
1771
1772 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 if (attrs.type == TYPE_INPUT_METHOD) {
1775 mInputMethodWindow = win;
1776 addInputMethodWindowToListLocked(win);
1777 imMayMove = false;
1778 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1779 mInputMethodDialogs.add(win);
1780 addWindowToListInOrderLocked(win, true);
1781 adjustInputMethodDialogsLocked();
1782 imMayMove = false;
1783 } else {
1784 addWindowToListInOrderLocked(win, true);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001785 if (attrs.type == TYPE_WALLPAPER ||
1786 (attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1787 adjustWallpaperWindowsLocked();
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Romain Guy06882f82009-06-10 13:36:04 -07001790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (mInTouchMode) {
1796 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1797 }
1798 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1799 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1800 }
Romain Guy06882f82009-06-10 13:36:04 -07001801
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001802 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001804 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1805 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 imMayMove = false;
1807 }
1808 }
Romain Guy06882f82009-06-10 13:36:04 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001811 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
Romain Guy06882f82009-06-10 13:36:04 -07001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 assignLayersLocked();
1815 // Don't do layout here, the window must call
1816 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 //dump();
1819
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001820 if (focusChanged) {
1821 if (mCurrentFocus != null) {
1822 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1823 }
1824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (localLOGV) Log.v(
1826 TAG, "New client " + client.asBinder()
1827 + ": window=" + win);
1828 }
1829
1830 // sendNewConfiguration() checks caller permissions so we must call it with
1831 // privilege. updateOrientationFromAppTokens() clears and resets the caller
1832 // identity anyway, so it's safe to just clear & restore around this whole
1833 // block.
1834 final long origId = Binder.clearCallingIdentity();
1835 if (reportNewConfig) {
1836 sendNewConfiguration();
1837 } else {
1838 // Update Orientation after adding a window, only if the window needs to be
1839 // displayed right away
1840 if (win.isVisibleOrAdding()) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07001841 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 sendNewConfiguration();
1843 }
1844 }
1845 }
1846 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 return res;
1849 }
Romain Guy06882f82009-06-10 13:36:04 -07001850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 public void removeWindow(Session session, IWindow client) {
1852 synchronized(mWindowMap) {
1853 WindowState win = windowForClientLocked(session, client);
1854 if (win == null) {
1855 return;
1856 }
1857 removeWindowLocked(session, win);
1858 }
1859 }
Romain Guy06882f82009-06-10 13:36:04 -07001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 public void removeWindowLocked(Session session, WindowState win) {
1862
1863 if (localLOGV || DEBUG_FOCUS) Log.v(
1864 TAG, "Remove " + win + " client="
1865 + Integer.toHexString(System.identityHashCode(
1866 win.mClient.asBinder()))
1867 + ", surface=" + win.mSurface);
1868
1869 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 if (DEBUG_APP_TRANSITIONS) Log.v(
1872 TAG, "Remove " + win + ": mSurface=" + win.mSurface
1873 + " mExiting=" + win.mExiting
1874 + " isAnimating=" + win.isAnimating()
1875 + " app-animation="
1876 + (win.mAppToken != null ? win.mAppToken.animation : null)
1877 + " inPendingTransaction="
1878 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
1879 + " mDisplayFrozen=" + mDisplayFrozen);
1880 // Visibility of the removed window. Will be used later to update orientation later on.
1881 boolean wasVisible = false;
1882 // First, see if we need to run an animation. If we do, we have
1883 // to hold off on removing the window until the animation is done.
1884 // If the display is frozen, just remove immediately, since the
1885 // animation wouldn't be seen.
1886 if (win.mSurface != null && !mDisplayFrozen) {
1887 // If we are not currently running the exit animation, we
1888 // need to see about starting one.
1889 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 int transit = WindowManagerPolicy.TRANSIT_EXIT;
1892 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
1893 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
1894 }
1895 // Try starting an animation.
1896 if (applyAnimationLocked(win, transit, false)) {
1897 win.mExiting = true;
1898 }
1899 }
1900 if (win.mExiting || win.isAnimating()) {
1901 // The exit animation is running... wait for it!
1902 //Log.i(TAG, "*** Running exit animation...");
1903 win.mExiting = true;
1904 win.mRemoveOnExit = true;
1905 mLayoutNeeded = true;
1906 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
1907 performLayoutAndPlaceSurfacesLocked();
1908 if (win.mAppToken != null) {
1909 win.mAppToken.updateReportedVisibilityLocked();
1910 }
1911 //dump();
1912 Binder.restoreCallingIdentity(origId);
1913 return;
1914 }
1915 }
1916
1917 removeWindowInnerLocked(session, win);
1918 // Removing a visible window will effect the computed orientation
1919 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001920 if (wasVisible && computeForcedAppOrientationLocked()
1921 != mForcedAppOrientation) {
1922 mH.sendMessage(mH.obtainMessage(H.COMPUTE_AND_SEND_NEW_CONFIGURATION));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
1925 Binder.restoreCallingIdentity(origId);
1926 }
Romain Guy06882f82009-06-10 13:36:04 -07001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001929 mKeyWaiter.finishedKey(session, win.mClient, true,
1930 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 mKeyWaiter.releasePendingPointerLocked(win.mSession);
1932 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07001933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07001935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (mInputMethodTarget == win) {
1937 moveInputMethodWindowsIfNeededLocked(false);
1938 }
Romain Guy06882f82009-06-10 13:36:04 -07001939
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07001940 if (false) {
1941 RuntimeException e = new RuntimeException("here");
1942 e.fillInStackTrace();
1943 Log.w(TAG, "Removing window " + win, e);
1944 }
1945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 mPolicy.removeWindowLw(win);
1947 win.removeLocked();
1948
1949 mWindowMap.remove(win.mClient.asBinder());
1950 mWindows.remove(win);
1951
1952 if (mInputMethodWindow == win) {
1953 mInputMethodWindow = null;
1954 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
1955 mInputMethodDialogs.remove(win);
1956 }
Romain Guy06882f82009-06-10 13:36:04 -07001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 final WindowToken token = win.mToken;
1959 final AppWindowToken atoken = win.mAppToken;
1960 token.windows.remove(win);
1961 if (atoken != null) {
1962 atoken.allAppWindows.remove(win);
1963 }
1964 if (localLOGV) Log.v(
1965 TAG, "**** Removing window " + win + ": count="
1966 + token.windows.size());
1967 if (token.windows.size() == 0) {
1968 if (!token.explicit) {
1969 mTokenMap.remove(token.token);
1970 mTokenList.remove(token);
1971 } else if (atoken != null) {
1972 atoken.firstWindowDrawn = false;
1973 }
1974 }
1975
1976 if (atoken != null) {
1977 if (atoken.startingWindow == win) {
1978 atoken.startingWindow = null;
1979 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
1980 // If this is the last window and we had requested a starting
1981 // transition window, well there is no point now.
1982 atoken.startingData = null;
1983 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
1984 // If this is the last window except for a starting transition
1985 // window, we need to get rid of the starting transition.
1986 if (DEBUG_STARTING_WINDOW) {
1987 Log.v(TAG, "Schedule remove starting " + token
1988 + ": no more real windows");
1989 }
1990 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
1991 mH.sendMessage(m);
1992 }
1993 }
Romain Guy06882f82009-06-10 13:36:04 -07001994
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001995 if (win.mAttrs.type == TYPE_WALLPAPER ||
1996 (win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
1997 adjustWallpaperWindowsLocked();
1998 }
1999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 if (!mInLayout) {
2001 assignLayersLocked();
2002 mLayoutNeeded = true;
2003 performLayoutAndPlaceSurfacesLocked();
2004 if (win.mAppToken != null) {
2005 win.mAppToken.updateReportedVisibilityLocked();
2006 }
2007 }
2008 }
2009
2010 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2011 long origId = Binder.clearCallingIdentity();
2012 try {
2013 synchronized (mWindowMap) {
2014 WindowState w = windowForClientLocked(session, client);
2015 if ((w != null) && (w.mSurface != null)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002016 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 Surface.openTransaction();
2018 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002019 if (SHOW_TRANSACTIONS) Log.i(
2020 TAG, " SURFACE " + w.mSurface
2021 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 w.mSurface.setTransparentRegionHint(region);
2023 } finally {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002024 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 Surface.closeTransaction();
2026 }
2027 }
2028 }
2029 } finally {
2030 Binder.restoreCallingIdentity(origId);
2031 }
2032 }
2033
2034 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002035 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 Rect visibleInsets) {
2037 long origId = Binder.clearCallingIdentity();
2038 try {
2039 synchronized (mWindowMap) {
2040 WindowState w = windowForClientLocked(session, client);
2041 if (w != null) {
2042 w.mGivenInsetsPending = false;
2043 w.mGivenContentInsets.set(contentInsets);
2044 w.mGivenVisibleInsets.set(visibleInsets);
2045 w.mTouchableInsets = touchableInsets;
2046 mLayoutNeeded = true;
2047 performLayoutAndPlaceSurfacesLocked();
2048 }
2049 }
2050 } finally {
2051 Binder.restoreCallingIdentity(origId);
2052 }
2053 }
Romain Guy06882f82009-06-10 13:36:04 -07002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 public void getWindowDisplayFrame(Session session, IWindow client,
2056 Rect outDisplayFrame) {
2057 synchronized(mWindowMap) {
2058 WindowState win = windowForClientLocked(session, client);
2059 if (win == null) {
2060 outDisplayFrame.setEmpty();
2061 return;
2062 }
2063 outDisplayFrame.set(win.mDisplayFrame);
2064 }
2065 }
2066
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002067 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y) {
2068 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2069 window.mWallpaperX = x;
2070 window.mWallpaperY = y;
2071
2072 if (mWallpaperTarget == window) {
2073 if (updateWallpaperOffsetLocked()) {
2074 performLayoutAndPlaceSurfacesLocked();
2075 }
2076 }
2077 }
2078 }
2079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 public int relayoutWindow(Session session, IWindow client,
2081 WindowManager.LayoutParams attrs, int requestedWidth,
2082 int requestedHeight, int viewVisibility, boolean insetsPending,
2083 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2084 Surface outSurface) {
2085 boolean displayed = false;
2086 boolean inTouchMode;
2087 Configuration newConfig = null;
2088 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 synchronized(mWindowMap) {
2091 WindowState win = windowForClientLocked(session, client);
2092 if (win == null) {
2093 return 0;
2094 }
2095 win.mRequestedWidth = requestedWidth;
2096 win.mRequestedHeight = requestedHeight;
2097
2098 if (attrs != null) {
2099 mPolicy.adjustWindowParamsLw(attrs);
2100 }
Romain Guy06882f82009-06-10 13:36:04 -07002101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 int attrChanges = 0;
2103 int flagChanges = 0;
2104 if (attrs != null) {
2105 flagChanges = win.mAttrs.flags ^= attrs.flags;
2106 attrChanges = win.mAttrs.copyFrom(attrs);
2107 }
2108
2109 if (localLOGV) Log.v(
2110 TAG, "Relayout given client " + client.asBinder()
2111 + " (" + win.mAttrs.getTitle() + ")");
2112
2113
2114 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2115 win.mAlpha = attrs.alpha;
2116 }
2117
2118 final boolean scaledWindow =
2119 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2120
2121 if (scaledWindow) {
2122 // requested{Width|Height} Surface's physical size
2123 // attrs.{width|height} Size on screen
2124 win.mHScale = (attrs.width != requestedWidth) ?
2125 (attrs.width / (float)requestedWidth) : 1.0f;
2126 win.mVScale = (attrs.height != requestedHeight) ?
2127 (attrs.height / (float)requestedHeight) : 1.0f;
2128 }
2129
2130 boolean imMayMove = (flagChanges&(
2131 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2132 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 boolean focusMayChange = win.mViewVisibility != viewVisibility
2135 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2136 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002137
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002138 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2139 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
2140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 win.mRelayoutCalled = true;
2142 final int oldVisibility = win.mViewVisibility;
2143 win.mViewVisibility = viewVisibility;
2144 if (viewVisibility == View.VISIBLE &&
2145 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2146 displayed = !win.isVisibleLw();
2147 if (win.mExiting) {
2148 win.mExiting = false;
2149 win.mAnimation = null;
2150 }
2151 if (win.mDestroying) {
2152 win.mDestroying = false;
2153 mDestroySurface.remove(win);
2154 }
2155 if (oldVisibility == View.GONE) {
2156 win.mEnterAnimationPending = true;
2157 }
2158 if (displayed && win.mSurface != null && !win.mDrawPending
2159 && !win.mCommitDrawPending && !mDisplayFrozen) {
2160 applyEnterAnimationLocked(win);
2161 }
2162 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2163 // To change the format, we need to re-build the surface.
2164 win.destroySurfaceLocked();
2165 displayed = true;
2166 }
2167 try {
2168 Surface surface = win.createSurfaceLocked();
2169 if (surface != null) {
2170 outSurface.copyFrom(surface);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002171 if (SHOW_TRANSACTIONS) Log.i(TAG,
2172 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002174 // For some reason there isn't a surface. Clear the
2175 // caller's object so they see the same state.
2176 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
2178 } catch (Exception e) {
2179 Log.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002180 + client + " (" + win.mAttrs.getTitle() + ")",
2181 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 Binder.restoreCallingIdentity(origId);
2183 return 0;
2184 }
2185 if (displayed) {
2186 focusMayChange = true;
2187 }
2188 if (win.mAttrs.type == TYPE_INPUT_METHOD
2189 && mInputMethodWindow == null) {
2190 mInputMethodWindow = win;
2191 imMayMove = true;
2192 }
2193 } else {
2194 win.mEnterAnimationPending = false;
2195 if (win.mSurface != null) {
2196 // If we are not currently running the exit animation, we
2197 // need to see about starting one.
2198 if (!win.mExiting) {
2199 // Try starting an animation; if there isn't one, we
2200 // can destroy the surface right away.
2201 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2202 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2203 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2204 }
2205 if (win.isWinVisibleLw() &&
2206 applyAnimationLocked(win, transit, false)) {
2207 win.mExiting = true;
2208 mKeyWaiter.finishedKey(session, client, true,
2209 KeyWaiter.RETURN_NOTHING);
2210 } else if (win.isAnimating()) {
2211 // Currently in a hide animation... turn this into
2212 // an exit.
2213 win.mExiting = true;
2214 } else {
2215 if (mInputMethodWindow == win) {
2216 mInputMethodWindow = null;
2217 }
2218 win.destroySurfaceLocked();
2219 }
2220 }
2221 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002222 // We are being called from a local process, which
2223 // means outSurface holds its current surface. Ensure the
2224 // surface object is cleared, but we don't want it actually
2225 // destroyed at this point.
2226 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
2228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 if (focusMayChange) {
2230 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2231 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 imMayMove = false;
2233 }
2234 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2235 }
Romain Guy06882f82009-06-10 13:36:04 -07002236
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002237 // updateFocusedWindowLocked() already assigned layers so we only need to
2238 // reassign them at this point if the IM window state gets shuffled
2239 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 if (imMayMove) {
2242 if (moveInputMethodWindowsIfNeededLocked(false)) {
2243 assignLayers = true;
2244 }
2245 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002246 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002247 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002248 assignLayers = true;
2249 }
2250 }
Romain Guy06882f82009-06-10 13:36:04 -07002251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 mLayoutNeeded = true;
2253 win.mGivenInsetsPending = insetsPending;
2254 if (assignLayers) {
2255 assignLayersLocked();
2256 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002257 newConfig = updateOrientationFromAppTokensLocked(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002259 if (displayed && win.mIsWallpaper) {
2260 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
2261 mDisplay.getHeight());
2262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 if (win.mAppToken != null) {
2264 win.mAppToken.updateReportedVisibilityLocked();
2265 }
2266 outFrame.set(win.mFrame);
2267 outContentInsets.set(win.mContentInsets);
2268 outVisibleInsets.set(win.mVisibleInsets);
2269 if (localLOGV) Log.v(
2270 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002271 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 + ", requestedHeight=" + requestedHeight
2273 + ", viewVisibility=" + viewVisibility
2274 + "\nRelayout returning frame=" + outFrame
2275 + ", surface=" + outSurface);
2276
2277 if (localLOGV || DEBUG_FOCUS) Log.v(
2278 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2279
2280 inTouchMode = mInTouchMode;
2281 }
2282
2283 if (newConfig != null) {
2284 sendNewConfiguration();
2285 }
Romain Guy06882f82009-06-10 13:36:04 -07002286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2290 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2291 }
2292
2293 public void finishDrawingWindow(Session session, IWindow client) {
2294 final long origId = Binder.clearCallingIdentity();
2295 synchronized(mWindowMap) {
2296 WindowState win = windowForClientLocked(session, client);
2297 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002298 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2299 adjustWallpaperWindowsLocked();
2300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 mLayoutNeeded = true;
2302 performLayoutAndPlaceSurfacesLocked();
2303 }
2304 }
2305 Binder.restoreCallingIdentity(origId);
2306 }
2307
2308 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
2309 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: params package="
2310 + (lp != null ? lp.packageName : null)
2311 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2312 if (lp != null && lp.windowAnimations != 0) {
2313 // If this is a system resource, don't try to load it from the
2314 // application resources. It is nice to avoid loading application
2315 // resources if we can.
2316 String packageName = lp.packageName != null ? lp.packageName : "android";
2317 int resId = lp.windowAnimations;
2318 if ((resId&0xFF000000) == 0x01000000) {
2319 packageName = "android";
2320 }
2321 if (DEBUG_ANIM) Log.v(TAG, "Loading animations: picked package="
2322 + packageName);
2323 return AttributeCache.instance().get(packageName, resId,
2324 com.android.internal.R.styleable.WindowAnimation);
2325 }
2326 return null;
2327 }
Romain Guy06882f82009-06-10 13:36:04 -07002328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 private void applyEnterAnimationLocked(WindowState win) {
2330 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2331 if (win.mEnterAnimationPending) {
2332 win.mEnterAnimationPending = false;
2333 transit = WindowManagerPolicy.TRANSIT_ENTER;
2334 }
2335
2336 applyAnimationLocked(win, transit, true);
2337 }
2338
2339 private boolean applyAnimationLocked(WindowState win,
2340 int transit, boolean isEntrance) {
2341 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2342 // If we are trying to apply an animation, but already running
2343 // an animation of the same type, then just leave that one alone.
2344 return true;
2345 }
Romain Guy06882f82009-06-10 13:36:04 -07002346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 // Only apply an animation if the display isn't frozen. If it is
2348 // frozen, there is no reason to animate and it can cause strange
2349 // artifacts when we unfreeze the display if some different animation
2350 // is running.
2351 if (!mDisplayFrozen) {
2352 int anim = mPolicy.selectAnimationLw(win, transit);
2353 int attr = -1;
2354 Animation a = null;
2355 if (anim != 0) {
2356 a = AnimationUtils.loadAnimation(mContext, anim);
2357 } else {
2358 switch (transit) {
2359 case WindowManagerPolicy.TRANSIT_ENTER:
2360 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2361 break;
2362 case WindowManagerPolicy.TRANSIT_EXIT:
2363 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2364 break;
2365 case WindowManagerPolicy.TRANSIT_SHOW:
2366 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2367 break;
2368 case WindowManagerPolicy.TRANSIT_HIDE:
2369 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2370 break;
2371 }
2372 if (attr >= 0) {
2373 a = loadAnimation(win.mAttrs, attr);
2374 }
2375 }
2376 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: win=" + win
2377 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2378 + " mAnimation=" + win.mAnimation
2379 + " isEntrance=" + isEntrance);
2380 if (a != null) {
2381 if (DEBUG_ANIM) {
2382 RuntimeException e = new RuntimeException();
2383 e.fillInStackTrace();
2384 Log.v(TAG, "Loaded animation " + a + " for " + win, e);
2385 }
2386 win.setAnimation(a);
2387 win.mAnimationIsEntrance = isEntrance;
2388 }
2389 } else {
2390 win.clearAnimation();
2391 }
2392
2393 return win.mAnimation != null;
2394 }
2395
2396 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2397 int anim = 0;
2398 Context context = mContext;
2399 if (animAttr >= 0) {
2400 AttributeCache.Entry ent = getCachedAnimations(lp);
2401 if (ent != null) {
2402 context = ent.context;
2403 anim = ent.array.getResourceId(animAttr, 0);
2404 }
2405 }
2406 if (anim != 0) {
2407 return AnimationUtils.loadAnimation(context, anim);
2408 }
2409 return null;
2410 }
Romain Guy06882f82009-06-10 13:36:04 -07002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 private boolean applyAnimationLocked(AppWindowToken wtoken,
2413 WindowManager.LayoutParams lp, int transit, boolean enter) {
2414 // Only apply an animation if the display isn't frozen. If it is
2415 // frozen, there is no reason to animate and it can cause strange
2416 // artifacts when we unfreeze the display if some different animation
2417 // is running.
2418 if (!mDisplayFrozen) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002419 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002420 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002421 a = new FadeInOutAnimation(enter);
2422 if (DEBUG_ANIM) Log.v(TAG,
2423 "applying FadeInOutAnimation for a window in compatibility mode");
2424 } else {
2425 int animAttr = 0;
2426 switch (transit) {
2427 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2428 animAttr = enter
2429 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2430 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2431 break;
2432 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2433 animAttr = enter
2434 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2435 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2436 break;
2437 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2438 animAttr = enter
2439 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2440 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2441 break;
2442 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2443 animAttr = enter
2444 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2445 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2446 break;
2447 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2448 animAttr = enter
2449 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2450 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2451 break;
2452 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2453 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002454 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002455 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2456 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002457 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002458 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002459 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2460 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002461 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002462 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002463 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002464 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2465 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2466 break;
2467 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2468 animAttr = enter
2469 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2470 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2471 break;
2472 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2473 animAttr = enter
2474 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2475 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002476 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002477 }
2478 a = loadAnimation(lp, animAttr);
2479 if (DEBUG_ANIM) Log.v(TAG, "applyAnimation: wtoken=" + wtoken
2480 + " anim=" + a
2481 + " animAttr=0x" + Integer.toHexString(animAttr)
2482 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 if (a != null) {
2485 if (DEBUG_ANIM) {
2486 RuntimeException e = new RuntimeException();
2487 e.fillInStackTrace();
2488 Log.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
2489 }
2490 wtoken.setAnimation(a);
2491 }
2492 } else {
2493 wtoken.clearAnimation();
2494 }
2495
2496 return wtoken.animation != null;
2497 }
2498
2499 // -------------------------------------------------------------
2500 // Application Window Tokens
2501 // -------------------------------------------------------------
2502
2503 public void validateAppTokens(List tokens) {
2504 int v = tokens.size()-1;
2505 int m = mAppTokens.size()-1;
2506 while (v >= 0 && m >= 0) {
2507 AppWindowToken wtoken = mAppTokens.get(m);
2508 if (wtoken.removed) {
2509 m--;
2510 continue;
2511 }
2512 if (tokens.get(v) != wtoken.token) {
2513 Log.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
2514 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2515 }
2516 v--;
2517 m--;
2518 }
2519 while (v >= 0) {
2520 Log.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
2521 v--;
2522 }
2523 while (m >= 0) {
2524 AppWindowToken wtoken = mAppTokens.get(m);
2525 if (!wtoken.removed) {
2526 Log.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
2527 }
2528 m--;
2529 }
2530 }
2531
2532 boolean checkCallingPermission(String permission, String func) {
2533 // Quick check: if the calling permission is me, it's all okay.
2534 if (Binder.getCallingPid() == Process.myPid()) {
2535 return true;
2536 }
Romain Guy06882f82009-06-10 13:36:04 -07002537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 if (mContext.checkCallingPermission(permission)
2539 == PackageManager.PERMISSION_GRANTED) {
2540 return true;
2541 }
2542 String msg = "Permission Denial: " + func + " from pid="
2543 + Binder.getCallingPid()
2544 + ", uid=" + Binder.getCallingUid()
2545 + " requires " + permission;
2546 Log.w(TAG, msg);
2547 return false;
2548 }
Romain Guy06882f82009-06-10 13:36:04 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 AppWindowToken findAppWindowToken(IBinder token) {
2551 WindowToken wtoken = mTokenMap.get(token);
2552 if (wtoken == null) {
2553 return null;
2554 }
2555 return wtoken.appWindowToken;
2556 }
Romain Guy06882f82009-06-10 13:36:04 -07002557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 public void addWindowToken(IBinder token, int type) {
2559 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2560 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002561 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 }
Romain Guy06882f82009-06-10 13:36:04 -07002563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 synchronized(mWindowMap) {
2565 WindowToken wtoken = mTokenMap.get(token);
2566 if (wtoken != null) {
2567 Log.w(TAG, "Attempted to add existing input method token: " + token);
2568 return;
2569 }
2570 wtoken = new WindowToken(token, type, true);
2571 mTokenMap.put(token, wtoken);
2572 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002573 if (type == TYPE_WALLPAPER) {
2574 mWallpaperTokens.add(wtoken);
2575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
2577 }
Romain Guy06882f82009-06-10 13:36:04 -07002578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 public void removeWindowToken(IBinder token) {
2580 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2581 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002582 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584
2585 final long origId = Binder.clearCallingIdentity();
2586 synchronized(mWindowMap) {
2587 WindowToken wtoken = mTokenMap.remove(token);
2588 mTokenList.remove(wtoken);
2589 if (wtoken != null) {
2590 boolean delayed = false;
2591 if (!wtoken.hidden) {
2592 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 final int N = wtoken.windows.size();
2595 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 for (int i=0; i<N; i++) {
2598 WindowState win = wtoken.windows.get(i);
2599
2600 if (win.isAnimating()) {
2601 delayed = true;
2602 }
Romain Guy06882f82009-06-10 13:36:04 -07002603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 if (win.isVisibleNow()) {
2605 applyAnimationLocked(win,
2606 WindowManagerPolicy.TRANSIT_EXIT, false);
2607 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2608 KeyWaiter.RETURN_NOTHING);
2609 changed = true;
2610 }
2611 }
2612
2613 if (changed) {
2614 mLayoutNeeded = true;
2615 performLayoutAndPlaceSurfacesLocked();
2616 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2617 }
Romain Guy06882f82009-06-10 13:36:04 -07002618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 if (delayed) {
2620 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002621 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2622 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 }
2624 }
Romain Guy06882f82009-06-10 13:36:04 -07002625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 } else {
2627 Log.w(TAG, "Attempted to remove non-existing token: " + token);
2628 }
2629 }
2630 Binder.restoreCallingIdentity(origId);
2631 }
2632
2633 public void addAppToken(int addPos, IApplicationToken token,
2634 int groupId, int requestedOrientation, boolean fullscreen) {
2635 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2636 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002637 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
Romain Guy06882f82009-06-10 13:36:04 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 synchronized(mWindowMap) {
2641 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2642 if (wtoken != null) {
2643 Log.w(TAG, "Attempted to add existing app token: " + token);
2644 return;
2645 }
2646 wtoken = new AppWindowToken(token);
2647 wtoken.groupId = groupId;
2648 wtoken.appFullscreen = fullscreen;
2649 wtoken.requestedOrientation = requestedOrientation;
2650 mAppTokens.add(addPos, wtoken);
Dave Bortcfe65242009-04-09 14:51:04 -07002651 if (localLOGV) Log.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 mTokenMap.put(token.asBinder(), wtoken);
2653 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 // Application tokens start out hidden.
2656 wtoken.hidden = true;
2657 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 //dump();
2660 }
2661 }
Romain Guy06882f82009-06-10 13:36:04 -07002662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 public void setAppGroupId(IBinder token, int groupId) {
2664 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2665 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002666 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
2668
2669 synchronized(mWindowMap) {
2670 AppWindowToken wtoken = findAppWindowToken(token);
2671 if (wtoken == null) {
2672 Log.w(TAG, "Attempted to set group id of non-existing app token: " + token);
2673 return;
2674 }
2675 wtoken.groupId = groupId;
2676 }
2677 }
Romain Guy06882f82009-06-10 13:36:04 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 public int getOrientationFromWindowsLocked() {
2680 int pos = mWindows.size() - 1;
2681 while (pos >= 0) {
2682 WindowState wtoken = (WindowState) mWindows.get(pos);
2683 pos--;
2684 if (wtoken.mAppToken != null) {
2685 // We hit an application window. so the orientation will be determined by the
2686 // app window. No point in continuing further.
2687 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2688 }
2689 if (!wtoken.isVisibleLw()) {
2690 continue;
2691 }
2692 int req = wtoken.mAttrs.screenOrientation;
2693 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2694 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2695 continue;
2696 } else {
2697 return req;
2698 }
2699 }
2700 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2701 }
Romain Guy06882f82009-06-10 13:36:04 -07002702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 public int getOrientationFromAppTokensLocked() {
2704 int pos = mAppTokens.size() - 1;
2705 int curGroup = 0;
2706 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Owen Lin3413b892009-05-01 17:12:32 -07002707 boolean findingBehind = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 boolean haveGroup = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002709 boolean lastFullscreen = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 while (pos >= 0) {
2711 AppWindowToken wtoken = mAppTokens.get(pos);
2712 pos--;
Owen Lin3413b892009-05-01 17:12:32 -07002713 // if we're about to tear down this window and not seek for
2714 // the behind activity, don't use it for orientation
2715 if (!findingBehind
2716 && (!wtoken.hidden && wtoken.hiddenRequested)) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002717 continue;
2718 }
2719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 if (!haveGroup) {
2721 // We ignore any hidden applications on the top.
2722 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
2723 continue;
2724 }
2725 haveGroup = true;
2726 curGroup = wtoken.groupId;
2727 lastOrientation = wtoken.requestedOrientation;
2728 } else if (curGroup != wtoken.groupId) {
2729 // If we have hit a new application group, and the bottom
2730 // of the previous group didn't explicitly say to use
The Android Open Source Project4df24232009-03-05 14:34:35 -08002731 // the orientation behind it, and the last app was
2732 // full screen, then we'll stick with the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 // user's orientation.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002734 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
2735 && lastFullscreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 return lastOrientation;
2737 }
2738 }
2739 int or = wtoken.requestedOrientation;
Owen Lin3413b892009-05-01 17:12:32 -07002740 // If this application is fullscreen, and didn't explicitly say
2741 // to use the orientation behind it, then just take whatever
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 // orientation it has and ignores whatever is under it.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002743 lastFullscreen = wtoken.appFullscreen;
Romain Guy06882f82009-06-10 13:36:04 -07002744 if (lastFullscreen
Owen Lin3413b892009-05-01 17:12:32 -07002745 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 return or;
2747 }
2748 // If this application has requested an explicit orientation,
2749 // then use it.
2750 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
2751 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
2752 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
2753 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
2754 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
2755 return or;
2756 }
Owen Lin3413b892009-05-01 17:12:32 -07002757 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
2759 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2760 }
Romain Guy06882f82009-06-10 13:36:04 -07002761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002763 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002764 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2765 "updateOrientationFromAppTokens()")) {
2766 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
2767 }
2768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 Configuration config;
2770 long ident = Binder.clearCallingIdentity();
Dianne Hackborncfaef692009-06-15 14:24:44 -07002771 config = updateOrientationFromAppTokensUnchecked(currentConfig,
2772 freezeThisOneIfNeeded);
2773 Binder.restoreCallingIdentity(ident);
2774 return config;
2775 }
2776
2777 Configuration updateOrientationFromAppTokensUnchecked(
2778 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
2779 Configuration config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 synchronized(mWindowMap) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002781 config = updateOrientationFromAppTokensLocked(currentConfig, freezeThisOneIfNeeded);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002782 if (config != null) {
2783 mLayoutNeeded = true;
2784 performLayoutAndPlaceSurfacesLocked();
2785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 return config;
2788 }
Romain Guy06882f82009-06-10 13:36:04 -07002789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 /*
2791 * The orientation is computed from non-application windows first. If none of
2792 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07002793 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
2795 * android.os.IBinder)
2796 */
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002797 Configuration updateOrientationFromAppTokensLocked(
The Android Open Source Project10592532009-03-18 17:39:46 -07002798 Configuration appConfig, IBinder freezeThisOneIfNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 long ident = Binder.clearCallingIdentity();
2801 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002802 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07002803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 if (req != mForcedAppOrientation) {
2805 changed = true;
2806 mForcedAppOrientation = req;
2807 //send a message to Policy indicating orientation change to take
2808 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002809 mPolicy.setCurrentOrientationLw(req);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
Romain Guy06882f82009-06-10 13:36:04 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 if (changed) {
2813 changed = setRotationUncheckedLocked(
Dianne Hackborn321ae682009-03-27 16:16:03 -07002814 WindowManagerPolicy.USE_LAST_ROTATION,
2815 mLastRotationFlags & (~Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 if (changed) {
2817 if (freezeThisOneIfNeeded != null) {
2818 AppWindowToken wtoken = findAppWindowToken(
2819 freezeThisOneIfNeeded);
2820 if (wtoken != null) {
2821 startAppFreezingScreenLocked(wtoken,
2822 ActivityInfo.CONFIG_ORIENTATION);
2823 }
2824 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07002825 return computeNewConfigurationLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002828
2829 // No obvious action we need to take, but if our current
2830 // state mismatches the activity maanager's, update it
2831 if (appConfig != null) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002832 mTempConfiguration.setToDefaults();
2833 if (computeNewConfigurationLocked(mTempConfiguration)) {
2834 if (appConfig.diff(mTempConfiguration) != 0) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07002835 return new Configuration(mTempConfiguration);
2836 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002837 }
2838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 } finally {
2840 Binder.restoreCallingIdentity(ident);
2841 }
Romain Guy06882f82009-06-10 13:36:04 -07002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 return null;
2844 }
Romain Guy06882f82009-06-10 13:36:04 -07002845
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002846 int computeForcedAppOrientationLocked() {
2847 int req = getOrientationFromWindowsLocked();
2848 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
2849 req = getOrientationFromAppTokensLocked();
2850 }
2851 return req;
2852 }
Romain Guy06882f82009-06-10 13:36:04 -07002853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
2855 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2856 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002857 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
Romain Guy06882f82009-06-10 13:36:04 -07002859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 synchronized(mWindowMap) {
2861 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2862 if (wtoken == null) {
2863 Log.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
2864 return;
2865 }
Romain Guy06882f82009-06-10 13:36:04 -07002866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 wtoken.requestedOrientation = requestedOrientation;
2868 }
2869 }
Romain Guy06882f82009-06-10 13:36:04 -07002870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 public int getAppOrientation(IApplicationToken token) {
2872 synchronized(mWindowMap) {
2873 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2874 if (wtoken == null) {
2875 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2876 }
Romain Guy06882f82009-06-10 13:36:04 -07002877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 return wtoken.requestedOrientation;
2879 }
2880 }
Romain Guy06882f82009-06-10 13:36:04 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
2883 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2884 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002885 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 }
2887
2888 synchronized(mWindowMap) {
2889 boolean changed = false;
2890 if (token == null) {
2891 if (DEBUG_FOCUS) Log.v(TAG, "Clearing focused app, was " + mFocusedApp);
2892 changed = mFocusedApp != null;
2893 mFocusedApp = null;
2894 mKeyWaiter.tickle();
2895 } else {
2896 AppWindowToken newFocus = findAppWindowToken(token);
2897 if (newFocus == null) {
2898 Log.w(TAG, "Attempted to set focus to non-existing app token: " + token);
2899 return;
2900 }
2901 changed = mFocusedApp != newFocus;
2902 mFocusedApp = newFocus;
2903 if (DEBUG_FOCUS) Log.v(TAG, "Set focused app to: " + mFocusedApp);
2904 mKeyWaiter.tickle();
2905 }
2906
2907 if (moveFocusNow && changed) {
2908 final long origId = Binder.clearCallingIdentity();
2909 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2910 Binder.restoreCallingIdentity(origId);
2911 }
2912 }
2913 }
2914
2915 public void prepareAppTransition(int transit) {
2916 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2917 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002918 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 }
Romain Guy06882f82009-06-10 13:36:04 -07002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 synchronized(mWindowMap) {
2922 if (DEBUG_APP_TRANSITIONS) Log.v(
2923 TAG, "Prepare app transition: transit=" + transit
2924 + " mNextAppTransition=" + mNextAppTransition);
2925 if (!mDisplayFrozen) {
2926 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
2927 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002928 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
2929 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
2930 // Opening a new task always supersedes a close for the anim.
2931 mNextAppTransition = transit;
2932 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2933 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
2934 // Opening a new activity always supersedes a close for the anim.
2935 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937 mAppTransitionReady = false;
2938 mAppTransitionTimeout = false;
2939 mStartingIconInTransition = false;
2940 mSkipAppTransitionAnimation = false;
2941 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
2942 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
2943 5000);
2944 }
2945 }
2946 }
2947
2948 public int getPendingAppTransition() {
2949 return mNextAppTransition;
2950 }
Romain Guy06882f82009-06-10 13:36:04 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 public void executeAppTransition() {
2953 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2954 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002955 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
Romain Guy06882f82009-06-10 13:36:04 -07002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 synchronized(mWindowMap) {
2959 if (DEBUG_APP_TRANSITIONS) Log.v(
2960 TAG, "Execute app transition: mNextAppTransition=" + mNextAppTransition);
2961 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
2962 mAppTransitionReady = true;
2963 final long origId = Binder.clearCallingIdentity();
2964 performLayoutAndPlaceSurfacesLocked();
2965 Binder.restoreCallingIdentity(origId);
2966 }
2967 }
2968 }
2969
2970 public void setAppStartingWindow(IBinder token, String pkg,
2971 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
2972 IBinder transferFrom, boolean createIfNeeded) {
2973 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2974 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002975 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 }
2977
2978 synchronized(mWindowMap) {
2979 if (DEBUG_STARTING_WINDOW) Log.v(
2980 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
2981 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07002982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 AppWindowToken wtoken = findAppWindowToken(token);
2984 if (wtoken == null) {
2985 Log.w(TAG, "Attempted to set icon of non-existing app token: " + token);
2986 return;
2987 }
2988
2989 // If the display is frozen, we won't do anything until the
2990 // actual window is displayed so there is no reason to put in
2991 // the starting window.
2992 if (mDisplayFrozen) {
2993 return;
2994 }
Romain Guy06882f82009-06-10 13:36:04 -07002995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 if (wtoken.startingData != null) {
2997 return;
2998 }
Romain Guy06882f82009-06-10 13:36:04 -07002999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 if (transferFrom != null) {
3001 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3002 if (ttoken != null) {
3003 WindowState startingWindow = ttoken.startingWindow;
3004 if (startingWindow != null) {
3005 if (mStartingIconInTransition) {
3006 // In this case, the starting icon has already
3007 // been displayed, so start letting windows get
3008 // shown immediately without any more transitions.
3009 mSkipAppTransitionAnimation = true;
3010 }
3011 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3012 "Moving existing starting from " + ttoken
3013 + " to " + wtoken);
3014 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 // Transfer the starting window over to the new
3017 // token.
3018 wtoken.startingData = ttoken.startingData;
3019 wtoken.startingView = ttoken.startingView;
3020 wtoken.startingWindow = startingWindow;
3021 ttoken.startingData = null;
3022 ttoken.startingView = null;
3023 ttoken.startingWindow = null;
3024 ttoken.startingMoved = true;
3025 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003026 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 startingWindow.mAppToken = wtoken;
3028 mWindows.remove(startingWindow);
3029 ttoken.windows.remove(startingWindow);
3030 ttoken.allAppWindows.remove(startingWindow);
3031 addWindowToListInOrderLocked(startingWindow, true);
3032 wtoken.allAppWindows.add(startingWindow);
Romain Guy06882f82009-06-10 13:36:04 -07003033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 // Propagate other interesting state between the
3035 // tokens. If the old token is displayed, we should
3036 // immediately force the new one to be displayed. If
3037 // it is animating, we need to move that animation to
3038 // the new one.
3039 if (ttoken.allDrawn) {
3040 wtoken.allDrawn = true;
3041 }
3042 if (ttoken.firstWindowDrawn) {
3043 wtoken.firstWindowDrawn = true;
3044 }
3045 if (!ttoken.hidden) {
3046 wtoken.hidden = false;
3047 wtoken.hiddenRequested = false;
3048 wtoken.willBeHidden = false;
3049 }
3050 if (wtoken.clientHidden != ttoken.clientHidden) {
3051 wtoken.clientHidden = ttoken.clientHidden;
3052 wtoken.sendAppVisibilityToClients();
3053 }
3054 if (ttoken.animation != null) {
3055 wtoken.animation = ttoken.animation;
3056 wtoken.animating = ttoken.animating;
3057 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3058 ttoken.animation = null;
3059 ttoken.animLayerAdjustment = 0;
3060 wtoken.updateLayers();
3061 ttoken.updateLayers();
3062 }
Romain Guy06882f82009-06-10 13:36:04 -07003063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 mLayoutNeeded = true;
3066 performLayoutAndPlaceSurfacesLocked();
3067 Binder.restoreCallingIdentity(origId);
3068 return;
3069 } else if (ttoken.startingData != null) {
3070 // The previous app was getting ready to show a
3071 // starting window, but hasn't yet done so. Steal it!
3072 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
3073 "Moving pending starting from " + ttoken
3074 + " to " + wtoken);
3075 wtoken.startingData = ttoken.startingData;
3076 ttoken.startingData = null;
3077 ttoken.startingMoved = true;
3078 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3079 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3080 // want to process the message ASAP, before any other queued
3081 // messages.
3082 mH.sendMessageAtFrontOfQueue(m);
3083 return;
3084 }
3085 }
3086 }
3087
3088 // There is no existing starting window, and the caller doesn't
3089 // want us to create one, so that's it!
3090 if (!createIfNeeded) {
3091 return;
3092 }
Romain Guy06882f82009-06-10 13:36:04 -07003093
Dianne Hackborn284ac932009-08-28 10:34:25 -07003094 // If this is a translucent or wallpaper window, then don't
3095 // show a starting window -- the current effect (a full-screen
3096 // opaque starting window that fades away to the real contents
3097 // when it is ready) does not work for this.
3098 if (theme != 0) {
3099 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3100 com.android.internal.R.styleable.Window);
3101 if (ent.array.getBoolean(
3102 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3103 return;
3104 }
3105 if (ent.array.getBoolean(
3106 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3107 return;
3108 }
3109 }
3110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 mStartingIconInTransition = true;
3112 wtoken.startingData = new StartingData(
3113 pkg, theme, nonLocalizedLabel,
3114 labelRes, icon);
3115 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3116 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3117 // want to process the message ASAP, before any other queued
3118 // messages.
3119 mH.sendMessageAtFrontOfQueue(m);
3120 }
3121 }
3122
3123 public void setAppWillBeHidden(IBinder token) {
3124 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3125 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003126 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 }
3128
3129 AppWindowToken wtoken;
3130
3131 synchronized(mWindowMap) {
3132 wtoken = findAppWindowToken(token);
3133 if (wtoken == null) {
3134 Log.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
3135 return;
3136 }
3137 wtoken.willBeHidden = true;
3138 }
3139 }
Romain Guy06882f82009-06-10 13:36:04 -07003140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3142 boolean visible, int transit, boolean performLayout) {
3143 boolean delayed = false;
3144
3145 if (wtoken.clientHidden == visible) {
3146 wtoken.clientHidden = !visible;
3147 wtoken.sendAppVisibilityToClients();
3148 }
Romain Guy06882f82009-06-10 13:36:04 -07003149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 wtoken.willBeHidden = false;
3151 if (wtoken.hidden == visible) {
3152 final int N = wtoken.allAppWindows.size();
3153 boolean changed = false;
3154 if (DEBUG_APP_TRANSITIONS) Log.v(
3155 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3156 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 if (transit != WindowManagerPolicy.TRANSIT_NONE) {
3161 if (wtoken.animation == sDummyAnimation) {
3162 wtoken.animation = null;
3163 }
3164 applyAnimationLocked(wtoken, lp, transit, visible);
3165 changed = true;
3166 if (wtoken.animation != null) {
3167 delayed = runningAppAnimation = true;
3168 }
3169 }
Romain Guy06882f82009-06-10 13:36:04 -07003170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 for (int i=0; i<N; i++) {
3172 WindowState win = wtoken.allAppWindows.get(i);
3173 if (win == wtoken.startingWindow) {
3174 continue;
3175 }
3176
3177 if (win.isAnimating()) {
3178 delayed = true;
3179 }
Romain Guy06882f82009-06-10 13:36:04 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 //Log.i(TAG, "Window " + win + ": vis=" + win.isVisible());
3182 //win.dump(" ");
3183 if (visible) {
3184 if (!win.isVisibleNow()) {
3185 if (!runningAppAnimation) {
3186 applyAnimationLocked(win,
3187 WindowManagerPolicy.TRANSIT_ENTER, true);
3188 }
3189 changed = true;
3190 }
3191 } else if (win.isVisibleNow()) {
3192 if (!runningAppAnimation) {
3193 applyAnimationLocked(win,
3194 WindowManagerPolicy.TRANSIT_EXIT, false);
3195 }
3196 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3197 KeyWaiter.RETURN_NOTHING);
3198 changed = true;
3199 }
3200 }
3201
3202 wtoken.hidden = wtoken.hiddenRequested = !visible;
3203 if (!visible) {
3204 unsetAppFreezingScreenLocked(wtoken, true, true);
3205 } else {
3206 // If we are being set visible, and the starting window is
3207 // not yet displayed, then make sure it doesn't get displayed.
3208 WindowState swin = wtoken.startingWindow;
3209 if (swin != null && (swin.mDrawPending
3210 || swin.mCommitDrawPending)) {
3211 swin.mPolicyVisibility = false;
3212 swin.mPolicyVisibilityAfterAnim = false;
3213 }
3214 }
Romain Guy06882f82009-06-10 13:36:04 -07003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "setTokenVisibilityLocked: " + wtoken
3217 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3218 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 if (changed && performLayout) {
3221 mLayoutNeeded = true;
3222 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 performLayoutAndPlaceSurfacesLocked();
3224 }
3225 }
3226
3227 if (wtoken.animation != null) {
3228 delayed = true;
3229 }
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 return delayed;
3232 }
3233
3234 public void setAppVisibility(IBinder token, boolean visible) {
3235 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3236 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003237 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239
3240 AppWindowToken wtoken;
3241
3242 synchronized(mWindowMap) {
3243 wtoken = findAppWindowToken(token);
3244 if (wtoken == null) {
3245 Log.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
3246 return;
3247 }
3248
3249 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3250 RuntimeException e = new RuntimeException();
3251 e.fillInStackTrace();
3252 Log.v(TAG, "setAppVisibility(" + token + ", " + visible
3253 + "): mNextAppTransition=" + mNextAppTransition
3254 + " hidden=" + wtoken.hidden
3255 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3256 }
Romain Guy06882f82009-06-10 13:36:04 -07003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 // If we are preparing an app transition, then delay changing
3259 // the visibility of this token until we execute that transition.
3260 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3261 // Already in requested state, don't do anything more.
3262 if (wtoken.hiddenRequested != visible) {
3263 return;
3264 }
3265 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 if (DEBUG_APP_TRANSITIONS) Log.v(
3268 TAG, "Setting dummy animation on: " + wtoken);
3269 wtoken.setDummyAnimation();
3270 mOpeningApps.remove(wtoken);
3271 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003272 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 wtoken.inPendingTransaction = true;
3274 if (visible) {
3275 mOpeningApps.add(wtoken);
3276 wtoken.allDrawn = false;
3277 wtoken.startingDisplayed = false;
3278 wtoken.startingMoved = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07003279 wtoken.waitingToShow = true;
Romain Guy06882f82009-06-10 13:36:04 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 if (wtoken.clientHidden) {
3282 // In the case where we are making an app visible
3283 // but holding off for a transition, we still need
3284 // to tell the client to make its windows visible so
3285 // they get drawn. Otherwise, we will wait on
3286 // performing the transition until all windows have
3287 // been drawn, they never will be, and we are sad.
3288 wtoken.clientHidden = false;
3289 wtoken.sendAppVisibilityToClients();
3290 }
3291 } else {
3292 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003293 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 }
3295 return;
3296 }
Romain Guy06882f82009-06-10 13:36:04 -07003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 final long origId = Binder.clearCallingIdentity();
3299 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_NONE, true);
3300 wtoken.updateReportedVisibilityLocked();
3301 Binder.restoreCallingIdentity(origId);
3302 }
3303 }
3304
3305 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3306 boolean unfreezeSurfaceNow, boolean force) {
3307 if (wtoken.freezingScreen) {
3308 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + wtoken
3309 + " force=" + force);
3310 final int N = wtoken.allAppWindows.size();
3311 boolean unfrozeWindows = false;
3312 for (int i=0; i<N; i++) {
3313 WindowState w = wtoken.allAppWindows.get(i);
3314 if (w.mAppFreezing) {
3315 w.mAppFreezing = false;
3316 if (w.mSurface != null && !w.mOrientationChanging) {
3317 w.mOrientationChanging = true;
3318 }
3319 unfrozeWindows = true;
3320 }
3321 }
3322 if (force || unfrozeWindows) {
3323 if (DEBUG_ORIENTATION) Log.v(TAG, "No longer freezing: " + wtoken);
3324 wtoken.freezingScreen = false;
3325 mAppsFreezingScreen--;
3326 }
3327 if (unfreezeSurfaceNow) {
3328 if (unfrozeWindows) {
3329 mLayoutNeeded = true;
3330 performLayoutAndPlaceSurfacesLocked();
3331 }
3332 if (mAppsFreezingScreen == 0 && !mWindowsFreezingScreen) {
3333 stopFreezingDisplayLocked();
3334 }
3335 }
3336 }
3337 }
Romain Guy06882f82009-06-10 13:36:04 -07003338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3340 int configChanges) {
3341 if (DEBUG_ORIENTATION) {
3342 RuntimeException e = new RuntimeException();
3343 e.fillInStackTrace();
3344 Log.i(TAG, "Set freezing of " + wtoken.appToken
3345 + ": hidden=" + wtoken.hidden + " freezing="
3346 + wtoken.freezingScreen, e);
3347 }
3348 if (!wtoken.hiddenRequested) {
3349 if (!wtoken.freezingScreen) {
3350 wtoken.freezingScreen = true;
3351 mAppsFreezingScreen++;
3352 if (mAppsFreezingScreen == 1) {
3353 startFreezingDisplayLocked();
3354 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3355 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3356 5000);
3357 }
3358 }
3359 final int N = wtoken.allAppWindows.size();
3360 for (int i=0; i<N; i++) {
3361 WindowState w = wtoken.allAppWindows.get(i);
3362 w.mAppFreezing = true;
3363 }
3364 }
3365 }
Romain Guy06882f82009-06-10 13:36:04 -07003366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 public void startAppFreezingScreen(IBinder token, int configChanges) {
3368 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3369 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003370 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
3372
3373 synchronized(mWindowMap) {
3374 if (configChanges == 0 && !mDisplayFrozen) {
3375 if (DEBUG_ORIENTATION) Log.v(TAG, "Skipping set freeze of " + token);
3376 return;
3377 }
Romain Guy06882f82009-06-10 13:36:04 -07003378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 AppWindowToken wtoken = findAppWindowToken(token);
3380 if (wtoken == null || wtoken.appToken == null) {
3381 Log.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
3382 return;
3383 }
3384 final long origId = Binder.clearCallingIdentity();
3385 startAppFreezingScreenLocked(wtoken, configChanges);
3386 Binder.restoreCallingIdentity(origId);
3387 }
3388 }
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 public void stopAppFreezingScreen(IBinder token, boolean force) {
3391 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3392 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003393 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 }
3395
3396 synchronized(mWindowMap) {
3397 AppWindowToken wtoken = findAppWindowToken(token);
3398 if (wtoken == null || wtoken.appToken == null) {
3399 return;
3400 }
3401 final long origId = Binder.clearCallingIdentity();
3402 if (DEBUG_ORIENTATION) Log.v(TAG, "Clear freezing of " + token
3403 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3404 unsetAppFreezingScreenLocked(wtoken, true, force);
3405 Binder.restoreCallingIdentity(origId);
3406 }
3407 }
Romain Guy06882f82009-06-10 13:36:04 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 public void removeAppToken(IBinder token) {
3410 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3411 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003412 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414
3415 AppWindowToken wtoken = null;
3416 AppWindowToken startingToken = null;
3417 boolean delayed = false;
3418
3419 final long origId = Binder.clearCallingIdentity();
3420 synchronized(mWindowMap) {
3421 WindowToken basewtoken = mTokenMap.remove(token);
3422 mTokenList.remove(basewtoken);
3423 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
3424 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "Removing app token: " + wtoken);
3425 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_NONE, true);
3426 wtoken.inPendingTransaction = false;
3427 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003428 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 if (mClosingApps.contains(wtoken)) {
3430 delayed = true;
3431 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3432 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003433 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 delayed = true;
3435 }
3436 if (DEBUG_APP_TRANSITIONS) Log.v(
3437 TAG, "Removing app " + wtoken + " delayed=" + delayed
3438 + " animation=" + wtoken.animation
3439 + " animating=" + wtoken.animating);
3440 if (delayed) {
3441 // set the token aside because it has an active animation to be finished
3442 mExitingAppTokens.add(wtoken);
3443 }
3444 mAppTokens.remove(wtoken);
3445 wtoken.removed = true;
3446 if (wtoken.startingData != null) {
3447 startingToken = wtoken;
3448 }
3449 unsetAppFreezingScreenLocked(wtoken, true, true);
3450 if (mFocusedApp == wtoken) {
3451 if (DEBUG_FOCUS) Log.v(TAG, "Removing focused app token:" + wtoken);
3452 mFocusedApp = null;
3453 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3454 mKeyWaiter.tickle();
3455 }
3456 } else {
3457 Log.w(TAG, "Attempted to remove non-existing app token: " + token);
3458 }
Romain Guy06882f82009-06-10 13:36:04 -07003459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 if (!delayed && wtoken != null) {
3461 wtoken.updateReportedVisibilityLocked();
3462 }
3463 }
3464 Binder.restoreCallingIdentity(origId);
3465
3466 if (startingToken != null) {
3467 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Schedule remove starting "
3468 + startingToken + ": app token removed");
3469 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3470 mH.sendMessage(m);
3471 }
3472 }
3473
3474 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3475 final int NW = token.windows.size();
3476 for (int i=0; i<NW; i++) {
3477 WindowState win = token.windows.get(i);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003478 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Tmp removing window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 mWindows.remove(win);
3480 int j = win.mChildWindows.size();
3481 while (j > 0) {
3482 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003483 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3484 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
3485 "Tmp removing child window " + cwin);
3486 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489 return NW > 0;
3490 }
3491
3492 void dumpAppTokensLocked() {
3493 for (int i=mAppTokens.size()-1; i>=0; i--) {
3494 Log.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
3495 }
3496 }
Romain Guy06882f82009-06-10 13:36:04 -07003497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 void dumpWindowsLocked() {
3499 for (int i=mWindows.size()-1; i>=0; i--) {
3500 Log.v(TAG, " #" + i + ": " + mWindows.get(i));
3501 }
3502 }
Romain Guy06882f82009-06-10 13:36:04 -07003503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 private int findWindowOffsetLocked(int tokenPos) {
3505 final int NW = mWindows.size();
3506
3507 if (tokenPos >= mAppTokens.size()) {
3508 int i = NW;
3509 while (i > 0) {
3510 i--;
3511 WindowState win = (WindowState)mWindows.get(i);
3512 if (win.getAppToken() != null) {
3513 return i+1;
3514 }
3515 }
3516 }
3517
3518 while (tokenPos > 0) {
3519 // Find the first app token below the new position that has
3520 // a window displayed.
3521 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
3522 if (DEBUG_REORDER) Log.v(TAG, "Looking for lower windows @ "
3523 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003524 if (wtoken.sendingToBottom) {
3525 if (DEBUG_REORDER) Log.v(TAG,
3526 "Skipping token -- currently sending to bottom");
3527 tokenPos--;
3528 continue;
3529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 int i = wtoken.windows.size();
3531 while (i > 0) {
3532 i--;
3533 WindowState win = wtoken.windows.get(i);
3534 int j = win.mChildWindows.size();
3535 while (j > 0) {
3536 j--;
3537 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003538 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 for (int pos=NW-1; pos>=0; pos--) {
3540 if (mWindows.get(pos) == cwin) {
3541 if (DEBUG_REORDER) Log.v(TAG,
3542 "Found child win @" + (pos+1));
3543 return pos+1;
3544 }
3545 }
3546 }
3547 }
3548 for (int pos=NW-1; pos>=0; pos--) {
3549 if (mWindows.get(pos) == win) {
3550 if (DEBUG_REORDER) Log.v(TAG, "Found win @" + (pos+1));
3551 return pos+1;
3552 }
3553 }
3554 }
3555 tokenPos--;
3556 }
3557
3558 return 0;
3559 }
3560
3561 private final int reAddWindowLocked(int index, WindowState win) {
3562 final int NCW = win.mChildWindows.size();
3563 boolean added = false;
3564 for (int j=0; j<NCW; j++) {
3565 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3566 if (!added && cwin.mSubLayer >= 0) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003567 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding child window at "
3568 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 mWindows.add(index, win);
3570 index++;
3571 added = true;
3572 }
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003573 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3574 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 mWindows.add(index, cwin);
3576 index++;
3577 }
3578 if (!added) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003579 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG, "Re-adding window at "
3580 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 mWindows.add(index, win);
3582 index++;
3583 }
3584 return index;
3585 }
Romain Guy06882f82009-06-10 13:36:04 -07003586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3588 final int NW = token.windows.size();
3589 for (int i=0; i<NW; i++) {
3590 index = reAddWindowLocked(index, token.windows.get(i));
3591 }
3592 return index;
3593 }
3594
3595 public void moveAppToken(int index, IBinder token) {
3596 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3597 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003598 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
3600
3601 synchronized(mWindowMap) {
3602 if (DEBUG_REORDER) Log.v(TAG, "Initial app tokens:");
3603 if (DEBUG_REORDER) dumpAppTokensLocked();
3604 final AppWindowToken wtoken = findAppWindowToken(token);
3605 if (wtoken == null || !mAppTokens.remove(wtoken)) {
3606 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3607 + token + " (" + wtoken + ")");
3608 return;
3609 }
3610 mAppTokens.add(index, wtoken);
3611 if (DEBUG_REORDER) Log.v(TAG, "Moved " + token + " to " + index + ":");
3612 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 final long origId = Binder.clearCallingIdentity();
3615 if (DEBUG_REORDER) Log.v(TAG, "Removing windows in " + token + ":");
3616 if (DEBUG_REORDER) dumpWindowsLocked();
3617 if (tmpRemoveAppWindowsLocked(wtoken)) {
3618 if (DEBUG_REORDER) Log.v(TAG, "Adding windows back in:");
3619 if (DEBUG_REORDER) dumpWindowsLocked();
3620 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
3621 if (DEBUG_REORDER) Log.v(TAG, "Final window list:");
3622 if (DEBUG_REORDER) dumpWindowsLocked();
3623 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 mLayoutNeeded = true;
3625 performLayoutAndPlaceSurfacesLocked();
3626 }
3627 Binder.restoreCallingIdentity(origId);
3628 }
3629 }
3630
3631 private void removeAppTokensLocked(List<IBinder> tokens) {
3632 // XXX This should be done more efficiently!
3633 // (take advantage of the fact that both lists should be
3634 // ordered in the same way.)
3635 int N = tokens.size();
3636 for (int i=0; i<N; i++) {
3637 IBinder token = tokens.get(i);
3638 final AppWindowToken wtoken = findAppWindowToken(token);
3639 if (!mAppTokens.remove(wtoken)) {
3640 Log.w(TAG, "Attempting to reorder token that doesn't exist: "
3641 + token + " (" + wtoken + ")");
3642 i--;
3643 N--;
3644 }
3645 }
3646 }
3647
Dianne Hackborna8f60182009-09-01 19:01:50 -07003648 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3649 boolean updateFocusAndLayout) {
3650 // First remove all of the windows from the list.
3651 tmpRemoveAppWindowsLocked(wtoken);
3652
3653 // Where to start adding?
3654 int pos = findWindowOffsetLocked(tokenPos);
3655
3656 // And now add them back at the correct place.
3657 pos = reAddAppWindowsLocked(pos, wtoken);
3658
3659 if (updateFocusAndLayout) {
3660 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3661 assignLayersLocked();
3662 }
3663 mLayoutNeeded = true;
3664 performLayoutAndPlaceSurfacesLocked();
3665 }
3666 }
3667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
3669 // First remove all of the windows from the list.
3670 final int N = tokens.size();
3671 int i;
3672 for (i=0; i<N; i++) {
3673 WindowToken token = mTokenMap.get(tokens.get(i));
3674 if (token != null) {
3675 tmpRemoveAppWindowsLocked(token);
3676 }
3677 }
3678
3679 // Where to start adding?
3680 int pos = findWindowOffsetLocked(tokenPos);
3681
3682 // And now add them back at the correct place.
3683 for (i=0; i<N; i++) {
3684 WindowToken token = mTokenMap.get(tokens.get(i));
3685 if (token != null) {
3686 pos = reAddAppWindowsLocked(pos, token);
3687 }
3688 }
3689
Dianne Hackborna8f60182009-09-01 19:01:50 -07003690 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3691 assignLayersLocked();
3692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 mLayoutNeeded = true;
3694 performLayoutAndPlaceSurfacesLocked();
3695
3696 //dump();
3697 }
3698
3699 public void moveAppTokensToTop(List<IBinder> tokens) {
3700 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3701 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003702 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 }
3704
3705 final long origId = Binder.clearCallingIdentity();
3706 synchronized(mWindowMap) {
3707 removeAppTokensLocked(tokens);
3708 final int N = tokens.size();
3709 for (int i=0; i<N; i++) {
3710 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3711 if (wt != null) {
3712 mAppTokens.add(wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003713 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3714 mToTopApps.remove(wt);
3715 mToBottomApps.remove(wt);
3716 mToTopApps.add(wt);
3717 wt.sendingToBottom = false;
3718 wt.sendingToTop = true;
3719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003722
3723 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3724 moveAppWindowsLocked(tokens, mAppTokens.size());
3725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 }
3727 Binder.restoreCallingIdentity(origId);
3728 }
3729
3730 public void moveAppTokensToBottom(List<IBinder> tokens) {
3731 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3732 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003733 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
3735
3736 final long origId = Binder.clearCallingIdentity();
3737 synchronized(mWindowMap) {
3738 removeAppTokensLocked(tokens);
3739 final int N = tokens.size();
3740 int pos = 0;
3741 for (int i=0; i<N; i++) {
3742 AppWindowToken wt = findAppWindowToken(tokens.get(i));
3743 if (wt != null) {
3744 mAppTokens.add(pos, wt);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003745 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
3746 mToTopApps.remove(wt);
3747 mToBottomApps.remove(wt);
3748 mToBottomApps.add(i, wt);
3749 wt.sendingToTop = false;
3750 wt.sendingToBottom = true;
3751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 pos++;
3753 }
3754 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07003755
3756 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
3757 moveAppWindowsLocked(tokens, 0);
3758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
3760 Binder.restoreCallingIdentity(origId);
3761 }
3762
3763 // -------------------------------------------------------------
3764 // Misc IWindowSession methods
3765 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07003766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 public void disableKeyguard(IBinder token, String tag) {
3768 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3769 != PackageManager.PERMISSION_GRANTED) {
3770 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3771 }
3772 mKeyguardDisabled.acquire(token, tag);
3773 }
3774
3775 public void reenableKeyguard(IBinder token) {
3776 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3777 != PackageManager.PERMISSION_GRANTED) {
3778 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3779 }
3780 synchronized (mKeyguardDisabled) {
3781 mKeyguardDisabled.release(token);
3782
3783 if (!mKeyguardDisabled.isAcquired()) {
3784 // if we are the last one to reenable the keyguard wait until
3785 // we have actaully finished reenabling until returning
3786 mWaitingUntilKeyguardReenabled = true;
3787 while (mWaitingUntilKeyguardReenabled) {
3788 try {
3789 mKeyguardDisabled.wait();
3790 } catch (InterruptedException e) {
3791 Thread.currentThread().interrupt();
3792 }
3793 }
3794 }
3795 }
3796 }
3797
3798 /**
3799 * @see android.app.KeyguardManager#exitKeyguardSecurely
3800 */
3801 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
3802 if (mContext.checkCallingPermission(android.Manifest.permission.DISABLE_KEYGUARD)
3803 != PackageManager.PERMISSION_GRANTED) {
3804 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
3805 }
3806 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
3807 public void onKeyguardExitResult(boolean success) {
3808 try {
3809 callback.onKeyguardExitResult(success);
3810 } catch (RemoteException e) {
3811 // Client has died, we don't care.
3812 }
3813 }
3814 });
3815 }
3816
3817 public boolean inKeyguardRestrictedInputMode() {
3818 return mPolicy.inKeyguardRestrictedKeyInputMode();
3819 }
Romain Guy06882f82009-06-10 13:36:04 -07003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 static float fixScale(float scale) {
3822 if (scale < 0) scale = 0;
3823 else if (scale > 20) scale = 20;
3824 return Math.abs(scale);
3825 }
Romain Guy06882f82009-06-10 13:36:04 -07003826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 public void setAnimationScale(int which, float scale) {
3828 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3829 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003830 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 }
3832
3833 if (scale < 0) scale = 0;
3834 else if (scale > 20) scale = 20;
3835 scale = Math.abs(scale);
3836 switch (which) {
3837 case 0: mWindowAnimationScale = fixScale(scale); break;
3838 case 1: mTransitionAnimationScale = fixScale(scale); break;
3839 }
Romain Guy06882f82009-06-10 13:36:04 -07003840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 // Persist setting
3842 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3843 }
Romain Guy06882f82009-06-10 13:36:04 -07003844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 public void setAnimationScales(float[] scales) {
3846 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
3847 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003848 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 }
3850
3851 if (scales != null) {
3852 if (scales.length >= 1) {
3853 mWindowAnimationScale = fixScale(scales[0]);
3854 }
3855 if (scales.length >= 2) {
3856 mTransitionAnimationScale = fixScale(scales[1]);
3857 }
3858 }
Romain Guy06882f82009-06-10 13:36:04 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 // Persist setting
3861 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
3862 }
Romain Guy06882f82009-06-10 13:36:04 -07003863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 public float getAnimationScale(int which) {
3865 switch (which) {
3866 case 0: return mWindowAnimationScale;
3867 case 1: return mTransitionAnimationScale;
3868 }
3869 return 0;
3870 }
Romain Guy06882f82009-06-10 13:36:04 -07003871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 public float[] getAnimationScales() {
3873 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
3874 }
Romain Guy06882f82009-06-10 13:36:04 -07003875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 public int getSwitchState(int sw) {
3877 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3878 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003879 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881 return KeyInputQueue.getSwitchState(sw);
3882 }
Romain Guy06882f82009-06-10 13:36:04 -07003883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 public int getSwitchStateForDevice(int devid, int sw) {
3885 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3886 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003887 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889 return KeyInputQueue.getSwitchState(devid, sw);
3890 }
Romain Guy06882f82009-06-10 13:36:04 -07003891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 public int getScancodeState(int sw) {
3893 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3894 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003895 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
3897 return KeyInputQueue.getScancodeState(sw);
3898 }
Romain Guy06882f82009-06-10 13:36:04 -07003899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 public int getScancodeStateForDevice(int devid, int sw) {
3901 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3902 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003903 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905 return KeyInputQueue.getScancodeState(devid, sw);
3906 }
Romain Guy06882f82009-06-10 13:36:04 -07003907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 public int getKeycodeState(int sw) {
3909 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3910 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003911 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 }
3913 return KeyInputQueue.getKeycodeState(sw);
3914 }
Romain Guy06882f82009-06-10 13:36:04 -07003915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 public int getKeycodeStateForDevice(int devid, int sw) {
3917 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
3918 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003919 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 }
3921 return KeyInputQueue.getKeycodeState(devid, sw);
3922 }
Romain Guy06882f82009-06-10 13:36:04 -07003923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
3925 return KeyInputQueue.hasKeys(keycodes, keyExists);
3926 }
Romain Guy06882f82009-06-10 13:36:04 -07003927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 public void enableScreenAfterBoot() {
3929 synchronized(mWindowMap) {
3930 if (mSystemBooted) {
3931 return;
3932 }
3933 mSystemBooted = true;
3934 }
Romain Guy06882f82009-06-10 13:36:04 -07003935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 performEnableScreen();
3937 }
Romain Guy06882f82009-06-10 13:36:04 -07003938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 public void enableScreenIfNeededLocked() {
3940 if (mDisplayEnabled) {
3941 return;
3942 }
3943 if (!mSystemBooted) {
3944 return;
3945 }
3946 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
3947 }
Romain Guy06882f82009-06-10 13:36:04 -07003948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 public void performEnableScreen() {
3950 synchronized(mWindowMap) {
3951 if (mDisplayEnabled) {
3952 return;
3953 }
3954 if (!mSystemBooted) {
3955 return;
3956 }
Romain Guy06882f82009-06-10 13:36:04 -07003957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 // Don't enable the screen until all existing windows
3959 // have been drawn.
3960 final int N = mWindows.size();
3961 for (int i=0; i<N; i++) {
3962 WindowState w = (WindowState)mWindows.get(i);
3963 if (w.isVisibleLw() && !w.isDisplayedLw()) {
3964 return;
3965 }
3966 }
Romain Guy06882f82009-06-10 13:36:04 -07003967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 mDisplayEnabled = true;
3969 if (false) {
3970 Log.i(TAG, "ENABLING SCREEN!");
3971 StringWriter sw = new StringWriter();
3972 PrintWriter pw = new PrintWriter(sw);
3973 this.dump(null, pw, null);
3974 Log.i(TAG, sw.toString());
3975 }
3976 try {
3977 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
3978 if (surfaceFlinger != null) {
3979 //Log.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
3980 Parcel data = Parcel.obtain();
3981 data.writeInterfaceToken("android.ui.ISurfaceComposer");
3982 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
3983 data, null, 0);
3984 data.recycle();
3985 }
3986 } catch (RemoteException ex) {
3987 Log.e(TAG, "Boot completed: SurfaceFlinger is dead!");
3988 }
3989 }
Romain Guy06882f82009-06-10 13:36:04 -07003990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07003994 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
3995 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 }
Romain Guy06882f82009-06-10 13:36:04 -07003997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 public void setInTouchMode(boolean mode) {
3999 synchronized(mWindowMap) {
4000 mInTouchMode = mode;
4001 }
4002 }
4003
Romain Guy06882f82009-06-10 13:36:04 -07004004 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004005 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004007 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004008 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 }
4010
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004011 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 }
Romain Guy06882f82009-06-10 13:36:04 -07004013
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004014 public void setRotationUnchecked(int rotation,
4015 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 if(DEBUG_ORIENTATION) Log.v(TAG,
4017 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 long origId = Binder.clearCallingIdentity();
4020 boolean changed;
4021 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004022 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 }
Romain Guy06882f82009-06-10 13:36:04 -07004024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 if (changed) {
4026 sendNewConfiguration();
4027 synchronized(mWindowMap) {
4028 mLayoutNeeded = true;
4029 performLayoutAndPlaceSurfacesLocked();
4030 }
4031 } else if (alwaysSendConfiguration) {
4032 //update configuration ignoring orientation change
4033 sendNewConfiguration();
4034 }
Romain Guy06882f82009-06-10 13:36:04 -07004035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 Binder.restoreCallingIdentity(origId);
4037 }
Romain Guy06882f82009-06-10 13:36:04 -07004038
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004039 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 boolean changed;
4041 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4042 rotation = mRequestedRotation;
4043 } else {
4044 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004045 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 }
4047 if (DEBUG_ORIENTATION) Log.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004048 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 mRotation, mDisplayEnabled);
4050 if (DEBUG_ORIENTATION) Log.v(TAG, "new rotation is set to " + rotation);
4051 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 if (changed) {
Romain Guy06882f82009-06-10 13:36:04 -07004054 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 "Rotation changed to " + rotation
4056 + " from " + mRotation
4057 + " (forceApp=" + mForcedAppOrientation
4058 + ", req=" + mRequestedRotation + ")");
4059 mRotation = rotation;
4060 mWindowsFreezingScreen = true;
4061 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4062 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4063 2000);
4064 startFreezingDisplayLocked();
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004065 Log.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 mQueue.setOrientation(rotation);
4067 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004068 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
4070 for (int i=mWindows.size()-1; i>=0; i--) {
4071 WindowState w = (WindowState)mWindows.get(i);
4072 if (w.mSurface != null) {
4073 w.mOrientationChanging = true;
4074 }
4075 }
4076 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4077 try {
4078 mRotationWatchers.get(i).onRotationChanged(rotation);
4079 } catch (RemoteException e) {
4080 }
4081 }
4082 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 return changed;
4085 }
Romain Guy06882f82009-06-10 13:36:04 -07004086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 public int getRotation() {
4088 return mRotation;
4089 }
4090
4091 public int watchRotation(IRotationWatcher watcher) {
4092 final IBinder watcherBinder = watcher.asBinder();
4093 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4094 public void binderDied() {
4095 synchronized (mWindowMap) {
4096 for (int i=0; i<mRotationWatchers.size(); i++) {
4097 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004098 IRotationWatcher removed = mRotationWatchers.remove(i);
4099 if (removed != null) {
4100 removed.asBinder().unlinkToDeath(this, 0);
4101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 i--;
4103 }
4104 }
4105 }
4106 }
4107 };
Romain Guy06882f82009-06-10 13:36:04 -07004108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 synchronized (mWindowMap) {
4110 try {
4111 watcher.asBinder().linkToDeath(dr, 0);
4112 mRotationWatchers.add(watcher);
4113 } catch (RemoteException e) {
4114 // Client died, no cleanup needed.
4115 }
Romain Guy06882f82009-06-10 13:36:04 -07004116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 return mRotation;
4118 }
4119 }
4120
4121 /**
4122 * Starts the view server on the specified port.
4123 *
4124 * @param port The port to listener to.
4125 *
4126 * @return True if the server was successfully started, false otherwise.
4127 *
4128 * @see com.android.server.ViewServer
4129 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4130 */
4131 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004132 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 return false;
4134 }
4135
4136 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4137 return false;
4138 }
4139
4140 if (port < 1024) {
4141 return false;
4142 }
4143
4144 if (mViewServer != null) {
4145 if (!mViewServer.isRunning()) {
4146 try {
4147 return mViewServer.start();
4148 } catch (IOException e) {
Romain Guy06882f82009-06-10 13:36:04 -07004149 Log.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151 }
4152 return false;
4153 }
4154
4155 try {
4156 mViewServer = new ViewServer(this, port);
4157 return mViewServer.start();
4158 } catch (IOException e) {
4159 Log.w(TAG, "View server did not start");
4160 }
4161 return false;
4162 }
4163
Romain Guy06882f82009-06-10 13:36:04 -07004164 private boolean isSystemSecure() {
4165 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4166 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4167 }
4168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 /**
4170 * Stops the view server if it exists.
4171 *
4172 * @return True if the server stopped, false if it wasn't started or
4173 * couldn't be stopped.
4174 *
4175 * @see com.android.server.ViewServer
4176 */
4177 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004178 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 return false;
4180 }
4181
4182 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4183 return false;
4184 }
4185
4186 if (mViewServer != null) {
4187 return mViewServer.stop();
4188 }
4189 return false;
4190 }
4191
4192 /**
4193 * Indicates whether the view server is running.
4194 *
4195 * @return True if the server is running, false otherwise.
4196 *
4197 * @see com.android.server.ViewServer
4198 */
4199 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004200 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 return false;
4202 }
4203
4204 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4205 return false;
4206 }
4207
4208 return mViewServer != null && mViewServer.isRunning();
4209 }
4210
4211 /**
4212 * Lists all availble windows in the system. The listing is written in the
4213 * specified Socket's output stream with the following syntax:
4214 * windowHashCodeInHexadecimal windowName
4215 * Each line of the ouput represents a different window.
4216 *
4217 * @param client The remote client to send the listing to.
4218 * @return False if an error occured, true otherwise.
4219 */
4220 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004221 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 return false;
4223 }
4224
4225 boolean result = true;
4226
4227 Object[] windows;
4228 synchronized (mWindowMap) {
4229 windows = new Object[mWindows.size()];
4230 //noinspection unchecked
4231 windows = mWindows.toArray(windows);
4232 }
4233
4234 BufferedWriter out = null;
4235
4236 // Any uncaught exception will crash the system process
4237 try {
4238 OutputStream clientStream = client.getOutputStream();
4239 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4240
4241 final int count = windows.length;
4242 for (int i = 0; i < count; i++) {
4243 final WindowState w = (WindowState) windows[i];
4244 out.write(Integer.toHexString(System.identityHashCode(w)));
4245 out.write(' ');
4246 out.append(w.mAttrs.getTitle());
4247 out.write('\n');
4248 }
4249
4250 out.write("DONE.\n");
4251 out.flush();
4252 } catch (Exception e) {
4253 result = false;
4254 } finally {
4255 if (out != null) {
4256 try {
4257 out.close();
4258 } catch (IOException e) {
4259 result = false;
4260 }
4261 }
4262 }
4263
4264 return result;
4265 }
4266
4267 /**
4268 * Sends a command to a target window. The result of the command, if any, will be
4269 * written in the output stream of the specified socket.
4270 *
4271 * The parameters must follow this syntax:
4272 * windowHashcode extra
4273 *
4274 * Where XX is the length in characeters of the windowTitle.
4275 *
4276 * The first parameter is the target window. The window with the specified hashcode
4277 * will be the target. If no target can be found, nothing happens. The extra parameters
4278 * will be delivered to the target window and as parameters to the command itself.
4279 *
4280 * @param client The remote client to sent the result, if any, to.
4281 * @param command The command to execute.
4282 * @param parameters The command parameters.
4283 *
4284 * @return True if the command was successfully delivered, false otherwise. This does
4285 * not indicate whether the command itself was successful.
4286 */
4287 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004288 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 return false;
4290 }
4291
4292 boolean success = true;
4293 Parcel data = null;
4294 Parcel reply = null;
4295
4296 // Any uncaught exception will crash the system process
4297 try {
4298 // Find the hashcode of the window
4299 int index = parameters.indexOf(' ');
4300 if (index == -1) {
4301 index = parameters.length();
4302 }
4303 final String code = parameters.substring(0, index);
4304 int hashCode = "ffffffff".equals(code) ? -1 : Integer.parseInt(code, 16);
4305
4306 // Extract the command's parameter after the window description
4307 if (index < parameters.length()) {
4308 parameters = parameters.substring(index + 1);
4309 } else {
4310 parameters = "";
4311 }
4312
4313 final WindowManagerService.WindowState window = findWindow(hashCode);
4314 if (window == null) {
4315 return false;
4316 }
4317
4318 data = Parcel.obtain();
4319 data.writeInterfaceToken("android.view.IWindow");
4320 data.writeString(command);
4321 data.writeString(parameters);
4322 data.writeInt(1);
4323 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4324
4325 reply = Parcel.obtain();
4326
4327 final IBinder binder = window.mClient.asBinder();
4328 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4329 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4330
4331 reply.readException();
4332
4333 } catch (Exception e) {
4334 Log.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
4335 success = false;
4336 } finally {
4337 if (data != null) {
4338 data.recycle();
4339 }
4340 if (reply != null) {
4341 reply.recycle();
4342 }
4343 }
4344
4345 return success;
4346 }
4347
4348 private WindowState findWindow(int hashCode) {
4349 if (hashCode == -1) {
4350 return getFocusedWindow();
4351 }
4352
4353 synchronized (mWindowMap) {
4354 final ArrayList windows = mWindows;
4355 final int count = windows.size();
4356
4357 for (int i = 0; i < count; i++) {
4358 WindowState w = (WindowState) windows.get(i);
4359 if (System.identityHashCode(w) == hashCode) {
4360 return w;
4361 }
4362 }
4363 }
4364
4365 return null;
4366 }
4367
4368 /*
4369 * Instruct the Activity Manager to fetch the current configuration and broadcast
4370 * that to config-changed listeners if appropriate.
4371 */
4372 void sendNewConfiguration() {
4373 try {
4374 mActivityManager.updateConfiguration(null);
4375 } catch (RemoteException e) {
4376 }
4377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 public Configuration computeNewConfiguration() {
4380 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004381 return computeNewConfigurationLocked();
4382 }
4383 }
Romain Guy06882f82009-06-10 13:36:04 -07004384
Dianne Hackbornc485a602009-03-24 22:39:49 -07004385 Configuration computeNewConfigurationLocked() {
4386 Configuration config = new Configuration();
4387 if (!computeNewConfigurationLocked(config)) {
4388 return null;
4389 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004390 return config;
4391 }
Romain Guy06882f82009-06-10 13:36:04 -07004392
Dianne Hackbornc485a602009-03-24 22:39:49 -07004393 boolean computeNewConfigurationLocked(Configuration config) {
4394 if (mDisplay == null) {
4395 return false;
4396 }
4397 mQueue.getInputConfiguration(config);
4398 final int dw = mDisplay.getWidth();
4399 final int dh = mDisplay.getHeight();
4400 int orientation = Configuration.ORIENTATION_SQUARE;
4401 if (dw < dh) {
4402 orientation = Configuration.ORIENTATION_PORTRAIT;
4403 } else if (dw > dh) {
4404 orientation = Configuration.ORIENTATION_LANDSCAPE;
4405 }
4406 config.orientation = orientation;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004407
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004408 DisplayMetrics dm = new DisplayMetrics();
4409 mDisplay.getMetrics(dm);
4410 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4411
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004412 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004413 // Note we only do this once because at this point we don't
4414 // expect the screen to change in this way at runtime, and want
4415 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004416 int longSize = dw;
4417 int shortSize = dh;
4418 if (longSize < shortSize) {
4419 int tmp = longSize;
4420 longSize = shortSize;
4421 shortSize = tmp;
4422 }
4423 longSize = (int)(longSize/dm.density);
4424 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004425
Dianne Hackborn723738c2009-06-25 19:48:04 -07004426 // These semi-magic numbers define our compatibility modes for
4427 // applications with different screens. Don't change unless you
4428 // make sure to test lots and lots of apps!
4429 if (longSize < 470) {
4430 // This is shorter than an HVGA normal density screen (which
4431 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004432 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4433 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004434 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004435 // Is this a large screen?
4436 if (longSize > 640 && shortSize >= 480) {
4437 // VGA or larger screens at medium density are the point
4438 // at which we consider it to be a large screen.
4439 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4440 } else {
4441 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
4442
4443 // If this screen is wider than normal HVGA, or taller
4444 // than FWVGA, then for old apps we want to run in size
4445 // compatibility mode.
4446 if (shortSize > 321 || longSize > 570) {
4447 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4448 }
4449 }
4450
4451 // Is this a long screen?
4452 if (((longSize*3)/5) >= (shortSize-1)) {
4453 // Anything wider than WVGA (5:3) is considering to be long.
4454 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4455 } else {
4456 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4457 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004458 }
4459 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004460 config.screenLayout = mScreenLayout;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004461
Dianne Hackbornc485a602009-03-24 22:39:49 -07004462 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4463 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4464 mPolicy.adjustConfigurationLw(config);
4465 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 }
Romain Guy06882f82009-06-10 13:36:04 -07004467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 // -------------------------------------------------------------
4469 // Input Events and Focus Management
4470 // -------------------------------------------------------------
4471
4472 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004473 long curTime = SystemClock.uptimeMillis();
4474
Michael Chane10de972009-05-18 11:24:50 -07004475 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004476 if (mLastTouchEventType == eventType &&
4477 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4478 return;
4479 }
4480 mLastUserActivityCallTime = curTime;
4481 mLastTouchEventType = eventType;
4482 }
4483
4484 if (targetWin == null
4485 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4486 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
4488 }
4489
4490 // tells if it's a cheek event or not -- this function is stateful
4491 private static final int EVENT_NONE = 0;
4492 private static final int EVENT_UNKNOWN = 0;
4493 private static final int EVENT_CHEEK = 0;
4494 private static final int EVENT_IGNORE_DURATION = 300; // ms
4495 private static final float CHEEK_THRESHOLD = 0.6f;
4496 private int mEventState = EVENT_NONE;
4497 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 private int eventType(MotionEvent ev) {
4500 float size = ev.getSize();
4501 switch (ev.getAction()) {
4502 case MotionEvent.ACTION_DOWN:
4503 mEventSize = size;
4504 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4505 case MotionEvent.ACTION_UP:
4506 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004507 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 case MotionEvent.ACTION_MOVE:
4509 final int N = ev.getHistorySize();
4510 if (size > mEventSize) mEventSize = size;
4511 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4512 for (int i=0; i<N; i++) {
4513 size = ev.getHistoricalSize(i);
4514 if (size > mEventSize) mEventSize = size;
4515 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4516 }
4517 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4518 return TOUCH_EVENT;
4519 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004520 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522 default:
4523 // not good
4524 return OTHER_EVENT;
4525 }
4526 }
4527
4528 /**
4529 * @return Returns true if event was dispatched, false if it was dropped for any reason
4530 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004531 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Log.v(TAG,
4533 "dispatchPointer " + ev);
4534
Michael Chan53071d62009-05-13 17:29:48 -07004535 if (MEASURE_LATENCY) {
4536 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4537 }
4538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004540 ev, true, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541
Michael Chan53071d62009-05-13 17:29:48 -07004542 if (MEASURE_LATENCY) {
4543 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4544 }
4545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 if (action == MotionEvent.ACTION_UP) {
4549 // let go of our target
4550 mKeyWaiter.mMotionTarget = null;
4551 mPowerManager.logPointerUpEvent();
4552 } else if (action == MotionEvent.ACTION_DOWN) {
4553 mPowerManager.logPointerDownEvent();
4554 }
4555
4556 if (targetObj == null) {
4557 // In this case we are either dropping the event, or have received
4558 // a move or up without a down. It is common to receive move
4559 // events in such a way, since this means the user is moving the
4560 // pointer without actually pressing down. All other cases should
4561 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004562 if (action != MotionEvent.ACTION_MOVE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 Log.w(TAG, "No window to dispatch pointer action " + ev.getAction());
4564 }
4565 if (qev != null) {
4566 mQueue.recycleEvent(qev);
4567 }
4568 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004569 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 }
4571 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4572 if (qev != null) {
4573 mQueue.recycleEvent(qev);
4574 }
4575 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004576 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 }
Romain Guy06882f82009-06-10 13:36:04 -07004578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004582 final long eventTimeNano = ev.getEventTimeNano();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583
4584 //Log.i(TAG, "Sending " + ev + " to " + target);
4585
4586 if (uid != 0 && uid != target.mSession.mUid) {
4587 if (mContext.checkPermission(
4588 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4589 != PackageManager.PERMISSION_GRANTED) {
4590 Log.w(TAG, "Permission denied: injecting pointer event from pid "
4591 + pid + " uid " + uid + " to window " + target
4592 + " owned by uid " + target.mSession.mUid);
4593 if (qev != null) {
4594 mQueue.recycleEvent(qev);
4595 }
4596 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004597 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 }
4599 }
4600
Michael Chan53071d62009-05-13 17:29:48 -07004601 if (MEASURE_LATENCY) {
4602 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4603 }
4604
Romain Guy06882f82009-06-10 13:36:04 -07004605 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
4607 //target wants to ignore fat touch events
4608 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
4609 //explicit flag to return without processing event further
4610 boolean returnFlag = false;
4611 if((action == MotionEvent.ACTION_DOWN)) {
4612 mFatTouch = false;
4613 if(cheekPress) {
4614 mFatTouch = true;
4615 returnFlag = true;
4616 }
4617 } else {
4618 if(action == MotionEvent.ACTION_UP) {
4619 if(mFatTouch) {
4620 //earlier even was invalid doesnt matter if current up is cheekpress or not
4621 mFatTouch = false;
4622 returnFlag = true;
4623 } else if(cheekPress) {
4624 //cancel the earlier event
4625 ev.setAction(MotionEvent.ACTION_CANCEL);
4626 action = MotionEvent.ACTION_CANCEL;
4627 }
4628 } else if(action == MotionEvent.ACTION_MOVE) {
4629 if(mFatTouch) {
4630 //two cases here
4631 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07004632 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 returnFlag = true;
4634 } else if(cheekPress) {
4635 //valid down followed by invalid moves
4636 //an invalid move have to cancel earlier action
4637 ev.setAction(MotionEvent.ACTION_CANCEL);
4638 action = MotionEvent.ACTION_CANCEL;
4639 if (DEBUG_INPUT) Log.v(TAG, "Sending cancel for invalid ACTION_MOVE");
4640 //note that the subsequent invalid moves will not get here
4641 mFatTouch = true;
4642 }
4643 }
4644 } //else if action
4645 if(returnFlag) {
4646 //recycle que, ev
4647 if (qev != null) {
4648 mQueue.recycleEvent(qev);
4649 }
4650 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004651 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 }
4653 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07004654
Michael Chan9f028e62009-08-04 17:37:46 -07004655 // Enable this for testing the "right" value
4656 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07004657 int max_events_per_sec = 35;
4658 try {
4659 max_events_per_sec = Integer.parseInt(SystemProperties
4660 .get("windowsmgr.max_events_per_sec"));
4661 if (max_events_per_sec < 1) {
4662 max_events_per_sec = 35;
4663 }
4664 } catch (NumberFormatException e) {
4665 }
4666 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
4667 }
4668
4669 /*
4670 * Throttle events to minimize CPU usage when there's a flood of events
4671 * e.g. constant contact with the screen
4672 */
4673 if (action == MotionEvent.ACTION_MOVE) {
4674 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
4675 long now = SystemClock.uptimeMillis();
4676 if (now < nextEventTime) {
4677 try {
4678 Thread.sleep(nextEventTime - now);
4679 } catch (InterruptedException e) {
4680 }
4681 mLastTouchEventTime = nextEventTime;
4682 } else {
4683 mLastTouchEventTime = now;
4684 }
4685 }
4686
Michael Chan53071d62009-05-13 17:29:48 -07004687 if (MEASURE_LATENCY) {
4688 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
4689 }
4690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004691 synchronized(mWindowMap) {
4692 if (qev != null && action == MotionEvent.ACTION_MOVE) {
4693 mKeyWaiter.bindTargetWindowLocked(target,
4694 KeyWaiter.RETURN_PENDING_POINTER, qev);
4695 ev = null;
4696 } else {
4697 if (action == MotionEvent.ACTION_DOWN) {
4698 WindowState out = mKeyWaiter.mOutsideTouchTargets;
4699 if (out != null) {
4700 MotionEvent oev = MotionEvent.obtain(ev);
4701 oev.setAction(MotionEvent.ACTION_OUTSIDE);
4702 do {
4703 final Rect frame = out.mFrame;
4704 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
4705 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004706 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 } catch (android.os.RemoteException e) {
4708 Log.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
4709 }
4710 oev.offsetLocation((float)frame.left, (float)frame.top);
4711 out = out.mNextOutsideTouch;
4712 } while (out != null);
4713 mKeyWaiter.mOutsideTouchTargets = null;
4714 }
4715 }
4716 final Rect frame = target.mFrame;
4717 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
4718 mKeyWaiter.bindTargetWindowLocked(target);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004719
4720 // If we are on top of the wallpaper, then the wallpaper also
4721 // gets to see this movement.
4722 if (mWallpaperTarget == target) {
4723 sendPointerToWallpaperLocked(target, ev, eventTime);
4724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
4726 }
Romain Guy06882f82009-06-10 13:36:04 -07004727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 // finally offset the event to the target's coordinate system and
4729 // dispatch the event.
4730 try {
4731 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
4732 Log.v(TAG, "Delivering pointer " + qev + " to " + target);
4733 }
Michael Chan53071d62009-05-13 17:29:48 -07004734
4735 if (MEASURE_LATENCY) {
4736 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
4737 }
4738
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004739 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07004740
4741 if (MEASURE_LATENCY) {
4742 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
4743 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004744 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 } catch (android.os.RemoteException e) {
4746 Log.i(TAG, "WINDOW DIED during motion dispatch: " + target);
4747 mKeyWaiter.mMotionTarget = null;
4748 try {
4749 removeWindow(target.mSession, target.mClient);
4750 } catch (java.util.NoSuchElementException ex) {
4751 // This will happen if the window has already been
4752 // removed.
4753 }
4754 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07004755 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 }
Romain Guy06882f82009-06-10 13:36:04 -07004757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 /**
4759 * @return Returns true if event was dispatched, false if it was dropped for any reason
4760 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004761 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 if (DEBUG_INPUT) Log.v(
4763 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07004764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004766 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767 if (focusObj == null) {
4768 Log.w(TAG, "No focus window, dropping trackball: " + ev);
4769 if (qev != null) {
4770 mQueue.recycleEvent(qev);
4771 }
4772 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004773 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 }
4775 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
4776 if (qev != null) {
4777 mQueue.recycleEvent(qev);
4778 }
4779 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004780 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 }
Romain Guy06882f82009-06-10 13:36:04 -07004782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 if (uid != 0 && uid != focus.mSession.mUid) {
4786 if (mContext.checkPermission(
4787 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4788 != PackageManager.PERMISSION_GRANTED) {
4789 Log.w(TAG, "Permission denied: injecting key event from pid "
4790 + pid + " uid " + uid + " to window " + focus
4791 + " owned by uid " + focus.mSession.mUid);
4792 if (qev != null) {
4793 mQueue.recycleEvent(qev);
4794 }
4795 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004796 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 }
4798 }
Romain Guy06882f82009-06-10 13:36:04 -07004799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07004801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 synchronized(mWindowMap) {
4803 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
4804 mKeyWaiter.bindTargetWindowLocked(focus,
4805 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
4806 // We don't deliver movement events to the client, we hold
4807 // them and wait for them to call back.
4808 ev = null;
4809 } else {
4810 mKeyWaiter.bindTargetWindowLocked(focus);
4811 }
4812 }
Romain Guy06882f82009-06-10 13:36:04 -07004813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07004815 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004816 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 } catch (android.os.RemoteException e) {
4818 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4819 try {
4820 removeWindow(focus.mSession, focus.mClient);
4821 } catch (java.util.NoSuchElementException ex) {
4822 // This will happen if the window has already been
4823 // removed.
4824 }
4825 }
Romain Guy06882f82009-06-10 13:36:04 -07004826
Dianne Hackborncfaef692009-06-15 14:24:44 -07004827 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 }
Romain Guy06882f82009-06-10 13:36:04 -07004829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 /**
4831 * @return Returns true if event was dispatched, false if it was dropped for any reason
4832 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004833 private int dispatchKey(KeyEvent event, int pid, int uid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 if (DEBUG_INPUT) Log.v(TAG, "Dispatch key: " + event);
4835
4836 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004837 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 if (focusObj == null) {
4839 Log.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004840 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 }
4842 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004843 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 }
Romain Guy06882f82009-06-10 13:36:04 -07004845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07004847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 if (DEBUG_INPUT) Log.v(
4849 TAG, "Dispatching to " + focus + ": " + event);
4850
4851 if (uid != 0 && uid != focus.mSession.mUid) {
4852 if (mContext.checkPermission(
4853 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4854 != PackageManager.PERMISSION_GRANTED) {
4855 Log.w(TAG, "Permission denied: injecting key event from pid "
4856 + pid + " uid " + uid + " to window " + focus
4857 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004858 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 }
4860 }
Romain Guy06882f82009-06-10 13:36:04 -07004861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 synchronized(mWindowMap) {
4863 mKeyWaiter.bindTargetWindowLocked(focus);
4864 }
4865
4866 // NOSHIP extra state logging
4867 mKeyWaiter.recordDispatchState(event, focus);
4868 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07004869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 try {
4871 if (DEBUG_INPUT || DEBUG_FOCUS) {
4872 Log.v(TAG, "Delivering key " + event.getKeyCode()
4873 + " to " + focus);
4874 }
4875 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004876 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 } catch (android.os.RemoteException e) {
4878 Log.i(TAG, "WINDOW DIED during key dispatch: " + focus);
4879 try {
4880 removeWindow(focus.mSession, focus.mClient);
4881 } catch (java.util.NoSuchElementException ex) {
4882 // This will happen if the window has already been
4883 // removed.
4884 }
4885 }
Romain Guy06882f82009-06-10 13:36:04 -07004886
Dianne Hackborncfaef692009-06-15 14:24:44 -07004887 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 }
Romain Guy06882f82009-06-10 13:36:04 -07004889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 public void pauseKeyDispatching(IBinder _token) {
4891 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4892 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004893 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 }
4895
4896 synchronized (mWindowMap) {
4897 WindowToken token = mTokenMap.get(_token);
4898 if (token != null) {
4899 mKeyWaiter.pauseDispatchingLocked(token);
4900 }
4901 }
4902 }
4903
4904 public void resumeKeyDispatching(IBinder _token) {
4905 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4906 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004907 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 }
4909
4910 synchronized (mWindowMap) {
4911 WindowToken token = mTokenMap.get(_token);
4912 if (token != null) {
4913 mKeyWaiter.resumeDispatchingLocked(token);
4914 }
4915 }
4916 }
4917
4918 public void setEventDispatching(boolean enabled) {
4919 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4920 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004921 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 }
4923
4924 synchronized (mWindowMap) {
4925 mKeyWaiter.setEventDispatchingLocked(enabled);
4926 }
4927 }
Romain Guy06882f82009-06-10 13:36:04 -07004928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 /**
4930 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004931 *
4932 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 * {@link SystemClock#uptimeMillis()} as the timebase.)
4934 * @param sync If true, wait for the event to be completed before returning to the caller.
4935 * @return Returns true if event was dispatched, false if it was dropped for any reason
4936 */
4937 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
4938 long downTime = ev.getDownTime();
4939 long eventTime = ev.getEventTime();
4940
4941 int action = ev.getAction();
4942 int code = ev.getKeyCode();
4943 int repeatCount = ev.getRepeatCount();
4944 int metaState = ev.getMetaState();
4945 int deviceId = ev.getDeviceId();
4946 int scancode = ev.getScanCode();
4947
4948 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
4949 if (downTime == 0) downTime = eventTime;
4950
4951 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07004952 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004954 final int pid = Binder.getCallingPid();
4955 final int uid = Binder.getCallingUid();
4956 final long ident = Binder.clearCallingIdentity();
4957 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004959 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004961 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004962 switch (result) {
4963 case INJECT_NO_PERMISSION:
4964 throw new SecurityException(
4965 "Injecting to another application requires INJECT_EVENT permission");
4966 case INJECT_SUCCEEDED:
4967 return true;
4968 }
4969 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
4971
4972 /**
4973 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07004974 *
4975 * @param ev A motion event describing the pointer (touch) action. (As noted in
4976 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 * {@link SystemClock#uptimeMillis()} as the timebase.)
4978 * @param sync If true, wait for the event to be completed before returning to the caller.
4979 * @return Returns true if event was dispatched, false if it was dropped for any reason
4980 */
4981 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004982 final int pid = Binder.getCallingPid();
4983 final int uid = Binder.getCallingUid();
4984 final long ident = Binder.clearCallingIdentity();
4985 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004987 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004989 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07004990 switch (result) {
4991 case INJECT_NO_PERMISSION:
4992 throw new SecurityException(
4993 "Injecting to another application requires INJECT_EVENT permission");
4994 case INJECT_SUCCEEDED:
4995 return true;
4996 }
4997 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 }
Romain Guy06882f82009-06-10 13:36:04 -07004999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 /**
5001 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005002 *
5003 * @param ev A motion event describing the trackball action. (As noted in
5004 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 * {@link SystemClock#uptimeMillis()} as the timebase.)
5006 * @param sync If true, wait for the event to be completed before returning to the caller.
5007 * @return Returns true if event was dispatched, false if it was dropped for any reason
5008 */
5009 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005010 final int pid = Binder.getCallingPid();
5011 final int uid = Binder.getCallingUid();
5012 final long ident = Binder.clearCallingIdentity();
5013 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005015 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005017 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005018 switch (result) {
5019 case INJECT_NO_PERMISSION:
5020 throw new SecurityException(
5021 "Injecting to another application requires INJECT_EVENT permission");
5022 case INJECT_SUCCEEDED:
5023 return true;
5024 }
5025 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 }
Romain Guy06882f82009-06-10 13:36:04 -07005027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 private WindowState getFocusedWindow() {
5029 synchronized (mWindowMap) {
5030 return getFocusedWindowLocked();
5031 }
5032 }
5033
5034 private WindowState getFocusedWindowLocked() {
5035 return mCurrentFocus;
5036 }
Romain Guy06882f82009-06-10 13:36:04 -07005037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 /**
5039 * This class holds the state for dispatching key events. This state
5040 * is protected by the KeyWaiter instance, NOT by the window lock. You
5041 * can be holding the main window lock while acquire the KeyWaiter lock,
5042 * but not the other way around.
5043 */
5044 final class KeyWaiter {
5045 // NOSHIP debugging
5046 public class DispatchState {
5047 private KeyEvent event;
5048 private WindowState focus;
5049 private long time;
5050 private WindowState lastWin;
5051 private IBinder lastBinder;
5052 private boolean finished;
5053 private boolean gotFirstWindow;
5054 private boolean eventDispatching;
5055 private long timeToSwitch;
5056 private boolean wasFrozen;
5057 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005058 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5061 focus = theFocus;
5062 event = theEvent;
5063 time = System.currentTimeMillis();
5064 // snapshot KeyWaiter state
5065 lastWin = mLastWin;
5066 lastBinder = mLastBinder;
5067 finished = mFinished;
5068 gotFirstWindow = mGotFirstWindow;
5069 eventDispatching = mEventDispatching;
5070 timeToSwitch = mTimeToSwitch;
5071 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005072 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 // cache the paused state at ctor time as well
5074 if (theFocus == null || theFocus.mToken == null) {
5075 Log.i(TAG, "focus " + theFocus + " mToken is null at event dispatch!");
5076 focusPaused = false;
5077 } else {
5078 focusPaused = theFocus.mToken.paused;
5079 }
5080 }
Romain Guy06882f82009-06-10 13:36:04 -07005081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 public String toString() {
5083 return "{{" + event + " to " + focus + " @ " + time
5084 + " lw=" + lastWin + " lb=" + lastBinder
5085 + " fin=" + finished + " gfw=" + gotFirstWindow
5086 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005087 + " wf=" + wasFrozen + " fp=" + focusPaused
5088 + " mcf=" + mCurrentFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 }
5090 };
5091 private DispatchState mDispatchState = null;
5092 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5093 mDispatchState = new DispatchState(theEvent, theFocus);
5094 }
5095 // END NOSHIP
5096
5097 public static final int RETURN_NOTHING = 0;
5098 public static final int RETURN_PENDING_POINTER = 1;
5099 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 final Object SKIP_TARGET_TOKEN = new Object();
5102 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 private WindowState mLastWin = null;
5105 private IBinder mLastBinder = null;
5106 private boolean mFinished = true;
5107 private boolean mGotFirstWindow = false;
5108 private boolean mEventDispatching = true;
5109 private long mTimeToSwitch = 0;
5110 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 // Target of Motion events
5113 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 // Windows above the target who would like to receive an "outside"
5116 // touch event for any down events outside of them.
5117 WindowState mOutsideTouchTargets;
5118
5119 /**
5120 * Wait for the last event dispatch to complete, then find the next
5121 * target that should receive the given event and wait for that one
5122 * to be ready to receive it.
5123 */
5124 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5125 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005126 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 long startTime = SystemClock.uptimeMillis();
5128 long keyDispatchingTimeout = 5 * 1000;
5129 long waitedFor = 0;
5130
5131 while (true) {
5132 // Figure out which window we care about. It is either the
5133 // last window we are waiting to have process the event or,
5134 // if none, then the next window we think the event should go
5135 // to. Note: we retrieve mLastWin outside of the lock, so
5136 // it may change before we lock. Thus we must check it again.
5137 WindowState targetWin = mLastWin;
5138 boolean targetIsNew = targetWin == null;
5139 if (DEBUG_INPUT) Log.v(
5140 TAG, "waitForLastKey: mFinished=" + mFinished +
5141 ", mLastWin=" + mLastWin);
5142 if (targetIsNew) {
5143 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005144 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (target == SKIP_TARGET_TOKEN) {
5146 // The user has pressed a special key, and we are
5147 // dropping all pending events before it.
5148 if (DEBUG_INPUT) Log.v(TAG, "Skipping: " + nextKey
5149 + " " + nextMotion);
5150 return null;
5151 }
5152 if (target == CONSUMED_EVENT_TOKEN) {
5153 if (DEBUG_INPUT) Log.v(TAG, "Consumed: " + nextKey
5154 + " " + nextMotion);
5155 return target;
5156 }
5157 targetWin = (WindowState)target;
5158 }
Romain Guy06882f82009-06-10 13:36:04 -07005159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 // Now: is it okay to send the next event to this window?
5163 synchronized (this) {
5164 // First: did we come here based on the last window not
5165 // being null, but it changed by the time we got here?
5166 // If so, try again.
5167 if (!targetIsNew && mLastWin == null) {
5168 continue;
5169 }
Romain Guy06882f82009-06-10 13:36:04 -07005170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 // We never dispatch events if not finished with the
5172 // last one, or the display is frozen.
5173 if (mFinished && !mDisplayFrozen) {
5174 // If event dispatching is disabled, then we
5175 // just consume the events.
5176 if (!mEventDispatching) {
5177 if (DEBUG_INPUT) Log.v(TAG,
5178 "Skipping event; dispatching disabled: "
5179 + nextKey + " " + nextMotion);
5180 return null;
5181 }
5182 if (targetWin != null) {
5183 // If this is a new target, and that target is not
5184 // paused or unresponsive, then all looks good to
5185 // handle the event.
5186 if (targetIsNew && !targetWin.mToken.paused) {
5187 return targetWin;
5188 }
Romain Guy06882f82009-06-10 13:36:04 -07005189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 // If we didn't find a target window, and there is no
5191 // focused app window, then just eat the events.
5192 } else if (mFocusedApp == null) {
5193 if (DEBUG_INPUT) Log.v(TAG,
5194 "Skipping event; no focused app: "
5195 + nextKey + " " + nextMotion);
5196 return null;
5197 }
5198 }
Romain Guy06882f82009-06-10 13:36:04 -07005199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 if (DEBUG_INPUT) Log.v(
5201 TAG, "Waiting for last key in " + mLastBinder
5202 + " target=" + targetWin
5203 + " mFinished=" + mFinished
5204 + " mDisplayFrozen=" + mDisplayFrozen
5205 + " targetIsNew=" + targetIsNew
5206 + " paused="
5207 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005208 + " mFocusedApp=" + mFocusedApp
5209 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 targetApp = targetWin != null
5212 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 long curTimeout = keyDispatchingTimeout;
5215 if (mTimeToSwitch != 0) {
5216 long now = SystemClock.uptimeMillis();
5217 if (mTimeToSwitch <= now) {
5218 // If an app switch key has been pressed, and we have
5219 // waited too long for the current app to finish
5220 // processing keys, then wait no more!
5221 doFinishedKeyLocked(true);
5222 continue;
5223 }
5224 long switchTimeout = mTimeToSwitch - now;
5225 if (curTimeout > switchTimeout) {
5226 curTimeout = switchTimeout;
5227 }
5228 }
Romain Guy06882f82009-06-10 13:36:04 -07005229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 try {
5231 // after that continue
5232 // processing keys, so we don't get stuck.
5233 if (DEBUG_INPUT) Log.v(
5234 TAG, "Waiting for key dispatch: " + curTimeout);
5235 wait(curTimeout);
5236 if (DEBUG_INPUT) Log.v(TAG, "Finished waiting @"
5237 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005238 + startTime + " switchTime=" + mTimeToSwitch
5239 + " target=" + targetWin + " mLW=" + mLastWin
5240 + " mLB=" + mLastBinder + " fin=" + mFinished
5241 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 } catch (InterruptedException e) {
5243 }
5244 }
5245
5246 // If we were frozen during configuration change, restart the
5247 // timeout checks from now; otherwise look at whether we timed
5248 // out before awakening.
5249 if (mWasFrozen) {
5250 waitedFor = 0;
5251 mWasFrozen = false;
5252 } else {
5253 waitedFor = SystemClock.uptimeMillis() - startTime;
5254 }
5255
5256 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5257 IApplicationToken at = null;
5258 synchronized (this) {
5259 Log.w(TAG, "Key dispatching timed out sending to " +
5260 (targetWin != null ? targetWin.mAttrs.getTitle()
5261 : "<null>"));
5262 // NOSHIP debugging
5263 Log.w(TAG, "Dispatch state: " + mDispatchState);
5264 Log.w(TAG, "Current state: " + new DispatchState(nextKey, targetWin));
5265 // END NOSHIP
5266 //dump();
5267 if (targetWin != null) {
5268 at = targetWin.getAppToken();
5269 } else if (targetApp != null) {
5270 at = targetApp.appToken;
5271 }
5272 }
5273
5274 boolean abort = true;
5275 if (at != null) {
5276 try {
5277 long timeout = at.getKeyDispatchingTimeout();
5278 if (timeout > waitedFor) {
5279 // we did not wait the proper amount of time for this application.
5280 // set the timeout to be the real timeout and wait again.
5281 keyDispatchingTimeout = timeout - waitedFor;
5282 continue;
5283 } else {
5284 abort = at.keyDispatchingTimedOut();
5285 }
5286 } catch (RemoteException ex) {
5287 }
5288 }
5289
5290 synchronized (this) {
5291 if (abort && (mLastWin == targetWin || targetWin == null)) {
5292 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005293 if (mLastWin != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 if (DEBUG_INPUT) Log.v(TAG,
5295 "Window " + mLastWin +
5296 " timed out on key input");
5297 if (mLastWin.mToken.paused) {
5298 Log.w(TAG, "Un-pausing dispatching to this window");
5299 mLastWin.mToken.paused = false;
5300 }
5301 }
5302 if (mMotionTarget == targetWin) {
5303 mMotionTarget = null;
5304 }
5305 mLastWin = null;
5306 mLastBinder = null;
5307 if (failIfTimeout || targetWin == null) {
5308 return null;
5309 }
5310 } else {
5311 Log.w(TAG, "Continuing to wait for key to be dispatched");
5312 startTime = SystemClock.uptimeMillis();
5313 }
5314 }
5315 }
5316 }
5317 }
Romain Guy06882f82009-06-10 13:36:04 -07005318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005320 MotionEvent nextMotion, boolean isPointerEvent,
5321 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 if (nextKey != null) {
5325 // Find the target window for a normal key event.
5326 final int keycode = nextKey.getKeyCode();
5327 final int repeatCount = nextKey.getRepeatCount();
5328 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5329 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005332 if (callingUid == 0 ||
5333 mContext.checkPermission(
5334 android.Manifest.permission.INJECT_EVENTS,
5335 callingPid, callingUid)
5336 == PackageManager.PERMISSION_GRANTED) {
5337 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005338 nextKey.getMetaState(), down, repeatCount,
5339 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 Log.w(TAG, "Event timeout during app switch: dropping "
5342 + nextKey);
5343 return SKIP_TARGET_TOKEN;
5344 }
Romain Guy06882f82009-06-10 13:36:04 -07005345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 WindowState focus = null;
5349 synchronized(mWindowMap) {
5350 focus = getFocusedWindowLocked();
5351 }
Romain Guy06882f82009-06-10 13:36:04 -07005352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005354
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005355 if (callingUid == 0 ||
5356 (focus != null && callingUid == focus.mSession.mUid) ||
5357 mContext.checkPermission(
5358 android.Manifest.permission.INJECT_EVENTS,
5359 callingPid, callingUid)
5360 == PackageManager.PERMISSION_GRANTED) {
5361 if (mPolicy.interceptKeyTi(focus,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005362 keycode, nextKey.getMetaState(), down, repeatCount,
5363 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005364 return CONSUMED_EVENT_TOKEN;
5365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 }
Romain Guy06882f82009-06-10 13:36:04 -07005367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 } else if (!isPointerEvent) {
5371 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5372 if (!dispatch) {
5373 Log.w(TAG, "Event timeout during app switch: dropping trackball "
5374 + nextMotion);
5375 return SKIP_TARGET_TOKEN;
5376 }
Romain Guy06882f82009-06-10 13:36:04 -07005377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005378 WindowState focus = null;
5379 synchronized(mWindowMap) {
5380 focus = getFocusedWindowLocked();
5381 }
Romain Guy06882f82009-06-10 13:36:04 -07005382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5384 return focus;
5385 }
Romain Guy06882f82009-06-10 13:36:04 -07005386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 if (nextMotion == null) {
5388 return SKIP_TARGET_TOKEN;
5389 }
Romain Guy06882f82009-06-10 13:36:04 -07005390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5392 KeyEvent.KEYCODE_UNKNOWN);
5393 if (!dispatch) {
5394 Log.w(TAG, "Event timeout during app switch: dropping pointer "
5395 + nextMotion);
5396 return SKIP_TARGET_TOKEN;
5397 }
Romain Guy06882f82009-06-10 13:36:04 -07005398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 // Find the target window for a pointer event.
5400 int action = nextMotion.getAction();
5401 final float xf = nextMotion.getX();
5402 final float yf = nextMotion.getY();
5403 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 final boolean screenWasOff = qev != null
5406 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 synchronized(mWindowMap) {
5411 synchronized (this) {
5412 if (action == MotionEvent.ACTION_DOWN) {
5413 if (mMotionTarget != null) {
5414 // this is weird, we got a pen down, but we thought it was
5415 // already down!
5416 // XXX: We should probably send an ACTION_UP to the current
5417 // target.
5418 Log.w(TAG, "Pointer down received while already down in: "
5419 + mMotionTarget);
5420 mMotionTarget = null;
5421 }
Romain Guy06882f82009-06-10 13:36:04 -07005422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 // ACTION_DOWN is special, because we need to lock next events to
5424 // the window we'll land onto.
5425 final int x = (int)xf;
5426 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 final ArrayList windows = mWindows;
5429 final int N = windows.size();
5430 WindowState topErrWindow = null;
5431 final Rect tmpRect = mTempRect;
5432 for (int i=N-1; i>=0; i--) {
5433 WindowState child = (WindowState)windows.get(i);
5434 //Log.i(TAG, "Checking dispatch to: " + child);
5435 final int flags = child.mAttrs.flags;
5436 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5437 if (topErrWindow == null) {
5438 topErrWindow = child;
5439 }
5440 }
5441 if (!child.isVisibleLw()) {
5442 //Log.i(TAG, "Not visible!");
5443 continue;
5444 }
5445 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
5446 //Log.i(TAG, "Not touchable!");
5447 if ((flags & WindowManager.LayoutParams
5448 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5449 child.mNextOutsideTouch = mOutsideTouchTargets;
5450 mOutsideTouchTargets = child;
5451 }
5452 continue;
5453 }
5454 tmpRect.set(child.mFrame);
5455 if (child.mTouchableInsets == ViewTreeObserver
5456 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5457 // The touch is inside of the window if it is
5458 // inside the frame, AND the content part of that
5459 // frame that was given by the application.
5460 tmpRect.left += child.mGivenContentInsets.left;
5461 tmpRect.top += child.mGivenContentInsets.top;
5462 tmpRect.right -= child.mGivenContentInsets.right;
5463 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5464 } else if (child.mTouchableInsets == ViewTreeObserver
5465 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5466 // The touch is inside of the window if it is
5467 // inside the frame, AND the visible part of that
5468 // frame that was given by the application.
5469 tmpRect.left += child.mGivenVisibleInsets.left;
5470 tmpRect.top += child.mGivenVisibleInsets.top;
5471 tmpRect.right -= child.mGivenVisibleInsets.right;
5472 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5473 }
5474 final int touchFlags = flags &
5475 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5476 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5477 if (tmpRect.contains(x, y) || touchFlags == 0) {
5478 //Log.i(TAG, "Using this target!");
5479 if (!screenWasOff || (flags &
5480 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5481 mMotionTarget = child;
5482 } else {
5483 //Log.i(TAG, "Waking, skip!");
5484 mMotionTarget = null;
5485 }
5486 break;
5487 }
Romain Guy06882f82009-06-10 13:36:04 -07005488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 if ((flags & WindowManager.LayoutParams
5490 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5491 child.mNextOutsideTouch = mOutsideTouchTargets;
5492 mOutsideTouchTargets = child;
5493 //Log.i(TAG, "Adding to outside target list: " + child);
5494 }
5495 }
5496
5497 // if there's an error window but it's not accepting
5498 // focus (typically because it is not yet visible) just
5499 // wait for it -- any other focused window may in fact
5500 // be in ANR state.
5501 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5502 mMotionTarget = null;
5503 }
5504 }
Romain Guy06882f82009-06-10 13:36:04 -07005505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 target = mMotionTarget;
5507 }
5508 }
Romain Guy06882f82009-06-10 13:36:04 -07005509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 // Pointer events are a little different -- if there isn't a
5513 // target found for any event, then just drop it.
5514 return target != null ? target : SKIP_TARGET_TOKEN;
5515 }
Romain Guy06882f82009-06-10 13:36:04 -07005516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 boolean checkShouldDispatchKey(int keycode) {
5518 synchronized (this) {
5519 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5520 mTimeToSwitch = 0;
5521 return true;
5522 }
5523 if (mTimeToSwitch != 0
5524 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5525 return false;
5526 }
5527 return true;
5528 }
5529 }
Romain Guy06882f82009-06-10 13:36:04 -07005530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 void bindTargetWindowLocked(WindowState win,
5532 int pendingWhat, QueuedEvent pendingMotion) {
5533 synchronized (this) {
5534 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5535 }
5536 }
Romain Guy06882f82009-06-10 13:36:04 -07005537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 void bindTargetWindowLocked(WindowState win) {
5539 synchronized (this) {
5540 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5541 }
5542 }
5543
5544 void bindTargetWindowLockedLocked(WindowState win,
5545 int pendingWhat, QueuedEvent pendingMotion) {
5546 mLastWin = win;
5547 mLastBinder = win.mClient.asBinder();
5548 mFinished = false;
5549 if (pendingMotion != null) {
5550 final Session s = win.mSession;
5551 if (pendingWhat == RETURN_PENDING_POINTER) {
5552 releasePendingPointerLocked(s);
5553 s.mPendingPointerMove = pendingMotion;
5554 s.mPendingPointerWindow = win;
Romain Guy06882f82009-06-10 13:36:04 -07005555 if (DEBUG_INPUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 "bindTargetToWindow " + s.mPendingPointerMove);
5557 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5558 releasePendingTrackballLocked(s);
5559 s.mPendingTrackballMove = pendingMotion;
5560 s.mPendingTrackballWindow = win;
5561 }
5562 }
5563 }
Romain Guy06882f82009-06-10 13:36:04 -07005564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 void releasePendingPointerLocked(Session s) {
5566 if (DEBUG_INPUT) Log.v(TAG,
5567 "releasePendingPointer " + s.mPendingPointerMove);
5568 if (s.mPendingPointerMove != null) {
5569 mQueue.recycleEvent(s.mPendingPointerMove);
5570 s.mPendingPointerMove = null;
5571 }
5572 }
Romain Guy06882f82009-06-10 13:36:04 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 void releasePendingTrackballLocked(Session s) {
5575 if (s.mPendingTrackballMove != null) {
5576 mQueue.recycleEvent(s.mPendingTrackballMove);
5577 s.mPendingTrackballMove = null;
5578 }
5579 }
Romain Guy06882f82009-06-10 13:36:04 -07005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5582 int returnWhat) {
5583 if (DEBUG_INPUT) Log.v(
5584 TAG, "finishedKey: client=" + client + ", force=" + force);
5585
5586 if (client == null) {
5587 return null;
5588 }
5589
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005590 MotionEvent res = null;
5591 QueuedEvent qev = null;
5592 WindowState win = null;
5593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 synchronized (this) {
5595 if (DEBUG_INPUT) Log.v(
5596 TAG, "finishedKey: client=" + client.asBinder()
5597 + ", force=" + force + ", last=" + mLastBinder
5598 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
5599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 if (returnWhat == RETURN_PENDING_POINTER) {
5601 qev = session.mPendingPointerMove;
5602 win = session.mPendingPointerWindow;
5603 session.mPendingPointerMove = null;
5604 session.mPendingPointerWindow = null;
5605 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
5606 qev = session.mPendingTrackballMove;
5607 win = session.mPendingTrackballWindow;
5608 session.mPendingTrackballMove = null;
5609 session.mPendingTrackballWindow = null;
5610 }
Romain Guy06882f82009-06-10 13:36:04 -07005611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 if (mLastBinder == client.asBinder()) {
5613 if (DEBUG_INPUT) Log.v(
5614 TAG, "finishedKey: last paused="
5615 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
5616 if (mLastWin != null && (!mLastWin.mToken.paused || force
5617 || !mEventDispatching)) {
5618 doFinishedKeyLocked(false);
5619 } else {
5620 // Make sure to wake up anyone currently waiting to
5621 // dispatch a key, so they can re-evaluate their
5622 // current situation.
5623 mFinished = true;
5624 notifyAll();
5625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 }
Romain Guy06882f82009-06-10 13:36:04 -07005627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005629 res = (MotionEvent)qev.event;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 if (DEBUG_INPUT) Log.v(TAG,
5631 "Returning pending motion: " + res);
5632 mQueue.recycleEvent(qev);
5633 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
5634 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
5635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005637 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005638
5639 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
5640 synchronized (mWindowMap) {
5641 if (mWallpaperTarget == win) {
5642 sendPointerToWallpaperLocked(win, res, res.getEventTime());
5643 }
5644 }
5645 }
5646
5647 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 }
5649
5650 void tickle() {
5651 synchronized (this) {
5652 notifyAll();
5653 }
5654 }
Romain Guy06882f82009-06-10 13:36:04 -07005655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 void handleNewWindowLocked(WindowState newWindow) {
5657 if (!newWindow.canReceiveKeys()) {
5658 return;
5659 }
5660 synchronized (this) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005661 if (DEBUG_INPUT) Log.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 TAG, "New key dispatch window: win="
5663 + newWindow.mClient.asBinder()
5664 + ", last=" + mLastBinder
5665 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5666 + "), finished=" + mFinished + ", paused="
5667 + newWindow.mToken.paused);
5668
5669 // Displaying a window implicitly causes dispatching to
5670 // be unpaused. (This is to protect against bugs if someone
5671 // pauses dispatching but forgets to resume.)
5672 newWindow.mToken.paused = false;
5673
5674 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675
5676 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
5677 if (DEBUG_INPUT) Log.v(TAG,
5678 "New SYSTEM_ERROR window; resetting state");
5679 mLastWin = null;
5680 mLastBinder = null;
5681 mMotionTarget = null;
5682 mFinished = true;
5683 } else if (mLastWin != null) {
5684 // If the new window is above the window we are
5685 // waiting on, then stop waiting and let key dispatching
5686 // start on the new guy.
5687 if (DEBUG_INPUT) Log.v(
5688 TAG, "Last win layer=" + mLastWin.mLayer
5689 + ", new win layer=" + newWindow.mLayer);
5690 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005691 // The new window is above the old; finish pending input to the last
5692 // window and start directing it to the new one.
5693 mLastWin.mToken.paused = false;
5694 doFinishedKeyLocked(true); // does a notifyAll()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 }
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005696 // Either the new window is lower, so there is no need to wake key waiters,
5697 // or we just finished key input to the previous window, which implicitly
5698 // notified the key waiters. In both cases, we don't need to issue the
5699 // notification here.
5700 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 }
5702
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005703 // Now that we've put a new window state in place, make the event waiter
5704 // take notice and retarget its attentions.
5705 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 }
5707 }
5708
5709 void pauseDispatchingLocked(WindowToken token) {
5710 synchronized (this)
5711 {
5712 if (DEBUG_INPUT) Log.v(TAG, "Pausing WindowToken " + token);
5713 token.paused = true;
5714
5715 /*
5716 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
5717 mPaused = true;
5718 } else {
5719 if (mLastWin == null) {
Dave Bortcfe65242009-04-09 14:51:04 -07005720 Log.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 } else if (mFinished) {
Dave Bortcfe65242009-04-09 14:51:04 -07005722 Log.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 } else {
Dave Bortcfe65242009-04-09 14:51:04 -07005724 Log.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 }
5726 }
5727 */
5728 }
5729 }
5730
5731 void resumeDispatchingLocked(WindowToken token) {
5732 synchronized (this) {
5733 if (token.paused) {
5734 if (DEBUG_INPUT) Log.v(
5735 TAG, "Resuming WindowToken " + token
5736 + ", last=" + mLastBinder
5737 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
5738 + "), finished=" + mFinished + ", paused="
5739 + token.paused);
5740 token.paused = false;
5741 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
5742 doFinishedKeyLocked(true);
5743 } else {
5744 notifyAll();
5745 }
5746 }
5747 }
5748 }
5749
5750 void setEventDispatchingLocked(boolean enabled) {
5751 synchronized (this) {
5752 mEventDispatching = enabled;
5753 notifyAll();
5754 }
5755 }
Romain Guy06882f82009-06-10 13:36:04 -07005756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 void appSwitchComing() {
5758 synchronized (this) {
5759 // Don't wait for more than .5 seconds for app to finish
5760 // processing the pending events.
5761 long now = SystemClock.uptimeMillis() + 500;
5762 if (DEBUG_INPUT) Log.v(TAG, "appSwitchComing: " + now);
5763 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
5764 mTimeToSwitch = now;
5765 }
5766 notifyAll();
5767 }
5768 }
Romain Guy06882f82009-06-10 13:36:04 -07005769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 private final void doFinishedKeyLocked(boolean doRecycle) {
5771 if (mLastWin != null) {
5772 releasePendingPointerLocked(mLastWin.mSession);
5773 releasePendingTrackballLocked(mLastWin.mSession);
5774 }
Romain Guy06882f82009-06-10 13:36:04 -07005775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 if (mLastWin == null || !mLastWin.mToken.paused
5777 || !mLastWin.isVisibleLw()) {
5778 // If the current window has been paused, we aren't -really-
5779 // finished... so let the waiters still wait.
5780 mLastWin = null;
5781 mLastBinder = null;
5782 }
5783 mFinished = true;
5784 notifyAll();
5785 }
5786 }
5787
5788 private class KeyQ extends KeyInputQueue
5789 implements KeyInputQueue.FilterCallback {
5790 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07005791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005793 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
5795 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
5796 "KEEP_SCREEN_ON_FLAG");
5797 mHoldingScreen.setReferenceCounted(false);
5798 }
5799
5800 @Override
5801 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
5802 if (mPolicy.preprocessInputEventTq(event)) {
5803 return true;
5804 }
Romain Guy06882f82009-06-10 13:36:04 -07005805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 switch (event.type) {
5807 case RawInputEvent.EV_KEY: {
5808 // XXX begin hack
5809 if (DEBUG) {
5810 if (event.keycode == KeyEvent.KEYCODE_G) {
5811 if (event.value != 0) {
5812 // G down
5813 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
5814 }
5815 return false;
5816 }
5817 if (event.keycode == KeyEvent.KEYCODE_D) {
5818 if (event.value != 0) {
5819 //dump();
5820 }
5821 return false;
5822 }
5823 }
5824 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07005825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 boolean screenIsOff = !mPowerManager.screenIsOn();
5827 boolean screenIsDim = !mPowerManager.screenIsBright();
5828 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07005829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
5831 mPowerManager.goToSleep(event.when);
5832 }
5833
5834 if (screenIsOff) {
5835 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5836 }
5837 if (screenIsDim) {
5838 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5839 }
5840 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
5841 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07005842 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 }
Romain Guy06882f82009-06-10 13:36:04 -07005844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
5846 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
5847 filterQueue(this);
5848 mKeyWaiter.appSwitchComing();
5849 }
5850 return true;
5851 } else {
5852 return false;
5853 }
5854 }
Romain Guy06882f82009-06-10 13:36:04 -07005855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 case RawInputEvent.EV_REL: {
5857 boolean screenIsOff = !mPowerManager.screenIsOn();
5858 boolean screenIsDim = !mPowerManager.screenIsBright();
5859 if (screenIsOff) {
5860 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
5861 device.classes, event)) {
5862 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5863 return false;
5864 }
5865 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5866 }
5867 if (screenIsDim) {
5868 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5869 }
5870 return true;
5871 }
Romain Guy06882f82009-06-10 13:36:04 -07005872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 case RawInputEvent.EV_ABS: {
5874 boolean screenIsOff = !mPowerManager.screenIsOn();
5875 boolean screenIsDim = !mPowerManager.screenIsBright();
5876 if (screenIsOff) {
5877 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
5878 device.classes, event)) {
5879 //Log.i(TAG, "dropping because screenIsOff and !isWakeKey");
5880 return false;
5881 }
5882 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
5883 }
5884 if (screenIsDim) {
5885 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
5886 }
5887 return true;
5888 }
Romain Guy06882f82009-06-10 13:36:04 -07005889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 default:
5891 return true;
5892 }
5893 }
5894
5895 public int filterEvent(QueuedEvent ev) {
5896 switch (ev.classType) {
5897 case RawInputEvent.CLASS_KEYBOARD:
5898 KeyEvent ke = (KeyEvent)ev.event;
5899 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
5900 Log.w(TAG, "Dropping movement key during app switch: "
5901 + ke.getKeyCode() + ", action=" + ke.getAction());
5902 return FILTER_REMOVE;
5903 }
5904 return FILTER_ABORT;
5905 default:
5906 return FILTER_KEEP;
5907 }
5908 }
Romain Guy06882f82009-06-10 13:36:04 -07005909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 /**
5911 * Must be called with the main window manager lock held.
5912 */
5913 void setHoldScreenLocked(boolean holding) {
5914 boolean state = mHoldingScreen.isHeld();
5915 if (holding != state) {
5916 if (holding) {
5917 mHoldingScreen.acquire();
5918 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005919 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920 mHoldingScreen.release();
5921 }
5922 }
5923 }
Michael Chan53071d62009-05-13 17:29:48 -07005924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925
5926 public boolean detectSafeMode() {
5927 mSafeMode = mPolicy.detectSafeMode();
5928 return mSafeMode;
5929 }
Romain Guy06882f82009-06-10 13:36:04 -07005930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 public void systemReady() {
5932 mPolicy.systemReady();
5933 }
Romain Guy06882f82009-06-10 13:36:04 -07005934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 private final class InputDispatcherThread extends Thread {
5936 // Time to wait when there is nothing to do: 9999 seconds.
5937 static final int LONG_WAIT=9999*1000;
5938
5939 public InputDispatcherThread() {
5940 super("InputDispatcher");
5941 }
Romain Guy06882f82009-06-10 13:36:04 -07005942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 @Override
5944 public void run() {
5945 while (true) {
5946 try {
5947 process();
5948 } catch (Exception e) {
5949 Log.e(TAG, "Exception in input dispatcher", e);
5950 }
5951 }
5952 }
Romain Guy06882f82009-06-10 13:36:04 -07005953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 private void process() {
5955 android.os.Process.setThreadPriority(
5956 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07005957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 // The last key event we saw
5959 KeyEvent lastKey = null;
5960
5961 // Last keydown time for auto-repeating keys
5962 long lastKeyTime = SystemClock.uptimeMillis();
5963 long nextKeyTime = lastKeyTime+LONG_WAIT;
5964
Romain Guy06882f82009-06-10 13:36:04 -07005965 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 int keyRepeatCount = 0;
5967
5968 // Need to report that configuration has changed?
5969 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07005970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 while (true) {
5972 long curTime = SystemClock.uptimeMillis();
5973
5974 if (DEBUG_INPUT) Log.v(
5975 TAG, "Waiting for next key: now=" + curTime
5976 + ", repeat @ " + nextKeyTime);
5977
5978 // Retrieve next event, waiting only as long as the next
5979 // repeat timeout. If the configuration has changed, then
5980 // don't wait at all -- we'll report the change as soon as
5981 // we have processed all events.
5982 QueuedEvent ev = mQueue.getEvent(
5983 (int)((!configChanged && curTime < nextKeyTime)
5984 ? (nextKeyTime-curTime) : 0));
5985
5986 if (DEBUG_INPUT && ev != null) Log.v(
5987 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
5988
Michael Chan53071d62009-05-13 17:29:48 -07005989 if (MEASURE_LATENCY) {
5990 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
5991 }
5992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 try {
5994 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07005995 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 int eventType;
5997 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
5998 eventType = eventType((MotionEvent)ev.event);
5999 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6000 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6001 eventType = LocalPowerManager.BUTTON_EVENT;
6002 } else {
6003 eventType = LocalPowerManager.OTHER_EVENT;
6004 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006005 try {
Michael Chan53071d62009-05-13 17:29:48 -07006006 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006007 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006008 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006009 mBatteryStats.noteInputEvent();
6010 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006011 } catch (RemoteException e) {
6012 // Ignore
6013 }
Michael Chane10de972009-05-18 11:24:50 -07006014
6015 if (eventType != TOUCH_EVENT
6016 && eventType != LONG_TOUCH_EVENT
6017 && eventType != CHEEK_EVENT) {
6018 mPowerManager.userActivity(curTime, false,
6019 eventType, false);
6020 } else if (mLastTouchEventType != eventType
6021 || (curTime - mLastUserActivityCallTime)
6022 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6023 mLastUserActivityCallTime = curTime;
6024 mLastTouchEventType = eventType;
6025 mPowerManager.userActivity(curTime, false,
6026 eventType, false);
6027 }
6028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 switch (ev.classType) {
6030 case RawInputEvent.CLASS_KEYBOARD:
6031 KeyEvent ke = (KeyEvent)ev.event;
6032 if (ke.isDown()) {
6033 lastKey = ke;
6034 keyRepeatCount = 0;
6035 lastKeyTime = curTime;
6036 nextKeyTime = lastKeyTime
6037 + KEY_REPEAT_FIRST_DELAY;
6038 if (DEBUG_INPUT) Log.v(
6039 TAG, "Received key down: first repeat @ "
6040 + nextKeyTime);
6041 } else {
6042 lastKey = null;
6043 // Arbitrary long timeout.
6044 lastKeyTime = curTime;
6045 nextKeyTime = curTime + LONG_WAIT;
6046 if (DEBUG_INPUT) Log.v(
6047 TAG, "Received key up: ignore repeat @ "
6048 + nextKeyTime);
6049 }
6050 dispatchKey((KeyEvent)ev.event, 0, 0);
6051 mQueue.recycleEvent(ev);
6052 break;
6053 case RawInputEvent.CLASS_TOUCHSCREEN:
6054 //Log.i(TAG, "Read next event " + ev);
6055 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6056 break;
6057 case RawInputEvent.CLASS_TRACKBALL:
6058 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6059 break;
6060 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6061 configChanged = true;
6062 break;
6063 default:
6064 mQueue.recycleEvent(ev);
6065 break;
6066 }
Romain Guy06882f82009-06-10 13:36:04 -07006067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 } else if (configChanged) {
6069 configChanged = false;
6070 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 } else if (lastKey != null) {
6073 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 // Timeout occurred while key was down. If it is at or
6076 // past the key repeat time, dispatch the repeat.
6077 if (DEBUG_INPUT) Log.v(
6078 TAG, "Key timeout: repeat=" + nextKeyTime
6079 + ", now=" + curTime);
6080 if (curTime < nextKeyTime) {
6081 continue;
6082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 lastKeyTime = nextKeyTime;
6085 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6086 keyRepeatCount++;
6087 if (DEBUG_INPUT) Log.v(
6088 TAG, "Key repeat: count=" + keyRepeatCount
6089 + ", next @ " + nextKeyTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07006090 dispatchKey(KeyEvent.changeTimeRepeat(lastKey, curTime, keyRepeatCount), 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 } else {
6093 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 lastKeyTime = curTime;
6096 nextKeyTime = curTime + LONG_WAIT;
6097 }
Romain Guy06882f82009-06-10 13:36:04 -07006098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 } catch (Exception e) {
6100 Log.e(TAG,
6101 "Input thread received uncaught exception: " + e, e);
6102 }
6103 }
6104 }
6105 }
6106
6107 // -------------------------------------------------------------
6108 // Client Session State
6109 // -------------------------------------------------------------
6110
6111 private final class Session extends IWindowSession.Stub
6112 implements IBinder.DeathRecipient {
6113 final IInputMethodClient mClient;
6114 final IInputContext mInputContext;
6115 final int mUid;
6116 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006117 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 SurfaceSession mSurfaceSession;
6119 int mNumWindow = 0;
6120 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 /**
6123 * Current pointer move event being dispatched to client window... must
6124 * hold key lock to access.
6125 */
6126 QueuedEvent mPendingPointerMove;
6127 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 /**
6130 * Current trackball move event being dispatched to client window... must
6131 * hold key lock to access.
6132 */
6133 QueuedEvent mPendingTrackballMove;
6134 WindowState mPendingTrackballWindow;
6135
6136 public Session(IInputMethodClient client, IInputContext inputContext) {
6137 mClient = client;
6138 mInputContext = inputContext;
6139 mUid = Binder.getCallingUid();
6140 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006141 StringBuilder sb = new StringBuilder();
6142 sb.append("Session{");
6143 sb.append(Integer.toHexString(System.identityHashCode(this)));
6144 sb.append(" uid ");
6145 sb.append(mUid);
6146 sb.append("}");
6147 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 synchronized (mWindowMap) {
6150 if (mInputMethodManager == null && mHaveInputMethods) {
6151 IBinder b = ServiceManager.getService(
6152 Context.INPUT_METHOD_SERVICE);
6153 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6154 }
6155 }
6156 long ident = Binder.clearCallingIdentity();
6157 try {
6158 // Note: it is safe to call in to the input method manager
6159 // here because we are not holding our lock.
6160 if (mInputMethodManager != null) {
6161 mInputMethodManager.addClient(client, inputContext,
6162 mUid, mPid);
6163 } else {
6164 client.setUsingInputMethod(false);
6165 }
6166 client.asBinder().linkToDeath(this, 0);
6167 } catch (RemoteException e) {
6168 // The caller has died, so we can just forget about this.
6169 try {
6170 if (mInputMethodManager != null) {
6171 mInputMethodManager.removeClient(client);
6172 }
6173 } catch (RemoteException ee) {
6174 }
6175 } finally {
6176 Binder.restoreCallingIdentity(ident);
6177 }
6178 }
Romain Guy06882f82009-06-10 13:36:04 -07006179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 @Override
6181 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6182 throws RemoteException {
6183 try {
6184 return super.onTransact(code, data, reply, flags);
6185 } catch (RuntimeException e) {
6186 // Log all 'real' exceptions thrown to the caller
6187 if (!(e instanceof SecurityException)) {
6188 Log.e(TAG, "Window Session Crash", e);
6189 }
6190 throw e;
6191 }
6192 }
6193
6194 public void binderDied() {
6195 // Note: it is safe to call in to the input method manager
6196 // here because we are not holding our lock.
6197 try {
6198 if (mInputMethodManager != null) {
6199 mInputMethodManager.removeClient(mClient);
6200 }
6201 } catch (RemoteException e) {
6202 }
6203 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006204 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 mClientDead = true;
6206 killSessionLocked();
6207 }
6208 }
6209
6210 public int add(IWindow window, WindowManager.LayoutParams attrs,
6211 int viewVisibility, Rect outContentInsets) {
6212 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6213 }
Romain Guy06882f82009-06-10 13:36:04 -07006214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 public void remove(IWindow window) {
6216 removeWindow(this, window);
6217 }
Romain Guy06882f82009-06-10 13:36:04 -07006218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6220 int requestedWidth, int requestedHeight, int viewFlags,
6221 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6222 Rect outVisibleInsets, Surface outSurface) {
6223 return relayoutWindow(this, window, attrs,
6224 requestedWidth, requestedHeight, viewFlags, insetsPending,
6225 outFrame, outContentInsets, outVisibleInsets, outSurface);
6226 }
Romain Guy06882f82009-06-10 13:36:04 -07006227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 public void setTransparentRegion(IWindow window, Region region) {
6229 setTransparentRegionWindow(this, window, region);
6230 }
Romain Guy06882f82009-06-10 13:36:04 -07006231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 public void setInsets(IWindow window, int touchableInsets,
6233 Rect contentInsets, Rect visibleInsets) {
6234 setInsetsWindow(this, window, touchableInsets, contentInsets,
6235 visibleInsets);
6236 }
Romain Guy06882f82009-06-10 13:36:04 -07006237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6239 getWindowDisplayFrame(this, window, outDisplayFrame);
6240 }
Romain Guy06882f82009-06-10 13:36:04 -07006241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006242 public void finishDrawing(IWindow window) {
6243 if (localLOGV) Log.v(
6244 TAG, "IWindow finishDrawing called for " + window);
6245 finishDrawingWindow(this, window);
6246 }
6247
6248 public void finishKey(IWindow window) {
6249 if (localLOGV) Log.v(
6250 TAG, "IWindow finishKey called for " + window);
6251 mKeyWaiter.finishedKey(this, window, false,
6252 KeyWaiter.RETURN_NOTHING);
6253 }
6254
6255 public MotionEvent getPendingPointerMove(IWindow window) {
6256 if (localLOGV) Log.v(
6257 TAG, "IWindow getPendingMotionEvent called for " + window);
6258 return mKeyWaiter.finishedKey(this, window, false,
6259 KeyWaiter.RETURN_PENDING_POINTER);
6260 }
Romain Guy06882f82009-06-10 13:36:04 -07006261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 public MotionEvent getPendingTrackballMove(IWindow window) {
6263 if (localLOGV) Log.v(
6264 TAG, "IWindow getPendingMotionEvent called for " + window);
6265 return mKeyWaiter.finishedKey(this, window, false,
6266 KeyWaiter.RETURN_PENDING_TRACKBALL);
6267 }
6268
6269 public void setInTouchMode(boolean mode) {
6270 synchronized(mWindowMap) {
6271 mInTouchMode = mode;
6272 }
6273 }
6274
6275 public boolean getInTouchMode() {
6276 synchronized(mWindowMap) {
6277 return mInTouchMode;
6278 }
6279 }
6280
6281 public boolean performHapticFeedback(IWindow window, int effectId,
6282 boolean always) {
6283 synchronized(mWindowMap) {
6284 long ident = Binder.clearCallingIdentity();
6285 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006286 return mPolicy.performHapticFeedbackLw(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 windowForClientLocked(this, window), effectId, always);
6288 } finally {
6289 Binder.restoreCallingIdentity(ident);
6290 }
6291 }
6292 }
Romain Guy06882f82009-06-10 13:36:04 -07006293
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006294 public void setWallpaperPosition(IBinder window, float x, float y) {
6295 synchronized(mWindowMap) {
6296 long ident = Binder.clearCallingIdentity();
6297 try {
6298 setWindowWallpaperPositionLocked(windowForClientLocked(this, window),
6299 x, y);
6300 } finally {
6301 Binder.restoreCallingIdentity(ident);
6302 }
6303 }
6304 }
6305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 void windowAddedLocked() {
6307 if (mSurfaceSession == null) {
6308 if (localLOGV) Log.v(
6309 TAG, "First window added to " + this + ", creating SurfaceSession");
6310 mSurfaceSession = new SurfaceSession();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006311 if (SHOW_TRANSACTIONS) Log.i(
6312 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 mSessions.add(this);
6314 }
6315 mNumWindow++;
6316 }
6317
6318 void windowRemovedLocked() {
6319 mNumWindow--;
6320 killSessionLocked();
6321 }
Romain Guy06882f82009-06-10 13:36:04 -07006322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006323 void killSessionLocked() {
6324 if (mNumWindow <= 0 && mClientDead) {
6325 mSessions.remove(this);
6326 if (mSurfaceSession != null) {
6327 if (localLOGV) Log.v(
6328 TAG, "Last window removed from " + this
6329 + ", destroying " + mSurfaceSession);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006330 if (SHOW_TRANSACTIONS) Log.i(
6331 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 try {
6333 mSurfaceSession.kill();
6334 } catch (Exception e) {
6335 Log.w(TAG, "Exception thrown when killing surface session "
6336 + mSurfaceSession + " in session " + this
6337 + ": " + e.toString());
6338 }
6339 mSurfaceSession = null;
6340 }
6341 }
6342 }
Romain Guy06882f82009-06-10 13:36:04 -07006343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006345 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6346 pw.print(" mClientDead="); pw.print(mClientDead);
6347 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6348 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6349 pw.print(prefix);
6350 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6351 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6352 }
6353 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6354 pw.print(prefix);
6355 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6356 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006358 }
6359
6360 @Override
6361 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006362 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 }
6364 }
6365
6366 // -------------------------------------------------------------
6367 // Client Window State
6368 // -------------------------------------------------------------
6369
6370 private final class WindowState implements WindowManagerPolicy.WindowState {
6371 final Session mSession;
6372 final IWindow mClient;
6373 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006374 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 AppWindowToken mAppToken;
6376 AppWindowToken mTargetAppToken;
6377 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6378 final DeathRecipient mDeathRecipient;
6379 final WindowState mAttachedWindow;
6380 final ArrayList mChildWindows = new ArrayList();
6381 final int mBaseLayer;
6382 final int mSubLayer;
6383 final boolean mLayoutAttached;
6384 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006385 final boolean mIsWallpaper;
6386 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 int mViewVisibility;
6388 boolean mPolicyVisibility = true;
6389 boolean mPolicyVisibilityAfterAnim = true;
6390 boolean mAppFreezing;
6391 Surface mSurface;
6392 boolean mAttachedHidden; // is our parent window hidden?
6393 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006394 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 int mRequestedWidth;
6396 int mRequestedHeight;
6397 int mLastRequestedWidth;
6398 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 int mLayer;
6400 int mAnimLayer;
6401 int mLastLayer;
6402 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006403 boolean mObscured;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404
6405 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 // Actual frame shown on-screen (may be modified by animation)
6408 final Rect mShownFrame = new Rect();
6409 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006411 /**
6412 * Insets that determine the actually visible area
6413 */
6414 final Rect mVisibleInsets = new Rect();
6415 final Rect mLastVisibleInsets = new Rect();
6416 boolean mVisibleInsetsChanged;
6417
6418 /**
6419 * Insets that are covered by system windows
6420 */
6421 final Rect mContentInsets = new Rect();
6422 final Rect mLastContentInsets = new Rect();
6423 boolean mContentInsetsChanged;
6424
6425 /**
6426 * Set to true if we are waiting for this window to receive its
6427 * given internal insets before laying out other windows based on it.
6428 */
6429 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 /**
6432 * These are the content insets that were given during layout for
6433 * this window, to be applied to windows behind it.
6434 */
6435 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 /**
6438 * These are the visible insets that were given during layout for
6439 * this window, to be applied to windows behind it.
6440 */
6441 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006443 /**
6444 * Flag indicating whether the touchable region should be adjusted by
6445 * the visible insets; if false the area outside the visible insets is
6446 * NOT touchable, so we must use those to adjust the frame during hit
6447 * tests.
6448 */
6449 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 // Current transformation being applied.
6452 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6453 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6454 float mHScale=1, mVScale=1;
6455 float mLastHScale=1, mLastVScale=1;
6456 final Matrix mTmpMatrix = new Matrix();
6457
6458 // "Real" frame that the application sees.
6459 final Rect mFrame = new Rect();
6460 final Rect mLastFrame = new Rect();
6461
6462 final Rect mContainingFrame = new Rect();
6463 final Rect mDisplayFrame = new Rect();
6464 final Rect mContentFrame = new Rect();
6465 final Rect mVisibleFrame = new Rect();
6466
6467 float mShownAlpha = 1;
6468 float mAlpha = 1;
6469 float mLastAlpha = 1;
6470
6471 // Set to true if, when the window gets displayed, it should perform
6472 // an enter animation.
6473 boolean mEnterAnimationPending;
6474
6475 // Currently running animation.
6476 boolean mAnimating;
6477 boolean mLocalAnimating;
6478 Animation mAnimation;
6479 boolean mAnimationIsEntrance;
6480 boolean mHasTransformation;
6481 boolean mHasLocalTransformation;
6482 final Transformation mTransformation = new Transformation();
6483
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006484 // If a window showing a wallpaper: the requested offset for the
6485 // wallpaper; if a wallpaper window: the currently applied offset.
6486 float mWallpaperX = -1;
6487 float mWallpaperY = -1;
6488
6489 // Wallpaper windows: pixels offset based on above variables.
6490 int mXOffset;
6491 int mYOffset;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 // This is set after IWindowSession.relayout() has been called at
6494 // least once for the window. It allows us to detect the situation
6495 // where we don't yet have a surface, but should have one soon, so
6496 // we can give the window focus before waiting for the relayout.
6497 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 // This is set after the Surface has been created but before the
6500 // window has been drawn. During this time the surface is hidden.
6501 boolean mDrawPending;
6502
6503 // This is set after the window has finished drawing for the first
6504 // time but before its surface is shown. The surface will be
6505 // displayed when the next layout is run.
6506 boolean mCommitDrawPending;
6507
6508 // This is set during the time after the window's drawing has been
6509 // committed, and before its surface is actually shown. It is used
6510 // to delay showing the surface until all windows in a token are ready
6511 // to be shown.
6512 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 // Set when the window has been shown in the screen the first time.
6515 boolean mHasDrawn;
6516
6517 // Currently running an exit animation?
6518 boolean mExiting;
6519
6520 // Currently on the mDestroySurface list?
6521 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 // Completely remove from window manager after exit animation?
6524 boolean mRemoveOnExit;
6525
6526 // Set when the orientation is changing and this window has not yet
6527 // been updated for the new orientation.
6528 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 // Is this window now (or just being) removed?
6531 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07006532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 WindowState(Session s, IWindow c, WindowToken token,
6534 WindowState attachedWindow, WindowManager.LayoutParams a,
6535 int viewVisibility) {
6536 mSession = s;
6537 mClient = c;
6538 mToken = token;
6539 mAttrs.copyFrom(a);
6540 mViewVisibility = viewVisibility;
6541 DeathRecipient deathRecipient = new DeathRecipient();
6542 mAlpha = a.alpha;
6543 if (localLOGV) Log.v(
6544 TAG, "Window " + this + " client=" + c.asBinder()
6545 + " token=" + token + " (" + mAttrs.token + ")");
6546 try {
6547 c.asBinder().linkToDeath(deathRecipient, 0);
6548 } catch (RemoteException e) {
6549 mDeathRecipient = null;
6550 mAttachedWindow = null;
6551 mLayoutAttached = false;
6552 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006553 mIsWallpaper = false;
6554 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 mBaseLayer = 0;
6556 mSubLayer = 0;
6557 return;
6558 }
6559 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07006560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
6562 mAttrs.type <= LAST_SUB_WINDOW)) {
6563 // The multiplier here is to reserve space for multiple
6564 // windows in the same type layer.
6565 mBaseLayer = mPolicy.windowTypeToLayerLw(
6566 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
6567 + TYPE_LAYER_OFFSET;
6568 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
6569 mAttachedWindow = attachedWindow;
6570 mAttachedWindow.mChildWindows.add(this);
6571 mLayoutAttached = mAttrs.type !=
6572 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
6573 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
6574 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006575 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
6576 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 } else {
6578 // The multiplier here is to reserve space for multiple
6579 // windows in the same type layer.
6580 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
6581 * TYPE_LAYER_MULTIPLIER
6582 + TYPE_LAYER_OFFSET;
6583 mSubLayer = 0;
6584 mAttachedWindow = null;
6585 mLayoutAttached = false;
6586 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
6587 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006588 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
6589 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 }
6591
6592 WindowState appWin = this;
6593 while (appWin.mAttachedWindow != null) {
6594 appWin = mAttachedWindow;
6595 }
6596 WindowToken appToken = appWin.mToken;
6597 while (appToken.appWindowToken == null) {
6598 WindowToken parent = mTokenMap.get(appToken.token);
6599 if (parent == null || appToken == parent) {
6600 break;
6601 }
6602 appToken = parent;
6603 }
The Android Open Source Project10592532009-03-18 17:39:46 -07006604 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006605 mAppToken = appToken.appWindowToken;
6606
6607 mSurface = null;
6608 mRequestedWidth = 0;
6609 mRequestedHeight = 0;
6610 mLastRequestedWidth = 0;
6611 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006612 mXOffset = 0;
6613 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 mLayer = 0;
6615 mAnimLayer = 0;
6616 mLastLayer = 0;
6617 }
6618
6619 void attach() {
6620 if (localLOGV) Log.v(
6621 TAG, "Attaching " + this + " token=" + mToken
6622 + ", list=" + mToken.windows);
6623 mSession.windowAddedLocked();
6624 }
6625
6626 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
6627 mHaveFrame = true;
6628
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006629 final Rect container = mContainingFrame;
6630 container.set(pf);
6631
6632 final Rect display = mDisplayFrame;
6633 display.set(df);
6634
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07006635 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006636 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07006637 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
6638 display.intersect(mCompatibleScreenFrame);
6639 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006640 }
6641
6642 final int pw = container.right - container.left;
6643 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644
6645 int w,h;
6646 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
6647 w = mAttrs.width < 0 ? pw : mAttrs.width;
6648 h = mAttrs.height< 0 ? ph : mAttrs.height;
6649 } else {
6650 w = mAttrs.width == mAttrs.FILL_PARENT ? pw : mRequestedWidth;
6651 h = mAttrs.height== mAttrs.FILL_PARENT ? ph : mRequestedHeight;
6652 }
Romain Guy06882f82009-06-10 13:36:04 -07006653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654 final Rect content = mContentFrame;
6655 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07006656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 final Rect visible = mVisibleFrame;
6658 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07006659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006661 final int fw = frame.width();
6662 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07006663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 //System.out.println("In: w=" + w + " h=" + h + " container=" +
6665 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
6666
6667 Gravity.apply(mAttrs.gravity, w, h, container,
6668 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
6669 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
6670
6671 //System.out.println("Out: " + mFrame);
6672
6673 // Now make sure the window fits in the overall display.
6674 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07006675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 // Make sure the content and visible frames are inside of the
6677 // final window frame.
6678 if (content.left < frame.left) content.left = frame.left;
6679 if (content.top < frame.top) content.top = frame.top;
6680 if (content.right > frame.right) content.right = frame.right;
6681 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
6682 if (visible.left < frame.left) visible.left = frame.left;
6683 if (visible.top < frame.top) visible.top = frame.top;
6684 if (visible.right > frame.right) visible.right = frame.right;
6685 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 final Rect contentInsets = mContentInsets;
6688 contentInsets.left = content.left-frame.left;
6689 contentInsets.top = content.top-frame.top;
6690 contentInsets.right = frame.right-content.right;
6691 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006693 final Rect visibleInsets = mVisibleInsets;
6694 visibleInsets.left = visible.left-frame.left;
6695 visibleInsets.top = visible.top-frame.top;
6696 visibleInsets.right = frame.right-visible.right;
6697 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07006698
Dianne Hackborn284ac932009-08-28 10:34:25 -07006699 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
6700 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
6701 mDisplay.getHeight());
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006702 }
6703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 if (localLOGV) {
6705 //if ("com.google.android.youtube".equals(mAttrs.packageName)
6706 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
6707 Log.v(TAG, "Resolving (mRequestedWidth="
6708 + mRequestedWidth + ", mRequestedheight="
6709 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
6710 + "): frame=" + mFrame.toShortString()
6711 + " ci=" + contentInsets.toShortString()
6712 + " vi=" + visibleInsets.toShortString());
6713 //}
6714 }
6715 }
Romain Guy06882f82009-06-10 13:36:04 -07006716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 public Rect getFrameLw() {
6718 return mFrame;
6719 }
6720
6721 public Rect getShownFrameLw() {
6722 return mShownFrame;
6723 }
6724
6725 public Rect getDisplayFrameLw() {
6726 return mDisplayFrame;
6727 }
6728
6729 public Rect getContentFrameLw() {
6730 return mContentFrame;
6731 }
6732
6733 public Rect getVisibleFrameLw() {
6734 return mVisibleFrame;
6735 }
6736
6737 public boolean getGivenInsetsPendingLw() {
6738 return mGivenInsetsPending;
6739 }
6740
6741 public Rect getGivenContentInsetsLw() {
6742 return mGivenContentInsets;
6743 }
Romain Guy06882f82009-06-10 13:36:04 -07006744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 public Rect getGivenVisibleInsetsLw() {
6746 return mGivenVisibleInsets;
6747 }
Romain Guy06882f82009-06-10 13:36:04 -07006748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 public WindowManager.LayoutParams getAttrs() {
6750 return mAttrs;
6751 }
6752
6753 public int getSurfaceLayer() {
6754 return mLayer;
6755 }
Romain Guy06882f82009-06-10 13:36:04 -07006756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 public IApplicationToken getAppToken() {
6758 return mAppToken != null ? mAppToken.appToken : null;
6759 }
6760
6761 public boolean hasAppShownWindows() {
6762 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
6763 }
6764
6765 public boolean hasAppStartingIcon() {
6766 return mAppToken != null ? (mAppToken.startingData != null) : false;
6767 }
6768
6769 public WindowManagerPolicy.WindowState getAppStartingWindow() {
6770 return mAppToken != null ? mAppToken.startingWindow : null;
6771 }
6772
6773 public void setAnimation(Animation anim) {
6774 if (localLOGV) Log.v(
6775 TAG, "Setting animation in " + this + ": " + anim);
6776 mAnimating = false;
6777 mLocalAnimating = false;
6778 mAnimation = anim;
6779 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
6780 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
6781 }
6782
6783 public void clearAnimation() {
6784 if (mAnimation != null) {
6785 mAnimating = true;
6786 mLocalAnimating = false;
6787 mAnimation = null;
6788 }
6789 }
Romain Guy06882f82009-06-10 13:36:04 -07006790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 Surface createSurfaceLocked() {
6792 if (mSurface == null) {
6793 mDrawPending = true;
6794 mCommitDrawPending = false;
6795 mReadyToShow = false;
6796 if (mAppToken != null) {
6797 mAppToken.allDrawn = false;
6798 }
6799
6800 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07006801 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 flags |= Surface.PUSH_BUFFERS;
6803 }
6804
6805 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
6806 flags |= Surface.SECURE;
6807 }
6808 if (DEBUG_VISIBILITY) Log.v(
6809 TAG, "Creating surface in session "
6810 + mSession.mSurfaceSession + " window " + this
6811 + " w=" + mFrame.width()
6812 + " h=" + mFrame.height() + " format="
6813 + mAttrs.format + " flags=" + flags);
6814
6815 int w = mFrame.width();
6816 int h = mFrame.height();
6817 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
6818 // for a scaled surface, we always want the requested
6819 // size.
6820 w = mRequestedWidth;
6821 h = mRequestedHeight;
6822 }
6823
6824 try {
6825 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07006826 mSession.mSurfaceSession, mSession.mPid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 0, w, h, mAttrs.format, flags);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006828 if (SHOW_TRANSACTIONS) Log.i(TAG, " CREATE SURFACE "
6829 + mSurface + " IN SESSION "
6830 + mSession.mSurfaceSession
6831 + ": pid=" + mSession.mPid + " format="
6832 + mAttrs.format + " flags=0x"
6833 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 } catch (Surface.OutOfResourcesException e) {
6835 Log.w(TAG, "OutOfResourcesException creating surface");
6836 reclaimSomeSurfaceMemoryLocked(this, "create");
6837 return null;
6838 } catch (Exception e) {
6839 Log.e(TAG, "Exception creating surface", e);
6840 return null;
6841 }
Romain Guy06882f82009-06-10 13:36:04 -07006842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 if (localLOGV) Log.v(
6844 TAG, "Got surface: " + mSurface
6845 + ", set left=" + mFrame.left + " top=" + mFrame.top
6846 + ", animLayer=" + mAnimLayer);
6847 if (SHOW_TRANSACTIONS) {
6848 Log.i(TAG, ">>> OPEN TRANSACTION");
6849 Log.i(TAG, " SURFACE " + mSurface + ": CREATE ("
6850 + mAttrs.getTitle() + ") pos=(" +
6851 mFrame.left + "," + mFrame.top + ") (" +
6852 mFrame.width() + "x" + mFrame.height() + "), layer=" +
6853 mAnimLayer + " HIDE");
6854 }
6855 Surface.openTransaction();
6856 try {
6857 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07006858 mSurface.setPosition(mFrame.left + mXOffset,
6859 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 mSurface.setLayer(mAnimLayer);
6861 mSurface.hide();
6862 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006863 if (SHOW_TRANSACTIONS) Log.i(TAG, " SURFACE "
6864 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 mSurface.setFlags(Surface.SURFACE_DITHER,
6866 Surface.SURFACE_DITHER);
6867 }
6868 } catch (RuntimeException e) {
6869 Log.w(TAG, "Error creating surface in " + w, e);
6870 reclaimSomeSurfaceMemoryLocked(this, "create-init");
6871 }
6872 mLastHidden = true;
6873 } finally {
6874 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
6875 Surface.closeTransaction();
6876 }
6877 if (localLOGV) Log.v(
6878 TAG, "Created surface " + this);
6879 }
6880 return mSurface;
6881 }
Romain Guy06882f82009-06-10 13:36:04 -07006882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 void destroySurfaceLocked() {
6884 // Window is no longer on-screen, so can no longer receive
6885 // key events... if we were waiting for it to finish
6886 // handling a key event, the wait is over!
6887 mKeyWaiter.finishedKey(mSession, mClient, true,
6888 KeyWaiter.RETURN_NOTHING);
6889 mKeyWaiter.releasePendingPointerLocked(mSession);
6890 mKeyWaiter.releasePendingTrackballLocked(mSession);
6891
6892 if (mAppToken != null && this == mAppToken.startingWindow) {
6893 mAppToken.startingDisplayed = false;
6894 }
Romain Guy06882f82009-06-10 13:36:04 -07006895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 if (mSurface != null) {
6897 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07006898 if (DEBUG_VISIBILITY) {
6899 RuntimeException e = new RuntimeException();
6900 e.fillInStackTrace();
6901 Log.w(TAG, "Window " + this + " destroying surface "
6902 + mSurface + ", session " + mSession, e);
6903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 if (SHOW_TRANSACTIONS) {
6905 RuntimeException ex = new RuntimeException();
6906 ex.fillInStackTrace();
6907 Log.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
6908 + mAttrs.getTitle() + ")", ex);
6909 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006910 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 } catch (RuntimeException e) {
6912 Log.w(TAG, "Exception thrown when destroying Window " + this
6913 + " surface " + mSurface + " session " + mSession
6914 + ": " + e.toString());
6915 }
6916 mSurface = null;
6917 mDrawPending = false;
6918 mCommitDrawPending = false;
6919 mReadyToShow = false;
6920
6921 int i = mChildWindows.size();
6922 while (i > 0) {
6923 i--;
6924 WindowState c = (WindowState)mChildWindows.get(i);
6925 c.mAttachedHidden = true;
6926 }
6927 }
6928 }
6929
6930 boolean finishDrawingLocked() {
6931 if (mDrawPending) {
6932 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.v(
6933 TAG, "finishDrawingLocked: " + mSurface);
6934 mCommitDrawPending = true;
6935 mDrawPending = false;
6936 return true;
6937 }
6938 return false;
6939 }
6940
6941 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006942 boolean commitFinishDrawingLocked(long currentTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 //Log.i(TAG, "commitFinishDrawingLocked: " + mSurface);
6944 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006945 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 }
6947 mCommitDrawPending = false;
6948 mReadyToShow = true;
6949 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
6950 final AppWindowToken atoken = mAppToken;
6951 if (atoken == null || atoken.allDrawn || starting) {
6952 performShowLocked();
6953 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07006954 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
6956
6957 // This must be called while inside a transaction.
6958 boolean performShowLocked() {
6959 if (DEBUG_VISIBILITY) {
6960 RuntimeException e = new RuntimeException();
6961 e.fillInStackTrace();
6962 Log.v(TAG, "performShow on " + this
6963 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
6964 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
6965 }
6966 if (mReadyToShow && isReadyForDisplay()) {
6967 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Log.i(
6968 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
6969 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + this
6970 + " during animation: policyVis=" + mPolicyVisibility
6971 + " attHidden=" + mAttachedHidden
6972 + " tok.hiddenRequested="
6973 + (mAppToken != null ? mAppToken.hiddenRequested : false)
6974 + " tok.idden="
6975 + (mAppToken != null ? mAppToken.hidden : false)
6976 + " animating=" + mAnimating
6977 + " tok animating="
6978 + (mAppToken != null ? mAppToken.animating : false));
6979 if (!showSurfaceRobustlyLocked(this)) {
6980 return false;
6981 }
6982 mLastAlpha = -1;
6983 mHasDrawn = true;
6984 mLastHidden = false;
6985 mReadyToShow = false;
6986 enableScreenIfNeededLocked();
6987
6988 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07006989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 int i = mChildWindows.size();
6991 while (i > 0) {
6992 i--;
6993 WindowState c = (WindowState)mChildWindows.get(i);
6994 if (c.mSurface != null && c.mAttachedHidden) {
6995 c.mAttachedHidden = false;
6996 c.performShowLocked();
6997 }
6998 }
Romain Guy06882f82009-06-10 13:36:04 -07006999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 if (mAttrs.type != TYPE_APPLICATION_STARTING
7001 && mAppToken != null) {
7002 mAppToken.firstWindowDrawn = true;
7003 if (mAnimation == null && mAppToken.startingData != null) {
7004 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7005 + mToken
7006 + ": first real window is shown, no animation");
7007 mFinishedStarting.add(mAppToken);
7008 mH.sendEmptyMessage(H.FINISHED_STARTING);
7009 }
7010 mAppToken.updateReportedVisibilityLocked();
7011 }
7012 }
7013 return true;
7014 }
Romain Guy06882f82009-06-10 13:36:04 -07007015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016 // This must be called while inside a transaction. Returns true if
7017 // there is more animation to run.
7018 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7019 if (!mDisplayFrozen) {
7020 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7023 mHasTransformation = true;
7024 mHasLocalTransformation = true;
7025 if (!mLocalAnimating) {
7026 if (DEBUG_ANIM) Log.v(
7027 TAG, "Starting animation in " + this +
7028 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7029 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7030 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7031 mAnimation.setStartTime(currentTime);
7032 mLocalAnimating = true;
7033 mAnimating = true;
7034 }
7035 mTransformation.clear();
7036 final boolean more = mAnimation.getTransformation(
7037 currentTime, mTransformation);
7038 if (DEBUG_ANIM) Log.v(
7039 TAG, "Stepped animation in " + this +
7040 ": more=" + more + ", xform=" + mTransformation);
7041 if (more) {
7042 // we're not done!
7043 return true;
7044 }
7045 if (DEBUG_ANIM) Log.v(
7046 TAG, "Finished animation in " + this +
7047 " @ " + currentTime);
7048 mAnimation = null;
7049 //WindowManagerService.this.dump();
7050 }
7051 mHasLocalTransformation = false;
7052 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007053 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 // When our app token is animating, we kind-of pretend like
7055 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7056 // part of this check means that we will only do this if
7057 // our window is not currently exiting, or it is not
7058 // locally animating itself. The idea being that one that
7059 // is exiting and doing a local animation should be removed
7060 // once that animation is done.
7061 mAnimating = true;
7062 mHasTransformation = true;
7063 mTransformation.clear();
7064 return false;
7065 } else if (mHasTransformation) {
7066 // Little trick to get through the path below to act like
7067 // we have finished an animation.
7068 mAnimating = true;
7069 } else if (isAnimating()) {
7070 mAnimating = true;
7071 }
7072 } else if (mAnimation != null) {
7073 // If the display is frozen, and there is a pending animation,
7074 // clear it and make sure we run the cleanup code.
7075 mAnimating = true;
7076 mLocalAnimating = true;
7077 mAnimation = null;
7078 }
Romain Guy06882f82009-06-10 13:36:04 -07007079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 if (!mAnimating && !mLocalAnimating) {
7081 return false;
7082 }
7083
7084 if (DEBUG_ANIM) Log.v(
7085 TAG, "Animation done in " + this + ": exiting=" + mExiting
7086 + ", reportedVisible="
7087 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 mAnimating = false;
7090 mLocalAnimating = false;
7091 mAnimation = null;
7092 mAnimLayer = mLayer;
7093 if (mIsImWindow) {
7094 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007095 } else if (mIsWallpaper) {
7096 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 }
7098 if (DEBUG_LAYERS) Log.v(TAG, "Stepping win " + this
7099 + " anim layer: " + mAnimLayer);
7100 mHasTransformation = false;
7101 mHasLocalTransformation = false;
7102 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7103 mTransformation.clear();
7104 if (mHasDrawn
7105 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7106 && mAppToken != null
7107 && mAppToken.firstWindowDrawn
7108 && mAppToken.startingData != null) {
7109 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Finish starting "
7110 + mToken + ": first real window done animating");
7111 mFinishedStarting.add(mAppToken);
7112 mH.sendEmptyMessage(H.FINISHED_STARTING);
7113 }
Romain Guy06882f82009-06-10 13:36:04 -07007114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 finishExit();
7116
7117 if (mAppToken != null) {
7118 mAppToken.updateReportedVisibilityLocked();
7119 }
7120
7121 return false;
7122 }
7123
7124 void finishExit() {
7125 if (DEBUG_ANIM) Log.v(
7126 TAG, "finishExit in " + this
7127 + ": exiting=" + mExiting
7128 + " remove=" + mRemoveOnExit
7129 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 final int N = mChildWindows.size();
7132 for (int i=0; i<N; i++) {
7133 ((WindowState)mChildWindows.get(i)).finishExit();
7134 }
Romain Guy06882f82009-06-10 13:36:04 -07007135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 if (!mExiting) {
7137 return;
7138 }
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 if (isWindowAnimating()) {
7141 return;
7142 }
7143
7144 if (localLOGV) Log.v(
7145 TAG, "Exit animation finished in " + this
7146 + ": remove=" + mRemoveOnExit);
7147 if (mSurface != null) {
7148 mDestroySurface.add(this);
7149 mDestroying = true;
7150 if (SHOW_TRANSACTIONS) Log.i(
7151 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7152 try {
7153 mSurface.hide();
7154 } catch (RuntimeException e) {
7155 Log.w(TAG, "Error hiding surface in " + this, e);
7156 }
7157 mLastHidden = true;
7158 mKeyWaiter.releasePendingPointerLocked(mSession);
7159 }
7160 mExiting = false;
7161 if (mRemoveOnExit) {
7162 mPendingRemove.add(this);
7163 mRemoveOnExit = false;
7164 }
7165 }
Romain Guy06882f82009-06-10 13:36:04 -07007166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7168 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7169 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7170 if (dtdx < -.000001f || dtdx > .000001f) return false;
7171 if (dsdy < -.000001f || dsdy > .000001f) return false;
7172 return true;
7173 }
Romain Guy06882f82009-06-10 13:36:04 -07007174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 void computeShownFrameLocked() {
7176 final boolean selfTransformation = mHasLocalTransformation;
7177 Transformation attachedTransformation =
7178 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7179 ? mAttachedWindow.mTransformation : null;
7180 Transformation appTransformation =
7181 (mAppToken != null && mAppToken.hasTransformation)
7182 ? mAppToken.transformation : null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007183
7184 // Wallpapers are animated based on the "real" window they
7185 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007186 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007187 && mWallpaperTarget != null) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007188 if (mWallpaperTarget.mHasLocalTransformation) {
7189 attachedTransformation = mWallpaperTarget.mTransformation;
7190 }
7191 if (mWallpaperTarget.mAppToken != null &&
7192 mWallpaperTarget.mAppToken.hasTransformation) {
7193 appTransformation = mWallpaperTarget.mAppToken.transformation;
7194 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007195 if (DEBUG_WALLPAPER && attachedTransformation != null) {
7196 Log.v(TAG, "WP target attached xform: " + attachedTransformation);
7197 }
7198 if (DEBUG_WALLPAPER && appTransformation != null) {
7199 Log.v(TAG, "WP target app xform: " + appTransformation);
7200 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007201 }
7202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 if (selfTransformation || attachedTransformation != null
7204 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007205 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 final Rect frame = mFrame;
7207 final float tmpFloats[] = mTmpFloats;
7208 final Matrix tmpMatrix = mTmpMatrix;
7209
7210 // Compute the desired transformation.
7211 tmpMatrix.setTranslate(frame.left, frame.top);
7212 if (selfTransformation) {
7213 tmpMatrix.preConcat(mTransformation.getMatrix());
7214 }
7215 if (attachedTransformation != null) {
7216 tmpMatrix.preConcat(attachedTransformation.getMatrix());
7217 }
7218 if (appTransformation != null) {
7219 tmpMatrix.preConcat(appTransformation.getMatrix());
7220 }
7221
7222 // "convert" it into SurfaceFlinger's format
7223 // (a 2x2 matrix + an offset)
7224 // Here we must not transform the position of the surface
7225 // since it is already included in the transformation.
7226 //Log.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 tmpMatrix.getValues(tmpFloats);
7229 mDsDx = tmpFloats[Matrix.MSCALE_X];
7230 mDtDx = tmpFloats[Matrix.MSKEW_X];
7231 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7232 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007233 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7234 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 int w = frame.width();
7236 int h = frame.height();
7237 mShownFrame.set(x, y, x+w, y+h);
7238
7239 // Now set the alpha... but because our current hardware
7240 // can't do alpha transformation on a non-opaque surface,
7241 // turn it off if we are running an animation that is also
7242 // transforming since it is more important to have that
7243 // animation be smooth.
7244 mShownAlpha = mAlpha;
7245 if (!mLimitedAlphaCompositing
7246 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7247 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7248 && x == frame.left && y == frame.top))) {
7249 //Log.i(TAG, "Applying alpha transform");
7250 if (selfTransformation) {
7251 mShownAlpha *= mTransformation.getAlpha();
7252 }
7253 if (attachedTransformation != null) {
7254 mShownAlpha *= attachedTransformation.getAlpha();
7255 }
7256 if (appTransformation != null) {
7257 mShownAlpha *= appTransformation.getAlpha();
7258 }
7259 } else {
7260 //Log.i(TAG, "Not applying alpha transform");
7261 }
Romain Guy06882f82009-06-10 13:36:04 -07007262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007263 if (localLOGV) Log.v(
7264 TAG, "Continuing animation in " + this +
7265 ": " + mShownFrame +
7266 ", alpha=" + mTransformation.getAlpha());
7267 return;
7268 }
Romain Guy06882f82009-06-10 13:36:04 -07007269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007271 if (mXOffset != 0 || mYOffset != 0) {
7272 mShownFrame.offset(mXOffset, mYOffset);
7273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 mShownAlpha = mAlpha;
7275 mDsDx = 1;
7276 mDtDx = 0;
7277 mDsDy = 0;
7278 mDtDy = 1;
7279 }
Romain Guy06882f82009-06-10 13:36:04 -07007280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 /**
7282 * Is this window visible? It is not visible if there is no
7283 * surface, or we are in the process of running an exit animation
7284 * that will remove the surface, or its app token has been hidden.
7285 */
7286 public boolean isVisibleLw() {
7287 final AppWindowToken atoken = mAppToken;
7288 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7289 && (atoken == null || !atoken.hiddenRequested)
7290 && !mExiting && !mDestroying;
7291 }
7292
7293 /**
7294 * Is this window visible, ignoring its app token? It is not visible
7295 * if there is no surface, or we are in the process of running an exit animation
7296 * that will remove the surface.
7297 */
7298 public boolean isWinVisibleLw() {
7299 final AppWindowToken atoken = mAppToken;
7300 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7301 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7302 && !mExiting && !mDestroying;
7303 }
7304
7305 /**
7306 * The same as isVisible(), but follows the current hidden state of
7307 * the associated app token, not the pending requested hidden state.
7308 */
7309 boolean isVisibleNow() {
7310 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007311 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 }
7313
7314 /**
7315 * Same as isVisible(), but we also count it as visible between the
7316 * call to IWindowSession.add() and the first relayout().
7317 */
7318 boolean isVisibleOrAdding() {
7319 final AppWindowToken atoken = mAppToken;
7320 return (mSurface != null
7321 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7322 && mPolicyVisibility && !mAttachedHidden
7323 && (atoken == null || !atoken.hiddenRequested)
7324 && !mExiting && !mDestroying;
7325 }
7326
7327 /**
7328 * Is this window currently on-screen? It is on-screen either if it
7329 * is visible or it is currently running an animation before no longer
7330 * being visible.
7331 */
7332 boolean isOnScreen() {
7333 final AppWindowToken atoken = mAppToken;
7334 if (atoken != null) {
7335 return mSurface != null && mPolicyVisibility && !mDestroying
7336 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007337 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 } else {
7339 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007340 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 }
7342 }
Romain Guy06882f82009-06-10 13:36:04 -07007343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007344 /**
7345 * Like isOnScreen(), but we don't return true if the window is part
7346 * of a transition that has not yet been started.
7347 */
7348 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007349 if (mRootToken.waitingToShow &&
7350 mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
7351 return false;
7352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007353 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007354 final boolean animating = atoken != null
7355 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007357 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7358 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007359 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 }
7361
7362 /** Is the window or its container currently animating? */
7363 boolean isAnimating() {
7364 final WindowState attached = mAttachedWindow;
7365 final AppWindowToken atoken = mAppToken;
7366 return mAnimation != null
7367 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007368 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 (atoken.animation != null
7370 || atoken.inPendingTransaction));
7371 }
7372
7373 /** Is this window currently animating? */
7374 boolean isWindowAnimating() {
7375 return mAnimation != null;
7376 }
7377
7378 /**
7379 * Like isOnScreen, but returns false if the surface hasn't yet
7380 * been drawn.
7381 */
7382 public boolean isDisplayedLw() {
7383 final AppWindowToken atoken = mAppToken;
7384 return mSurface != null && mPolicyVisibility && !mDestroying
7385 && !mDrawPending && !mCommitDrawPending
7386 && ((!mAttachedHidden &&
7387 (atoken == null || !atoken.hiddenRequested))
7388 || mAnimating);
7389 }
7390
7391 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7392 boolean shownFrame, boolean onlyOpaque) {
7393 if (mSurface == null) {
7394 return false;
7395 }
7396 if (mAppToken != null && !mAppToken.appFullscreen) {
7397 return false;
7398 }
7399 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7400 return false;
7401 }
7402 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007403
7404 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7405 return frame.left <= mCompatibleScreenFrame.left &&
7406 frame.top <= mCompatibleScreenFrame.top &&
7407 frame.right >= mCompatibleScreenFrame.right &&
7408 frame.bottom >= mCompatibleScreenFrame.bottom;
7409 } else {
7410 return frame.left <= 0 && frame.top <= 0
7411 && frame.right >= screenWidth
7412 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 }
Romain Guy06882f82009-06-10 13:36:04 -07007415
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007416 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007417 * Return true if the window is opaque and fully drawn. This indicates
7418 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007419 */
7420 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007421 return (mAttrs.format == PixelFormat.OPAQUE
7422 || mAttrs.type == TYPE_WALLPAPER)
7423 && mSurface != null && mAnimation == null
7424 && (mAppToken == null || mAppToken.animation == null)
7425 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007426 }
7427
7428 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7429 return
7430 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007431 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7432 // only if it's visible
7433 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007434 // and only if the application fills the compatible screen
7435 mFrame.left <= mCompatibleScreenFrame.left &&
7436 mFrame.top <= mCompatibleScreenFrame.top &&
7437 mFrame.right >= mCompatibleScreenFrame.right &&
7438 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007439 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007440 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007441 }
7442
7443 boolean isFullscreen(int screenWidth, int screenHeight) {
7444 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007445 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 }
7447
7448 void removeLocked() {
7449 if (mAttachedWindow != null) {
7450 mAttachedWindow.mChildWindows.remove(this);
7451 }
7452 destroySurfaceLocked();
7453 mSession.windowRemovedLocked();
7454 try {
7455 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7456 } catch (RuntimeException e) {
7457 // Ignore if it has already been removed (usually because
7458 // we are doing this as part of processing a death note.)
7459 }
7460 }
7461
7462 private class DeathRecipient implements IBinder.DeathRecipient {
7463 public void binderDied() {
7464 try {
7465 synchronized(mWindowMap) {
7466 WindowState win = windowForClientLocked(mSession, mClient);
7467 Log.i(TAG, "WIN DEATH: " + win);
7468 if (win != null) {
7469 removeWindowLocked(mSession, win);
7470 }
7471 }
7472 } catch (IllegalArgumentException ex) {
7473 // This will happen if the window has already been
7474 // removed.
7475 }
7476 }
7477 }
7478
7479 /** Returns true if this window desires key events. */
7480 public final boolean canReceiveKeys() {
7481 return isVisibleOrAdding()
7482 && (mViewVisibility == View.VISIBLE)
7483 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
7484 }
7485
7486 public boolean hasDrawnLw() {
7487 return mHasDrawn;
7488 }
7489
7490 public boolean showLw(boolean doAnimation) {
7491 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim) {
7492 mPolicyVisibility = true;
7493 mPolicyVisibilityAfterAnim = true;
7494 if (doAnimation) {
7495 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
7496 }
7497 requestAnimationLocked(0);
7498 return true;
7499 }
7500 return false;
7501 }
7502
7503 public boolean hideLw(boolean doAnimation) {
7504 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
7505 : mPolicyVisibility;
7506 if (current) {
7507 if (doAnimation) {
7508 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
7509 if (mAnimation == null) {
7510 doAnimation = false;
7511 }
7512 }
7513 if (doAnimation) {
7514 mPolicyVisibilityAfterAnim = false;
7515 } else {
7516 mPolicyVisibilityAfterAnim = false;
7517 mPolicyVisibility = false;
7518 }
7519 requestAnimationLocked(0);
7520 return true;
7521 }
7522 return false;
7523 }
7524
7525 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007526 StringBuilder sb = new StringBuilder(64);
Romain Guy06882f82009-06-10 13:36:04 -07007527
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007528 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
7529 pw.print(" mClient="); pw.println(mClient.asBinder());
7530 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
7531 if (mAttachedWindow != null || mLayoutAttached) {
7532 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
7533 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
7534 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007535 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
7536 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
7537 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007538 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
7539 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007540 }
7541 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
7542 pw.print(" mSubLayer="); pw.print(mSubLayer);
7543 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
7544 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
7545 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
7546 pw.print("="); pw.print(mAnimLayer);
7547 pw.print(" mLastLayer="); pw.println(mLastLayer);
7548 if (mSurface != null) {
7549 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
7550 }
7551 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
7552 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
7553 if (mAppToken != null) {
7554 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
7555 }
7556 if (mTargetAppToken != null) {
7557 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
7558 }
7559 pw.print(prefix); pw.print("mViewVisibility=0x");
7560 pw.print(Integer.toHexString(mViewVisibility));
7561 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007562 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
7563 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007564 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
7565 pw.print(prefix); pw.print("mPolicyVisibility=");
7566 pw.print(mPolicyVisibility);
7567 pw.print(" mPolicyVisibilityAfterAnim=");
7568 pw.print(mPolicyVisibilityAfterAnim);
7569 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
7570 }
7571 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007572 pw.print(" h="); pw.println(mRequestedHeight);
7573 if (mXOffset != 0 || mYOffset != 0) {
7574 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
7575 pw.print(" y="); pw.println(mYOffset);
7576 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007577 pw.print(prefix); pw.print("mGivenContentInsets=");
7578 mGivenContentInsets.printShortString(pw);
7579 pw.print(" mGivenVisibleInsets=");
7580 mGivenVisibleInsets.printShortString(pw);
7581 pw.println();
7582 if (mTouchableInsets != 0 || mGivenInsetsPending) {
7583 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
7584 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
7585 }
7586 pw.print(prefix); pw.print("mShownFrame=");
7587 mShownFrame.printShortString(pw);
7588 pw.print(" last="); mLastShownFrame.printShortString(pw);
7589 pw.println();
7590 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
7591 pw.print(" last="); mLastFrame.printShortString(pw);
7592 pw.println();
7593 pw.print(prefix); pw.print("mContainingFrame=");
7594 mContainingFrame.printShortString(pw);
7595 pw.print(" mDisplayFrame=");
7596 mDisplayFrame.printShortString(pw);
7597 pw.println();
7598 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
7599 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
7600 pw.println();
7601 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
7602 pw.print(" last="); mLastContentInsets.printShortString(pw);
7603 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
7604 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
7605 pw.println();
7606 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
7607 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
7608 pw.print(" mAlpha="); pw.print(mAlpha);
7609 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
7610 }
7611 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
7612 || mAnimation != null) {
7613 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
7614 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
7615 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
7616 pw.print(" mAnimation="); pw.println(mAnimation);
7617 }
7618 if (mHasTransformation || mHasLocalTransformation) {
7619 pw.print(prefix); pw.print("XForm: has=");
7620 pw.print(mHasTransformation);
7621 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
7622 pw.print(" "); mTransformation.printShortString(pw);
7623 pw.println();
7624 }
7625 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
7626 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
7627 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
7628 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
7629 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
7630 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
7631 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
7632 pw.print(" mDestroying="); pw.print(mDestroying);
7633 pw.print(" mRemoved="); pw.println(mRemoved);
7634 }
7635 if (mOrientationChanging || mAppFreezing) {
7636 pw.print(prefix); pw.print("mOrientationChanging=");
7637 pw.print(mOrientationChanging);
7638 pw.print(" mAppFreezing="); pw.println(mAppFreezing);
7639 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07007640 if (mHScale != 1 || mVScale != 1) {
7641 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
7642 pw.print(" mVScale="); pw.println(mVScale);
7643 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007644 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007645 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
7646 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
7647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 }
7649
7650 @Override
7651 public String toString() {
7652 return "Window{"
7653 + Integer.toHexString(System.identityHashCode(this))
7654 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
7655 }
7656 }
Romain Guy06882f82009-06-10 13:36:04 -07007657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 // -------------------------------------------------------------
7659 // Window Token State
7660 // -------------------------------------------------------------
7661
7662 class WindowToken {
7663 // The actual token.
7664 final IBinder token;
7665
7666 // The type of window this token is for, as per WindowManager.LayoutParams.
7667 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07007668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 // Set if this token was explicitly added by a client, so should
7670 // not be removed when all windows are removed.
7671 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07007672
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007673 // For printing.
7674 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07007675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 // If this is an AppWindowToken, this is non-null.
7677 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07007678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 // All of the windows associated with this token.
7680 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
7681
7682 // Is key dispatching paused for this token?
7683 boolean paused = false;
7684
7685 // Should this token's windows be hidden?
7686 boolean hidden;
7687
7688 // Temporary for finding which tokens no longer have visible windows.
7689 boolean hasVisible;
7690
Dianne Hackborna8f60182009-09-01 19:01:50 -07007691 // Set to true when this token is in a pending transaction where it
7692 // will be shown.
7693 boolean waitingToShow;
7694
7695 // Set to true when this token is in a pending transaction where it
7696 // will be hidden.
7697 boolean waitingToHide;
7698
7699 // Set to true when this token is in a pending transaction where its
7700 // windows will be put to the bottom of the list.
7701 boolean sendingToBottom;
7702
7703 // Set to true when this token is in a pending transaction where its
7704 // windows will be put to the top of the list.
7705 boolean sendingToTop;
7706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 WindowToken(IBinder _token, int type, boolean _explicit) {
7708 token = _token;
7709 windowType = type;
7710 explicit = _explicit;
7711 }
7712
7713 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007714 pw.print(prefix); pw.print("token="); pw.println(token);
7715 pw.print(prefix); pw.print("windows="); pw.println(windows);
7716 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
7717 pw.print(" hidden="); pw.print(hidden);
7718 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07007719 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
7720 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
7721 pw.print(" waitingToHide="); pw.print(waitingToHide);
7722 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
7723 pw.print(" sendingToTop="); pw.println(sendingToTop);
7724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 }
7726
7727 @Override
7728 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007729 if (stringName == null) {
7730 StringBuilder sb = new StringBuilder();
7731 sb.append("WindowToken{");
7732 sb.append(Integer.toHexString(System.identityHashCode(this)));
7733 sb.append(" token="); sb.append(token); sb.append('}');
7734 stringName = sb.toString();
7735 }
7736 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 }
7738 };
7739
7740 class AppWindowToken extends WindowToken {
7741 // Non-null only for application tokens.
7742 final IApplicationToken appToken;
7743
7744 // All of the windows and child windows that are included in this
7745 // application token. Note this list is NOT sorted!
7746 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
7747
7748 int groupId = -1;
7749 boolean appFullscreen;
7750 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07007751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007752 // These are used for determining when all windows associated with
7753 // an activity have been drawn, so they can be made visible together
7754 // at the same time.
7755 int lastTransactionSequence = mTransactionSequence-1;
7756 int numInterestingWindows;
7757 int numDrawnWindows;
7758 boolean inPendingTransaction;
7759 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07007760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 // Is this token going to be hidden in a little while? If so, it
7762 // won't be taken into account for setting the screen orientation.
7763 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 // Is this window's surface needed? This is almost like hidden, except
7766 // it will sometimes be true a little earlier: when the token has
7767 // been shown, but is still waiting for its app transition to execute
7768 // before making its windows shown.
7769 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07007770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 // Have we told the window clients to hide themselves?
7772 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07007773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 // Last visibility state we reported to the app token.
7775 boolean reportedVisible;
7776
7777 // Set to true when the token has been removed from the window mgr.
7778 boolean removed;
7779
7780 // Have we been asked to have this token keep the screen frozen?
7781 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 boolean animating;
7784 Animation animation;
7785 boolean hasTransformation;
7786 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07007787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 // Offset to the window of all layers in the token, for use by
7789 // AppWindowToken animations.
7790 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07007791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 // Information about an application starting window if displayed.
7793 StartingData startingData;
7794 WindowState startingWindow;
7795 View startingView;
7796 boolean startingDisplayed;
7797 boolean startingMoved;
7798 boolean firstWindowDrawn;
7799
7800 AppWindowToken(IApplicationToken _token) {
7801 super(_token.asBinder(),
7802 WindowManager.LayoutParams.TYPE_APPLICATION, true);
7803 appWindowToken = this;
7804 appToken = _token;
7805 }
Romain Guy06882f82009-06-10 13:36:04 -07007806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 public void setAnimation(Animation anim) {
7808 if (localLOGV) Log.v(
7809 TAG, "Setting animation in " + this + ": " + anim);
7810 animation = anim;
7811 animating = false;
7812 anim.restrictDuration(MAX_ANIMATION_DURATION);
7813 anim.scaleCurrentDuration(mTransitionAnimationScale);
7814 int zorder = anim.getZAdjustment();
7815 int adj = 0;
7816 if (zorder == Animation.ZORDER_TOP) {
7817 adj = TYPE_LAYER_OFFSET;
7818 } else if (zorder == Animation.ZORDER_BOTTOM) {
7819 adj = -TYPE_LAYER_OFFSET;
7820 }
Romain Guy06882f82009-06-10 13:36:04 -07007821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 if (animLayerAdjustment != adj) {
7823 animLayerAdjustment = adj;
7824 updateLayers();
7825 }
7826 }
Romain Guy06882f82009-06-10 13:36:04 -07007827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 public void setDummyAnimation() {
7829 if (animation == null) {
7830 if (localLOGV) Log.v(
7831 TAG, "Setting dummy animation in " + this);
7832 animation = sDummyAnimation;
7833 }
7834 }
7835
7836 public void clearAnimation() {
7837 if (animation != null) {
7838 animation = null;
7839 animating = true;
7840 }
7841 }
Romain Guy06882f82009-06-10 13:36:04 -07007842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007843 void updateLayers() {
7844 final int N = allAppWindows.size();
7845 final int adj = animLayerAdjustment;
7846 for (int i=0; i<N; i++) {
7847 WindowState w = allAppWindows.get(i);
7848 w.mAnimLayer = w.mLayer + adj;
7849 if (DEBUG_LAYERS) Log.v(TAG, "Updating layer " + w + ": "
7850 + w.mAnimLayer);
7851 if (w == mInputMethodTarget) {
7852 setInputMethodAnimLayerAdjustment(adj);
7853 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007854 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007855 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 }
7858 }
Romain Guy06882f82009-06-10 13:36:04 -07007859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 void sendAppVisibilityToClients() {
7861 final int N = allAppWindows.size();
7862 for (int i=0; i<N; i++) {
7863 WindowState win = allAppWindows.get(i);
7864 if (win == startingWindow && clientHidden) {
7865 // Don't hide the starting window.
7866 continue;
7867 }
7868 try {
7869 if (DEBUG_VISIBILITY) Log.v(TAG,
7870 "Setting visibility of " + win + ": " + (!clientHidden));
7871 win.mClient.dispatchAppVisibility(!clientHidden);
7872 } catch (RemoteException e) {
7873 }
7874 }
7875 }
Romain Guy06882f82009-06-10 13:36:04 -07007876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 void showAllWindowsLocked() {
7878 final int NW = allAppWindows.size();
7879 for (int i=0; i<NW; i++) {
7880 WindowState w = allAppWindows.get(i);
7881 if (DEBUG_VISIBILITY) Log.v(TAG,
7882 "performing show on: " + w);
7883 w.performShowLocked();
7884 }
7885 }
Romain Guy06882f82009-06-10 13:36:04 -07007886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 // This must be called while inside a transaction.
7888 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
7889 if (!mDisplayFrozen) {
7890 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 if (animation == sDummyAnimation) {
7893 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007894 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 // when it is really time to animate, this will be set to
7896 // a real animation and the next call will execute normally.
7897 return false;
7898 }
Romain Guy06882f82009-06-10 13:36:04 -07007899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007900 if ((allDrawn || animating || startingDisplayed) && animation != null) {
7901 if (!animating) {
7902 if (DEBUG_ANIM) Log.v(
7903 TAG, "Starting animation in " + this +
7904 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
7905 + " scale=" + mTransitionAnimationScale
7906 + " allDrawn=" + allDrawn + " animating=" + animating);
7907 animation.initialize(dw, dh, dw, dh);
7908 animation.setStartTime(currentTime);
7909 animating = true;
7910 }
7911 transformation.clear();
7912 final boolean more = animation.getTransformation(
7913 currentTime, transformation);
7914 if (DEBUG_ANIM) Log.v(
7915 TAG, "Stepped animation in " + this +
7916 ": more=" + more + ", xform=" + transformation);
7917 if (more) {
7918 // we're done!
7919 hasTransformation = true;
7920 return true;
7921 }
7922 if (DEBUG_ANIM) Log.v(
7923 TAG, "Finished animation in " + this +
7924 " @ " + currentTime);
7925 animation = null;
7926 }
7927 } else if (animation != null) {
7928 // If the display is frozen, and there is a pending animation,
7929 // clear it and make sure we run the cleanup code.
7930 animating = true;
7931 animation = null;
7932 }
7933
7934 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07007935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 if (!animating) {
7937 return false;
7938 }
7939
7940 clearAnimation();
7941 animating = false;
7942 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
7943 moveInputMethodWindowsIfNeededLocked(true);
7944 }
Romain Guy06882f82009-06-10 13:36:04 -07007945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 if (DEBUG_ANIM) Log.v(
7947 TAG, "Animation done in " + this
7948 + ": reportedVisible=" + reportedVisible);
7949
7950 transformation.clear();
7951 if (animLayerAdjustment != 0) {
7952 animLayerAdjustment = 0;
7953 updateLayers();
7954 }
Romain Guy06882f82009-06-10 13:36:04 -07007955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 final int N = windows.size();
7957 for (int i=0; i<N; i++) {
7958 ((WindowState)windows.get(i)).finishExit();
7959 }
7960 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07007961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 return false;
7963 }
7964
7965 void updateReportedVisibilityLocked() {
7966 if (appToken == null) {
7967 return;
7968 }
Romain Guy06882f82009-06-10 13:36:04 -07007969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 int numInteresting = 0;
7971 int numVisible = 0;
7972 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07007973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 if (DEBUG_VISIBILITY) Log.v(TAG, "Update reported visibility: " + this);
7975 final int N = allAppWindows.size();
7976 for (int i=0; i<N; i++) {
7977 WindowState win = allAppWindows.get(i);
7978 if (win == startingWindow || win.mAppFreezing) {
7979 continue;
7980 }
7981 if (DEBUG_VISIBILITY) {
7982 Log.v(TAG, "Win " + win + ": isDisplayed="
7983 + win.isDisplayedLw()
7984 + ", isAnimating=" + win.isAnimating());
7985 if (!win.isDisplayedLw()) {
7986 Log.v(TAG, "Not displayed: s=" + win.mSurface
7987 + " pv=" + win.mPolicyVisibility
7988 + " dp=" + win.mDrawPending
7989 + " cdp=" + win.mCommitDrawPending
7990 + " ah=" + win.mAttachedHidden
7991 + " th="
7992 + (win.mAppToken != null
7993 ? win.mAppToken.hiddenRequested : false)
7994 + " a=" + win.mAnimating);
7995 }
7996 }
7997 numInteresting++;
7998 if (win.isDisplayedLw()) {
7999 if (!win.isAnimating()) {
8000 numVisible++;
8001 }
8002 nowGone = false;
8003 } else if (win.isAnimating()) {
8004 nowGone = false;
8005 }
8006 }
Romain Guy06882f82009-06-10 13:36:04 -07008007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
8009 if (DEBUG_VISIBILITY) Log.v(TAG, "VIS " + this + ": interesting="
8010 + numInteresting + " visible=" + numVisible);
8011 if (nowVisible != reportedVisible) {
8012 if (DEBUG_VISIBILITY) Log.v(
8013 TAG, "Visibility changed in " + this
8014 + ": vis=" + nowVisible);
8015 reportedVisible = nowVisible;
8016 Message m = mH.obtainMessage(
8017 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8018 nowVisible ? 1 : 0,
8019 nowGone ? 1 : 0,
8020 this);
8021 mH.sendMessage(m);
8022 }
8023 }
Romain Guy06882f82009-06-10 13:36:04 -07008024
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008025 WindowState findMainWindow() {
8026 int j = windows.size();
8027 while (j > 0) {
8028 j--;
8029 WindowState win = windows.get(j);
8030 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8031 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8032 return win;
8033 }
8034 }
8035 return null;
8036 }
8037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 void dump(PrintWriter pw, String prefix) {
8039 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008040 if (appToken != null) {
8041 pw.print(prefix); pw.println("app=true");
8042 }
8043 if (allAppWindows.size() > 0) {
8044 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8045 }
8046 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008047 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008048 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8049 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8050 pw.print(" clientHidden="); pw.print(clientHidden);
8051 pw.print(" willBeHidden="); pw.print(willBeHidden);
8052 pw.print(" reportedVisible="); pw.println(reportedVisible);
8053 if (paused || freezingScreen) {
8054 pw.print(prefix); pw.print("paused="); pw.print(paused);
8055 pw.print(" freezingScreen="); pw.println(freezingScreen);
8056 }
8057 if (numInterestingWindows != 0 || numDrawnWindows != 0
8058 || inPendingTransaction || allDrawn) {
8059 pw.print(prefix); pw.print("numInterestingWindows=");
8060 pw.print(numInterestingWindows);
8061 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8062 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8063 pw.print(" allDrawn="); pw.println(allDrawn);
8064 }
8065 if (animating || animation != null) {
8066 pw.print(prefix); pw.print("animating="); pw.print(animating);
8067 pw.print(" animation="); pw.println(animation);
8068 }
8069 if (animLayerAdjustment != 0) {
8070 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8071 }
8072 if (hasTransformation) {
8073 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8074 pw.print(" transformation="); transformation.printShortString(pw);
8075 pw.println();
8076 }
8077 if (startingData != null || removed || firstWindowDrawn) {
8078 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8079 pw.print(" removed="); pw.print(removed);
8080 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8081 }
8082 if (startingWindow != null || startingView != null
8083 || startingDisplayed || startingMoved) {
8084 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8085 pw.print(" startingView="); pw.print(startingView);
8086 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8087 pw.print(" startingMoved"); pw.println(startingMoved);
8088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
8090
8091 @Override
8092 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008093 if (stringName == null) {
8094 StringBuilder sb = new StringBuilder();
8095 sb.append("AppWindowToken{");
8096 sb.append(Integer.toHexString(System.identityHashCode(this)));
8097 sb.append(" token="); sb.append(token); sb.append('}');
8098 stringName = sb.toString();
8099 }
8100 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 }
8102 }
Romain Guy06882f82009-06-10 13:36:04 -07008103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 // -------------------------------------------------------------
8105 // DummyAnimation
8106 // -------------------------------------------------------------
8107
8108 // This is an animation that does nothing: it just immediately finishes
8109 // itself every time it is called. It is used as a stub animation in cases
8110 // where we want to synchronize multiple things that may be animating.
8111 static final class DummyAnimation extends Animation {
8112 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8113 return false;
8114 }
8115 }
8116 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 // -------------------------------------------------------------
8119 // Async Handler
8120 // -------------------------------------------------------------
8121
8122 static final class StartingData {
8123 final String pkg;
8124 final int theme;
8125 final CharSequence nonLocalizedLabel;
8126 final int labelRes;
8127 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8130 int _labelRes, int _icon) {
8131 pkg = _pkg;
8132 theme = _theme;
8133 nonLocalizedLabel = _nonLocalizedLabel;
8134 labelRes = _labelRes;
8135 icon = _icon;
8136 }
8137 }
8138
8139 private final class H extends Handler {
8140 public static final int REPORT_FOCUS_CHANGE = 2;
8141 public static final int REPORT_LOSING_FOCUS = 3;
8142 public static final int ANIMATE = 4;
8143 public static final int ADD_STARTING = 5;
8144 public static final int REMOVE_STARTING = 6;
8145 public static final int FINISHED_STARTING = 7;
8146 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8148 public static final int HOLD_SCREEN_CHANGED = 12;
8149 public static final int APP_TRANSITION_TIMEOUT = 13;
8150 public static final int PERSIST_ANIMATION_SCALE = 14;
8151 public static final int FORCE_GC = 15;
8152 public static final int ENABLE_SCREEN = 16;
8153 public static final int APP_FREEZE_TIMEOUT = 17;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008154 public static final int COMPUTE_AND_SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 public H() {
8159 }
Romain Guy06882f82009-06-10 13:36:04 -07008160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 @Override
8162 public void handleMessage(Message msg) {
8163 switch (msg.what) {
8164 case REPORT_FOCUS_CHANGE: {
8165 WindowState lastFocus;
8166 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 synchronized(mWindowMap) {
8169 lastFocus = mLastFocus;
8170 newFocus = mCurrentFocus;
8171 if (lastFocus == newFocus) {
8172 // Focus is not changing, so nothing to do.
8173 return;
8174 }
8175 mLastFocus = newFocus;
8176 //Log.i(TAG, "Focus moving from " + lastFocus
8177 // + " to " + newFocus);
8178 if (newFocus != null && lastFocus != null
8179 && !newFocus.isDisplayedLw()) {
8180 //Log.i(TAG, "Delaying loss of focus...");
8181 mLosingFocus.add(lastFocus);
8182 lastFocus = null;
8183 }
8184 }
8185
8186 if (lastFocus != newFocus) {
8187 //System.out.println("Changing focus from " + lastFocus
8188 // + " to " + newFocus);
8189 if (newFocus != null) {
8190 try {
8191 //Log.i(TAG, "Gaining focus: " + newFocus);
8192 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8193 } catch (RemoteException e) {
8194 // Ignore if process has died.
8195 }
8196 }
8197
8198 if (lastFocus != null) {
8199 try {
8200 //Log.i(TAG, "Losing focus: " + lastFocus);
8201 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8202 } catch (RemoteException e) {
8203 // Ignore if process has died.
8204 }
8205 }
8206 }
8207 } break;
8208
8209 case REPORT_LOSING_FOCUS: {
8210 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 synchronized(mWindowMap) {
8213 losers = mLosingFocus;
8214 mLosingFocus = new ArrayList<WindowState>();
8215 }
8216
8217 final int N = losers.size();
8218 for (int i=0; i<N; i++) {
8219 try {
8220 //Log.i(TAG, "Losing delayed focus: " + losers.get(i));
8221 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8222 } catch (RemoteException e) {
8223 // Ignore if process has died.
8224 }
8225 }
8226 } break;
8227
8228 case ANIMATE: {
8229 synchronized(mWindowMap) {
8230 mAnimationPending = false;
8231 performLayoutAndPlaceSurfacesLocked();
8232 }
8233 } break;
8234
8235 case ADD_STARTING: {
8236 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8237 final StartingData sd = wtoken.startingData;
8238
8239 if (sd == null) {
8240 // Animation has been canceled... do nothing.
8241 return;
8242 }
Romain Guy06882f82009-06-10 13:36:04 -07008243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008244 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Add starting "
8245 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008247 View view = null;
8248 try {
8249 view = mPolicy.addStartingWindow(
8250 wtoken.token, sd.pkg,
8251 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8252 sd.icon);
8253 } catch (Exception e) {
8254 Log.w(TAG, "Exception when adding starting window", e);
8255 }
8256
8257 if (view != null) {
8258 boolean abort = false;
8259
8260 synchronized(mWindowMap) {
8261 if (wtoken.removed || wtoken.startingData == null) {
8262 // If the window was successfully added, then
8263 // we need to remove it.
8264 if (wtoken.startingWindow != null) {
8265 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8266 "Aborted starting " + wtoken
8267 + ": removed=" + wtoken.removed
8268 + " startingData=" + wtoken.startingData);
8269 wtoken.startingWindow = null;
8270 wtoken.startingData = null;
8271 abort = true;
8272 }
8273 } else {
8274 wtoken.startingView = view;
8275 }
8276 if (DEBUG_STARTING_WINDOW && !abort) Log.v(TAG,
8277 "Added starting " + wtoken
8278 + ": startingWindow="
8279 + wtoken.startingWindow + " startingView="
8280 + wtoken.startingView);
8281 }
8282
8283 if (abort) {
8284 try {
8285 mPolicy.removeStartingWindow(wtoken.token, view);
8286 } catch (Exception e) {
8287 Log.w(TAG, "Exception when removing starting window", e);
8288 }
8289 }
8290 }
8291 } break;
8292
8293 case REMOVE_STARTING: {
8294 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8295 IBinder token = null;
8296 View view = null;
8297 synchronized (mWindowMap) {
8298 if (DEBUG_STARTING_WINDOW) Log.v(TAG, "Remove starting "
8299 + wtoken + ": startingWindow="
8300 + wtoken.startingWindow + " startingView="
8301 + wtoken.startingView);
8302 if (wtoken.startingWindow != null) {
8303 view = wtoken.startingView;
8304 token = wtoken.token;
8305 wtoken.startingData = null;
8306 wtoken.startingView = null;
8307 wtoken.startingWindow = null;
8308 }
8309 }
8310 if (view != null) {
8311 try {
8312 mPolicy.removeStartingWindow(token, view);
8313 } catch (Exception e) {
8314 Log.w(TAG, "Exception when removing starting window", e);
8315 }
8316 }
8317 } break;
8318
8319 case FINISHED_STARTING: {
8320 IBinder token = null;
8321 View view = null;
8322 while (true) {
8323 synchronized (mWindowMap) {
8324 final int N = mFinishedStarting.size();
8325 if (N <= 0) {
8326 break;
8327 }
8328 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8329
8330 if (DEBUG_STARTING_WINDOW) Log.v(TAG,
8331 "Finished starting " + wtoken
8332 + ": startingWindow=" + wtoken.startingWindow
8333 + " startingView=" + wtoken.startingView);
8334
8335 if (wtoken.startingWindow == null) {
8336 continue;
8337 }
8338
8339 view = wtoken.startingView;
8340 token = wtoken.token;
8341 wtoken.startingData = null;
8342 wtoken.startingView = null;
8343 wtoken.startingWindow = null;
8344 }
8345
8346 try {
8347 mPolicy.removeStartingWindow(token, view);
8348 } catch (Exception e) {
8349 Log.w(TAG, "Exception when removing starting window", e);
8350 }
8351 }
8352 } break;
8353
8354 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8355 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8356
8357 boolean nowVisible = msg.arg1 != 0;
8358 boolean nowGone = msg.arg2 != 0;
8359
8360 try {
8361 if (DEBUG_VISIBILITY) Log.v(
8362 TAG, "Reporting visible in " + wtoken
8363 + " visible=" + nowVisible
8364 + " gone=" + nowGone);
8365 if (nowVisible) {
8366 wtoken.appToken.windowsVisible();
8367 } else {
8368 wtoken.appToken.windowsGone();
8369 }
8370 } catch (RemoteException ex) {
8371 }
8372 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008374 case WINDOW_FREEZE_TIMEOUT: {
8375 synchronized (mWindowMap) {
8376 Log.w(TAG, "Window freeze timeout expired.");
8377 int i = mWindows.size();
8378 while (i > 0) {
8379 i--;
8380 WindowState w = (WindowState)mWindows.get(i);
8381 if (w.mOrientationChanging) {
8382 w.mOrientationChanging = false;
8383 Log.w(TAG, "Force clearing orientation change: " + w);
8384 }
8385 }
8386 performLayoutAndPlaceSurfacesLocked();
8387 }
8388 break;
8389 }
Romain Guy06882f82009-06-10 13:36:04 -07008390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 case HOLD_SCREEN_CHANGED: {
8392 Session oldHold;
8393 Session newHold;
8394 synchronized (mWindowMap) {
8395 oldHold = mLastReportedHold;
8396 newHold = (Session)msg.obj;
8397 mLastReportedHold = newHold;
8398 }
Romain Guy06882f82009-06-10 13:36:04 -07008399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 if (oldHold != newHold) {
8401 try {
8402 if (oldHold != null) {
8403 mBatteryStats.noteStopWakelock(oldHold.mUid,
8404 "window",
8405 BatteryStats.WAKE_TYPE_WINDOW);
8406 }
8407 if (newHold != null) {
8408 mBatteryStats.noteStartWakelock(newHold.mUid,
8409 "window",
8410 BatteryStats.WAKE_TYPE_WINDOW);
8411 }
8412 } catch (RemoteException e) {
8413 }
8414 }
8415 break;
8416 }
Romain Guy06882f82009-06-10 13:36:04 -07008417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 case APP_TRANSITION_TIMEOUT: {
8419 synchronized (mWindowMap) {
8420 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
8421 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8422 "*** APP TRANSITION TIMEOUT");
8423 mAppTransitionReady = true;
8424 mAppTransitionTimeout = true;
8425 performLayoutAndPlaceSurfacesLocked();
8426 }
8427 }
8428 break;
8429 }
Romain Guy06882f82009-06-10 13:36:04 -07008430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 case PERSIST_ANIMATION_SCALE: {
8432 Settings.System.putFloat(mContext.getContentResolver(),
8433 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8434 Settings.System.putFloat(mContext.getContentResolver(),
8435 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8436 break;
8437 }
Romain Guy06882f82009-06-10 13:36:04 -07008438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 case FORCE_GC: {
8440 synchronized(mWindowMap) {
8441 if (mAnimationPending) {
8442 // If we are animating, don't do the gc now but
8443 // delay a bit so we don't interrupt the animation.
8444 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
8445 2000);
8446 return;
8447 }
8448 // If we are currently rotating the display, it will
8449 // schedule a new message when done.
8450 if (mDisplayFrozen) {
8451 return;
8452 }
8453 mFreezeGcPending = 0;
8454 }
8455 Runtime.getRuntime().gc();
8456 break;
8457 }
Romain Guy06882f82009-06-10 13:36:04 -07008458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 case ENABLE_SCREEN: {
8460 performEnableScreen();
8461 break;
8462 }
Romain Guy06882f82009-06-10 13:36:04 -07008463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 case APP_FREEZE_TIMEOUT: {
8465 synchronized (mWindowMap) {
8466 Log.w(TAG, "App freeze timeout expired.");
8467 int i = mAppTokens.size();
8468 while (i > 0) {
8469 i--;
8470 AppWindowToken tok = mAppTokens.get(i);
8471 if (tok.freezingScreen) {
8472 Log.w(TAG, "Force clearing freeze: " + tok);
8473 unsetAppFreezingScreenLocked(tok, true, true);
8474 }
8475 }
8476 }
8477 break;
8478 }
Romain Guy06882f82009-06-10 13:36:04 -07008479
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008480 case COMPUTE_AND_SEND_NEW_CONFIGURATION: {
Dianne Hackborncfaef692009-06-15 14:24:44 -07008481 if (updateOrientationFromAppTokensUnchecked(null, null) != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07008482 sendNewConfiguration();
8483 }
8484 break;
8485 }
Romain Guy06882f82009-06-10 13:36:04 -07008486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 }
8488 }
8489 }
8490
8491 // -------------------------------------------------------------
8492 // IWindowManager API
8493 // -------------------------------------------------------------
8494
8495 public IWindowSession openSession(IInputMethodClient client,
8496 IInputContext inputContext) {
8497 if (client == null) throw new IllegalArgumentException("null client");
8498 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
8499 return new Session(client, inputContext);
8500 }
8501
8502 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
8503 synchronized (mWindowMap) {
8504 // The focus for the client is the window immediately below
8505 // where we would place the input method window.
8506 int idx = findDesiredInputMethodWindowIndexLocked(false);
8507 WindowState imFocus;
8508 if (idx > 0) {
8509 imFocus = (WindowState)mWindows.get(idx-1);
8510 if (imFocus != null) {
8511 if (imFocus.mSession.mClient != null &&
8512 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
8513 return true;
8514 }
8515 }
8516 }
8517 }
8518 return false;
8519 }
Romain Guy06882f82009-06-10 13:36:04 -07008520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 // -------------------------------------------------------------
8522 // Internals
8523 // -------------------------------------------------------------
8524
8525 final WindowState windowForClientLocked(Session session, IWindow client) {
8526 return windowForClientLocked(session, client.asBinder());
8527 }
Romain Guy06882f82009-06-10 13:36:04 -07008528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 final WindowState windowForClientLocked(Session session, IBinder client) {
8530 WindowState win = mWindowMap.get(client);
8531 if (localLOGV) Log.v(
8532 TAG, "Looking up client " + client + ": " + win);
8533 if (win == null) {
8534 RuntimeException ex = new RuntimeException();
8535 Log.w(TAG, "Requested window " + client + " does not exist", ex);
8536 return null;
8537 }
8538 if (session != null && win.mSession != session) {
8539 RuntimeException ex = new RuntimeException();
8540 Log.w(TAG, "Requested window " + client + " is in session " +
8541 win.mSession + ", not " + session, ex);
8542 return null;
8543 }
8544
8545 return win;
8546 }
8547
Dianne Hackborna8f60182009-09-01 19:01:50 -07008548 final void rebuildAppWindowListLocked() {
8549 int NW = mWindows.size();
8550 int i;
8551
8552 // First remove all existing app windows.
8553 i=0;
8554 while (i < NW) {
8555 if (((WindowState)mWindows.get(i)).mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008556 WindowState win = (WindowState)mWindows.remove(i);
8557 if (DEBUG_WINDOW_MOVEMENT) Log.v(TAG,
8558 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008559 NW--;
8560 continue;
8561 }
8562 i++;
8563 }
8564
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008565 // First add all of the exiting app tokens... these are no longer
8566 // in the main app list, but still have windows shown. We put them
8567 // in the back because now that the animation is over we no longer
8568 // will care about them.
8569 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07008570 i = 0;
8571 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008572 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
8573 }
8574
8575 // And add in the still active app tokens in Z order.
8576 NT = mAppTokens.size();
8577 for (int j=0; j<NT; j++) {
8578 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07008579 }
8580 }
8581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 private final void assignLayersLocked() {
8583 int N = mWindows.size();
8584 int curBaseLayer = 0;
8585 int curLayer = 0;
8586 int i;
Romain Guy06882f82009-06-10 13:36:04 -07008587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 for (i=0; i<N; i++) {
8589 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008590 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
8591 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 curLayer += WINDOW_LAYER_MULTIPLIER;
8593 w.mLayer = curLayer;
8594 } else {
8595 curBaseLayer = curLayer = w.mBaseLayer;
8596 w.mLayer = curLayer;
8597 }
8598 if (w.mTargetAppToken != null) {
8599 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
8600 } else if (w.mAppToken != null) {
8601 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
8602 } else {
8603 w.mAnimLayer = w.mLayer;
8604 }
8605 if (w.mIsImWindow) {
8606 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008607 } else if (w.mIsWallpaper) {
8608 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 }
8610 if (DEBUG_LAYERS) Log.v(TAG, "Assign layer " + w + ": "
8611 + w.mAnimLayer);
8612 //System.out.println(
8613 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
8614 }
8615 }
8616
8617 private boolean mInLayout = false;
8618 private final void performLayoutAndPlaceSurfacesLocked() {
8619 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07008620 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 throw new RuntimeException("Recursive call!");
8622 }
8623 Log.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
8624 return;
8625 }
8626
8627 boolean recoveringMemory = false;
8628 if (mForceRemoves != null) {
8629 recoveringMemory = true;
8630 // Wait a little it for things to settle down, and off we go.
8631 for (int i=0; i<mForceRemoves.size(); i++) {
8632 WindowState ws = mForceRemoves.get(i);
8633 Log.i(TAG, "Force removing: " + ws);
8634 removeWindowInnerLocked(ws.mSession, ws);
8635 }
8636 mForceRemoves = null;
8637 Log.w(TAG, "Due to memory failure, waiting a bit for next layout");
8638 Object tmp = new Object();
8639 synchronized (tmp) {
8640 try {
8641 tmp.wait(250);
8642 } catch (InterruptedException e) {
8643 }
8644 }
8645 }
Romain Guy06882f82009-06-10 13:36:04 -07008646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 mInLayout = true;
8648 try {
8649 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07008650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008651 int i = mPendingRemove.size()-1;
8652 if (i >= 0) {
8653 while (i >= 0) {
8654 WindowState w = mPendingRemove.get(i);
8655 removeWindowInnerLocked(w.mSession, w);
8656 i--;
8657 }
8658 mPendingRemove.clear();
8659
8660 mInLayout = false;
8661 assignLayersLocked();
8662 mLayoutNeeded = true;
8663 performLayoutAndPlaceSurfacesLocked();
8664
8665 } else {
8666 mInLayout = false;
8667 if (mLayoutNeeded) {
8668 requestAnimationLocked(0);
8669 }
8670 }
8671 } catch (RuntimeException e) {
8672 mInLayout = false;
8673 Log.e(TAG, "Unhandled exception while layout out windows", e);
8674 }
8675 }
8676
8677 private final void performLayoutLockedInner() {
8678 final int dw = mDisplay.getWidth();
8679 final int dh = mDisplay.getHeight();
8680
8681 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008682 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 int i;
8684
8685 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07008686
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008687 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 mPolicy.beginLayoutLw(dw, dh);
8689
8690 // First perform layout of any root windows (not attached
8691 // to another window).
8692 int topAttached = -1;
8693 for (i = N-1; i >= 0; i--) {
8694 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008695
8696 // Don't do layout of a window if it is not visible, or
8697 // soon won't be visible, to avoid wasting time and funky
8698 // changes while a window is animating away.
8699 final AppWindowToken atoken = win.mAppToken;
8700 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008702 || win.mRootToken.hidden
8703 || (atoken != null && atoken.hiddenRequested)
8704 || !win.mPolicyVisibility
8705 || win.mAttachedHidden
8706 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707
8708 // If this view is GONE, then skip it -- keep the current
8709 // frame, and let the caller know so they can ignore it
8710 // if they want. (We do the normal layout for INVISIBLE
8711 // windows, since that means "perform layout as normal,
8712 // just don't display").
8713 if (!gone || !win.mHaveFrame) {
8714 if (!win.mLayoutAttached) {
8715 mPolicy.layoutWindowLw(win, win.mAttrs, null);
8716 } else {
8717 if (topAttached < 0) topAttached = i;
8718 }
8719 }
8720 }
Romain Guy06882f82009-06-10 13:36:04 -07008721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 // Now perform layout of attached windows, which usually
8723 // depend on the position of the window they are attached to.
8724 // XXX does not deal with windows that are attached to windows
8725 // that are themselves attached.
8726 for (i = topAttached; i >= 0; i--) {
8727 WindowState win = (WindowState) mWindows.get(i);
8728
8729 // If this view is GONE, then skip it -- keep the current
8730 // frame, and let the caller know so they can ignore it
8731 // if they want. (We do the normal layout for INVISIBLE
8732 // windows, since that means "perform layout as normal,
8733 // just don't display").
8734 if (win.mLayoutAttached) {
8735 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
8736 || !win.mHaveFrame) {
8737 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
8738 }
8739 }
8740 }
8741
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008742 if (!mPolicy.finishLayoutLw()) {
8743 mLayoutNeeded = false;
8744 } else if (repeats > 2) {
8745 Log.w(TAG, "Layout repeat aborted after too many iterations");
8746 mLayoutNeeded = false;
8747 } else {
8748 repeats++;
8749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 }
8751 }
Romain Guy06882f82009-06-10 13:36:04 -07008752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 private final void performLayoutAndPlaceSurfacesLockedInner(
8754 boolean recoveringMemory) {
8755 final long currentTime = SystemClock.uptimeMillis();
8756 final int dw = mDisplay.getWidth();
8757 final int dh = mDisplay.getHeight();
8758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 int i;
8760
8761 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07008763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 if (mFxSession == null) {
8765 mFxSession = new SurfaceSession();
8766 }
Romain Guy06882f82009-06-10 13:36:04 -07008767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 if (SHOW_TRANSACTIONS) Log.i(TAG, ">>> OPEN TRANSACTION");
8769
8770 // Initialize state of exiting tokens.
8771 for (i=mExitingTokens.size()-1; i>=0; i--) {
8772 mExitingTokens.get(i).hasVisible = false;
8773 }
8774
8775 // Initialize state of exiting applications.
8776 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
8777 mExitingAppTokens.get(i).hasVisible = false;
8778 }
8779
8780 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 boolean orientationChangeComplete = true;
8782 Session holdScreen = null;
8783 float screenBrightness = -1;
8784 boolean focusDisplayed = false;
8785 boolean animating = false;
8786
8787 Surface.openTransaction();
8788 try {
8789 boolean restart;
8790
8791 do {
8792 final int transactionSequence = ++mTransactionSequence;
8793
8794 // Update animations of all applications, including those
8795 // associated with exiting/removed apps
8796 boolean tokensAnimating = false;
8797 final int NAT = mAppTokens.size();
8798 for (i=0; i<NAT; i++) {
8799 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8800 tokensAnimating = true;
8801 }
8802 }
8803 final int NEAT = mExitingAppTokens.size();
8804 for (i=0; i<NEAT; i++) {
8805 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
8806 tokensAnimating = true;
8807 }
8808 }
8809
8810 animating = tokensAnimating;
8811 restart = false;
8812
8813 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008814 boolean wallpaperMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008815
8816 mPolicy.beginAnimationLw(dw, dh);
8817
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07008818 final int N = mWindows.size();
8819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 for (i=N-1; i>=0; i--) {
8821 WindowState w = (WindowState)mWindows.get(i);
8822
8823 final WindowManager.LayoutParams attrs = w.mAttrs;
8824
8825 if (w.mSurface != null) {
8826 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008827 if (w.commitFinishDrawingLocked(currentTime)) {
8828 if ((w.mAttrs.flags
8829 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07008830 if (DEBUG_WALLPAPER) Log.v(TAG,
8831 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008832 wallpaperMayChange = true;
8833 }
8834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 if (w.stepAnimationLocked(currentTime, dw, dh)) {
8836 animating = true;
8837 //w.dump(" ");
8838 }
8839
8840 mPolicy.animatingWindowLw(w, attrs);
8841 }
8842
8843 final AppWindowToken atoken = w.mAppToken;
8844 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
8845 if (atoken.lastTransactionSequence != transactionSequence) {
8846 atoken.lastTransactionSequence = transactionSequence;
8847 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
8848 atoken.startingDisplayed = false;
8849 }
8850 if ((w.isOnScreen() || w.mAttrs.type
8851 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
8852 && !w.mExiting && !w.mDestroying) {
8853 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
8854 Log.v(TAG, "Eval win " + w + ": isDisplayed="
8855 + w.isDisplayedLw()
8856 + ", isAnimating=" + w.isAnimating());
8857 if (!w.isDisplayedLw()) {
8858 Log.v(TAG, "Not displayed: s=" + w.mSurface
8859 + " pv=" + w.mPolicyVisibility
8860 + " dp=" + w.mDrawPending
8861 + " cdp=" + w.mCommitDrawPending
8862 + " ah=" + w.mAttachedHidden
8863 + " th=" + atoken.hiddenRequested
8864 + " a=" + w.mAnimating);
8865 }
8866 }
8867 if (w != atoken.startingWindow) {
8868 if (!atoken.freezingScreen || !w.mAppFreezing) {
8869 atoken.numInterestingWindows++;
8870 if (w.isDisplayedLw()) {
8871 atoken.numDrawnWindows++;
8872 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Log.v(TAG,
8873 "tokenMayBeDrawn: " + atoken
8874 + " freezingScreen=" + atoken.freezingScreen
8875 + " mAppFreezing=" + w.mAppFreezing);
8876 tokenMayBeDrawn = true;
8877 }
8878 }
8879 } else if (w.isDisplayedLw()) {
8880 atoken.startingDisplayed = true;
8881 }
8882 }
8883 } else if (w.mReadyToShow) {
8884 w.performShowLocked();
8885 }
8886 }
8887
8888 if (mPolicy.finishAnimationLw()) {
8889 restart = true;
8890 }
8891
8892 if (tokenMayBeDrawn) {
8893 // See if any windows have been drawn, so they (and others
8894 // associated with them) can now be shown.
8895 final int NT = mTokenList.size();
8896 for (i=0; i<NT; i++) {
8897 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
8898 if (wtoken == null) {
8899 continue;
8900 }
8901 if (wtoken.freezingScreen) {
8902 int numInteresting = wtoken.numInterestingWindows;
8903 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8904 if (DEBUG_VISIBILITY) Log.v(TAG,
8905 "allDrawn: " + wtoken
8906 + " interesting=" + numInteresting
8907 + " drawn=" + wtoken.numDrawnWindows);
8908 wtoken.showAllWindowsLocked();
8909 unsetAppFreezingScreenLocked(wtoken, false, true);
8910 orientationChangeComplete = true;
8911 }
8912 } else if (!wtoken.allDrawn) {
8913 int numInteresting = wtoken.numInterestingWindows;
8914 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
8915 if (DEBUG_VISIBILITY) Log.v(TAG,
8916 "allDrawn: " + wtoken
8917 + " interesting=" + numInteresting
8918 + " drawn=" + wtoken.numDrawnWindows);
8919 wtoken.allDrawn = true;
8920 restart = true;
8921
8922 // We can now show all of the drawn windows!
8923 if (!mOpeningApps.contains(wtoken)) {
8924 wtoken.showAllWindowsLocked();
8925 }
8926 }
8927 }
8928 }
8929 }
8930
8931 // If we are ready to perform an app transition, check through
8932 // all of the app tokens to be shown and see if they are ready
8933 // to go.
8934 if (mAppTransitionReady) {
8935 int NN = mOpeningApps.size();
8936 boolean goodToGo = true;
8937 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8938 "Checking " + NN + " opening apps (frozen="
8939 + mDisplayFrozen + " timeout="
8940 + mAppTransitionTimeout + ")...");
8941 if (!mDisplayFrozen && !mAppTransitionTimeout) {
8942 // If the display isn't frozen, wait to do anything until
8943 // all of the apps are ready. Otherwise just go because
8944 // we'll unfreeze the display when everyone is ready.
8945 for (i=0; i<NN && goodToGo; i++) {
8946 AppWindowToken wtoken = mOpeningApps.get(i);
8947 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
8948 "Check opening app" + wtoken + ": allDrawn="
8949 + wtoken.allDrawn + " startingDisplayed="
8950 + wtoken.startingDisplayed);
8951 if (!wtoken.allDrawn && !wtoken.startingDisplayed
8952 && !wtoken.startingMoved) {
8953 goodToGo = false;
8954 }
8955 }
8956 }
8957 if (goodToGo) {
8958 if (DEBUG_APP_TRANSITIONS) Log.v(TAG, "**** GOOD TO GO");
8959 int transit = mNextAppTransition;
8960 if (mSkipAppTransitionAnimation) {
8961 transit = WindowManagerPolicy.TRANSIT_NONE;
8962 }
8963 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
8964 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07008965 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008966 mAppTransitionTimeout = false;
8967 mStartingIconInTransition = false;
8968 mSkipAppTransitionAnimation = false;
8969
8970 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
8971
Dianne Hackborna8f60182009-09-01 19:01:50 -07008972 // If there are applications waiting to come to the
8973 // top of the stack, now is the time to move their windows.
8974 // (Note that we don't do apps going to the bottom
8975 // here -- we want to keep their windows in the old
8976 // Z-order until the animation completes.)
8977 if (mToTopApps.size() > 0) {
8978 NN = mAppTokens.size();
8979 for (i=0; i<NN; i++) {
8980 AppWindowToken wtoken = mAppTokens.get(i);
8981 if (wtoken.sendingToTop) {
8982 wtoken.sendingToTop = false;
8983 moveAppWindowsLocked(wtoken, NN, false);
8984 }
8985 }
8986 mToTopApps.clear();
8987 }
8988
Dianne Hackborn25994b42009-09-04 14:21:19 -07008989 WindowState oldWallpaper = mWallpaperTarget;
8990
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008991 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07008992 wallpaperMayChange = false;
8993
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008994 // The top-most window will supply the layout params,
8995 // and we will determine it below.
8996 LayoutParams animLp = null;
8997 int bestAnimLayer = -1;
8998
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07008999 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009000 "New wallpaper target=" + mWallpaperTarget
9001 + ", lower target=" + mLowerWallpaperTarget
9002 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009003 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009004 // Do a first pass through the tokens for two
9005 // things:
9006 // (1) Determine if both the closing and opening
9007 // app token sets are wallpaper targets, in which
9008 // case special animations are needed
9009 // (since the wallpaper needs to stay static
9010 // behind them).
9011 // (2) Find the layout params of the top-most
9012 // application window in the tokens, which is
9013 // what will control the animation theme.
9014 final int NC = mClosingApps.size();
9015 NN = NC + mOpeningApps.size();
9016 for (i=0; i<NN; i++) {
9017 AppWindowToken wtoken;
9018 int mode;
9019 if (i < NC) {
9020 wtoken = mClosingApps.get(i);
9021 mode = 1;
9022 } else {
9023 wtoken = mOpeningApps.get(i-NC);
9024 mode = 2;
9025 }
9026 if (mLowerWallpaperTarget != null) {
9027 if (mLowerWallpaperTarget.mAppToken == wtoken
9028 || mUpperWallpaperTarget.mAppToken == wtoken) {
9029 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009030 }
9031 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009032 if (wtoken.appFullscreen) {
9033 WindowState ws = wtoken.findMainWindow();
9034 if (ws != null) {
9035 // If this is a compatibility mode
9036 // window, we will always use its anim.
9037 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9038 animLp = ws.mAttrs;
9039 bestAnimLayer = Integer.MAX_VALUE;
9040 } else if (ws.mLayer > bestAnimLayer) {
9041 animLp = ws.mAttrs;
9042 bestAnimLayer = ws.mLayer;
9043 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009044 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009045 }
9046 }
9047
Dianne Hackborn25994b42009-09-04 14:21:19 -07009048 if (foundWallpapers == 3) {
9049 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9050 "Wallpaper animation!");
9051 switch (transit) {
9052 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9053 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9054 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9055 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9056 break;
9057 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9058 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9059 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9060 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9061 break;
9062 }
9063 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9064 "New transit: " + transit);
9065 } else if (oldWallpaper != null) {
9066 // We are transitioning from an activity with
9067 // a wallpaper to one without.
9068 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
9069 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9070 "New transit away from wallpaper: " + transit);
9071 } else if (mWallpaperTarget != null) {
9072 // We are transitioning from an activity without
9073 // a wallpaper to now showing the wallpaper
9074 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
9075 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9076 "New transit into wallpaper: " + transit);
9077 }
9078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 NN = mOpeningApps.size();
9080 for (i=0; i<NN; i++) {
9081 AppWindowToken wtoken = mOpeningApps.get(i);
9082 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9083 "Now opening app" + wtoken);
9084 wtoken.reportedVisible = false;
9085 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009086 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009087 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009089 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 wtoken.showAllWindowsLocked();
9091 }
9092 NN = mClosingApps.size();
9093 for (i=0; i<NN; i++) {
9094 AppWindowToken wtoken = mClosingApps.get(i);
9095 if (DEBUG_APP_TRANSITIONS) Log.v(TAG,
9096 "Now closing app" + wtoken);
9097 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009098 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009099 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009101 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 // Force the allDrawn flag, because we want to start
9103 // this guy's animations regardless of whether it's
9104 // gotten drawn.
9105 wtoken.allDrawn = true;
9106 }
9107
9108 mOpeningApps.clear();
9109 mClosingApps.clear();
9110
9111 // This has changed the visibility of windows, so perform
9112 // a new layout to get them all up-to-date.
9113 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009114 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9115 assignLayersLocked();
9116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 performLayoutLockedInner();
9118 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
9119
9120 restart = true;
9121 }
9122 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009123
Dianne Hackborna8f60182009-09-01 19:01:50 -07009124 if (!animating && mAppTransitionRunning) {
9125 // We have finished the animation of an app transition. To do
9126 // this, we have delayed a lot of operations like showing and
9127 // hiding apps, moving apps in Z-order, etc. The app token list
9128 // reflects the correct Z-order, but the window list may now
9129 // be out of sync with it. So here we will just rebuild the
9130 // entire app window list. Fun!
9131 mAppTransitionRunning = false;
9132 // Clear information about apps that were moving.
9133 mToBottomApps.clear();
9134
9135 rebuildAppWindowListLocked();
9136 restart = true;
9137 moveInputMethodWindowsIfNeededLocked(false);
9138 wallpaperMayChange = true;
9139 mLayoutNeeded = true;
9140 }
9141
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009142 if (wallpaperMayChange) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009143 if (DEBUG_WALLPAPER) Log.v(TAG,
9144 "Wallpaper may change! Adjusting");
9145 int adjResult = adjustWallpaperWindowsLocked();
9146 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9147 if (DEBUG_WALLPAPER) Log.v(TAG,
9148 "Wallpaper layer changed: assigning layers + relayout");
9149 restart = true;
9150 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009151 assignLayersLocked();
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009152 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
9153 if (DEBUG_WALLPAPER) Log.v(TAG,
9154 "Wallpaper visibility changed: relayout");
9155 restart = true;
9156 mLayoutNeeded = true;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009157 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009158 if (mLayoutNeeded) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009159 restart = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009160 performLayoutLockedInner();
9161 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009162 }
9163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 } while (restart);
9165
9166 // THIRD LOOP: Update the surfaces of all windows.
9167
9168 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9169
9170 boolean obscured = false;
9171 boolean blurring = false;
9172 boolean dimming = false;
9173 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009174 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009175 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009177 final int N = mWindows.size();
9178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 for (i=N-1; i>=0; i--) {
9180 WindowState w = (WindowState)mWindows.get(i);
9181
9182 boolean displayed = false;
9183 final WindowManager.LayoutParams attrs = w.mAttrs;
9184 final int attrFlags = attrs.flags;
9185
9186 if (w.mSurface != null) {
9187 w.computeShownFrameLocked();
9188 if (localLOGV) Log.v(
9189 TAG, "Placing surface #" + i + " " + w.mSurface
9190 + ": new=" + w.mShownFrame + ", old="
9191 + w.mLastShownFrame);
9192
9193 boolean resize;
9194 int width, height;
9195 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9196 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9197 w.mLastRequestedHeight != w.mRequestedHeight;
9198 // for a scaled surface, we just want to use
9199 // the requested size.
9200 width = w.mRequestedWidth;
9201 height = w.mRequestedHeight;
9202 w.mLastRequestedWidth = width;
9203 w.mLastRequestedHeight = height;
9204 w.mLastShownFrame.set(w.mShownFrame);
9205 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009206 if (SHOW_TRANSACTIONS) Log.i(
9207 TAG, " SURFACE " + w.mSurface
9208 + ": POS " + w.mShownFrame.left
9209 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9211 } catch (RuntimeException e) {
9212 Log.w(TAG, "Error positioning surface in " + w, e);
9213 if (!recoveringMemory) {
9214 reclaimSomeSurfaceMemoryLocked(w, "position");
9215 }
9216 }
9217 } else {
9218 resize = !w.mLastShownFrame.equals(w.mShownFrame);
9219 width = w.mShownFrame.width();
9220 height = w.mShownFrame.height();
9221 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 }
9223
9224 if (resize) {
9225 if (width < 1) width = 1;
9226 if (height < 1) height = 1;
9227 if (w.mSurface != null) {
9228 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009229 if (SHOW_TRANSACTIONS) Log.i(
9230 TAG, " SURFACE " + w.mSurface + ": POS "
9231 + w.mShownFrame.left + ","
9232 + w.mShownFrame.top + " SIZE "
9233 + w.mShownFrame.width() + "x"
9234 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 w.mSurface.setSize(width, height);
9236 w.mSurface.setPosition(w.mShownFrame.left,
9237 w.mShownFrame.top);
9238 } catch (RuntimeException e) {
9239 // If something goes wrong with the surface (such
9240 // as running out of memory), don't take down the
9241 // entire system.
9242 Log.e(TAG, "Failure updating surface of " + w
9243 + "size=(" + width + "x" + height
9244 + "), pos=(" + w.mShownFrame.left
9245 + "," + w.mShownFrame.top + ")", e);
9246 if (!recoveringMemory) {
9247 reclaimSomeSurfaceMemoryLocked(w, "size");
9248 }
9249 }
9250 }
9251 }
9252 if (!w.mAppFreezing) {
9253 w.mContentInsetsChanged =
9254 !w.mLastContentInsets.equals(w.mContentInsets);
9255 w.mVisibleInsetsChanged =
9256 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Romain Guy06882f82009-06-10 13:36:04 -07009257 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 || w.mContentInsetsChanged
9259 || w.mVisibleInsetsChanged) {
9260 w.mLastFrame.set(w.mFrame);
9261 w.mLastContentInsets.set(w.mContentInsets);
9262 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009263 // If the screen is currently frozen, then keep
9264 // it frozen until this window draws at its new
9265 // orientation.
9266 if (mDisplayFrozen) {
9267 if (DEBUG_ORIENTATION) Log.v(TAG,
9268 "Resizing while display frozen: " + w);
9269 w.mOrientationChanging = true;
9270 if (mWindowsFreezingScreen) {
9271 mWindowsFreezingScreen = true;
9272 // XXX should probably keep timeout from
9273 // when we first froze the display.
9274 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9275 mH.sendMessageDelayed(mH.obtainMessage(
9276 H.WINDOW_FREEZE_TIMEOUT), 2000);
9277 }
9278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 // If the orientation is changing, then we need to
9280 // hold off on unfreezing the display until this
9281 // window has been redrawn; to do that, we need
9282 // to go through the process of getting informed
9283 // by the application when it has finished drawing.
9284 if (w.mOrientationChanging) {
9285 if (DEBUG_ORIENTATION) Log.v(TAG,
9286 "Orientation start waiting for draw in "
9287 + w + ", surface " + w.mSurface);
9288 w.mDrawPending = true;
9289 w.mCommitDrawPending = false;
9290 w.mReadyToShow = false;
9291 if (w.mAppToken != null) {
9292 w.mAppToken.allDrawn = false;
9293 }
9294 }
Romain Guy06882f82009-06-10 13:36:04 -07009295 if (DEBUG_ORIENTATION) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 "Resizing window " + w + " to " + w.mFrame);
9297 mResizingWindows.add(w);
9298 } else if (w.mOrientationChanging) {
9299 if (!w.mDrawPending && !w.mCommitDrawPending) {
9300 if (DEBUG_ORIENTATION) Log.v(TAG,
9301 "Orientation not waiting for draw in "
9302 + w + ", surface " + w.mSurface);
9303 w.mOrientationChanging = false;
9304 }
9305 }
9306 }
9307
9308 if (w.mAttachedHidden) {
9309 if (!w.mLastHidden) {
9310 //dump();
9311 w.mLastHidden = true;
9312 if (SHOW_TRANSACTIONS) Log.i(
9313 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-attached)");
9314 if (w.mSurface != null) {
9315 try {
9316 w.mSurface.hide();
9317 } catch (RuntimeException e) {
9318 Log.w(TAG, "Exception hiding surface in " + w);
9319 }
9320 }
9321 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9322 }
9323 // If we are waiting for this window to handle an
9324 // orientation change, well, it is hidden, so
9325 // doesn't really matter. Note that this does
9326 // introduce a potential glitch if the window
9327 // becomes unhidden before it has drawn for the
9328 // new orientation.
9329 if (w.mOrientationChanging) {
9330 w.mOrientationChanging = false;
9331 if (DEBUG_ORIENTATION) Log.v(TAG,
9332 "Orientation change skips hidden " + w);
9333 }
9334 } else if (!w.isReadyForDisplay()) {
9335 if (!w.mLastHidden) {
9336 //dump();
9337 w.mLastHidden = true;
9338 if (SHOW_TRANSACTIONS) Log.i(
9339 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout-ready)");
9340 if (w.mSurface != null) {
9341 try {
9342 w.mSurface.hide();
9343 } catch (RuntimeException e) {
9344 Log.w(TAG, "Exception exception hiding surface in " + w);
9345 }
9346 }
9347 mKeyWaiter.releasePendingPointerLocked(w.mSession);
9348 }
9349 // If we are waiting for this window to handle an
9350 // orientation change, well, it is hidden, so
9351 // doesn't really matter. Note that this does
9352 // introduce a potential glitch if the window
9353 // becomes unhidden before it has drawn for the
9354 // new orientation.
9355 if (w.mOrientationChanging) {
9356 w.mOrientationChanging = false;
9357 if (DEBUG_ORIENTATION) Log.v(TAG,
9358 "Orientation change skips hidden " + w);
9359 }
9360 } else if (w.mLastLayer != w.mAnimLayer
9361 || w.mLastAlpha != w.mShownAlpha
9362 || w.mLastDsDx != w.mDsDx
9363 || w.mLastDtDx != w.mDtDx
9364 || w.mLastDsDy != w.mDsDy
9365 || w.mLastDtDy != w.mDtDy
9366 || w.mLastHScale != w.mHScale
9367 || w.mLastVScale != w.mVScale
9368 || w.mLastHidden) {
9369 displayed = true;
9370 w.mLastAlpha = w.mShownAlpha;
9371 w.mLastLayer = w.mAnimLayer;
9372 w.mLastDsDx = w.mDsDx;
9373 w.mLastDtDx = w.mDtDx;
9374 w.mLastDsDy = w.mDsDy;
9375 w.mLastDtDy = w.mDtDy;
9376 w.mLastHScale = w.mHScale;
9377 w.mLastVScale = w.mVScale;
9378 if (SHOW_TRANSACTIONS) Log.i(
9379 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009380 + w.mShownAlpha + " layer=" + w.mAnimLayer
9381 + " matrix=[" + (w.mDsDx*w.mHScale)
9382 + "," + (w.mDtDx*w.mVScale)
9383 + "][" + (w.mDsDy*w.mHScale)
9384 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 if (w.mSurface != null) {
9386 try {
9387 w.mSurface.setAlpha(w.mShownAlpha);
9388 w.mSurface.setLayer(w.mAnimLayer);
9389 w.mSurface.setMatrix(
9390 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
9391 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
9392 } catch (RuntimeException e) {
9393 Log.w(TAG, "Error updating surface in " + w, e);
9394 if (!recoveringMemory) {
9395 reclaimSomeSurfaceMemoryLocked(w, "update");
9396 }
9397 }
9398 }
9399
9400 if (w.mLastHidden && !w.mDrawPending
9401 && !w.mCommitDrawPending
9402 && !w.mReadyToShow) {
9403 if (SHOW_TRANSACTIONS) Log.i(
9404 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
9405 if (DEBUG_VISIBILITY) Log.v(TAG, "Showing " + w
9406 + " during relayout");
9407 if (showSurfaceRobustlyLocked(w)) {
9408 w.mHasDrawn = true;
9409 w.mLastHidden = false;
9410 } else {
9411 w.mOrientationChanging = false;
9412 }
9413 }
9414 if (w.mSurface != null) {
9415 w.mToken.hasVisible = true;
9416 }
9417 } else {
9418 displayed = true;
9419 }
9420
9421 if (displayed) {
9422 if (!covered) {
9423 if (attrs.width == LayoutParams.FILL_PARENT
9424 && attrs.height == LayoutParams.FILL_PARENT) {
9425 covered = true;
9426 }
9427 }
9428 if (w.mOrientationChanging) {
9429 if (w.mDrawPending || w.mCommitDrawPending) {
9430 orientationChangeComplete = false;
9431 if (DEBUG_ORIENTATION) Log.v(TAG,
9432 "Orientation continue waiting for draw in " + w);
9433 } else {
9434 w.mOrientationChanging = false;
9435 if (DEBUG_ORIENTATION) Log.v(TAG,
9436 "Orientation change complete in " + w);
9437 }
9438 }
9439 w.mToken.hasVisible = true;
9440 }
9441 } else if (w.mOrientationChanging) {
9442 if (DEBUG_ORIENTATION) Log.v(TAG,
9443 "Orientation change skips hidden " + w);
9444 w.mOrientationChanging = false;
9445 }
9446
9447 final boolean canBeSeen = w.isDisplayedLw();
9448
9449 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
9450 focusDisplayed = true;
9451 }
9452
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009453 final boolean obscuredChanged = w.mObscured != obscured;
9454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009456 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009457 if (w.mSurface != null) {
9458 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
9459 holdScreen = w.mSession;
9460 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009461 if (!syswin && w.mAttrs.screenBrightness >= 0
9462 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 screenBrightness = w.mAttrs.screenBrightness;
9464 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009465 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
9466 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
9467 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
9468 syswin = true;
9469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009471
Dianne Hackborn25994b42009-09-04 14:21:19 -07009472 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
9473 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 // This window completely covers everything behind it,
9475 // so we want to leave all of them as unblurred (for
9476 // performance reasons).
9477 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009478 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
9479 if (SHOW_TRANSACTIONS) Log.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009480 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009481 obscured = true;
9482 if (mBackgroundFillerSurface == null) {
9483 try {
9484 mBackgroundFillerSurface = new Surface(mFxSession, 0,
9485 0, dw, dh,
9486 PixelFormat.OPAQUE,
9487 Surface.FX_SURFACE_NORMAL);
9488 } catch (Exception e) {
9489 Log.e(TAG, "Exception creating filler surface", e);
9490 }
9491 }
9492 try {
9493 mBackgroundFillerSurface.setPosition(0, 0);
9494 mBackgroundFillerSurface.setSize(dw, dh);
9495 // Using the same layer as Dim because they will never be shown at the
9496 // same time.
9497 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
9498 mBackgroundFillerSurface.show();
9499 } catch (RuntimeException e) {
9500 Log.e(TAG, "Exception showing filler surface");
9501 }
9502 backgroundFillerShown = true;
9503 mBackgroundFillerShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 } else if (canBeSeen && !obscured &&
9505 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
9506 if (localLOGV) Log.v(TAG, "Win " + w
9507 + ": blurring=" + blurring
9508 + " obscured=" + obscured
9509 + " displayed=" + displayed);
9510 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
9511 if (!dimming) {
9512 //Log.i(TAG, "DIM BEHIND: " + w);
9513 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009514 if (mDimAnimator == null) {
9515 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009517 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009518 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009519 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520 }
9521 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
9522 if (!blurring) {
9523 //Log.i(TAG, "BLUR BEHIND: " + w);
9524 blurring = true;
9525 mBlurShown = true;
9526 if (mBlurSurface == null) {
9527 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9528 + mBlurSurface + ": CREATE");
9529 try {
Romain Guy06882f82009-06-10 13:36:04 -07009530 mBlurSurface = new Surface(mFxSession, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 -1, 16, 16,
9532 PixelFormat.OPAQUE,
9533 Surface.FX_SURFACE_BLUR);
9534 } catch (Exception e) {
9535 Log.e(TAG, "Exception creating Blur surface", e);
9536 }
9537 }
9538 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR "
9539 + mBlurSurface + ": SHOW pos=(0,0) (" +
9540 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
9541 if (mBlurSurface != null) {
9542 mBlurSurface.setPosition(0, 0);
9543 mBlurSurface.setSize(dw, dh);
9544 try {
9545 mBlurSurface.show();
9546 } catch (RuntimeException e) {
9547 Log.w(TAG, "Failure showing blur surface", e);
9548 }
9549 }
9550 }
9551 mBlurSurface.setLayer(w.mAnimLayer-2);
9552 }
9553 }
9554 }
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07009555
9556 if (obscuredChanged && mWallpaperTarget == w) {
9557 // This is the wallpaper target and its obscured state
9558 // changed... make sure the current wallaper's visibility
9559 // has been updated accordingly.
9560 updateWallpaperVisibilityLocked();
9561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009562 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009563
9564 if (backgroundFillerShown == false && mBackgroundFillerShown) {
9565 mBackgroundFillerShown = false;
9566 if (SHOW_TRANSACTIONS) Log.d(TAG, "hiding background filler");
9567 try {
9568 mBackgroundFillerSurface.hide();
9569 } catch (RuntimeException e) {
9570 Log.e(TAG, "Exception hiding filler surface", e);
9571 }
9572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07009574 if (mDimAnimator != null && mDimAnimator.mDimShown) {
9575 animating |= mDimAnimator.updateSurface(dimming, currentTime, mDisplayFrozen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 }
Romain Guy06882f82009-06-10 13:36:04 -07009577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 if (!blurring && mBlurShown) {
9579 if (SHOW_TRANSACTIONS) Log.i(TAG, " BLUR " + mBlurSurface
9580 + ": HIDE");
9581 try {
9582 mBlurSurface.hide();
9583 } catch (IllegalArgumentException e) {
9584 Log.w(TAG, "Illegal argument exception hiding blur surface");
9585 }
9586 mBlurShown = false;
9587 }
9588
9589 if (SHOW_TRANSACTIONS) Log.i(TAG, "<<< CLOSE TRANSACTION");
9590 } catch (RuntimeException e) {
9591 Log.e(TAG, "Unhandled exception in Window Manager", e);
9592 }
9593
9594 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -07009595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 if (DEBUG_ORIENTATION && mDisplayFrozen) Log.v(TAG,
9597 "With display frozen, orientationChangeComplete="
9598 + orientationChangeComplete);
9599 if (orientationChangeComplete) {
9600 if (mWindowsFreezingScreen) {
9601 mWindowsFreezingScreen = false;
9602 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
9603 }
9604 if (mAppsFreezingScreen == 0) {
9605 stopFreezingDisplayLocked();
9606 }
9607 }
Romain Guy06882f82009-06-10 13:36:04 -07009608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009609 i = mResizingWindows.size();
9610 if (i > 0) {
9611 do {
9612 i--;
9613 WindowState win = mResizingWindows.get(i);
9614 try {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009615 if (DEBUG_ORIENTATION) Log.v(TAG, "Reporting new frame to "
9616 + win + ": " + win.mFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009617 win.mClient.resized(win.mFrame.width(),
9618 win.mFrame.height(), win.mLastContentInsets,
9619 win.mLastVisibleInsets, win.mDrawPending);
9620 win.mContentInsetsChanged = false;
9621 win.mVisibleInsetsChanged = false;
9622 } catch (RemoteException e) {
9623 win.mOrientationChanging = false;
9624 }
9625 } while (i > 0);
9626 mResizingWindows.clear();
9627 }
Romain Guy06882f82009-06-10 13:36:04 -07009628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009630 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 i = mDestroySurface.size();
9632 if (i > 0) {
9633 do {
9634 i--;
9635 WindowState win = mDestroySurface.get(i);
9636 win.mDestroying = false;
9637 if (mInputMethodWindow == win) {
9638 mInputMethodWindow = null;
9639 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009640 if (win == mWallpaperTarget) {
9641 wallpaperDestroyed = true;
9642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 win.destroySurfaceLocked();
9644 } while (i > 0);
9645 mDestroySurface.clear();
9646 }
9647
9648 // Time to remove any exiting tokens?
9649 for (i=mExitingTokens.size()-1; i>=0; i--) {
9650 WindowToken token = mExitingTokens.get(i);
9651 if (!token.hasVisible) {
9652 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07009653 if (token.windowType == TYPE_WALLPAPER) {
9654 mWallpaperTokens.remove(token);
9655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009656 }
9657 }
9658
9659 // Time to remove any exiting applications?
9660 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9661 AppWindowToken token = mExitingAppTokens.get(i);
9662 if (!token.hasVisible && !mClosingApps.contains(token)) {
9663 mAppTokens.remove(token);
9664 mExitingAppTokens.remove(i);
9665 }
9666 }
9667
Dianne Hackborna8f60182009-09-01 19:01:50 -07009668 boolean needRelayout = false;
9669
9670 if (!animating && mAppTransitionRunning) {
9671 // We have finished the animation of an app transition. To do
9672 // this, we have delayed a lot of operations like showing and
9673 // hiding apps, moving apps in Z-order, etc. The app token list
9674 // reflects the correct Z-order, but the window list may now
9675 // be out of sync with it. So here we will just rebuild the
9676 // entire app window list. Fun!
9677 mAppTransitionRunning = false;
9678 needRelayout = true;
9679 rebuildAppWindowListLocked();
9680 // Clear information about apps that were moving.
9681 mToBottomApps.clear();
9682 }
9683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009684 if (focusDisplayed) {
9685 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
9686 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009687 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009688 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009689 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009690 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009691 requestAnimationLocked(0);
9692 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
9694 }
9695 mQueue.setHoldScreenLocked(holdScreen != null);
9696 if (screenBrightness < 0 || screenBrightness > 1.0f) {
9697 mPowerManager.setScreenBrightnessOverride(-1);
9698 } else {
9699 mPowerManager.setScreenBrightnessOverride((int)
9700 (screenBrightness * Power.BRIGHTNESS_ON));
9701 }
9702 if (holdScreen != mHoldingScreenOn) {
9703 mHoldingScreenOn = holdScreen;
9704 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
9705 mH.sendMessage(m);
9706 }
9707 }
9708
9709 void requestAnimationLocked(long delay) {
9710 if (!mAnimationPending) {
9711 mAnimationPending = true;
9712 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
9713 }
9714 }
Romain Guy06882f82009-06-10 13:36:04 -07009715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 /**
9717 * Have the surface flinger show a surface, robustly dealing with
9718 * error conditions. In particular, if there is not enough memory
9719 * to show the surface, then we will try to get rid of other surfaces
9720 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -07009721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009722 * @return Returns true if the surface was successfully shown.
9723 */
9724 boolean showSurfaceRobustlyLocked(WindowState win) {
9725 try {
9726 if (win.mSurface != null) {
9727 win.mSurface.show();
9728 }
9729 return true;
9730 } catch (RuntimeException e) {
9731 Log.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
9732 }
Romain Guy06882f82009-06-10 13:36:04 -07009733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009734 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -07009735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736 return false;
9737 }
Romain Guy06882f82009-06-10 13:36:04 -07009738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
9740 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -07009741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009742 EventLog.writeEvent(LOG_WM_NO_SURFACE_MEMORY, win.toString(),
9743 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -07009744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 if (mForceRemoves == null) {
9746 mForceRemoves = new ArrayList<WindowState>();
9747 }
Romain Guy06882f82009-06-10 13:36:04 -07009748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 long callingIdentity = Binder.clearCallingIdentity();
9750 try {
9751 // There was some problem... first, do a sanity check of the
9752 // window list to make sure we haven't left any dangling surfaces
9753 // around.
9754 int N = mWindows.size();
9755 boolean leakedSurface = false;
9756 Log.i(TAG, "Out of memory for surface! Looking for leaks...");
9757 for (int i=0; i<N; i++) {
9758 WindowState ws = (WindowState)mWindows.get(i);
9759 if (ws.mSurface != null) {
9760 if (!mSessions.contains(ws.mSession)) {
9761 Log.w(TAG, "LEAKED SURFACE (session doesn't exist): "
9762 + ws + " surface=" + ws.mSurface
9763 + " token=" + win.mToken
9764 + " pid=" + ws.mSession.mPid
9765 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009766 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 ws.mSurface = null;
9768 mForceRemoves.add(ws);
9769 i--;
9770 N--;
9771 leakedSurface = true;
9772 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
9773 Log.w(TAG, "LEAKED SURFACE (app token hidden): "
9774 + ws + " surface=" + ws.mSurface
9775 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009776 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 ws.mSurface = null;
9778 leakedSurface = true;
9779 }
9780 }
9781 }
Romain Guy06882f82009-06-10 13:36:04 -07009782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 boolean killedApps = false;
9784 if (!leakedSurface) {
9785 Log.w(TAG, "No leaked surfaces; killing applicatons!");
9786 SparseIntArray pidCandidates = new SparseIntArray();
9787 for (int i=0; i<N; i++) {
9788 WindowState ws = (WindowState)mWindows.get(i);
9789 if (ws.mSurface != null) {
9790 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
9791 }
9792 }
9793 if (pidCandidates.size() > 0) {
9794 int[] pids = new int[pidCandidates.size()];
9795 for (int i=0; i<pids.length; i++) {
9796 pids[i] = pidCandidates.keyAt(i);
9797 }
9798 try {
9799 if (mActivityManager.killPidsForMemory(pids)) {
9800 killedApps = true;
9801 }
9802 } catch (RemoteException e) {
9803 }
9804 }
9805 }
Romain Guy06882f82009-06-10 13:36:04 -07009806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 if (leakedSurface || killedApps) {
9808 // We managed to reclaim some memory, so get rid of the trouble
9809 // surface and ask the app to request another one.
9810 Log.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
9811 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009812 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 win.mSurface = null;
9814 }
Romain Guy06882f82009-06-10 13:36:04 -07009815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 try {
9817 win.mClient.dispatchGetNewSurface();
9818 } catch (RemoteException e) {
9819 }
9820 }
9821 } finally {
9822 Binder.restoreCallingIdentity(callingIdentity);
9823 }
9824 }
Romain Guy06882f82009-06-10 13:36:04 -07009825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 private boolean updateFocusedWindowLocked(int mode) {
9827 WindowState newFocus = computeFocusedWindowLocked();
9828 if (mCurrentFocus != newFocus) {
9829 // This check makes sure that we don't already have the focus
9830 // change message pending.
9831 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
9832 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
9833 if (localLOGV) Log.v(
9834 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
9835 final WindowState oldFocus = mCurrentFocus;
9836 mCurrentFocus = newFocus;
9837 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -07009838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 final WindowState imWindow = mInputMethodWindow;
9840 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009841 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009843 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
9844 mLayoutNeeded = true;
9845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
9847 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009848 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
9849 // Client will do the layout, but we need to assign layers
9850 // for handleNewWindowLocked() below.
9851 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 }
9853 }
Romain Guy06882f82009-06-10 13:36:04 -07009854
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08009855 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
9856 mKeyWaiter.handleNewWindowLocked(newFocus);
9857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 return true;
9859 }
9860 return false;
9861 }
9862
9863 private WindowState computeFocusedWindowLocked() {
9864 WindowState result = null;
9865 WindowState win;
9866
9867 int i = mWindows.size() - 1;
9868 int nextAppIndex = mAppTokens.size()-1;
9869 WindowToken nextApp = nextAppIndex >= 0
9870 ? mAppTokens.get(nextAppIndex) : null;
9871
9872 while (i >= 0) {
9873 win = (WindowState)mWindows.get(i);
9874
9875 if (localLOGV || DEBUG_FOCUS) Log.v(
9876 TAG, "Looking for focus: " + i
9877 + " = " + win
9878 + ", flags=" + win.mAttrs.flags
9879 + ", canReceive=" + win.canReceiveKeys());
9880
9881 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -07009882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 // If this window's application has been removed, just skip it.
9884 if (thisApp != null && thisApp.removed) {
9885 i--;
9886 continue;
9887 }
Romain Guy06882f82009-06-10 13:36:04 -07009888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 // If there is a focused app, don't allow focus to go to any
9890 // windows below it. If this is an application window, step
9891 // through the app tokens until we find its app.
9892 if (thisApp != null && nextApp != null && thisApp != nextApp
9893 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
9894 int origAppIndex = nextAppIndex;
9895 while (nextAppIndex > 0) {
9896 if (nextApp == mFocusedApp) {
9897 // Whoops, we are below the focused app... no focus
9898 // for you!
9899 if (localLOGV || DEBUG_FOCUS) Log.v(
9900 TAG, "Reached focused app: " + mFocusedApp);
9901 return null;
9902 }
9903 nextAppIndex--;
9904 nextApp = mAppTokens.get(nextAppIndex);
9905 if (nextApp == thisApp) {
9906 break;
9907 }
9908 }
9909 if (thisApp != nextApp) {
9910 // Uh oh, the app token doesn't exist! This shouldn't
9911 // happen, but if it does we can get totally hosed...
9912 // so restart at the original app.
9913 nextAppIndex = origAppIndex;
9914 nextApp = mAppTokens.get(nextAppIndex);
9915 }
9916 }
9917
9918 // Dispatch to this window if it is wants key events.
9919 if (win.canReceiveKeys()) {
9920 if (DEBUG_FOCUS) Log.v(
9921 TAG, "Found focus @ " + i + " = " + win);
9922 result = win;
9923 break;
9924 }
9925
9926 i--;
9927 }
9928
9929 return result;
9930 }
9931
9932 private void startFreezingDisplayLocked() {
9933 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -07009934 // Freezing the display also suspends key event delivery, to
9935 // keep events from going astray while the display is reconfigured.
9936 // If someone has changed orientation again while the screen is
9937 // still frozen, the events will continue to be blocked while the
9938 // successive orientation change is processed. To prevent spurious
9939 // ANRs, we reset the event dispatch timeout in this case.
9940 synchronized (mKeyWaiter) {
9941 mKeyWaiter.mWasFrozen = true;
9942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 return;
9944 }
Romain Guy06882f82009-06-10 13:36:04 -07009945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -07009947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 long now = SystemClock.uptimeMillis();
9949 //Log.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
9950 if (mFreezeGcPending != 0) {
9951 if (now > (mFreezeGcPending+1000)) {
9952 //Log.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
9953 mH.removeMessages(H.FORCE_GC);
9954 Runtime.getRuntime().gc();
9955 mFreezeGcPending = now;
9956 }
9957 } else {
9958 mFreezeGcPending = now;
9959 }
Romain Guy06882f82009-06-10 13:36:04 -07009960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 mDisplayFrozen = true;
9962 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_NONE) {
9963 mNextAppTransition = WindowManagerPolicy.TRANSIT_NONE;
9964 mAppTransitionReady = true;
9965 }
Romain Guy06882f82009-06-10 13:36:04 -07009966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 if (PROFILE_ORIENTATION) {
9968 File file = new File("/data/system/frozen");
9969 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
9970 }
9971 Surface.freezeDisplay(0);
9972 }
Romain Guy06882f82009-06-10 13:36:04 -07009973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 private void stopFreezingDisplayLocked() {
9975 if (!mDisplayFrozen) {
9976 return;
9977 }
Romain Guy06882f82009-06-10 13:36:04 -07009978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 mDisplayFrozen = false;
9980 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
9981 if (PROFILE_ORIENTATION) {
9982 Debug.stopMethodTracing();
9983 }
9984 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -07009985
Chris Tate2ad63a92009-03-25 17:36:48 -07009986 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
9987 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 synchronized (mKeyWaiter) {
9989 mKeyWaiter.mWasFrozen = true;
9990 mKeyWaiter.notifyAll();
9991 }
9992
9993 // A little kludge: a lot could have happened while the
9994 // display was frozen, so now that we are coming back we
9995 // do a gc so that any remote references the system
9996 // processes holds on others can be released if they are
9997 // no longer needed.
9998 mH.removeMessages(H.FORCE_GC);
9999 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10000 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 mScreenFrozenLock.release();
10003 }
Romain Guy06882f82009-06-10 13:36:04 -070010004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 @Override
10006 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10007 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10008 != PackageManager.PERMISSION_GRANTED) {
10009 pw.println("Permission Denial: can't dump WindowManager from from pid="
10010 + Binder.getCallingPid()
10011 + ", uid=" + Binder.getCallingUid());
10012 return;
10013 }
Romain Guy06882f82009-06-10 13:36:04 -070010014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 synchronized(mWindowMap) {
10016 pw.println("Current Window Manager state:");
10017 for (int i=mWindows.size()-1; i>=0; i--) {
10018 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010019 pw.print(" Window #"); pw.print(i); pw.print(' ');
10020 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 w.dump(pw, " ");
10022 }
10023 if (mInputMethodDialogs.size() > 0) {
10024 pw.println(" ");
10025 pw.println(" Input method dialogs:");
10026 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10027 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010028 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 }
10030 }
10031 if (mPendingRemove.size() > 0) {
10032 pw.println(" ");
10033 pw.println(" Remove pending for:");
10034 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10035 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010036 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10037 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 w.dump(pw, " ");
10039 }
10040 }
10041 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10042 pw.println(" ");
10043 pw.println(" Windows force removing:");
10044 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10045 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010046 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10047 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 w.dump(pw, " ");
10049 }
10050 }
10051 if (mDestroySurface.size() > 0) {
10052 pw.println(" ");
10053 pw.println(" Windows waiting to destroy their surface:");
10054 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10055 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010056 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10057 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 w.dump(pw, " ");
10059 }
10060 }
10061 if (mLosingFocus.size() > 0) {
10062 pw.println(" ");
10063 pw.println(" Windows losing focus:");
10064 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10065 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010066 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10067 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 w.dump(pw, " ");
10069 }
10070 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010071 if (mResizingWindows.size() > 0) {
10072 pw.println(" ");
10073 pw.println(" Windows waiting to resize:");
10074 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10075 WindowState w = mResizingWindows.get(i);
10076 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10077 pw.print(w); pw.println(":");
10078 w.dump(pw, " ");
10079 }
10080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 if (mSessions.size() > 0) {
10082 pw.println(" ");
10083 pw.println(" All active sessions:");
10084 Iterator<Session> it = mSessions.iterator();
10085 while (it.hasNext()) {
10086 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010087 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 s.dump(pw, " ");
10089 }
10090 }
10091 if (mTokenMap.size() > 0) {
10092 pw.println(" ");
10093 pw.println(" All tokens:");
10094 Iterator<WindowToken> it = mTokenMap.values().iterator();
10095 while (it.hasNext()) {
10096 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010097 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 token.dump(pw, " ");
10099 }
10100 }
10101 if (mTokenList.size() > 0) {
10102 pw.println(" ");
10103 pw.println(" Window token list:");
10104 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010105 pw.print(" #"); pw.print(i); pw.print(": ");
10106 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 }
10108 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010109 if (mWallpaperTokens.size() > 0) {
10110 pw.println(" ");
10111 pw.println(" Wallpaper tokens:");
10112 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10113 WindowToken token = mWallpaperTokens.get(i);
10114 pw.print(" Wallpaper #"); pw.print(i);
10115 pw.print(' '); pw.print(token); pw.println(':');
10116 token.dump(pw, " ");
10117 }
10118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 if (mAppTokens.size() > 0) {
10120 pw.println(" ");
10121 pw.println(" Application tokens in Z order:");
10122 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010123 pw.print(" App #"); pw.print(i); pw.print(": ");
10124 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010125 }
10126 }
10127 if (mFinishedStarting.size() > 0) {
10128 pw.println(" ");
10129 pw.println(" Finishing start of application tokens:");
10130 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10131 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010132 pw.print(" Finished Starting #"); pw.print(i);
10133 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010134 token.dump(pw, " ");
10135 }
10136 }
10137 if (mExitingTokens.size() > 0) {
10138 pw.println(" ");
10139 pw.println(" Exiting tokens:");
10140 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10141 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010142 pw.print(" Exiting #"); pw.print(i);
10143 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 token.dump(pw, " ");
10145 }
10146 }
10147 if (mExitingAppTokens.size() > 0) {
10148 pw.println(" ");
10149 pw.println(" Exiting application tokens:");
10150 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10151 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010152 pw.print(" Exiting App #"); pw.print(i);
10153 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 token.dump(pw, " ");
10155 }
10156 }
10157 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010158 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10159 pw.print(" mLastFocus="); pw.println(mLastFocus);
10160 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10161 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10162 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010163 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010164 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10165 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10166 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10167 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010168 pw.print(" mInTouchMode="); pw.println(mInTouchMode);
10169 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10170 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10171 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10172 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010173 if (mDimAnimator != null) {
10174 mDimAnimator.printTo(pw);
10175 } else {
10176 pw.print( " no DimAnimator ");
10177 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010178 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010179 pw.print(mInputMethodAnimLayerAdjustment);
10180 pw.print(" mWallpaperAnimLayerAdjustment=");
10181 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010182 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10183 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010184 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
10185 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
10186 pw.print(" mAppsFreezingScreen="); pw.println(mAppsFreezingScreen);
10187 pw.print(" mRotation="); pw.print(mRotation);
10188 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
10189 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
10190 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
10191 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
10192 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
10193 pw.print(" mNextAppTransition=0x");
10194 pw.print(Integer.toHexString(mNextAppTransition));
10195 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070010196 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010197 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
10198 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
10199 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
10200 if (mOpeningApps.size() > 0) {
10201 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
10202 }
10203 if (mClosingApps.size() > 0) {
10204 pw.print(" mClosingApps="); pw.println(mClosingApps);
10205 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010206 if (mToTopApps.size() > 0) {
10207 pw.print(" mToTopApps="); pw.println(mToTopApps);
10208 }
10209 if (mToBottomApps.size() > 0) {
10210 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
10211 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010212 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
10213 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010215 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
10216 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
10217 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
10218 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
10219 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
10220 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 }
10222 }
10223
10224 public void monitor() {
10225 synchronized (mWindowMap) { }
10226 synchronized (mKeyguardDisabled) { }
10227 synchronized (mKeyWaiter) { }
10228 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010229
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070010230 public void virtualKeyFeedback(KeyEvent event) {
10231 mPolicy.keyFeedbackFromInput(event);
10232 }
10233
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010234 /**
10235 * DimAnimator class that controls the dim animation. This holds the surface and
10236 * all state used for dim animation.
10237 */
10238 private static class DimAnimator {
10239 Surface mDimSurface;
10240 boolean mDimShown = false;
10241 float mDimCurrentAlpha;
10242 float mDimTargetAlpha;
10243 float mDimDeltaPerMs;
10244 long mLastDimAnimTime;
10245
10246 DimAnimator (SurfaceSession session) {
10247 if (mDimSurface == null) {
10248 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10249 + mDimSurface + ": CREATE");
10250 try {
10251 mDimSurface = new Surface(session, 0, -1, 16, 16, PixelFormat.OPAQUE,
10252 Surface.FX_SURFACE_DIM);
10253 } catch (Exception e) {
10254 Log.e(TAG, "Exception creating Dim surface", e);
10255 }
10256 }
10257 }
10258
10259 /**
10260 * Show the dim surface.
10261 */
10262 void show(int dw, int dh) {
10263 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
10264 dw + "x" + dh + ")");
10265 mDimShown = true;
10266 try {
10267 mDimSurface.setPosition(0, 0);
10268 mDimSurface.setSize(dw, dh);
10269 mDimSurface.show();
10270 } catch (RuntimeException e) {
10271 Log.w(TAG, "Failure showing dim surface", e);
10272 }
10273 }
10274
10275 /**
10276 * Set's the dim surface's layer and update dim parameters that will be used in
10277 * {@link updateSurface} after all windows are examined.
10278 */
10279 void updateParameters(WindowState w, long currentTime) {
10280 mDimSurface.setLayer(w.mAnimLayer-1);
10281
10282 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010283 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10284 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010285 if (mDimTargetAlpha != target) {
10286 // If the desired dim level has changed, then
10287 // start an animation to it.
10288 mLastDimAnimTime = currentTime;
10289 long duration = (w.mAnimating && w.mAnimation != null)
10290 ? w.mAnimation.computeDurationHint()
10291 : DEFAULT_DIM_DURATION;
10292 if (target > mDimTargetAlpha) {
10293 // This is happening behind the activity UI,
10294 // so we can make it run a little longer to
10295 // give a stronger impression without disrupting
10296 // the user.
10297 duration *= DIM_DURATION_MULTIPLIER;
10298 }
10299 if (duration < 1) {
10300 // Don't divide by zero
10301 duration = 1;
10302 }
10303 mDimTargetAlpha = target;
10304 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
10305 }
10306 }
10307
10308 /**
10309 * Updating the surface's alpha. Returns true if the animation continues, or returns
10310 * false when the animation is finished and the dim surface is hidden.
10311 */
10312 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
10313 if (!dimming) {
10314 if (mDimTargetAlpha != 0) {
10315 mLastDimAnimTime = currentTime;
10316 mDimTargetAlpha = 0;
10317 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
10318 }
10319 }
10320
10321 boolean animating = false;
10322 if (mLastDimAnimTime != 0) {
10323 mDimCurrentAlpha += mDimDeltaPerMs
10324 * (currentTime-mLastDimAnimTime);
10325 boolean more = true;
10326 if (displayFrozen) {
10327 // If the display is frozen, there is no reason to animate.
10328 more = false;
10329 } else if (mDimDeltaPerMs > 0) {
10330 if (mDimCurrentAlpha > mDimTargetAlpha) {
10331 more = false;
10332 }
10333 } else if (mDimDeltaPerMs < 0) {
10334 if (mDimCurrentAlpha < mDimTargetAlpha) {
10335 more = false;
10336 }
10337 } else {
10338 more = false;
10339 }
10340
10341 // Do we need to continue animating?
10342 if (more) {
10343 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10344 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
10345 mLastDimAnimTime = currentTime;
10346 mDimSurface.setAlpha(mDimCurrentAlpha);
10347 animating = true;
10348 } else {
10349 mDimCurrentAlpha = mDimTargetAlpha;
10350 mLastDimAnimTime = 0;
10351 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM "
10352 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
10353 mDimSurface.setAlpha(mDimCurrentAlpha);
10354 if (!dimming) {
10355 if (SHOW_TRANSACTIONS) Log.i(TAG, " DIM " + mDimSurface
10356 + ": HIDE");
10357 try {
10358 mDimSurface.hide();
10359 } catch (RuntimeException e) {
10360 Log.w(TAG, "Illegal argument exception hiding dim surface");
10361 }
10362 mDimShown = false;
10363 }
10364 }
10365 }
10366 return animating;
10367 }
10368
10369 public void printTo(PrintWriter pw) {
10370 pw.print(" mDimShown="); pw.print(mDimShown);
10371 pw.print(" current="); pw.print(mDimCurrentAlpha);
10372 pw.print(" target="); pw.print(mDimTargetAlpha);
10373 pw.print(" delta="); pw.print(mDimDeltaPerMs);
10374 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
10375 }
10376 }
10377
10378 /**
10379 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
10380 * This is used for opening/closing transition for apps in compatible mode.
10381 */
10382 private static class FadeInOutAnimation extends Animation {
10383 int mWidth;
10384 boolean mFadeIn;
10385
10386 public FadeInOutAnimation(boolean fadeIn) {
10387 setInterpolator(new AccelerateInterpolator());
10388 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
10389 mFadeIn = fadeIn;
10390 }
10391
10392 @Override
10393 protected void applyTransformation(float interpolatedTime, Transformation t) {
10394 float x = interpolatedTime;
10395 if (!mFadeIn) {
10396 x = 1.0f - x; // reverse the interpolation for fade out
10397 }
10398 if (x < 0.5) {
10399 // move the window out of the screen.
10400 t.getMatrix().setTranslate(mWidth, 0);
10401 } else {
10402 t.getMatrix().setTranslate(0, 0);// show
10403 t.setAlpha((x - 0.5f) * 2);
10404 }
10405 }
10406
10407 @Override
10408 public void initialize(int width, int height, int parentWidth, int parentHeight) {
10409 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
10410 mWidth = width;
10411 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010412
10413 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070010414 public int getZAdjustment() {
10415 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010416 }
10417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418}