blob: ba1efb9506da3fe57331d06e65ec830772e5f200 [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;
Dianne Hackborn75804932009-10-20 20:15:20 -070066import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Debug;
68import android.os.Handler;
69import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070070import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.LocalPowerManager;
72import android.os.Looper;
73import android.os.Message;
74import android.os.Parcel;
75import android.os.ParcelFileDescriptor;
76import android.os.Power;
77import android.os.PowerManager;
78import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
81import android.os.SystemClock;
82import android.os.SystemProperties;
83import android.os.TokenWatcher;
84import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070085import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080087import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.SparseIntArray;
89import android.view.Display;
90import android.view.Gravity;
91import android.view.IApplicationToken;
92import android.view.IOnKeyguardExitResult;
93import android.view.IRotationWatcher;
94import android.view.IWindow;
95import android.view.IWindowManager;
96import android.view.IWindowSession;
97import android.view.KeyEvent;
98import android.view.MotionEvent;
99import android.view.RawInputEvent;
100import android.view.Surface;
101import android.view.SurfaceSession;
102import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700103import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.ViewTreeObserver;
105import android.view.WindowManager;
106import android.view.WindowManagerImpl;
107import android.view.WindowManagerPolicy;
108import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700109import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.animation.Animation;
111import android.view.animation.AnimationUtils;
112import android.view.animation.Transformation;
113
114import java.io.BufferedWriter;
115import java.io.File;
116import java.io.FileDescriptor;
117import java.io.IOException;
118import java.io.OutputStream;
119import java.io.OutputStreamWriter;
120import java.io.PrintWriter;
121import java.io.StringWriter;
122import java.net.Socket;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128
129/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700130public class WindowManagerService extends IWindowManager.Stub
131 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final String TAG = "WindowManager";
133 static final boolean DEBUG = false;
134 static final boolean DEBUG_FOCUS = false;
135 static final boolean DEBUG_ANIM = false;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800136 static final boolean DEBUG_LAYOUT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final boolean DEBUG_LAYERS = false;
138 static final boolean DEBUG_INPUT = false;
139 static final boolean DEBUG_INPUT_METHOD = false;
140 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700141 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 static final boolean DEBUG_ORIENTATION = false;
143 static final boolean DEBUG_APP_TRANSITIONS = false;
144 static final boolean DEBUG_STARTING_WINDOW = false;
145 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700146 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700148 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700149 static final boolean MEASURE_LATENCY = false;
150 static private LatencyTimer lt;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean PROFILE_ORIENTATION = false;
153 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700154 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /** How long to wait for subsequent key repeats, in milliseconds */
157 static final int KEY_REPEAT_DELAY = 50;
158
159 /** How much to multiply the policy's type layer, to reserve room
160 * for multiple windows of the same type and Z-ordering adjustment
161 * with TYPE_LAYER_OFFSET. */
162 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
165 * or below others in the same layer. */
166 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** How much to increment the layer for each window, to reserve room
169 * for effect surfaces between them.
170 */
171 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** The maximum length we will accept for a loaded animation duration:
174 * this is 10 seconds.
175 */
176 static final int MAX_ANIMATION_DURATION = 10*1000;
177
178 /** Amount of time (in milliseconds) to animate the dim surface from one
179 * value to another, when no window animation is driving it.
180 */
181 static final int DEFAULT_DIM_DURATION = 200;
182
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700183 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
184 * compatible windows.
185 */
186 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /** Adjustment to time to perform a dim, to make it more dramatic.
189 */
190 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700191
Dianne Hackborncfaef692009-06-15 14:24:44 -0700192 static final int INJECT_FAILED = 0;
193 static final int INJECT_SUCCEEDED = 1;
194 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 static final int UPDATE_FOCUS_NORMAL = 0;
197 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
198 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
199 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700200
Michael Chane96440f2009-05-06 10:27:36 -0700201 /** The minimum time between dispatching touch events. */
202 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
203
204 // Last touch event time
205 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700206
Michael Chane96440f2009-05-06 10:27:36 -0700207 // Last touch event type
208 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700209
Michael Chane96440f2009-05-06 10:27:36 -0700210 // Time to wait before calling useractivity again. This saves CPU usage
211 // when we get a flood of touch events.
212 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
213
214 // Last time we call user activity
215 long mLastUserActivityCallTime = 0;
216
Romain Guy06882f82009-06-10 13:36:04 -0700217 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700218 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700221 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 /**
224 * Condition waited on by {@link #reenableKeyguard} to know the call to
225 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500226 * This is set to true only if mKeyguardTokenWatcher.acquired() has
227 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500229 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Mike Lockwood983ee092009-11-22 01:42:24 -0500231 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
232 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 public void acquired() {
234 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500235 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700238 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500239 synchronized (mKeyguardTokenWatcher) {
240 mKeyguardDisabled = false;
241 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 }
243 }
244 };
245
246 final Context mContext;
247
248 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
253
254 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 /**
259 * All currently active sessions with clients.
260 */
261 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Mapping from an IWindow IBinder to the server's Window object.
265 * This is also used as the lock for all of our state.
266 */
267 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
268
269 /**
270 * Mapping from a token IBinder to a WindowToken object.
271 */
272 final HashMap<IBinder, WindowToken> mTokenMap =
273 new HashMap<IBinder, WindowToken>();
274
275 /**
276 * The same tokens as mTokenMap, stored in a list for efficient iteration
277 * over them.
278 */
279 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * Window tokens that are in the process of exiting, but still
283 * on screen for animations.
284 */
285 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
286
287 /**
288 * Z-ordered (bottom-most first) list of all application tokens, for
289 * controlling the ordering of windows in different applications. This
290 * contains WindowToken objects.
291 */
292 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
293
294 /**
295 * Application tokens that are in the process of exiting, but still
296 * on screen for animations.
297 */
298 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
299
300 /**
301 * List of window tokens that have finished starting their application,
302 * and now need to have the policy remove their windows.
303 */
304 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
305
306 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700307 * This was the app token that was used to retrieve the last enter
308 * animation. It will be used for the next exit animation.
309 */
310 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700312 /**
313 * These were the layout params used to retrieve the last enter animation.
314 * They will be used for the next exit animation.
315 */
316 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * Z-ordered (bottom-most first) list of all Window objects.
320 */
321 final ArrayList mWindows = new ArrayList();
322
323 /**
324 * Windows that are being resized. Used so we can tell the client about
325 * the resize after closing the transaction in which we resized the
326 * underlying surface.
327 */
328 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
329
330 /**
331 * Windows whose animations have ended and now must be removed.
332 */
333 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
334
335 /**
336 * Windows whose surface should be destroyed.
337 */
338 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
339
340 /**
341 * Windows that have lost input focus and are waiting for the new
342 * focus window to be displayed before they are told about this.
343 */
344 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
345
346 /**
347 * This is set when we have run out of memory, and will either be an empty
348 * list or contain windows that need to be force removed.
349 */
350 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700355 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 Surface mBlurSurface;
357 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 final float[] mTmpFloats = new float[9];
362
363 boolean mSafeMode;
364 boolean mDisplayEnabled = false;
365 boolean mSystemBooted = false;
366 int mRotation = 0;
367 int mRequestedRotation = 0;
368 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700369 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 ArrayList<IRotationWatcher> mRotationWatchers
371 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 boolean mLayoutNeeded = true;
374 boolean mAnimationPending = false;
375 boolean mDisplayFrozen = false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800376 boolean mWaitingForConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 boolean mWindowsFreezingScreen = false;
378 long mFreezeGcPending = 0;
379 int mAppsFreezingScreen = 0;
380
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800381 int mLayoutSeq = 0;
382
383 Configuration mCurConfiguration = new Configuration();
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 // This is held as long as we have the screen frozen, to give us time to
386 // perform a rotation animation when turning off shows the lock screen which
387 // changes the orientation.
388 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 // State management of app transitions. When we are preparing for a
391 // transition, mNextAppTransition will be the kind of transition to
392 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
393 // mOpeningApps and mClosingApps are the lists of tokens that will be
394 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700395 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700396 String mNextAppTransitionPackage;
397 int mNextAppTransitionEnter;
398 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700400 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 boolean mAppTransitionTimeout = false;
402 boolean mStartingIconInTransition = false;
403 boolean mSkipAppTransitionAnimation = false;
404 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
405 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700406 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
407 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 //flag to detect fat touch events
410 boolean mFatTouch = false;
411 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 H mH = new H();
414
415 WindowState mCurrentFocus = null;
416 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 // This just indicates the window the input method is on top of, not
419 // necessarily the window its input is going to.
420 WindowState mInputMethodTarget = null;
421 WindowState mUpcomingInputMethodTarget = null;
422 boolean mInputMethodTargetWaitingAnim;
423 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 WindowState mInputMethodWindow = null;
426 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
427
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700428 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700430 // If non-null, this is the currently visible window that is associated
431 // with the wallpaper.
432 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700433 // If non-null, we are in the middle of animating from one wallpaper target
434 // to another, and this is the lower one in Z-order.
435 WindowState mLowerWallpaperTarget = null;
436 // If non-null, we are in the middle of animating from one wallpaper target
437 // to another, and this is the higher one in Z-order.
438 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700439 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700440 float mLastWallpaperX = -1;
441 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800442 float mLastWallpaperXStep = -1;
443 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800444 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700445 // This is set when we are waiting for a wallpaper to tell us it is done
446 // changing its scroll position.
447 WindowState mWaitingOnWallpaper;
448 // The last time we had a timeout when waiting for a wallpaper.
449 long mLastWallpaperTimeoutTime;
450 // We give a wallpaper up to 150ms to finish scrolling.
451 static final long WALLPAPER_TIMEOUT = 150;
452 // Time we wait after a timeout before trying to wait again.
453 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 AppWindowToken mFocusedApp = null;
456
457 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 float mWindowAnimationScale = 1.0f;
460 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final KeyWaiter mKeyWaiter = new KeyWaiter();
463 final KeyQ mQueue;
464 final InputDispatcherThread mInputThread;
465
466 // Who is holding the screen on.
467 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700468
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700469 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 /**
472 * Whether the UI is currently running in touch mode (not showing
473 * navigational focus because the user is directly pressing the screen).
474 */
475 boolean mInTouchMode = false;
476
477 private ViewServer mViewServer;
478
479 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700480
Dianne Hackbornc485a602009-03-24 22:39:49 -0700481 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700482 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700483
484 // The frame use to limit the size of the app running in compatibility mode.
485 Rect mCompatibleScreenFrame = new Rect();
486 // The surface used to fill the outer rim of the app running in compatibility mode.
487 Surface mBackgroundFillerSurface = null;
488 boolean mBackgroundFillerShown = false;
489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public static WindowManagerService main(Context context,
491 PowerManagerService pm, boolean haveInputMethods) {
492 WMThread thr = new WMThread(context, pm, haveInputMethods);
493 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 synchronized (thr) {
496 while (thr.mService == null) {
497 try {
498 thr.wait();
499 } catch (InterruptedException e) {
500 }
501 }
502 }
Romain Guy06882f82009-06-10 13:36:04 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 return thr.mService;
505 }
Romain Guy06882f82009-06-10 13:36:04 -0700506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 static class WMThread extends Thread {
508 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 private final Context mContext;
511 private final PowerManagerService mPM;
512 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public WMThread(Context context, PowerManagerService pm,
515 boolean haveInputMethods) {
516 super("WindowManager");
517 mContext = context;
518 mPM = pm;
519 mHaveInputMethods = haveInputMethods;
520 }
Romain Guy06882f82009-06-10 13:36:04 -0700521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public void run() {
523 Looper.prepare();
524 WindowManagerService s = new WindowManagerService(mContext, mPM,
525 mHaveInputMethods);
526 android.os.Process.setThreadPriority(
527 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 synchronized (this) {
530 mService = s;
531 notifyAll();
532 }
Romain Guy06882f82009-06-10 13:36:04 -0700533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 Looper.loop();
535 }
536 }
537
538 static class PolicyThread extends Thread {
539 private final WindowManagerPolicy mPolicy;
540 private final WindowManagerService mService;
541 private final Context mContext;
542 private final PowerManagerService mPM;
543 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 public PolicyThread(WindowManagerPolicy policy,
546 WindowManagerService service, Context context,
547 PowerManagerService pm) {
548 super("WindowManagerPolicy");
549 mPolicy = policy;
550 mService = service;
551 mContext = context;
552 mPM = pm;
553 }
Romain Guy06882f82009-06-10 13:36:04 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public void run() {
556 Looper.prepare();
557 //Looper.myLooper().setMessageLogging(new LogPrinter(
Joe Onorato8a9b2202010-02-26 18:56:32 -0800558 // Log.VERBOSE, "WindowManagerPolicy", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 android.os.Process.setThreadPriority(
560 android.os.Process.THREAD_PRIORITY_FOREGROUND);
561 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 synchronized (this) {
564 mRunning = true;
565 notifyAll();
566 }
Romain Guy06882f82009-06-10 13:36:04 -0700567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 Looper.loop();
569 }
570 }
571
572 private WindowManagerService(Context context, PowerManagerService pm,
573 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700574 if (MEASURE_LATENCY) {
575 lt = new LatencyTimer(100, 1000);
576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 mContext = context;
579 mHaveInputMethods = haveInputMethods;
580 mLimitedAlphaCompositing = context.getResources().getBoolean(
581 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 mPowerManager = pm;
584 mPowerManager.setPolicy(mPolicy);
585 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
586 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
587 "SCREEN_FROZEN");
588 mScreenFrozenLock.setReferenceCounted(false);
589
590 mActivityManager = ActivityManagerNative.getDefault();
591 mBatteryStats = BatteryStatsService.getService();
592
593 // Get persisted window scale setting
594 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
595 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
596 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
597 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700598
Michael Chan9f028e62009-08-04 17:37:46 -0700599 int max_events_per_sec = 35;
600 try {
601 max_events_per_sec = Integer.parseInt(SystemProperties
602 .get("windowsmgr.max_events_per_sec"));
603 if (max_events_per_sec < 1) {
604 max_events_per_sec = 35;
605 }
606 } catch (NumberFormatException e) {
607 }
608 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mQueue = new KeyQ();
611
612 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
615 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 synchronized (thr) {
618 while (!thr.mRunning) {
619 try {
620 thr.wait();
621 } catch (InterruptedException e) {
622 }
623 }
624 }
Romain Guy06882f82009-06-10 13:36:04 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 // Add ourself to the Watchdog monitors.
629 Watchdog.getInstance().addMonitor(this);
630 }
631
632 @Override
633 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
634 throws RemoteException {
635 try {
636 return super.onTransact(code, data, reply, flags);
637 } catch (RuntimeException e) {
638 // The window manager only throws security exceptions, so let's
639 // log all others.
640 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800641 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643 throw e;
644 }
645 }
646
647 private void placeWindowAfter(Object pos, WindowState window) {
648 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800649 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 TAG, "Adding window " + window + " at "
651 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
652 mWindows.add(i+1, window);
653 }
654
655 private void placeWindowBefore(Object pos, WindowState window) {
656 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800657 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 TAG, "Adding window " + window + " at "
659 + i + " of " + mWindows.size() + " (before " + pos + ")");
660 mWindows.add(i, window);
661 }
662
663 //This method finds out the index of a window that has the same app token as
664 //win. used for z ordering the windows in mWindows
665 private int findIdxBasedOnAppTokens(WindowState win) {
666 //use a local variable to cache mWindows
667 ArrayList localmWindows = mWindows;
668 int jmax = localmWindows.size();
669 if(jmax == 0) {
670 return -1;
671 }
672 for(int j = (jmax-1); j >= 0; j--) {
673 WindowState wentry = (WindowState)localmWindows.get(j);
674 if(wentry.mAppToken == win.mAppToken) {
675 return j;
676 }
677 }
678 return -1;
679 }
Romain Guy06882f82009-06-10 13:36:04 -0700680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
682 final IWindow client = win.mClient;
683 final WindowToken token = win.mToken;
684 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 final int N = localmWindows.size();
687 final WindowState attached = win.mAttachedWindow;
688 int i;
689 if (attached == null) {
690 int tokenWindowsPos = token.windows.size();
691 if (token.appWindowToken != null) {
692 int index = tokenWindowsPos-1;
693 if (index >= 0) {
694 // If this application has existing windows, we
695 // simply place the new window on top of them... but
696 // keep the starting window on top.
697 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
698 // Base windows go behind everything else.
699 placeWindowBefore(token.windows.get(0), win);
700 tokenWindowsPos = 0;
701 } else {
702 AppWindowToken atoken = win.mAppToken;
703 if (atoken != null &&
704 token.windows.get(index) == atoken.startingWindow) {
705 placeWindowBefore(token.windows.get(index), win);
706 tokenWindowsPos--;
707 } else {
708 int newIdx = findIdxBasedOnAppTokens(win);
709 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700710 //there is a window above this one associated with the same
711 //apptoken note that the window could be a floating window
712 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800714 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700715 TAG, "Adding window " + win + " at "
716 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 }
721 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 TAG, "Figuring out where to add app window "
724 + client.asBinder() + " (token=" + token + ")");
725 // Figure out where the window should go, based on the
726 // order of applications.
727 final int NA = mAppTokens.size();
728 Object pos = null;
729 for (i=NA-1; i>=0; i--) {
730 AppWindowToken t = mAppTokens.get(i);
731 if (t == token) {
732 i--;
733 break;
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
Dianne Hackborna8f60182009-09-01 19:01:50 -0700736 // We haven't reached the token yet; if this token
737 // is not going to the bottom and has windows, we can
738 // use it as an anchor for when we do reach the token.
739 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 pos = t.windows.get(0);
741 }
742 }
743 // We now know the index into the apps. If we found
744 // an app window above, that gives us the position; else
745 // we need to look some more.
746 if (pos != null) {
747 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700748 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 mTokenMap.get(((WindowState)pos).mClient.asBinder());
750 if (atoken != null) {
751 final int NC = atoken.windows.size();
752 if (NC > 0) {
753 WindowState bottom = atoken.windows.get(0);
754 if (bottom.mSubLayer < 0) {
755 pos = bottom;
756 }
757 }
758 }
759 placeWindowBefore(pos, win);
760 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700761 // Continue looking down until we find the first
762 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 while (i >= 0) {
764 AppWindowToken t = mAppTokens.get(i);
765 final int NW = t.windows.size();
766 if (NW > 0) {
767 pos = t.windows.get(NW-1);
768 break;
769 }
770 i--;
771 }
772 if (pos != null) {
773 // Move in front of any windows attached to this
774 // one.
775 WindowToken atoken =
776 mTokenMap.get(((WindowState)pos).mClient.asBinder());
777 if (atoken != null) {
778 final int NC = atoken.windows.size();
779 if (NC > 0) {
780 WindowState top = atoken.windows.get(NC-1);
781 if (top.mSubLayer >= 0) {
782 pos = top;
783 }
784 }
785 }
786 placeWindowAfter(pos, win);
787 } else {
788 // Just search for the start of this layer.
789 final int myLayer = win.mBaseLayer;
790 for (i=0; i<N; i++) {
791 WindowState w = (WindowState)localmWindows.get(i);
792 if (w.mBaseLayer > myLayer) {
793 break;
794 }
795 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800796 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700797 TAG, "Adding window " + win + " at "
798 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 localmWindows.add(i, win);
800 }
801 }
802 }
803 } else {
804 // Figure out where window should go, based on layer.
805 final int myLayer = win.mBaseLayer;
806 for (i=N-1; i>=0; i--) {
807 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
808 i++;
809 break;
810 }
811 }
812 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800813 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700814 TAG, "Adding window " + win + " at "
815 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 localmWindows.add(i, win);
817 }
818 if (addToToken) {
819 token.windows.add(tokenWindowsPos, win);
820 }
821
822 } else {
823 // Figure out this window's ordering relative to the window
824 // it is attached to.
825 final int NA = token.windows.size();
826 final int sublayer = win.mSubLayer;
827 int largestSublayer = Integer.MIN_VALUE;
828 WindowState windowWithLargestSublayer = null;
829 for (i=0; i<NA; i++) {
830 WindowState w = token.windows.get(i);
831 final int wSublayer = w.mSubLayer;
832 if (wSublayer >= largestSublayer) {
833 largestSublayer = wSublayer;
834 windowWithLargestSublayer = w;
835 }
836 if (sublayer < 0) {
837 // For negative sublayers, we go below all windows
838 // in the same sublayer.
839 if (wSublayer >= sublayer) {
840 if (addToToken) {
841 token.windows.add(i, win);
842 }
843 placeWindowBefore(
844 wSublayer >= 0 ? attached : w, win);
845 break;
846 }
847 } else {
848 // For positive sublayers, we go above all windows
849 // in the same sublayer.
850 if (wSublayer > sublayer) {
851 if (addToToken) {
852 token.windows.add(i, win);
853 }
854 placeWindowBefore(w, win);
855 break;
856 }
857 }
858 }
859 if (i >= NA) {
860 if (addToToken) {
861 token.windows.add(win);
862 }
863 if (sublayer < 0) {
864 placeWindowBefore(attached, win);
865 } else {
866 placeWindowAfter(largestSublayer >= 0
867 ? windowWithLargestSublayer
868 : attached,
869 win);
870 }
871 }
872 }
Romain Guy06882f82009-06-10 13:36:04 -0700873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 if (win.mAppToken != null && addToToken) {
875 win.mAppToken.allAppWindows.add(win);
876 }
877 }
Romain Guy06882f82009-06-10 13:36:04 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 static boolean canBeImeTarget(WindowState w) {
880 final int fl = w.mAttrs.flags
881 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
882 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
883 return w.isVisibleOrAdding();
884 }
885 return false;
886 }
Romain Guy06882f82009-06-10 13:36:04 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
889 final ArrayList localmWindows = mWindows;
890 final int N = localmWindows.size();
891 WindowState w = null;
892 int i = N;
893 while (i > 0) {
894 i--;
895 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700896
Joe Onorato8a9b2202010-02-26 18:56:32 -0800897 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // + Integer.toHexString(w.mAttrs.flags));
899 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800900 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Yet more tricksyness! If this window is a "starting"
903 // window, we do actually want to be on top of it, but
904 // it is not -really- where input will go. So if the caller
905 // is not actually looking to move the IME, look down below
906 // for a real window to target...
907 if (!willMove
908 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
909 && i > 0) {
910 WindowState wb = (WindowState)localmWindows.get(i-1);
911 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
912 i--;
913 w = wb;
914 }
915 }
916 break;
917 }
918 }
Romain Guy06882f82009-06-10 13:36:04 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700921
Joe Onorato8a9b2202010-02-26 18:56:32 -0800922 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 if (willMove && w != null) {
926 final WindowState curTarget = mInputMethodTarget;
927 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // Now some fun for dealing with window animations that
930 // modify the Z order. We need to look at all windows below
931 // the current target that are in this app, finding the highest
932 // visible one in layering.
933 AppWindowToken token = curTarget.mAppToken;
934 WindowState highestTarget = null;
935 int highestPos = 0;
936 if (token.animating || token.animation != null) {
937 int pos = 0;
938 pos = localmWindows.indexOf(curTarget);
939 while (pos >= 0) {
940 WindowState win = (WindowState)localmWindows.get(pos);
941 if (win.mAppToken != token) {
942 break;
943 }
944 if (!win.mRemoved) {
945 if (highestTarget == null || win.mAnimLayer >
946 highestTarget.mAnimLayer) {
947 highestTarget = win;
948 highestPos = pos;
949 }
950 }
951 pos--;
952 }
953 }
Romain Guy06882f82009-06-10 13:36:04 -0700954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800956 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 + mNextAppTransition + " " + highestTarget
958 + " animating=" + highestTarget.isAnimating()
959 + " layer=" + highestTarget.mAnimLayer
960 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700961
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700962 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 // If we are currently setting up for an animation,
964 // hold everything until we can find out what will happen.
965 mInputMethodTargetWaitingAnim = true;
966 mInputMethodTarget = highestTarget;
967 return highestPos + 1;
968 } else if (highestTarget.isAnimating() &&
969 highestTarget.mAnimLayer > w.mAnimLayer) {
970 // If the window we are currently targeting is involved
971 // with an animation, and it is on top of the next target
972 // we will be over, then hold off on moving until
973 // that is done.
974 mInputMethodTarget = highestTarget;
975 return highestPos + 1;
976 }
977 }
978 }
979 }
Romain Guy06882f82009-06-10 13:36:04 -0700980
Joe Onorato8a9b2202010-02-26 18:56:32 -0800981 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 if (w != null) {
983 if (willMove) {
984 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700985 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 if (DEBUG_INPUT_METHOD) Slog.w(TAG, "Moving IM target from "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 + mInputMethodTarget + " to " + w, e);
988 mInputMethodTarget = w;
989 if (w.mAppToken != null) {
990 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
991 } else {
992 setInputMethodAnimLayerAdjustment(0);
993 }
994 }
995 return i+1;
996 }
997 if (willMove) {
998 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700999 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001000 if (DEBUG_INPUT_METHOD) Slog.w(TAG, "Moving IM target from "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 + mInputMethodTarget + " to null", e);
1002 mInputMethodTarget = null;
1003 setInputMethodAnimLayerAdjustment(0);
1004 }
1005 return -1;
1006 }
Romain Guy06882f82009-06-10 13:36:04 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 void addInputMethodWindowToListLocked(WindowState win) {
1009 int pos = findDesiredInputMethodWindowIndexLocked(true);
1010 if (pos >= 0) {
1011 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001012 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001013 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 mWindows.add(pos, win);
1015 moveInputMethodDialogsLocked(pos+1);
1016 return;
1017 }
1018 win.mTargetAppToken = null;
1019 addWindowToListInOrderLocked(win, true);
1020 moveInputMethodDialogsLocked(pos);
1021 }
Romain Guy06882f82009-06-10 13:36:04 -07001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001024 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 mInputMethodAnimLayerAdjustment = adj;
1026 WindowState imw = mInputMethodWindow;
1027 if (imw != null) {
1028 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001029 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 + " anim layer: " + imw.mAnimLayer);
1031 int wi = imw.mChildWindows.size();
1032 while (wi > 0) {
1033 wi--;
1034 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1035 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001036 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 + " anim layer: " + cw.mAnimLayer);
1038 }
1039 }
1040 int di = mInputMethodDialogs.size();
1041 while (di > 0) {
1042 di --;
1043 imw = mInputMethodDialogs.get(di);
1044 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 + " anim layer: " + imw.mAnimLayer);
1047 }
1048 }
Romain Guy06882f82009-06-10 13:36:04 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1051 int wpos = mWindows.indexOf(win);
1052 if (wpos >= 0) {
1053 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001054 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 mWindows.remove(wpos);
1056 int NC = win.mChildWindows.size();
1057 while (NC > 0) {
1058 NC--;
1059 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1060 int cpos = mWindows.indexOf(cw);
1061 if (cpos >= 0) {
1062 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001064 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 mWindows.remove(cpos);
1066 }
1067 }
1068 }
1069 return interestingPos;
1070 }
Romain Guy06882f82009-06-10 13:36:04 -07001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 private void reAddWindowToListInOrderLocked(WindowState win) {
1073 addWindowToListInOrderLocked(win, false);
1074 // This is a hack to get all of the child windows added as well
1075 // at the right position. Child windows should be rare and
1076 // this case should be rare, so it shouldn't be that big a deal.
1077 int wpos = mWindows.indexOf(win);
1078 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001079 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001080 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 mWindows.remove(wpos);
1082 reAddWindowLocked(wpos, win);
1083 }
1084 }
Romain Guy06882f82009-06-10 13:36:04 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 void logWindowList(String prefix) {
1087 int N = mWindows.size();
1088 while (N > 0) {
1089 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Romain Guy06882f82009-06-10 13:36:04 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 void moveInputMethodDialogsLocked(int pos) {
1095 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001098 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 for (int i=0; i<N; i++) {
1100 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1101 }
1102 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001103 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 logWindowList(" ");
1105 }
Romain Guy06882f82009-06-10 13:36:04 -07001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (pos >= 0) {
1108 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1109 if (pos < mWindows.size()) {
1110 WindowState wp = (WindowState)mWindows.get(pos);
1111 if (wp == mInputMethodWindow) {
1112 pos++;
1113 }
1114 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 for (int i=0; i<N; i++) {
1117 WindowState win = dialogs.get(i);
1118 win.mTargetAppToken = targetAppToken;
1119 pos = reAddWindowLocked(pos, win);
1120 }
1121 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001122 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 logWindowList(" ");
1124 }
1125 return;
1126 }
1127 for (int i=0; i<N; i++) {
1128 WindowState win = dialogs.get(i);
1129 win.mTargetAppToken = null;
1130 reAddWindowToListInOrderLocked(win);
1131 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 logWindowList(" ");
1134 }
1135 }
1136 }
Romain Guy06882f82009-06-10 13:36:04 -07001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1139 final WindowState imWin = mInputMethodWindow;
1140 final int DN = mInputMethodDialogs.size();
1141 if (imWin == null && DN == 0) {
1142 return false;
1143 }
Romain Guy06882f82009-06-10 13:36:04 -07001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1146 if (imPos >= 0) {
1147 // In this case, the input method windows are to be placed
1148 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 // First check to see if the input method windows are already
1151 // located here, and contiguous.
1152 final int N = mWindows.size();
1153 WindowState firstImWin = imPos < N
1154 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 // Figure out the actual input method window that should be
1157 // at the bottom of their stack.
1158 WindowState baseImWin = imWin != null
1159 ? imWin : mInputMethodDialogs.get(0);
1160 if (baseImWin.mChildWindows.size() > 0) {
1161 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1162 if (cw.mSubLayer < 0) baseImWin = cw;
1163 }
Romain Guy06882f82009-06-10 13:36:04 -07001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 if (firstImWin == baseImWin) {
1166 // The windows haven't moved... but are they still contiguous?
1167 // First find the top IM window.
1168 int pos = imPos+1;
1169 while (pos < N) {
1170 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1171 break;
1172 }
1173 pos++;
1174 }
1175 pos++;
1176 // Now there should be no more input method windows above.
1177 while (pos < N) {
1178 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1179 break;
1180 }
1181 pos++;
1182 }
1183 if (pos >= N) {
1184 // All is good!
1185 return false;
1186 }
1187 }
Romain Guy06882f82009-06-10 13:36:04 -07001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 if (imWin != null) {
1190 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001191 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 logWindowList(" ");
1193 }
1194 imPos = tmpRemoveWindowLocked(imPos, imWin);
1195 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 logWindowList(" ");
1198 }
1199 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1200 reAddWindowLocked(imPos, imWin);
1201 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001202 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 logWindowList(" ");
1204 }
1205 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1206 } else {
1207 moveInputMethodDialogsLocked(imPos);
1208 }
Romain Guy06882f82009-06-10 13:36:04 -07001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 } else {
1211 // In this case, the input method windows go in a fixed layer,
1212 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 tmpRemoveWindowLocked(0, imWin);
1217 imWin.mTargetAppToken = null;
1218 reAddWindowToListInOrderLocked(imWin);
1219 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 logWindowList(" ");
1222 }
1223 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1224 } else {
1225 moveInputMethodDialogsLocked(-1);;
1226 }
Romain Guy06882f82009-06-10 13:36:04 -07001227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
Romain Guy06882f82009-06-10 13:36:04 -07001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 if (needAssignLayers) {
1231 assignLayersLocked();
1232 }
Romain Guy06882f82009-06-10 13:36:04 -07001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 return true;
1235 }
Romain Guy06882f82009-06-10 13:36:04 -07001236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 void adjustInputMethodDialogsLocked() {
1238 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1239 }
Romain Guy06882f82009-06-10 13:36:04 -07001240
Dianne Hackborn25994b42009-09-04 14:21:19 -07001241 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001243 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1244 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1245 ? wallpaperTarget.mAppToken.animation : null)
1246 + " upper=" + mUpperWallpaperTarget
1247 + " lower=" + mLowerWallpaperTarget);
1248 return (wallpaperTarget != null
1249 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1250 && wallpaperTarget.mAppToken.animation != null)))
1251 || mUpperWallpaperTarget != null
1252 || mLowerWallpaperTarget != null;
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001255 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1256 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001258 int adjustWallpaperWindowsLocked() {
1259 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001261 final int dw = mDisplay.getWidth();
1262 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001264 // First find top-most window that has asked to be on top of the
1265 // wallpaper; all wallpapers go behind it.
1266 final ArrayList localmWindows = mWindows;
1267 int N = localmWindows.size();
1268 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001269 WindowState foundW = null;
1270 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001271 WindowState topCurW = null;
1272 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001273 int i = N;
1274 while (i > 0) {
1275 i--;
1276 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001277 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1278 if (topCurW == null) {
1279 topCurW = w;
1280 topCurI = i;
1281 }
1282 continue;
1283 }
1284 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001285 if (w.mAppToken != null) {
1286 // If this window's app token is hidden and not animating,
1287 // it is of no interest to us.
1288 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001289 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001290 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001291 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001292 continue;
1293 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001294 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001296 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1297 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001298 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001299 && (mWallpaperTarget == w
1300 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001301 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001302 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001303 foundW = w;
1304 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001305 if (w == mWallpaperTarget && ((w.mAppToken != null
1306 && w.mAppToken.animation != null)
1307 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001308 // The current wallpaper target is animating, so we'll
1309 // look behind it for another possible target and figure
1310 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001312 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001313 continue;
1314 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001315 break;
1316 }
1317 }
1318
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001319 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001320 // If we are currently waiting for an app transition, and either
1321 // the current target or the next target are involved with it,
1322 // then hold off on doing anything with the wallpaper.
1323 // Note that we are checking here for just whether the target
1324 // is part of an app token... which is potentially overly aggressive
1325 // (the app token may not be involved in the transition), but good
1326 // enough (we'll just wait until whatever transition is pending
1327 // executes).
1328 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001329 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001330 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001331 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 }
1333 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001334 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001335 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001336 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001337 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001340 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001341 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001342 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001343 + " oldTarget: " + mWallpaperTarget);
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001346 mLowerWallpaperTarget = null;
1347 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001349 WindowState oldW = mWallpaperTarget;
1350 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001352 // Now what is happening... if the current and new targets are
1353 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001354 if (foundW != null && oldW != null) {
1355 boolean oldAnim = oldW.mAnimation != null
1356 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1357 boolean foundAnim = foundW.mAnimation != null
1358 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001359 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001361 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001362 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001363 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001364 int oldI = localmWindows.indexOf(oldW);
1365 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001366 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001367 }
1368 if (oldI >= 0) {
1369 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001371 + "=" + oldW + "; new#" + foundI
1372 + "=" + foundW);
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001375 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001376 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001377 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001379 }
1380 mWallpaperTarget = oldW;
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001383 // Now set the upper and lower wallpaper targets
1384 // correctly, and make sure that we are positioning
1385 // the wallpaper below the lower.
1386 if (foundI > oldI) {
1387 // The new target is on top of the old one.
1388 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001389 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001390 }
1391 mUpperWallpaperTarget = foundW;
1392 mLowerWallpaperTarget = oldW;
1393 foundW = oldW;
1394 foundI = oldI;
1395 } else {
1396 // The new target is below the old one.
1397 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001398 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001399 }
1400 mUpperWallpaperTarget = oldW;
1401 mLowerWallpaperTarget = foundW;
1402 }
1403 }
1404 }
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001407 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001408 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001409 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1410 || (mLowerWallpaperTarget.mAppToken != null
1411 && mLowerWallpaperTarget.mAppToken.animation != null);
1412 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1413 || (mUpperWallpaperTarget.mAppToken != null
1414 && mUpperWallpaperTarget.mAppToken.animation != null);
1415 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001416 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001418 }
1419 mLowerWallpaperTarget = null;
1420 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001421 }
1422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001424 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001425 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001426 // The window is visible to the compositor... but is it visible
1427 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001428 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001429 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001431 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001432 // its layer adjustment. Only do this if we are not transfering
1433 // between two wallpaper targets.
1434 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001435 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001436 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001438 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1439 * TYPE_LAYER_MULTIPLIER
1440 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001442 // Now w is the window we are supposed to be behind... but we
1443 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001444 // AND any starting window associated with it, AND below the
1445 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001446 while (foundI > 0) {
1447 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001448 if (wb.mBaseLayer < maxLayer &&
1449 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001450 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001451 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001452 // This window is not related to the previous one in any
1453 // interesting way, so stop here.
1454 break;
1455 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001456 foundW = wb;
1457 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001458 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001459 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001460 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001463 if (foundW == null && topCurW != null) {
1464 // There is no wallpaper target, so it goes at the bottom.
1465 // We will assume it is the same place as last time, if known.
1466 foundW = topCurW;
1467 foundI = topCurI+1;
1468 } else {
1469 // Okay i is the position immediately above the wallpaper. Look at
1470 // what is below it for later.
1471 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
Dianne Hackborn284ac932009-08-28 10:34:25 -07001474 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001475 if (mWallpaperTarget.mWallpaperX >= 0) {
1476 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001477 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001478 }
1479 if (mWallpaperTarget.mWallpaperY >= 0) {
1480 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001481 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001482 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001485 // Start stepping backwards from here, ensuring that our wallpaper windows
1486 // are correctly placed.
1487 int curTokenIndex = mWallpaperTokens.size();
1488 while (curTokenIndex > 0) {
1489 curTokenIndex--;
1490 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001491 if (token.hidden == visible) {
1492 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1493 token.hidden = !visible;
1494 // Need to do a layout to ensure the wallpaper now has the
1495 // correct size.
1496 mLayoutNeeded = true;
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001499 int curWallpaperIndex = token.windows.size();
1500 while (curWallpaperIndex > 0) {
1501 curWallpaperIndex--;
1502 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001504 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001505 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001508 // First, make sure the client has the current visibility
1509 // state.
1510 if (wallpaper.mWallpaperVisible != visible) {
1511 wallpaper.mWallpaperVisible = visible;
1512 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001513 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001514 "Setting visibility of wallpaper " + wallpaper
1515 + ": " + visible);
1516 wallpaper.mClient.dispatchAppVisibility(visible);
1517 } catch (RemoteException e) {
1518 }
1519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001521 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001522 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001523 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001524
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001525 // First, if this window is at the current index, then all
1526 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001527 if (wallpaper == foundW) {
1528 foundI--;
1529 foundW = foundI > 0
1530 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001531 continue;
1532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001533
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001534 // The window didn't match... the current wallpaper window,
1535 // wherever it is, is in the wrong place, so make sure it is
1536 // not in the list.
1537 int oldIndex = localmWindows.indexOf(wallpaper);
1538 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001539 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001540 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001541 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001542 if (oldIndex < foundI) {
1543 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001544 }
1545 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001547 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001548 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001549 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001550 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001552 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001553 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001554 }
1555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001556
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001557 return changed;
1558 }
1559
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001560 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001562 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001563 mWallpaperAnimLayerAdjustment = adj;
1564 int curTokenIndex = mWallpaperTokens.size();
1565 while (curTokenIndex > 0) {
1566 curTokenIndex--;
1567 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1568 int curWallpaperIndex = token.windows.size();
1569 while (curWallpaperIndex > 0) {
1570 curWallpaperIndex--;
1571 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1572 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001574 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001575 }
1576 }
1577 }
1578
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001579 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1580 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001581 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001582 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001583 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001584 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001585 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1586 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1587 changed = wallpaperWin.mXOffset != offset;
1588 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001590 + wallpaperWin + " x: " + offset);
1591 wallpaperWin.mXOffset = offset;
1592 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001593 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001594 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001595 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001596 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001599 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001600 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001601 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1602 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1603 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001604 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001605 + wallpaperWin + " y: " + offset);
1606 changed = true;
1607 wallpaperWin.mYOffset = offset;
1608 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001609 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001610 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001611 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001612 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001615 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001616 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001618 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1619 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001620 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001621 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001622 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001623 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001624 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1625 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001626 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001627 if (mWaitingOnWallpaper != null) {
1628 long start = SystemClock.uptimeMillis();
1629 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1630 < start) {
1631 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001632 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001633 "Waiting for offset complete...");
1634 mWindowMap.wait(WALLPAPER_TIMEOUT);
1635 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001636 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001637 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001638 if ((start+WALLPAPER_TIMEOUT)
1639 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001640 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001641 + wallpaperWin);
1642 mLastWallpaperTimeoutTime = start;
1643 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001644 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001645 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001646 }
1647 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001648 } catch (RemoteException e) {
1649 }
1650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001652 return changed;
1653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001654
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001655 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001656 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001657 if (mWaitingOnWallpaper != null &&
1658 mWaitingOnWallpaper.mClient.asBinder() == window) {
1659 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001660 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001661 }
1662 }
1663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001665 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001666 final int dw = mDisplay.getWidth();
1667 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001669 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001671 WindowState target = mWallpaperTarget;
1672 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001673 if (target.mWallpaperX >= 0) {
1674 mLastWallpaperX = target.mWallpaperX;
1675 } else if (changingTarget.mWallpaperX >= 0) {
1676 mLastWallpaperX = changingTarget.mWallpaperX;
1677 }
1678 if (target.mWallpaperY >= 0) {
1679 mLastWallpaperY = target.mWallpaperY;
1680 } else if (changingTarget.mWallpaperY >= 0) {
1681 mLastWallpaperY = changingTarget.mWallpaperY;
1682 }
1683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001684
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001685 int curTokenIndex = mWallpaperTokens.size();
1686 while (curTokenIndex > 0) {
1687 curTokenIndex--;
1688 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1689 int curWallpaperIndex = token.windows.size();
1690 while (curWallpaperIndex > 0) {
1691 curWallpaperIndex--;
1692 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1693 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1694 wallpaper.computeShownFrameLocked();
1695 changed = true;
1696 // We only want to be synchronous with one wallpaper.
1697 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001698 }
1699 }
1700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001702 return changed;
1703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001705 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001706 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001707 final int dw = mDisplay.getWidth();
1708 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001710 int curTokenIndex = mWallpaperTokens.size();
1711 while (curTokenIndex > 0) {
1712 curTokenIndex--;
1713 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001714 if (token.hidden == visible) {
1715 token.hidden = !visible;
1716 // Need to do a layout to ensure the wallpaper now has the
1717 // correct size.
1718 mLayoutNeeded = true;
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001721 int curWallpaperIndex = token.windows.size();
1722 while (curWallpaperIndex > 0) {
1723 curWallpaperIndex--;
1724 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1725 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001726 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001729 if (wallpaper.mWallpaperVisible != visible) {
1730 wallpaper.mWallpaperVisible = visible;
1731 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001732 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001733 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001734 + ": " + visible);
1735 wallpaper.mClient.dispatchAppVisibility(visible);
1736 } catch (RemoteException e) {
1737 }
1738 }
1739 }
1740 }
1741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001743 void sendPointerToWallpaperLocked(WindowState srcWin,
1744 MotionEvent pointer, long eventTime) {
1745 int curTokenIndex = mWallpaperTokens.size();
1746 while (curTokenIndex > 0) {
1747 curTokenIndex--;
1748 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1749 int curWallpaperIndex = token.windows.size();
1750 while (curWallpaperIndex > 0) {
1751 curWallpaperIndex--;
1752 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1753 if ((wallpaper.mAttrs.flags &
1754 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1755 continue;
1756 }
1757 try {
1758 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001759 if (srcWin != null) {
1760 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1761 srcWin.mFrame.top-wallpaper.mFrame.top);
1762 } else {
1763 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1764 }
1765 switch (pointer.getAction()) {
1766 case MotionEvent.ACTION_DOWN:
1767 mSendingPointersToWallpaper = true;
1768 break;
1769 case MotionEvent.ACTION_UP:
1770 mSendingPointersToWallpaper = false;
1771 break;
1772 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001773 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1774 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001775 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001776 }
1777 }
1778 }
1779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001780
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001781 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1782 MotionEvent pointer, long eventTime, boolean skipped) {
1783 if (relWin != null) {
1784 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1785 } else {
1786 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1787 }
1788
1789 // If we sent an initial down to the wallpaper, then continue
1790 // sending events until the final up.
1791 if (mSendingPointersToWallpaper) {
1792 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001793 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001794 }
1795 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1796
1797 // If we are on top of the wallpaper, then the wallpaper also
1798 // gets to see this movement.
1799 } else if (srcWin != null
1800 && pointer.getAction() == MotionEvent.ACTION_DOWN
1801 && mWallpaperTarget == srcWin
1802 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1803 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1804 }
1805 }
1806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 public int addWindow(Session session, IWindow client,
1808 WindowManager.LayoutParams attrs, int viewVisibility,
1809 Rect outContentInsets) {
1810 int res = mPolicy.checkAddPermission(attrs);
1811 if (res != WindowManagerImpl.ADD_OKAY) {
1812 return res;
1813 }
Romain Guy06882f82009-06-10 13:36:04 -07001814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 boolean reportNewConfig = false;
1816 WindowState attachedWindow = null;
1817 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 synchronized(mWindowMap) {
1820 // Instantiating a Display requires talking with the simulator,
1821 // so don't do it until we know the system is mostly up and
1822 // running.
1823 if (mDisplay == null) {
1824 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1825 mDisplay = wm.getDefaultDisplay();
1826 mQueue.setDisplay(mDisplay);
1827 reportNewConfig = true;
1828 }
Romain Guy06882f82009-06-10 13:36:04 -07001829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001831 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1833 }
1834
1835 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001836 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001838 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 + attrs.token + ". Aborting.");
1840 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1841 }
1842 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1843 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001844 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 + attrs.token + ". Aborting.");
1846 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1847 }
1848 }
1849
1850 boolean addToken = false;
1851 WindowToken token = mTokenMap.get(attrs.token);
1852 if (token == null) {
1853 if (attrs.type >= FIRST_APPLICATION_WINDOW
1854 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001855 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 + attrs.token + ". Aborting.");
1857 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1858 }
1859 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001860 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 + attrs.token + ". Aborting.");
1862 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1863 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001864 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001865 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001866 + attrs.token + ". Aborting.");
1867 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 token = new WindowToken(attrs.token, -1, false);
1870 addToken = true;
1871 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1872 && attrs.type <= LAST_APPLICATION_WINDOW) {
1873 AppWindowToken atoken = token.appWindowToken;
1874 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001875 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 + token + ". Aborting.");
1877 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1878 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 + token + ". Aborting.");
1881 return WindowManagerImpl.ADD_APP_EXITING;
1882 }
1883 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1884 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001885 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1887 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1888 }
1889 } else if (attrs.type == TYPE_INPUT_METHOD) {
1890 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001891 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 + attrs.token + ". Aborting.");
1893 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1894 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001895 } else if (attrs.type == TYPE_WALLPAPER) {
1896 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001897 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001898 + attrs.token + ". Aborting.");
1899 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 }
1902
1903 win = new WindowState(session, client, token,
1904 attachedWindow, attrs, viewVisibility);
1905 if (win.mDeathRecipient == null) {
1906 // Client has apparently died, so there is no reason to
1907 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001908 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 + " that is dead, aborting.");
1910 return WindowManagerImpl.ADD_APP_EXITING;
1911 }
1912
1913 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 res = mPolicy.prepareAddWindowLw(win, attrs);
1916 if (res != WindowManagerImpl.ADD_OKAY) {
1917 return res;
1918 }
1919
1920 // From now on, no exceptions or errors allowed!
1921
1922 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (addToken) {
1927 mTokenMap.put(attrs.token, token);
1928 mTokenList.add(token);
1929 }
1930 win.attach();
1931 mWindowMap.put(client.asBinder(), win);
1932
1933 if (attrs.type == TYPE_APPLICATION_STARTING &&
1934 token.appWindowToken != null) {
1935 token.appWindowToken.startingWindow = win;
1936 }
1937
1938 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (attrs.type == TYPE_INPUT_METHOD) {
1941 mInputMethodWindow = win;
1942 addInputMethodWindowToListLocked(win);
1943 imMayMove = false;
1944 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1945 mInputMethodDialogs.add(win);
1946 addWindowToListInOrderLocked(win, true);
1947 adjustInputMethodDialogsLocked();
1948 imMayMove = false;
1949 } else {
1950 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001951 if (attrs.type == TYPE_WALLPAPER) {
1952 mLastWallpaperTimeoutTime = 0;
1953 adjustWallpaperWindowsLocked();
1954 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001955 adjustWallpaperWindowsLocked();
1956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
Romain Guy06882f82009-06-10 13:36:04 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 if (mInTouchMode) {
1964 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1965 }
1966 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1967 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1968 }
Romain Guy06882f82009-06-10 13:36:04 -07001969
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001970 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001972 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1973 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 imMayMove = false;
1975 }
1976 }
Romain Guy06882f82009-06-10 13:36:04 -07001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001979 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
Romain Guy06882f82009-06-10 13:36:04 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 assignLayersLocked();
1983 // Don't do layout here, the window must call
1984 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07001985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 //dump();
1987
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001988 if (focusChanged) {
1989 if (mCurrentFocus != null) {
1990 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
1991 }
1992 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001993 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 TAG, "New client " + client.asBinder()
1995 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001996
1997 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
1998 reportNewConfig = true;
1999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
2002 // sendNewConfiguration() checks caller permissions so we must call it with
2003 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2004 // identity anyway, so it's safe to just clear & restore around this whole
2005 // block.
2006 final long origId = Binder.clearCallingIdentity();
2007 if (reportNewConfig) {
2008 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
2010 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 return res;
2013 }
Romain Guy06882f82009-06-10 13:36:04 -07002014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 public void removeWindow(Session session, IWindow client) {
2016 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002017 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 if (win == null) {
2019 return;
2020 }
2021 removeWindowLocked(session, win);
2022 }
2023 }
Romain Guy06882f82009-06-10 13:36:04 -07002024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 public void removeWindowLocked(Session session, WindowState win) {
2026
Joe Onorato8a9b2202010-02-26 18:56:32 -08002027 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 TAG, "Remove " + win + " client="
2029 + Integer.toHexString(System.identityHashCode(
2030 win.mClient.asBinder()))
2031 + ", surface=" + win.mSurface);
2032
2033 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002034
Joe Onorato8a9b2202010-02-26 18:56:32 -08002035 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2037 + " mExiting=" + win.mExiting
2038 + " isAnimating=" + win.isAnimating()
2039 + " app-animation="
2040 + (win.mAppToken != null ? win.mAppToken.animation : null)
2041 + " inPendingTransaction="
2042 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2043 + " mDisplayFrozen=" + mDisplayFrozen);
2044 // Visibility of the removed window. Will be used later to update orientation later on.
2045 boolean wasVisible = false;
2046 // First, see if we need to run an animation. If we do, we have
2047 // to hold off on removing the window until the animation is done.
2048 // If the display is frozen, just remove immediately, since the
2049 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002050 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 // If we are not currently running the exit animation, we
2052 // need to see about starting one.
2053 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2056 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2057 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2058 }
2059 // Try starting an animation.
2060 if (applyAnimationLocked(win, transit, false)) {
2061 win.mExiting = true;
2062 }
2063 }
2064 if (win.mExiting || win.isAnimating()) {
2065 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002066 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 win.mExiting = true;
2068 win.mRemoveOnExit = true;
2069 mLayoutNeeded = true;
2070 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2071 performLayoutAndPlaceSurfacesLocked();
2072 if (win.mAppToken != null) {
2073 win.mAppToken.updateReportedVisibilityLocked();
2074 }
2075 //dump();
2076 Binder.restoreCallingIdentity(origId);
2077 return;
2078 }
2079 }
2080
2081 removeWindowInnerLocked(session, win);
2082 // Removing a visible window will effect the computed orientation
2083 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002084 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002085 != mForcedAppOrientation
2086 && updateOrientationFromAppTokensLocked()) {
2087 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2090 Binder.restoreCallingIdentity(origId);
2091 }
Romain Guy06882f82009-06-10 13:36:04 -07002092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002094 mKeyWaiter.finishedKey(session, win.mClient, true,
2095 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2097 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 if (mInputMethodTarget == win) {
2102 moveInputMethodWindowsIfNeededLocked(false);
2103 }
Romain Guy06882f82009-06-10 13:36:04 -07002104
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002105 if (false) {
2106 RuntimeException e = new RuntimeException("here");
2107 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002108 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 mPolicy.removeWindowLw(win);
2112 win.removeLocked();
2113
2114 mWindowMap.remove(win.mClient.asBinder());
2115 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002116 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117
2118 if (mInputMethodWindow == win) {
2119 mInputMethodWindow = null;
2120 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2121 mInputMethodDialogs.remove(win);
2122 }
Romain Guy06882f82009-06-10 13:36:04 -07002123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 final WindowToken token = win.mToken;
2125 final AppWindowToken atoken = win.mAppToken;
2126 token.windows.remove(win);
2127 if (atoken != null) {
2128 atoken.allAppWindows.remove(win);
2129 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002130 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 TAG, "**** Removing window " + win + ": count="
2132 + token.windows.size());
2133 if (token.windows.size() == 0) {
2134 if (!token.explicit) {
2135 mTokenMap.remove(token.token);
2136 mTokenList.remove(token);
2137 } else if (atoken != null) {
2138 atoken.firstWindowDrawn = false;
2139 }
2140 }
2141
2142 if (atoken != null) {
2143 if (atoken.startingWindow == win) {
2144 atoken.startingWindow = null;
2145 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2146 // If this is the last window and we had requested a starting
2147 // transition window, well there is no point now.
2148 atoken.startingData = null;
2149 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2150 // If this is the last window except for a starting transition
2151 // window, we need to get rid of the starting transition.
2152 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002153 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 + ": no more real windows");
2155 }
2156 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2157 mH.sendMessage(m);
2158 }
2159 }
Romain Guy06882f82009-06-10 13:36:04 -07002160
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002161 if (win.mAttrs.type == TYPE_WALLPAPER) {
2162 mLastWallpaperTimeoutTime = 0;
2163 adjustWallpaperWindowsLocked();
2164 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002165 adjustWallpaperWindowsLocked();
2166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 if (!mInLayout) {
2169 assignLayersLocked();
2170 mLayoutNeeded = true;
2171 performLayoutAndPlaceSurfacesLocked();
2172 if (win.mAppToken != null) {
2173 win.mAppToken.updateReportedVisibilityLocked();
2174 }
2175 }
2176 }
2177
2178 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2179 long origId = Binder.clearCallingIdentity();
2180 try {
2181 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002182 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002184 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 Surface.openTransaction();
2186 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002187 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002188 TAG, " SURFACE " + w.mSurface
2189 + ": transparentRegionHint=" + region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 w.mSurface.setTransparentRegionHint(region);
2191 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002192 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 Surface.closeTransaction();
2194 }
2195 }
2196 }
2197 } finally {
2198 Binder.restoreCallingIdentity(origId);
2199 }
2200 }
2201
2202 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002203 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 Rect visibleInsets) {
2205 long origId = Binder.clearCallingIdentity();
2206 try {
2207 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002208 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 if (w != null) {
2210 w.mGivenInsetsPending = false;
2211 w.mGivenContentInsets.set(contentInsets);
2212 w.mGivenVisibleInsets.set(visibleInsets);
2213 w.mTouchableInsets = touchableInsets;
2214 mLayoutNeeded = true;
2215 performLayoutAndPlaceSurfacesLocked();
2216 }
2217 }
2218 } finally {
2219 Binder.restoreCallingIdentity(origId);
2220 }
2221 }
Romain Guy06882f82009-06-10 13:36:04 -07002222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 public void getWindowDisplayFrame(Session session, IWindow client,
2224 Rect outDisplayFrame) {
2225 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002226 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 if (win == null) {
2228 outDisplayFrame.setEmpty();
2229 return;
2230 }
2231 outDisplayFrame.set(win.mDisplayFrame);
2232 }
2233 }
2234
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002235 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2236 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002237 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2238 window.mWallpaperX = x;
2239 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002240 window.mWallpaperXStep = xStep;
2241 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002242 if (updateWallpaperOffsetLocked(window, true)) {
2243 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002244 }
2245 }
2246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002247
Dianne Hackborn75804932009-10-20 20:15:20 -07002248 void wallpaperCommandComplete(IBinder window, Bundle result) {
2249 synchronized (mWindowMap) {
2250 if (mWaitingOnWallpaper != null &&
2251 mWaitingOnWallpaper.mClient.asBinder() == window) {
2252 mWaitingOnWallpaper = null;
2253 mWindowMap.notifyAll();
2254 }
2255 }
2256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
Dianne Hackborn75804932009-10-20 20:15:20 -07002258 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2259 String action, int x, int y, int z, Bundle extras, boolean sync) {
2260 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2261 || window == mUpperWallpaperTarget) {
2262 boolean doWait = sync;
2263 int curTokenIndex = mWallpaperTokens.size();
2264 while (curTokenIndex > 0) {
2265 curTokenIndex--;
2266 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2267 int curWallpaperIndex = token.windows.size();
2268 while (curWallpaperIndex > 0) {
2269 curWallpaperIndex--;
2270 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2271 try {
2272 wallpaper.mClient.dispatchWallpaperCommand(action,
2273 x, y, z, extras, sync);
2274 // We only want to be synchronous with one wallpaper.
2275 sync = false;
2276 } catch (RemoteException e) {
2277 }
2278 }
2279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002280
Dianne Hackborn75804932009-10-20 20:15:20 -07002281 if (doWait) {
2282 // XXX Need to wait for result.
2283 }
2284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
Dianne Hackborn75804932009-10-20 20:15:20 -07002286 return null;
2287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 public int relayoutWindow(Session session, IWindow client,
2290 WindowManager.LayoutParams attrs, int requestedWidth,
2291 int requestedHeight, int viewVisibility, boolean insetsPending,
2292 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
2293 Surface outSurface) {
2294 boolean displayed = false;
2295 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002296 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002300 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 if (win == null) {
2302 return 0;
2303 }
2304 win.mRequestedWidth = requestedWidth;
2305 win.mRequestedHeight = requestedHeight;
2306
2307 if (attrs != null) {
2308 mPolicy.adjustWindowParamsLw(attrs);
2309 }
Romain Guy06882f82009-06-10 13:36:04 -07002310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 int attrChanges = 0;
2312 int flagChanges = 0;
2313 if (attrs != null) {
2314 flagChanges = win.mAttrs.flags ^= attrs.flags;
2315 attrChanges = win.mAttrs.copyFrom(attrs);
2316 }
2317
Joe Onorato8a9b2202010-02-26 18:56:32 -08002318 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319
2320 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2321 win.mAlpha = attrs.alpha;
2322 }
2323
2324 final boolean scaledWindow =
2325 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2326
2327 if (scaledWindow) {
2328 // requested{Width|Height} Surface's physical size
2329 // attrs.{width|height} Size on screen
2330 win.mHScale = (attrs.width != requestedWidth) ?
2331 (attrs.width / (float)requestedWidth) : 1.0f;
2332 win.mVScale = (attrs.height != requestedHeight) ?
2333 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002334 } else {
2335 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337
2338 boolean imMayMove = (flagChanges&(
2339 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2340 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 boolean focusMayChange = win.mViewVisibility != viewVisibility
2343 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2344 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002345
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002346 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2347 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 win.mRelayoutCalled = true;
2350 final int oldVisibility = win.mViewVisibility;
2351 win.mViewVisibility = viewVisibility;
2352 if (viewVisibility == View.VISIBLE &&
2353 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2354 displayed = !win.isVisibleLw();
2355 if (win.mExiting) {
2356 win.mExiting = false;
2357 win.mAnimation = null;
2358 }
2359 if (win.mDestroying) {
2360 win.mDestroying = false;
2361 mDestroySurface.remove(win);
2362 }
2363 if (oldVisibility == View.GONE) {
2364 win.mEnterAnimationPending = true;
2365 }
2366 if (displayed && win.mSurface != null && !win.mDrawPending
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002367 && !win.mCommitDrawPending && !mDisplayFrozen
2368 && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 applyEnterAnimationLocked(win);
2370 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002371 if (displayed && (win.mAttrs.flags
2372 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2373 win.mTurnOnScreen = true;
2374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2376 // To change the format, we need to re-build the surface.
2377 win.destroySurfaceLocked();
2378 displayed = true;
2379 }
2380 try {
2381 Surface surface = win.createSurfaceLocked();
2382 if (surface != null) {
2383 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002384 win.mReportDestroySurface = false;
2385 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002386 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002387 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002389 // For some reason there isn't a surface. Clear the
2390 // caller's object so they see the same state.
2391 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002394 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002395 + client + " (" + win.mAttrs.getTitle() + ")",
2396 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 Binder.restoreCallingIdentity(origId);
2398 return 0;
2399 }
2400 if (displayed) {
2401 focusMayChange = true;
2402 }
2403 if (win.mAttrs.type == TYPE_INPUT_METHOD
2404 && mInputMethodWindow == null) {
2405 mInputMethodWindow = win;
2406 imMayMove = true;
2407 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002408 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2409 && win.mAppToken != null
2410 && win.mAppToken.startingWindow != null) {
2411 // Special handling of starting window over the base
2412 // window of the app: propagate lock screen flags to it,
2413 // to provide the correct semantics while starting.
2414 final int mask =
2415 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002416 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2417 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002418 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2419 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 } else {
2422 win.mEnterAnimationPending = false;
2423 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002424 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002425 + ": mExiting=" + win.mExiting
2426 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 // If we are not currently running the exit animation, we
2428 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002429 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 // Try starting an animation; if there isn't one, we
2431 // can destroy the surface right away.
2432 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2433 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2434 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2435 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002436 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002438 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 win.mExiting = true;
2440 mKeyWaiter.finishedKey(session, client, true,
2441 KeyWaiter.RETURN_NOTHING);
2442 } else if (win.isAnimating()) {
2443 // Currently in a hide animation... turn this into
2444 // an exit.
2445 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002446 } else if (win == mWallpaperTarget) {
2447 // If the wallpaper is currently behind this
2448 // window, we need to change both of them inside
2449 // of a transaction to avoid artifacts.
2450 win.mExiting = true;
2451 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 } else {
2453 if (mInputMethodWindow == win) {
2454 mInputMethodWindow = null;
2455 }
2456 win.destroySurfaceLocked();
2457 }
2458 }
2459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002460
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002461 if (win.mSurface == null || (win.getAttrs().flags
2462 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2463 || win.mSurfacePendingDestroy) {
2464 // We are being called from a local process, which
2465 // means outSurface holds its current surface. Ensure the
2466 // surface object is cleared, but we don't want it actually
2467 // destroyed at this point.
2468 win.mSurfacePendingDestroy = false;
2469 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002470 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002471 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002472 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002473 "Keeping surface, will report destroy: " + win);
2474 win.mReportDestroySurface = true;
2475 outSurface.copyFrom(win.mSurface);
2476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 }
2478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 if (focusMayChange) {
2480 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2481 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 imMayMove = false;
2483 }
2484 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2485 }
Romain Guy06882f82009-06-10 13:36:04 -07002486
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002487 // updateFocusedWindowLocked() already assigned layers so we only need to
2488 // reassign them at this point if the IM window state gets shuffled
2489 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002492 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2493 // Little hack here -- we -should- be able to rely on the
2494 // function to return true if the IME has moved and needs
2495 // its layer recomputed. However, if the IME was hidden
2496 // and isn't actually moved in the list, its layer may be
2497 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 assignLayers = true;
2499 }
2500 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002501 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002502 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002503 assignLayers = true;
2504 }
2505 }
Romain Guy06882f82009-06-10 13:36:04 -07002506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 mLayoutNeeded = true;
2508 win.mGivenInsetsPending = insetsPending;
2509 if (assignLayers) {
2510 assignLayersLocked();
2511 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002512 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002514 if (displayed && win.mIsWallpaper) {
2515 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002516 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 if (win.mAppToken != null) {
2519 win.mAppToken.updateReportedVisibilityLocked();
2520 }
2521 outFrame.set(win.mFrame);
2522 outContentInsets.set(win.mContentInsets);
2523 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002524 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002526 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 + ", requestedHeight=" + requestedHeight
2528 + ", viewVisibility=" + viewVisibility
2529 + "\nRelayout returning frame=" + outFrame
2530 + ", surface=" + outSurface);
2531
Joe Onorato8a9b2202010-02-26 18:56:32 -08002532 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2534
2535 inTouchMode = mInTouchMode;
2536 }
2537
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002538 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 sendNewConfiguration();
2540 }
Romain Guy06882f82009-06-10 13:36:04 -07002541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2545 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2546 }
2547
2548 public void finishDrawingWindow(Session session, IWindow client) {
2549 final long origId = Binder.clearCallingIdentity();
2550 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002551 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002553 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2554 adjustWallpaperWindowsLocked();
2555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 mLayoutNeeded = true;
2557 performLayoutAndPlaceSurfacesLocked();
2558 }
2559 }
2560 Binder.restoreCallingIdentity(origId);
2561 }
2562
2563 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002564 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 + (lp != null ? lp.packageName : null)
2566 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2567 if (lp != null && lp.windowAnimations != 0) {
2568 // If this is a system resource, don't try to load it from the
2569 // application resources. It is nice to avoid loading application
2570 // resources if we can.
2571 String packageName = lp.packageName != null ? lp.packageName : "android";
2572 int resId = lp.windowAnimations;
2573 if ((resId&0xFF000000) == 0x01000000) {
2574 packageName = "android";
2575 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002576 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 + packageName);
2578 return AttributeCache.instance().get(packageName, resId,
2579 com.android.internal.R.styleable.WindowAnimation);
2580 }
2581 return null;
2582 }
Romain Guy06882f82009-06-10 13:36:04 -07002583
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002584 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002585 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002586 + packageName + " resId=0x" + Integer.toHexString(resId));
2587 if (packageName != null) {
2588 if ((resId&0xFF000000) == 0x01000000) {
2589 packageName = "android";
2590 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002591 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002592 + packageName);
2593 return AttributeCache.instance().get(packageName, resId,
2594 com.android.internal.R.styleable.WindowAnimation);
2595 }
2596 return null;
2597 }
2598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 private void applyEnterAnimationLocked(WindowState win) {
2600 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2601 if (win.mEnterAnimationPending) {
2602 win.mEnterAnimationPending = false;
2603 transit = WindowManagerPolicy.TRANSIT_ENTER;
2604 }
2605
2606 applyAnimationLocked(win, transit, true);
2607 }
2608
2609 private boolean applyAnimationLocked(WindowState win,
2610 int transit, boolean isEntrance) {
2611 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2612 // If we are trying to apply an animation, but already running
2613 // an animation of the same type, then just leave that one alone.
2614 return true;
2615 }
Romain Guy06882f82009-06-10 13:36:04 -07002616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 // Only apply an animation if the display isn't frozen. If it is
2618 // frozen, there is no reason to animate and it can cause strange
2619 // artifacts when we unfreeze the display if some different animation
2620 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002621 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 int anim = mPolicy.selectAnimationLw(win, transit);
2623 int attr = -1;
2624 Animation a = null;
2625 if (anim != 0) {
2626 a = AnimationUtils.loadAnimation(mContext, anim);
2627 } else {
2628 switch (transit) {
2629 case WindowManagerPolicy.TRANSIT_ENTER:
2630 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2631 break;
2632 case WindowManagerPolicy.TRANSIT_EXIT:
2633 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2634 break;
2635 case WindowManagerPolicy.TRANSIT_SHOW:
2636 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2637 break;
2638 case WindowManagerPolicy.TRANSIT_HIDE:
2639 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2640 break;
2641 }
2642 if (attr >= 0) {
2643 a = loadAnimation(win.mAttrs, attr);
2644 }
2645 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002646 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2648 + " mAnimation=" + win.mAnimation
2649 + " isEntrance=" + isEntrance);
2650 if (a != null) {
2651 if (DEBUG_ANIM) {
2652 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002653 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002654 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
2656 win.setAnimation(a);
2657 win.mAnimationIsEntrance = isEntrance;
2658 }
2659 } else {
2660 win.clearAnimation();
2661 }
2662
2663 return win.mAnimation != null;
2664 }
2665
2666 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2667 int anim = 0;
2668 Context context = mContext;
2669 if (animAttr >= 0) {
2670 AttributeCache.Entry ent = getCachedAnimations(lp);
2671 if (ent != null) {
2672 context = ent.context;
2673 anim = ent.array.getResourceId(animAttr, 0);
2674 }
2675 }
2676 if (anim != 0) {
2677 return AnimationUtils.loadAnimation(context, anim);
2678 }
2679 return null;
2680 }
Romain Guy06882f82009-06-10 13:36:04 -07002681
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002682 private Animation loadAnimation(String packageName, int resId) {
2683 int anim = 0;
2684 Context context = mContext;
2685 if (resId >= 0) {
2686 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2687 if (ent != null) {
2688 context = ent.context;
2689 anim = resId;
2690 }
2691 }
2692 if (anim != 0) {
2693 return AnimationUtils.loadAnimation(context, anim);
2694 }
2695 return null;
2696 }
2697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 private boolean applyAnimationLocked(AppWindowToken wtoken,
2699 WindowManager.LayoutParams lp, int transit, boolean enter) {
2700 // Only apply an animation if the display isn't frozen. If it is
2701 // frozen, there is no reason to animate and it can cause strange
2702 // artifacts when we unfreeze the display if some different animation
2703 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002704 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002705 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002706 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002707 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002708 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002709 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002710 } else if (mNextAppTransitionPackage != null) {
2711 a = loadAnimation(mNextAppTransitionPackage, enter ?
2712 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002713 } else {
2714 int animAttr = 0;
2715 switch (transit) {
2716 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2717 animAttr = enter
2718 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2719 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2720 break;
2721 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2722 animAttr = enter
2723 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2724 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2725 break;
2726 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2727 animAttr = enter
2728 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2729 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2730 break;
2731 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2732 animAttr = enter
2733 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2734 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2735 break;
2736 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2737 animAttr = enter
2738 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2739 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2740 break;
2741 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2742 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002743 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002744 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2745 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002746 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002747 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002748 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2749 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002750 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002751 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002752 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002753 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2754 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2755 break;
2756 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2757 animAttr = enter
2758 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2759 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2760 break;
2761 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2762 animAttr = enter
2763 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2764 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002765 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002766 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002767 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002768 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002769 + " anim=" + a
2770 + " animAttr=0x" + Integer.toHexString(animAttr)
2771 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (a != null) {
2774 if (DEBUG_ANIM) {
2775 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07002776 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002777 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 }
2779 wtoken.setAnimation(a);
2780 }
2781 } else {
2782 wtoken.clearAnimation();
2783 }
2784
2785 return wtoken.animation != null;
2786 }
2787
2788 // -------------------------------------------------------------
2789 // Application Window Tokens
2790 // -------------------------------------------------------------
2791
2792 public void validateAppTokens(List tokens) {
2793 int v = tokens.size()-1;
2794 int m = mAppTokens.size()-1;
2795 while (v >= 0 && m >= 0) {
2796 AppWindowToken wtoken = mAppTokens.get(m);
2797 if (wtoken.removed) {
2798 m--;
2799 continue;
2800 }
2801 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002802 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2804 }
2805 v--;
2806 m--;
2807 }
2808 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002809 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 v--;
2811 }
2812 while (m >= 0) {
2813 AppWindowToken wtoken = mAppTokens.get(m);
2814 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002815 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
2817 m--;
2818 }
2819 }
2820
2821 boolean checkCallingPermission(String permission, String func) {
2822 // Quick check: if the calling permission is me, it's all okay.
2823 if (Binder.getCallingPid() == Process.myPid()) {
2824 return true;
2825 }
Romain Guy06882f82009-06-10 13:36:04 -07002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 if (mContext.checkCallingPermission(permission)
2828 == PackageManager.PERMISSION_GRANTED) {
2829 return true;
2830 }
2831 String msg = "Permission Denial: " + func + " from pid="
2832 + Binder.getCallingPid()
2833 + ", uid=" + Binder.getCallingUid()
2834 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002835 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 return false;
2837 }
Romain Guy06882f82009-06-10 13:36:04 -07002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 AppWindowToken findAppWindowToken(IBinder token) {
2840 WindowToken wtoken = mTokenMap.get(token);
2841 if (wtoken == null) {
2842 return null;
2843 }
2844 return wtoken.appWindowToken;
2845 }
Romain Guy06882f82009-06-10 13:36:04 -07002846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 public void addWindowToken(IBinder token, int type) {
2848 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2849 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002850 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 }
Romain Guy06882f82009-06-10 13:36:04 -07002852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 synchronized(mWindowMap) {
2854 WindowToken wtoken = mTokenMap.get(token);
2855 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002856 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 return;
2858 }
2859 wtoken = new WindowToken(token, type, true);
2860 mTokenMap.put(token, wtoken);
2861 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002862 if (type == TYPE_WALLPAPER) {
2863 mWallpaperTokens.add(wtoken);
2864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
2866 }
Romain Guy06882f82009-06-10 13:36:04 -07002867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 public void removeWindowToken(IBinder token) {
2869 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2870 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002871 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
2873
2874 final long origId = Binder.clearCallingIdentity();
2875 synchronized(mWindowMap) {
2876 WindowToken wtoken = mTokenMap.remove(token);
2877 mTokenList.remove(wtoken);
2878 if (wtoken != null) {
2879 boolean delayed = false;
2880 if (!wtoken.hidden) {
2881 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 final int N = wtoken.windows.size();
2884 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 for (int i=0; i<N; i++) {
2887 WindowState win = wtoken.windows.get(i);
2888
2889 if (win.isAnimating()) {
2890 delayed = true;
2891 }
Romain Guy06882f82009-06-10 13:36:04 -07002892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 if (win.isVisibleNow()) {
2894 applyAnimationLocked(win,
2895 WindowManagerPolicy.TRANSIT_EXIT, false);
2896 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2897 KeyWaiter.RETURN_NOTHING);
2898 changed = true;
2899 }
2900 }
2901
2902 if (changed) {
2903 mLayoutNeeded = true;
2904 performLayoutAndPlaceSurfacesLocked();
2905 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2906 }
Romain Guy06882f82009-06-10 13:36:04 -07002907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 if (delayed) {
2909 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002910 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2911 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 }
2913 }
Romain Guy06882f82009-06-10 13:36:04 -07002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002916 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
2918 }
2919 Binder.restoreCallingIdentity(origId);
2920 }
2921
2922 public void addAppToken(int addPos, IApplicationToken token,
2923 int groupId, int requestedOrientation, boolean fullscreen) {
2924 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2925 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002926 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
Romain Guy06882f82009-06-10 13:36:04 -07002928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 synchronized(mWindowMap) {
2930 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2931 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002932 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 return;
2934 }
2935 wtoken = new AppWindowToken(token);
2936 wtoken.groupId = groupId;
2937 wtoken.appFullscreen = fullscreen;
2938 wtoken.requestedOrientation = requestedOrientation;
2939 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002940 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 mTokenMap.put(token.asBinder(), wtoken);
2942 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 // Application tokens start out hidden.
2945 wtoken.hidden = true;
2946 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 //dump();
2949 }
2950 }
Romain Guy06882f82009-06-10 13:36:04 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 public void setAppGroupId(IBinder token, int groupId) {
2953 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2954 "setAppStartingIcon()")) {
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 }
2957
2958 synchronized(mWindowMap) {
2959 AppWindowToken wtoken = findAppWindowToken(token);
2960 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002961 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 return;
2963 }
2964 wtoken.groupId = groupId;
2965 }
2966 }
Romain Guy06882f82009-06-10 13:36:04 -07002967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 public int getOrientationFromWindowsLocked() {
2969 int pos = mWindows.size() - 1;
2970 while (pos >= 0) {
2971 WindowState wtoken = (WindowState) mWindows.get(pos);
2972 pos--;
2973 if (wtoken.mAppToken != null) {
2974 // We hit an application window. so the orientation will be determined by the
2975 // app window. No point in continuing further.
2976 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2977 }
2978 if (!wtoken.isVisibleLw()) {
2979 continue;
2980 }
2981 int req = wtoken.mAttrs.screenOrientation;
2982 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
2983 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
2984 continue;
2985 } else {
2986 return req;
2987 }
2988 }
2989 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2990 }
Romain Guy06882f82009-06-10 13:36:04 -07002991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002993 int pos = mAppTokens.size() - 1;
2994 int curGroup = 0;
2995 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2996 boolean findingBehind = false;
2997 boolean haveGroup = false;
2998 boolean lastFullscreen = false;
2999 while (pos >= 0) {
3000 AppWindowToken wtoken = mAppTokens.get(pos);
3001 pos--;
3002 // if we're about to tear down this window and not seek for
3003 // the behind activity, don't use it for orientation
3004 if (!findingBehind
3005 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3006 continue;
3007 }
3008
3009 if (!haveGroup) {
3010 // We ignore any hidden applications on the top.
3011 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003012 continue;
3013 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003014 haveGroup = true;
3015 curGroup = wtoken.groupId;
3016 lastOrientation = wtoken.requestedOrientation;
3017 } else if (curGroup != wtoken.groupId) {
3018 // If we have hit a new application group, and the bottom
3019 // of the previous group didn't explicitly say to use
3020 // the orientation behind it, and the last app was
3021 // full screen, then we'll stick with the
3022 // user's orientation.
3023 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3024 && lastFullscreen) {
3025 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003028 int or = wtoken.requestedOrientation;
3029 // If this application is fullscreen, and didn't explicitly say
3030 // to use the orientation behind it, then just take whatever
3031 // orientation it has and ignores whatever is under it.
3032 lastFullscreen = wtoken.appFullscreen;
3033 if (lastFullscreen
3034 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3035 return or;
3036 }
3037 // If this application has requested an explicit orientation,
3038 // then use it.
3039 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3040 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3041 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3042 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3043 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3044 return or;
3045 }
3046 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3047 }
3048 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 }
Romain Guy06882f82009-06-10 13:36:04 -07003050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003052 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003053 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3054 "updateOrientationFromAppTokens()")) {
3055 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003057
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003058 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003060
3061 synchronized(mWindowMap) {
3062 if (updateOrientationFromAppTokensLocked()) {
3063 if (freezeThisOneIfNeeded != null) {
3064 AppWindowToken wtoken = findAppWindowToken(
3065 freezeThisOneIfNeeded);
3066 if (wtoken != null) {
3067 startAppFreezingScreenLocked(wtoken,
3068 ActivityInfo.CONFIG_ORIENTATION);
3069 }
3070 }
3071 config = computeNewConfigurationLocked();
3072
3073 } else if (currentConfig != null) {
3074 // No obvious action we need to take, but if our current
3075 // state mismatches the activity maanager's, update it
3076 mTempConfiguration.setToDefaults();
3077 if (computeNewConfigurationLocked(mTempConfiguration)) {
3078 if (currentConfig.diff(mTempConfiguration) != 0) {
3079 mWaitingForConfig = true;
3080 mLayoutNeeded = true;
3081 startFreezingDisplayLocked();
3082 config = new Configuration(mTempConfiguration);
3083 }
3084 }
3085 }
3086 }
3087
Dianne Hackborncfaef692009-06-15 14:24:44 -07003088 Binder.restoreCallingIdentity(ident);
3089 return config;
3090 }
3091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003093 * Determine the new desired orientation of the display, returning
3094 * a non-null new Configuration if it has changed from the current
3095 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3096 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3097 * SCREEN. This will typically be done for you if you call
3098 * sendNewConfiguration().
3099 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 * The orientation is computed from non-application windows first. If none of
3101 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003102 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3104 * android.os.IBinder)
3105 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003106 boolean updateOrientationFromAppTokensLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 long ident = Binder.clearCallingIdentity();
3109 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003110 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 mForcedAppOrientation = req;
3114 //send a message to Policy indicating orientation change to take
3115 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003116 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003117 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3118 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3119 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
3121 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003122
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003123 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 } finally {
3125 Binder.restoreCallingIdentity(ident);
3126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 }
Romain Guy06882f82009-06-10 13:36:04 -07003128
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003129 int computeForcedAppOrientationLocked() {
3130 int req = getOrientationFromWindowsLocked();
3131 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3132 req = getOrientationFromAppTokensLocked();
3133 }
3134 return req;
3135 }
Romain Guy06882f82009-06-10 13:36:04 -07003136
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003137 public void setNewConfiguration(Configuration config) {
3138 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3139 "setNewConfiguration()")) {
3140 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3141 }
3142
3143 synchronized(mWindowMap) {
3144 mCurConfiguration = new Configuration(config);
3145 mWaitingForConfig = false;
3146 performLayoutAndPlaceSurfacesLocked();
3147 }
3148 }
3149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3151 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3152 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003153 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
Romain Guy06882f82009-06-10 13:36:04 -07003155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 synchronized(mWindowMap) {
3157 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3158 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003159 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 return;
3161 }
Romain Guy06882f82009-06-10 13:36:04 -07003162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 wtoken.requestedOrientation = requestedOrientation;
3164 }
3165 }
Romain Guy06882f82009-06-10 13:36:04 -07003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 public int getAppOrientation(IApplicationToken token) {
3168 synchronized(mWindowMap) {
3169 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3170 if (wtoken == null) {
3171 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3172 }
Romain Guy06882f82009-06-10 13:36:04 -07003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 return wtoken.requestedOrientation;
3175 }
3176 }
Romain Guy06882f82009-06-10 13:36:04 -07003177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3179 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3180 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003181 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
3183
3184 synchronized(mWindowMap) {
3185 boolean changed = false;
3186 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003187 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 changed = mFocusedApp != null;
3189 mFocusedApp = null;
3190 mKeyWaiter.tickle();
3191 } else {
3192 AppWindowToken newFocus = findAppWindowToken(token);
3193 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003194 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 return;
3196 }
3197 changed = mFocusedApp != newFocus;
3198 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003199 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 mKeyWaiter.tickle();
3201 }
3202
3203 if (moveFocusNow && changed) {
3204 final long origId = Binder.clearCallingIdentity();
3205 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3206 Binder.restoreCallingIdentity(origId);
3207 }
3208 }
3209 }
3210
3211 public void prepareAppTransition(int transit) {
3212 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3213 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003214 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003218 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 TAG, "Prepare app transition: transit=" + transit
3220 + " mNextAppTransition=" + mNextAppTransition);
3221 if (!mDisplayFrozen) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003222 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3223 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003225 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3226 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3227 // Opening a new task always supersedes a close for the anim.
3228 mNextAppTransition = transit;
3229 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3230 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3231 // Opening a new activity always supersedes a close for the anim.
3232 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234 mAppTransitionReady = false;
3235 mAppTransitionTimeout = false;
3236 mStartingIconInTransition = false;
3237 mSkipAppTransitionAnimation = false;
3238 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3239 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3240 5000);
3241 }
3242 }
3243 }
3244
3245 public int getPendingAppTransition() {
3246 return mNextAppTransition;
3247 }
Romain Guy06882f82009-06-10 13:36:04 -07003248
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003249 public void overridePendingAppTransition(String packageName,
3250 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003251 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003252 mNextAppTransitionPackage = packageName;
3253 mNextAppTransitionEnter = enterAnim;
3254 mNextAppTransitionExit = exitAnim;
3255 }
3256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 public void executeAppTransition() {
3259 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3260 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003261 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 }
Romain Guy06882f82009-06-10 13:36:04 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003265 if (DEBUG_APP_TRANSITIONS) {
3266 RuntimeException e = new RuntimeException("here");
3267 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003268 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003269 + mNextAppTransition, e);
3270 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003271 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 mAppTransitionReady = true;
3273 final long origId = Binder.clearCallingIdentity();
3274 performLayoutAndPlaceSurfacesLocked();
3275 Binder.restoreCallingIdentity(origId);
3276 }
3277 }
3278 }
3279
3280 public void setAppStartingWindow(IBinder token, String pkg,
3281 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3282 IBinder transferFrom, boolean createIfNeeded) {
3283 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3284 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003285 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 }
3287
3288 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003289 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3291 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 AppWindowToken wtoken = findAppWindowToken(token);
3294 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003295 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 return;
3297 }
3298
3299 // If the display is frozen, we won't do anything until the
3300 // actual window is displayed so there is no reason to put in
3301 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003302 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 return;
3304 }
Romain Guy06882f82009-06-10 13:36:04 -07003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 if (wtoken.startingData != null) {
3307 return;
3308 }
Romain Guy06882f82009-06-10 13:36:04 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 if (transferFrom != null) {
3311 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3312 if (ttoken != null) {
3313 WindowState startingWindow = ttoken.startingWindow;
3314 if (startingWindow != null) {
3315 if (mStartingIconInTransition) {
3316 // In this case, the starting icon has already
3317 // been displayed, so start letting windows get
3318 // shown immediately without any more transitions.
3319 mSkipAppTransitionAnimation = true;
3320 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003321 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 "Moving existing starting from " + ttoken
3323 + " to " + wtoken);
3324 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 // Transfer the starting window over to the new
3327 // token.
3328 wtoken.startingData = ttoken.startingData;
3329 wtoken.startingView = ttoken.startingView;
3330 wtoken.startingWindow = startingWindow;
3331 ttoken.startingData = null;
3332 ttoken.startingView = null;
3333 ttoken.startingWindow = null;
3334 ttoken.startingMoved = true;
3335 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003336 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003338 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003339 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 mWindows.remove(startingWindow);
3341 ttoken.windows.remove(startingWindow);
3342 ttoken.allAppWindows.remove(startingWindow);
3343 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 // Propagate other interesting state between the
3346 // tokens. If the old token is displayed, we should
3347 // immediately force the new one to be displayed. If
3348 // it is animating, we need to move that animation to
3349 // the new one.
3350 if (ttoken.allDrawn) {
3351 wtoken.allDrawn = true;
3352 }
3353 if (ttoken.firstWindowDrawn) {
3354 wtoken.firstWindowDrawn = true;
3355 }
3356 if (!ttoken.hidden) {
3357 wtoken.hidden = false;
3358 wtoken.hiddenRequested = false;
3359 wtoken.willBeHidden = false;
3360 }
3361 if (wtoken.clientHidden != ttoken.clientHidden) {
3362 wtoken.clientHidden = ttoken.clientHidden;
3363 wtoken.sendAppVisibilityToClients();
3364 }
3365 if (ttoken.animation != null) {
3366 wtoken.animation = ttoken.animation;
3367 wtoken.animating = ttoken.animating;
3368 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3369 ttoken.animation = null;
3370 ttoken.animLayerAdjustment = 0;
3371 wtoken.updateLayers();
3372 ttoken.updateLayers();
3373 }
Romain Guy06882f82009-06-10 13:36:04 -07003374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 mLayoutNeeded = true;
3377 performLayoutAndPlaceSurfacesLocked();
3378 Binder.restoreCallingIdentity(origId);
3379 return;
3380 } else if (ttoken.startingData != null) {
3381 // The previous app was getting ready to show a
3382 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003383 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 "Moving pending starting from " + ttoken
3385 + " to " + wtoken);
3386 wtoken.startingData = ttoken.startingData;
3387 ttoken.startingData = null;
3388 ttoken.startingMoved = true;
3389 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3390 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3391 // want to process the message ASAP, before any other queued
3392 // messages.
3393 mH.sendMessageAtFrontOfQueue(m);
3394 return;
3395 }
3396 }
3397 }
3398
3399 // There is no existing starting window, and the caller doesn't
3400 // want us to create one, so that's it!
3401 if (!createIfNeeded) {
3402 return;
3403 }
Romain Guy06882f82009-06-10 13:36:04 -07003404
Dianne Hackborn284ac932009-08-28 10:34:25 -07003405 // If this is a translucent or wallpaper window, then don't
3406 // show a starting window -- the current effect (a full-screen
3407 // opaque starting window that fades away to the real contents
3408 // when it is ready) does not work for this.
3409 if (theme != 0) {
3410 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3411 com.android.internal.R.styleable.Window);
3412 if (ent.array.getBoolean(
3413 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3414 return;
3415 }
3416 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003417 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3418 return;
3419 }
3420 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003421 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3422 return;
3423 }
3424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 mStartingIconInTransition = true;
3427 wtoken.startingData = new StartingData(
3428 pkg, theme, nonLocalizedLabel,
3429 labelRes, icon);
3430 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3431 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3432 // want to process the message ASAP, before any other queued
3433 // messages.
3434 mH.sendMessageAtFrontOfQueue(m);
3435 }
3436 }
3437
3438 public void setAppWillBeHidden(IBinder token) {
3439 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3440 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003441 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
3443
3444 AppWindowToken wtoken;
3445
3446 synchronized(mWindowMap) {
3447 wtoken = findAppWindowToken(token);
3448 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003449 Slog.w(TAG, "Attempted to set will be hidden of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 return;
3451 }
3452 wtoken.willBeHidden = true;
3453 }
3454 }
Romain Guy06882f82009-06-10 13:36:04 -07003455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3457 boolean visible, int transit, boolean performLayout) {
3458 boolean delayed = false;
3459
3460 if (wtoken.clientHidden == visible) {
3461 wtoken.clientHidden = !visible;
3462 wtoken.sendAppVisibilityToClients();
3463 }
Romain Guy06882f82009-06-10 13:36:04 -07003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 wtoken.willBeHidden = false;
3466 if (wtoken.hidden == visible) {
3467 final int N = wtoken.allAppWindows.size();
3468 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003469 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3471 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003474
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003475 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 if (wtoken.animation == sDummyAnimation) {
3477 wtoken.animation = null;
3478 }
3479 applyAnimationLocked(wtoken, lp, transit, visible);
3480 changed = true;
3481 if (wtoken.animation != null) {
3482 delayed = runningAppAnimation = true;
3483 }
3484 }
Romain Guy06882f82009-06-10 13:36:04 -07003485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 for (int i=0; i<N; i++) {
3487 WindowState win = wtoken.allAppWindows.get(i);
3488 if (win == wtoken.startingWindow) {
3489 continue;
3490 }
3491
3492 if (win.isAnimating()) {
3493 delayed = true;
3494 }
Romain Guy06882f82009-06-10 13:36:04 -07003495
Joe Onorato8a9b2202010-02-26 18:56:32 -08003496 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 //win.dump(" ");
3498 if (visible) {
3499 if (!win.isVisibleNow()) {
3500 if (!runningAppAnimation) {
3501 applyAnimationLocked(win,
3502 WindowManagerPolicy.TRANSIT_ENTER, true);
3503 }
3504 changed = true;
3505 }
3506 } else if (win.isVisibleNow()) {
3507 if (!runningAppAnimation) {
3508 applyAnimationLocked(win,
3509 WindowManagerPolicy.TRANSIT_EXIT, false);
3510 }
3511 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3512 KeyWaiter.RETURN_NOTHING);
3513 changed = true;
3514 }
3515 }
3516
3517 wtoken.hidden = wtoken.hiddenRequested = !visible;
3518 if (!visible) {
3519 unsetAppFreezingScreenLocked(wtoken, true, true);
3520 } else {
3521 // If we are being set visible, and the starting window is
3522 // not yet displayed, then make sure it doesn't get displayed.
3523 WindowState swin = wtoken.startingWindow;
3524 if (swin != null && (swin.mDrawPending
3525 || swin.mCommitDrawPending)) {
3526 swin.mPolicyVisibility = false;
3527 swin.mPolicyVisibilityAfterAnim = false;
3528 }
3529 }
Romain Guy06882f82009-06-10 13:36:04 -07003530
Joe Onorato8a9b2202010-02-26 18:56:32 -08003531 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3533 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003534
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003535 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003537 if (performLayout) {
3538 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3539 performLayoutAndPlaceSurfacesLocked();
3540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 }
3542 }
3543
3544 if (wtoken.animation != null) {
3545 delayed = true;
3546 }
Romain Guy06882f82009-06-10 13:36:04 -07003547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 return delayed;
3549 }
3550
3551 public void setAppVisibility(IBinder token, boolean visible) {
3552 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3553 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003554 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 }
3556
3557 AppWindowToken wtoken;
3558
3559 synchronized(mWindowMap) {
3560 wtoken = findAppWindowToken(token);
3561 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003562 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 return;
3564 }
3565
3566 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
3567 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003568 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003569 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 + "): mNextAppTransition=" + mNextAppTransition
3571 + " hidden=" + wtoken.hidden
3572 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3573 }
Romain Guy06882f82009-06-10 13:36:04 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 // If we are preparing an app transition, then delay changing
3576 // the visibility of this token until we execute that transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003577 if (!mDisplayFrozen && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 // Already in requested state, don't do anything more.
3579 if (wtoken.hiddenRequested != visible) {
3580 return;
3581 }
3582 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003583
Joe Onorato8a9b2202010-02-26 18:56:32 -08003584 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 TAG, "Setting dummy animation on: " + wtoken);
3586 wtoken.setDummyAnimation();
3587 mOpeningApps.remove(wtoken);
3588 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003589 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 wtoken.inPendingTransaction = true;
3591 if (visible) {
3592 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 wtoken.startingDisplayed = false;
3594 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003595
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003596 // If the token is currently hidden (should be the
3597 // common case), then we need to set up to wait for
3598 // its windows to be ready.
3599 if (wtoken.hidden) {
3600 wtoken.allDrawn = false;
3601 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003602
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003603 if (wtoken.clientHidden) {
3604 // In the case where we are making an app visible
3605 // but holding off for a transition, we still need
3606 // to tell the client to make its windows visible so
3607 // they get drawn. Otherwise, we will wait on
3608 // performing the transition until all windows have
3609 // been drawn, they never will be, and we are sad.
3610 wtoken.clientHidden = false;
3611 wtoken.sendAppVisibilityToClients();
3612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614 } else {
3615 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003616
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003617 // If the token is currently visible (should be the
3618 // common case), then set up to wait for it to be hidden.
3619 if (!wtoken.hidden) {
3620 wtoken.waitingToHide = true;
3621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 }
3623 return;
3624 }
Romain Guy06882f82009-06-10 13:36:04 -07003625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003627 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 wtoken.updateReportedVisibilityLocked();
3629 Binder.restoreCallingIdentity(origId);
3630 }
3631 }
3632
3633 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3634 boolean unfreezeSurfaceNow, boolean force) {
3635 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003636 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 + " force=" + force);
3638 final int N = wtoken.allAppWindows.size();
3639 boolean unfrozeWindows = false;
3640 for (int i=0; i<N; i++) {
3641 WindowState w = wtoken.allAppWindows.get(i);
3642 if (w.mAppFreezing) {
3643 w.mAppFreezing = false;
3644 if (w.mSurface != null && !w.mOrientationChanging) {
3645 w.mOrientationChanging = true;
3646 }
3647 unfrozeWindows = true;
3648 }
3649 }
3650 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003651 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 wtoken.freezingScreen = false;
3653 mAppsFreezingScreen--;
3654 }
3655 if (unfreezeSurfaceNow) {
3656 if (unfrozeWindows) {
3657 mLayoutNeeded = true;
3658 performLayoutAndPlaceSurfacesLocked();
3659 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003660 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
3662 }
3663 }
Romain Guy06882f82009-06-10 13:36:04 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3666 int configChanges) {
3667 if (DEBUG_ORIENTATION) {
3668 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003669 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003670 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 + ": hidden=" + wtoken.hidden + " freezing="
3672 + wtoken.freezingScreen, e);
3673 }
3674 if (!wtoken.hiddenRequested) {
3675 if (!wtoken.freezingScreen) {
3676 wtoken.freezingScreen = true;
3677 mAppsFreezingScreen++;
3678 if (mAppsFreezingScreen == 1) {
3679 startFreezingDisplayLocked();
3680 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3681 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3682 5000);
3683 }
3684 }
3685 final int N = wtoken.allAppWindows.size();
3686 for (int i=0; i<N; i++) {
3687 WindowState w = wtoken.allAppWindows.get(i);
3688 w.mAppFreezing = true;
3689 }
3690 }
3691 }
Romain Guy06882f82009-06-10 13:36:04 -07003692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 public void startAppFreezingScreen(IBinder token, int configChanges) {
3694 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3695 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003696 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 }
3698
3699 synchronized(mWindowMap) {
3700 if (configChanges == 0 && !mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003701 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 return;
3703 }
Romain Guy06882f82009-06-10 13:36:04 -07003704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 AppWindowToken wtoken = findAppWindowToken(token);
3706 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003707 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 return;
3709 }
3710 final long origId = Binder.clearCallingIdentity();
3711 startAppFreezingScreenLocked(wtoken, configChanges);
3712 Binder.restoreCallingIdentity(origId);
3713 }
3714 }
Romain Guy06882f82009-06-10 13:36:04 -07003715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 public void stopAppFreezingScreen(IBinder token, boolean force) {
3717 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3718 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003719 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
3721
3722 synchronized(mWindowMap) {
3723 AppWindowToken wtoken = findAppWindowToken(token);
3724 if (wtoken == null || wtoken.appToken == null) {
3725 return;
3726 }
3727 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003728 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3730 unsetAppFreezingScreenLocked(wtoken, true, force);
3731 Binder.restoreCallingIdentity(origId);
3732 }
3733 }
Romain Guy06882f82009-06-10 13:36:04 -07003734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 public void removeAppToken(IBinder token) {
3736 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3737 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003738 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
3740
3741 AppWindowToken wtoken = null;
3742 AppWindowToken startingToken = null;
3743 boolean delayed = false;
3744
3745 final long origId = Binder.clearCallingIdentity();
3746 synchronized(mWindowMap) {
3747 WindowToken basewtoken = mTokenMap.remove(token);
3748 mTokenList.remove(basewtoken);
3749 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003750 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003751 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 wtoken.inPendingTransaction = false;
3753 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003754 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 if (mClosingApps.contains(wtoken)) {
3756 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003757 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003759 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 delayed = true;
3761 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003762 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 TAG, "Removing app " + wtoken + " delayed=" + delayed
3764 + " animation=" + wtoken.animation
3765 + " animating=" + wtoken.animating);
3766 if (delayed) {
3767 // set the token aside because it has an active animation to be finished
3768 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003769 } else {
3770 // Make sure there is no animation running on this token,
3771 // so any windows associated with it will be removed as
3772 // soon as their animations are complete
3773 wtoken.animation = null;
3774 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
3776 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003777 if (mLastEnterAnimToken == wtoken) {
3778 mLastEnterAnimToken = null;
3779 mLastEnterAnimParams = null;
3780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 wtoken.removed = true;
3782 if (wtoken.startingData != null) {
3783 startingToken = wtoken;
3784 }
3785 unsetAppFreezingScreenLocked(wtoken, true, true);
3786 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003787 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 mFocusedApp = null;
3789 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3790 mKeyWaiter.tickle();
3791 }
3792 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003793 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 }
Romain Guy06882f82009-06-10 13:36:04 -07003795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 if (!delayed && wtoken != null) {
3797 wtoken.updateReportedVisibilityLocked();
3798 }
3799 }
3800 Binder.restoreCallingIdentity(origId);
3801
3802 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003803 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 + startingToken + ": app token removed");
3805 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3806 mH.sendMessage(m);
3807 }
3808 }
3809
3810 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3811 final int NW = token.windows.size();
3812 for (int i=0; i<NW; i++) {
3813 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003814 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 mWindows.remove(win);
3816 int j = win.mChildWindows.size();
3817 while (j > 0) {
3818 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003819 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003820 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003821 "Tmp removing child window " + cwin);
3822 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
3824 }
3825 return NW > 0;
3826 }
3827
3828 void dumpAppTokensLocked() {
3829 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003830 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 }
3832 }
Romain Guy06882f82009-06-10 13:36:04 -07003833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 void dumpWindowsLocked() {
3835 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003836 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
3838 }
Romain Guy06882f82009-06-10 13:36:04 -07003839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 private int findWindowOffsetLocked(int tokenPos) {
3841 final int NW = mWindows.size();
3842
3843 if (tokenPos >= mAppTokens.size()) {
3844 int i = NW;
3845 while (i > 0) {
3846 i--;
3847 WindowState win = (WindowState)mWindows.get(i);
3848 if (win.getAppToken() != null) {
3849 return i+1;
3850 }
3851 }
3852 }
3853
3854 while (tokenPos > 0) {
3855 // Find the first app token below the new position that has
3856 // a window displayed.
3857 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003858 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003860 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003861 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003862 "Skipping token -- currently sending to bottom");
3863 tokenPos--;
3864 continue;
3865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 int i = wtoken.windows.size();
3867 while (i > 0) {
3868 i--;
3869 WindowState win = wtoken.windows.get(i);
3870 int j = win.mChildWindows.size();
3871 while (j > 0) {
3872 j--;
3873 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003874 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 for (int pos=NW-1; pos>=0; pos--) {
3876 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003877 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 "Found child win @" + (pos+1));
3879 return pos+1;
3880 }
3881 }
3882 }
3883 }
3884 for (int pos=NW-1; pos>=0; pos--) {
3885 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003886 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 return pos+1;
3888 }
3889 }
3890 }
3891 tokenPos--;
3892 }
3893
3894 return 0;
3895 }
3896
3897 private final int reAddWindowLocked(int index, WindowState win) {
3898 final int NCW = win.mChildWindows.size();
3899 boolean added = false;
3900 for (int j=0; j<NCW; j++) {
3901 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3902 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003903 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003904 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 mWindows.add(index, win);
3906 index++;
3907 added = true;
3908 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003909 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003910 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 mWindows.add(index, cwin);
3912 index++;
3913 }
3914 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003915 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003916 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 mWindows.add(index, win);
3918 index++;
3919 }
3920 return index;
3921 }
Romain Guy06882f82009-06-10 13:36:04 -07003922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3924 final int NW = token.windows.size();
3925 for (int i=0; i<NW; i++) {
3926 index = reAddWindowLocked(index, token.windows.get(i));
3927 }
3928 return index;
3929 }
3930
3931 public void moveAppToken(int index, IBinder token) {
3932 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3933 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003934 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936
3937 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003938 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 if (DEBUG_REORDER) dumpAppTokensLocked();
3940 final AppWindowToken wtoken = findAppWindowToken(token);
3941 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003942 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 + token + " (" + wtoken + ")");
3944 return;
3945 }
3946 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003947 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003951 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 if (DEBUG_REORDER) dumpWindowsLocked();
3953 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003954 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 if (DEBUG_REORDER) dumpWindowsLocked();
3956 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003957 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 if (DEBUG_REORDER) dumpWindowsLocked();
3959 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 mLayoutNeeded = true;
3961 performLayoutAndPlaceSurfacesLocked();
3962 }
3963 Binder.restoreCallingIdentity(origId);
3964 }
3965 }
3966
3967 private void removeAppTokensLocked(List<IBinder> tokens) {
3968 // XXX This should be done more efficiently!
3969 // (take advantage of the fact that both lists should be
3970 // ordered in the same way.)
3971 int N = tokens.size();
3972 for (int i=0; i<N; i++) {
3973 IBinder token = tokens.get(i);
3974 final AppWindowToken wtoken = findAppWindowToken(token);
3975 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003976 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 + token + " (" + wtoken + ")");
3978 i--;
3979 N--;
3980 }
3981 }
3982 }
3983
Dianne Hackborna8f60182009-09-01 19:01:50 -07003984 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
3985 boolean updateFocusAndLayout) {
3986 // First remove all of the windows from the list.
3987 tmpRemoveAppWindowsLocked(wtoken);
3988
3989 // Where to start adding?
3990 int pos = findWindowOffsetLocked(tokenPos);
3991
3992 // And now add them back at the correct place.
3993 pos = reAddAppWindowsLocked(pos, wtoken);
3994
3995 if (updateFocusAndLayout) {
3996 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
3997 assignLayersLocked();
3998 }
3999 mLayoutNeeded = true;
4000 performLayoutAndPlaceSurfacesLocked();
4001 }
4002 }
4003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4005 // First remove all of the windows from the list.
4006 final int N = tokens.size();
4007 int i;
4008 for (i=0; i<N; i++) {
4009 WindowToken token = mTokenMap.get(tokens.get(i));
4010 if (token != null) {
4011 tmpRemoveAppWindowsLocked(token);
4012 }
4013 }
4014
4015 // Where to start adding?
4016 int pos = findWindowOffsetLocked(tokenPos);
4017
4018 // And now add them back at the correct place.
4019 for (i=0; i<N; i++) {
4020 WindowToken token = mTokenMap.get(tokens.get(i));
4021 if (token != null) {
4022 pos = reAddAppWindowsLocked(pos, token);
4023 }
4024 }
4025
Dianne Hackborna8f60182009-09-01 19:01:50 -07004026 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4027 assignLayersLocked();
4028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 mLayoutNeeded = true;
4030 performLayoutAndPlaceSurfacesLocked();
4031
4032 //dump();
4033 }
4034
4035 public void moveAppTokensToTop(List<IBinder> tokens) {
4036 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4037 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004038 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 final long origId = Binder.clearCallingIdentity();
4042 synchronized(mWindowMap) {
4043 removeAppTokensLocked(tokens);
4044 final int N = tokens.size();
4045 for (int i=0; i<N; i++) {
4046 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4047 if (wt != null) {
4048 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004049 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004050 mToTopApps.remove(wt);
4051 mToBottomApps.remove(wt);
4052 mToTopApps.add(wt);
4053 wt.sendingToBottom = false;
4054 wt.sendingToTop = true;
4055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004058
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004059 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004060 moveAppWindowsLocked(tokens, mAppTokens.size());
4061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063 Binder.restoreCallingIdentity(origId);
4064 }
4065
4066 public void moveAppTokensToBottom(List<IBinder> tokens) {
4067 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4068 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004069 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071
4072 final long origId = Binder.clearCallingIdentity();
4073 synchronized(mWindowMap) {
4074 removeAppTokensLocked(tokens);
4075 final int N = tokens.size();
4076 int pos = 0;
4077 for (int i=0; i<N; i++) {
4078 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4079 if (wt != null) {
4080 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004081 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004082 mToTopApps.remove(wt);
4083 mToBottomApps.remove(wt);
4084 mToBottomApps.add(i, wt);
4085 wt.sendingToTop = false;
4086 wt.sendingToBottom = true;
4087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 pos++;
4089 }
4090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004091
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004092 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004093 moveAppWindowsLocked(tokens, 0);
4094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096 Binder.restoreCallingIdentity(origId);
4097 }
4098
4099 // -------------------------------------------------------------
4100 // Misc IWindowSession methods
4101 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004104 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 != PackageManager.PERMISSION_GRANTED) {
4106 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4107 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004108 synchronized (mKeyguardTokenWatcher) {
4109 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 }
4112
4113 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004114 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 != PackageManager.PERMISSION_GRANTED) {
4116 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4117 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004118 synchronized (mKeyguardTokenWatcher) {
4119 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120
Mike Lockwood983ee092009-11-22 01:42:24 -05004121 if (!mKeyguardTokenWatcher.isAcquired()) {
4122 // If we are the last one to reenable the keyguard wait until
4123 // we have actaully finished reenabling until returning.
4124 // It is possible that reenableKeyguard() can be called before
4125 // the previous disableKeyguard() is handled, in which case
4126 // neither mKeyguardTokenWatcher.acquired() or released() would
4127 // be called. In that case mKeyguardDisabled will be false here
4128 // and we have nothing to wait for.
4129 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004131 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 } catch (InterruptedException e) {
4133 Thread.currentThread().interrupt();
4134 }
4135 }
4136 }
4137 }
4138 }
4139
4140 /**
4141 * @see android.app.KeyguardManager#exitKeyguardSecurely
4142 */
4143 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004144 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 != PackageManager.PERMISSION_GRANTED) {
4146 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4147 }
4148 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4149 public void onKeyguardExitResult(boolean success) {
4150 try {
4151 callback.onKeyguardExitResult(success);
4152 } catch (RemoteException e) {
4153 // Client has died, we don't care.
4154 }
4155 }
4156 });
4157 }
4158
4159 public boolean inKeyguardRestrictedInputMode() {
4160 return mPolicy.inKeyguardRestrictedKeyInputMode();
4161 }
Romain Guy06882f82009-06-10 13:36:04 -07004162
Dianne Hackbornffa42482009-09-23 22:20:11 -07004163 public void closeSystemDialogs(String reason) {
4164 synchronized(mWindowMap) {
4165 for (int i=mWindows.size()-1; i>=0; i--) {
4166 WindowState w = (WindowState)mWindows.get(i);
4167 if (w.mSurface != null) {
4168 try {
4169 w.mClient.closeSystemDialogs(reason);
4170 } catch (RemoteException e) {
4171 }
4172 }
4173 }
4174 }
4175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 static float fixScale(float scale) {
4178 if (scale < 0) scale = 0;
4179 else if (scale > 20) scale = 20;
4180 return Math.abs(scale);
4181 }
Romain Guy06882f82009-06-10 13:36:04 -07004182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 public void setAnimationScale(int which, float scale) {
4184 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4185 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004186 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188
4189 if (scale < 0) scale = 0;
4190 else if (scale > 20) scale = 20;
4191 scale = Math.abs(scale);
4192 switch (which) {
4193 case 0: mWindowAnimationScale = fixScale(scale); break;
4194 case 1: mTransitionAnimationScale = fixScale(scale); break;
4195 }
Romain Guy06882f82009-06-10 13:36:04 -07004196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 // Persist setting
4198 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4199 }
Romain Guy06882f82009-06-10 13:36:04 -07004200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 public void setAnimationScales(float[] scales) {
4202 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4203 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004204 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 }
4206
4207 if (scales != null) {
4208 if (scales.length >= 1) {
4209 mWindowAnimationScale = fixScale(scales[0]);
4210 }
4211 if (scales.length >= 2) {
4212 mTransitionAnimationScale = fixScale(scales[1]);
4213 }
4214 }
Romain Guy06882f82009-06-10 13:36:04 -07004215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 // Persist setting
4217 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4218 }
Romain Guy06882f82009-06-10 13:36:04 -07004219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 public float getAnimationScale(int which) {
4221 switch (which) {
4222 case 0: return mWindowAnimationScale;
4223 case 1: return mTransitionAnimationScale;
4224 }
4225 return 0;
4226 }
Romain Guy06882f82009-06-10 13:36:04 -07004227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 public float[] getAnimationScales() {
4229 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4230 }
Romain Guy06882f82009-06-10 13:36:04 -07004231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 public int getSwitchState(int sw) {
4233 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4234 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004235 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 }
4237 return KeyInputQueue.getSwitchState(sw);
4238 }
Romain Guy06882f82009-06-10 13:36:04 -07004239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 public int getSwitchStateForDevice(int devid, int sw) {
4241 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4242 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004243 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245 return KeyInputQueue.getSwitchState(devid, sw);
4246 }
Romain Guy06882f82009-06-10 13:36:04 -07004247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 public int getScancodeState(int sw) {
4249 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4250 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004251 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004253 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
Romain Guy06882f82009-06-10 13:36:04 -07004255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 public int getScancodeStateForDevice(int devid, int sw) {
4257 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4258 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004259 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004261 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 }
Romain Guy06882f82009-06-10 13:36:04 -07004263
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004264 public int getTrackballScancodeState(int sw) {
4265 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4266 "getTrackballScancodeState()")) {
4267 throw new SecurityException("Requires READ_INPUT_STATE permission");
4268 }
4269 return mQueue.getTrackballScancodeState(sw);
4270 }
4271
4272 public int getDPadScancodeState(int sw) {
4273 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4274 "getDPadScancodeState()")) {
4275 throw new SecurityException("Requires READ_INPUT_STATE permission");
4276 }
4277 return mQueue.getDPadScancodeState(sw);
4278 }
4279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 public int getKeycodeState(int sw) {
4281 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4282 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004283 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004284 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004285 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
Romain Guy06882f82009-06-10 13:36:04 -07004287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 public int getKeycodeStateForDevice(int devid, int sw) {
4289 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4290 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004291 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004293 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 }
Romain Guy06882f82009-06-10 13:36:04 -07004295
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004296 public int getTrackballKeycodeState(int sw) {
4297 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4298 "getTrackballKeycodeState()")) {
4299 throw new SecurityException("Requires READ_INPUT_STATE permission");
4300 }
4301 return mQueue.getTrackballKeycodeState(sw);
4302 }
4303
4304 public int getDPadKeycodeState(int sw) {
4305 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4306 "getDPadKeycodeState()")) {
4307 throw new SecurityException("Requires READ_INPUT_STATE permission");
4308 }
4309 return mQueue.getDPadKeycodeState(sw);
4310 }
4311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4313 return KeyInputQueue.hasKeys(keycodes, keyExists);
4314 }
Romain Guy06882f82009-06-10 13:36:04 -07004315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 public void enableScreenAfterBoot() {
4317 synchronized(mWindowMap) {
4318 if (mSystemBooted) {
4319 return;
4320 }
4321 mSystemBooted = true;
4322 }
Romain Guy06882f82009-06-10 13:36:04 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 performEnableScreen();
4325 }
Romain Guy06882f82009-06-10 13:36:04 -07004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 public void enableScreenIfNeededLocked() {
4328 if (mDisplayEnabled) {
4329 return;
4330 }
4331 if (!mSystemBooted) {
4332 return;
4333 }
4334 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4335 }
Romain Guy06882f82009-06-10 13:36:04 -07004336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 public void performEnableScreen() {
4338 synchronized(mWindowMap) {
4339 if (mDisplayEnabled) {
4340 return;
4341 }
4342 if (!mSystemBooted) {
4343 return;
4344 }
Romain Guy06882f82009-06-10 13:36:04 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 // Don't enable the screen until all existing windows
4347 // have been drawn.
4348 final int N = mWindows.size();
4349 for (int i=0; i<N; i++) {
4350 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004351 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 return;
4353 }
4354 }
Romain Guy06882f82009-06-10 13:36:04 -07004355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 mDisplayEnabled = true;
4357 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004358 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 StringWriter sw = new StringWriter();
4360 PrintWriter pw = new PrintWriter(sw);
4361 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004362 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 }
4364 try {
4365 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4366 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004367 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 Parcel data = Parcel.obtain();
4369 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4370 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4371 data, null, 0);
4372 data.recycle();
4373 }
4374 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004375 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377 }
Romain Guy06882f82009-06-10 13:36:04 -07004378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004382 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4383 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 }
Romain Guy06882f82009-06-10 13:36:04 -07004385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 public void setInTouchMode(boolean mode) {
4387 synchronized(mWindowMap) {
4388 mInTouchMode = mode;
4389 }
4390 }
4391
Romain Guy06882f82009-06-10 13:36:04 -07004392 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004393 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004395 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004396 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 }
4398
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004399 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 }
Romain Guy06882f82009-06-10 13:36:04 -07004401
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004402 public void setRotationUnchecked(int rotation,
4403 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004404 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 long origId = Binder.clearCallingIdentity();
4408 boolean changed;
4409 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004410 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 }
Romain Guy06882f82009-06-10 13:36:04 -07004412
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004413 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 sendNewConfiguration();
4415 }
Romain Guy06882f82009-06-10 13:36:04 -07004416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 Binder.restoreCallingIdentity(origId);
4418 }
Romain Guy06882f82009-06-10 13:36:04 -07004419
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004420 /**
4421 * Apply a new rotation to the screen, respecting the requests of
4422 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4423 * re-evaluate the desired rotation.
4424 *
4425 * Returns null if the rotation has been changed. In this case YOU
4426 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4427 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004428 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 boolean changed;
4430 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4431 rotation = mRequestedRotation;
4432 } else {
4433 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004434 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004436 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004437 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004439 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004443 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 "Rotation changed to " + rotation
4445 + " from " + mRotation
4446 + " (forceApp=" + mForcedAppOrientation
4447 + ", req=" + mRequestedRotation + ")");
4448 mRotation = rotation;
4449 mWindowsFreezingScreen = true;
4450 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4451 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4452 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004453 mWaitingForConfig = true;
4454 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004456 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 mQueue.setOrientation(rotation);
4458 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004459 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 }
4461 for (int i=mWindows.size()-1; i>=0; i--) {
4462 WindowState w = (WindowState)mWindows.get(i);
4463 if (w.mSurface != null) {
4464 w.mOrientationChanging = true;
4465 }
4466 }
4467 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4468 try {
4469 mRotationWatchers.get(i).onRotationChanged(rotation);
4470 } catch (RemoteException e) {
4471 }
4472 }
4473 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 return changed;
4476 }
Romain Guy06882f82009-06-10 13:36:04 -07004477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 public int getRotation() {
4479 return mRotation;
4480 }
4481
4482 public int watchRotation(IRotationWatcher watcher) {
4483 final IBinder watcherBinder = watcher.asBinder();
4484 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4485 public void binderDied() {
4486 synchronized (mWindowMap) {
4487 for (int i=0; i<mRotationWatchers.size(); i++) {
4488 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004489 IRotationWatcher removed = mRotationWatchers.remove(i);
4490 if (removed != null) {
4491 removed.asBinder().unlinkToDeath(this, 0);
4492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 i--;
4494 }
4495 }
4496 }
4497 }
4498 };
Romain Guy06882f82009-06-10 13:36:04 -07004499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 synchronized (mWindowMap) {
4501 try {
4502 watcher.asBinder().linkToDeath(dr, 0);
4503 mRotationWatchers.add(watcher);
4504 } catch (RemoteException e) {
4505 // Client died, no cleanup needed.
4506 }
Romain Guy06882f82009-06-10 13:36:04 -07004507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 return mRotation;
4509 }
4510 }
4511
4512 /**
4513 * Starts the view server on the specified port.
4514 *
4515 * @param port The port to listener to.
4516 *
4517 * @return True if the server was successfully started, false otherwise.
4518 *
4519 * @see com.android.server.ViewServer
4520 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4521 */
4522 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004523 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 return false;
4525 }
4526
4527 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4528 return false;
4529 }
4530
4531 if (port < 1024) {
4532 return false;
4533 }
4534
4535 if (mViewServer != null) {
4536 if (!mViewServer.isRunning()) {
4537 try {
4538 return mViewServer.start();
4539 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004540 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
4542 }
4543 return false;
4544 }
4545
4546 try {
4547 mViewServer = new ViewServer(this, port);
4548 return mViewServer.start();
4549 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004550 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 }
4552 return false;
4553 }
4554
Romain Guy06882f82009-06-10 13:36:04 -07004555 private boolean isSystemSecure() {
4556 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4557 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4558 }
4559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 /**
4561 * Stops the view server if it exists.
4562 *
4563 * @return True if the server stopped, false if it wasn't started or
4564 * couldn't be stopped.
4565 *
4566 * @see com.android.server.ViewServer
4567 */
4568 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004569 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 return false;
4571 }
4572
4573 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4574 return false;
4575 }
4576
4577 if (mViewServer != null) {
4578 return mViewServer.stop();
4579 }
4580 return false;
4581 }
4582
4583 /**
4584 * Indicates whether the view server is running.
4585 *
4586 * @return True if the server is running, false otherwise.
4587 *
4588 * @see com.android.server.ViewServer
4589 */
4590 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004591 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 return false;
4593 }
4594
4595 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4596 return false;
4597 }
4598
4599 return mViewServer != null && mViewServer.isRunning();
4600 }
4601
4602 /**
4603 * Lists all availble windows in the system. The listing is written in the
4604 * specified Socket's output stream with the following syntax:
4605 * windowHashCodeInHexadecimal windowName
4606 * Each line of the ouput represents a different window.
4607 *
4608 * @param client The remote client to send the listing to.
4609 * @return False if an error occured, true otherwise.
4610 */
4611 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004612 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 return false;
4614 }
4615
4616 boolean result = true;
4617
4618 Object[] windows;
4619 synchronized (mWindowMap) {
4620 windows = new Object[mWindows.size()];
4621 //noinspection unchecked
4622 windows = mWindows.toArray(windows);
4623 }
4624
4625 BufferedWriter out = null;
4626
4627 // Any uncaught exception will crash the system process
4628 try {
4629 OutputStream clientStream = client.getOutputStream();
4630 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4631
4632 final int count = windows.length;
4633 for (int i = 0; i < count; i++) {
4634 final WindowState w = (WindowState) windows[i];
4635 out.write(Integer.toHexString(System.identityHashCode(w)));
4636 out.write(' ');
4637 out.append(w.mAttrs.getTitle());
4638 out.write('\n');
4639 }
4640
4641 out.write("DONE.\n");
4642 out.flush();
4643 } catch (Exception e) {
4644 result = false;
4645 } finally {
4646 if (out != null) {
4647 try {
4648 out.close();
4649 } catch (IOException e) {
4650 result = false;
4651 }
4652 }
4653 }
4654
4655 return result;
4656 }
4657
4658 /**
4659 * Sends a command to a target window. The result of the command, if any, will be
4660 * written in the output stream of the specified socket.
4661 *
4662 * The parameters must follow this syntax:
4663 * windowHashcode extra
4664 *
4665 * Where XX is the length in characeters of the windowTitle.
4666 *
4667 * The first parameter is the target window. The window with the specified hashcode
4668 * will be the target. If no target can be found, nothing happens. The extra parameters
4669 * will be delivered to the target window and as parameters to the command itself.
4670 *
4671 * @param client The remote client to sent the result, if any, to.
4672 * @param command The command to execute.
4673 * @param parameters The command parameters.
4674 *
4675 * @return True if the command was successfully delivered, false otherwise. This does
4676 * not indicate whether the command itself was successful.
4677 */
4678 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004679 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 return false;
4681 }
4682
4683 boolean success = true;
4684 Parcel data = null;
4685 Parcel reply = null;
4686
4687 // Any uncaught exception will crash the system process
4688 try {
4689 // Find the hashcode of the window
4690 int index = parameters.indexOf(' ');
4691 if (index == -1) {
4692 index = parameters.length();
4693 }
4694 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004695 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696
4697 // Extract the command's parameter after the window description
4698 if (index < parameters.length()) {
4699 parameters = parameters.substring(index + 1);
4700 } else {
4701 parameters = "";
4702 }
4703
4704 final WindowManagerService.WindowState window = findWindow(hashCode);
4705 if (window == null) {
4706 return false;
4707 }
4708
4709 data = Parcel.obtain();
4710 data.writeInterfaceToken("android.view.IWindow");
4711 data.writeString(command);
4712 data.writeString(parameters);
4713 data.writeInt(1);
4714 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4715
4716 reply = Parcel.obtain();
4717
4718 final IBinder binder = window.mClient.asBinder();
4719 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4720 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4721
4722 reply.readException();
4723
4724 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004725 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 success = false;
4727 } finally {
4728 if (data != null) {
4729 data.recycle();
4730 }
4731 if (reply != null) {
4732 reply.recycle();
4733 }
4734 }
4735
4736 return success;
4737 }
4738
4739 private WindowState findWindow(int hashCode) {
4740 if (hashCode == -1) {
4741 return getFocusedWindow();
4742 }
4743
4744 synchronized (mWindowMap) {
4745 final ArrayList windows = mWindows;
4746 final int count = windows.size();
4747
4748 for (int i = 0; i < count; i++) {
4749 WindowState w = (WindowState) windows.get(i);
4750 if (System.identityHashCode(w) == hashCode) {
4751 return w;
4752 }
4753 }
4754 }
4755
4756 return null;
4757 }
4758
4759 /*
4760 * Instruct the Activity Manager to fetch the current configuration and broadcast
4761 * that to config-changed listeners if appropriate.
4762 */
4763 void sendNewConfiguration() {
4764 try {
4765 mActivityManager.updateConfiguration(null);
4766 } catch (RemoteException e) {
4767 }
4768 }
Romain Guy06882f82009-06-10 13:36:04 -07004769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 public Configuration computeNewConfiguration() {
4771 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004772 return computeNewConfigurationLocked();
4773 }
4774 }
Romain Guy06882f82009-06-10 13:36:04 -07004775
Dianne Hackbornc485a602009-03-24 22:39:49 -07004776 Configuration computeNewConfigurationLocked() {
4777 Configuration config = new Configuration();
4778 if (!computeNewConfigurationLocked(config)) {
4779 return null;
4780 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004781 return config;
4782 }
Romain Guy06882f82009-06-10 13:36:04 -07004783
Dianne Hackbornc485a602009-03-24 22:39:49 -07004784 boolean computeNewConfigurationLocked(Configuration config) {
4785 if (mDisplay == null) {
4786 return false;
4787 }
4788 mQueue.getInputConfiguration(config);
4789 final int dw = mDisplay.getWidth();
4790 final int dh = mDisplay.getHeight();
4791 int orientation = Configuration.ORIENTATION_SQUARE;
4792 if (dw < dh) {
4793 orientation = Configuration.ORIENTATION_PORTRAIT;
4794 } else if (dw > dh) {
4795 orientation = Configuration.ORIENTATION_LANDSCAPE;
4796 }
4797 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004798
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004799 DisplayMetrics dm = new DisplayMetrics();
4800 mDisplay.getMetrics(dm);
4801 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4802
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004803 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004804 // Note we only do this once because at this point we don't
4805 // expect the screen to change in this way at runtime, and want
4806 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004807 int longSize = dw;
4808 int shortSize = dh;
4809 if (longSize < shortSize) {
4810 int tmp = longSize;
4811 longSize = shortSize;
4812 shortSize = tmp;
4813 }
4814 longSize = (int)(longSize/dm.density);
4815 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004816
Dianne Hackborn723738c2009-06-25 19:48:04 -07004817 // These semi-magic numbers define our compatibility modes for
4818 // applications with different screens. Don't change unless you
4819 // make sure to test lots and lots of apps!
4820 if (longSize < 470) {
4821 // This is shorter than an HVGA normal density screen (which
4822 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004823 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4824 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004825 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004826 // Is this a large screen?
4827 if (longSize > 640 && shortSize >= 480) {
4828 // VGA or larger screens at medium density are the point
4829 // at which we consider it to be a large screen.
4830 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4831 } else {
4832 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004833
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004834 // If this screen is wider than normal HVGA, or taller
4835 // than FWVGA, then for old apps we want to run in size
4836 // compatibility mode.
4837 if (shortSize > 321 || longSize > 570) {
4838 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4839 }
4840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004841
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004842 // Is this a long screen?
4843 if (((longSize*3)/5) >= (shortSize-1)) {
4844 // Anything wider than WVGA (5:3) is considering to be long.
4845 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4846 } else {
4847 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4848 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004849 }
4850 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004851 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004852
Dianne Hackbornc485a602009-03-24 22:39:49 -07004853 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4854 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4855 mPolicy.adjustConfigurationLw(config);
4856 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 }
Romain Guy06882f82009-06-10 13:36:04 -07004858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 // -------------------------------------------------------------
4860 // Input Events and Focus Management
4861 // -------------------------------------------------------------
4862
4863 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004864 long curTime = SystemClock.uptimeMillis();
4865
Michael Chane10de972009-05-18 11:24:50 -07004866 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004867 if (mLastTouchEventType == eventType &&
4868 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4869 return;
4870 }
4871 mLastUserActivityCallTime = curTime;
4872 mLastTouchEventType = eventType;
4873 }
4874
4875 if (targetWin == null
4876 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4877 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
4879 }
4880
4881 // tells if it's a cheek event or not -- this function is stateful
4882 private static final int EVENT_NONE = 0;
4883 private static final int EVENT_UNKNOWN = 0;
4884 private static final int EVENT_CHEEK = 0;
4885 private static final int EVENT_IGNORE_DURATION = 300; // ms
4886 private static final float CHEEK_THRESHOLD = 0.6f;
4887 private int mEventState = EVENT_NONE;
4888 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 private int eventType(MotionEvent ev) {
4891 float size = ev.getSize();
4892 switch (ev.getAction()) {
4893 case MotionEvent.ACTION_DOWN:
4894 mEventSize = size;
4895 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4896 case MotionEvent.ACTION_UP:
4897 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004898 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 case MotionEvent.ACTION_MOVE:
4900 final int N = ev.getHistorySize();
4901 if (size > mEventSize) mEventSize = size;
4902 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4903 for (int i=0; i<N; i++) {
4904 size = ev.getHistoricalSize(i);
4905 if (size > mEventSize) mEventSize = size;
4906 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4907 }
4908 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4909 return TOUCH_EVENT;
4910 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004911 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 }
4913 default:
4914 // not good
4915 return OTHER_EVENT;
4916 }
4917 }
4918
4919 /**
4920 * @return Returns true if event was dispatched, false if it was dropped for any reason
4921 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004922 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004923 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 "dispatchPointer " + ev);
4925
Michael Chan53071d62009-05-13 17:29:48 -07004926 if (MEASURE_LATENCY) {
4927 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4928 }
4929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004931 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004932
Michael Chan53071d62009-05-13 17:29:48 -07004933 if (MEASURE_LATENCY) {
4934 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4935 }
4936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 if (action == MotionEvent.ACTION_UP) {
4940 // let go of our target
4941 mKeyWaiter.mMotionTarget = null;
4942 mPowerManager.logPointerUpEvent();
4943 } else if (action == MotionEvent.ACTION_DOWN) {
4944 mPowerManager.logPointerDownEvent();
4945 }
4946
4947 if (targetObj == null) {
4948 // In this case we are either dropping the event, or have received
4949 // a move or up without a down. It is common to receive move
4950 // events in such a way, since this means the user is moving the
4951 // pointer without actually pressing down. All other cases should
4952 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07004953 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004954 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08004956 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08004957 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08004958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 if (qev != null) {
4960 mQueue.recycleEvent(qev);
4961 }
4962 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004963 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 }
4965 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08004966 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08004967 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08004968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 if (qev != null) {
4970 mQueue.recycleEvent(qev);
4971 }
4972 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004973 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 }
Romain Guy06882f82009-06-10 13:36:04 -07004975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07004977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07004979 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004980
Joe Onorato8a9b2202010-02-26 18:56:32 -08004981 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982
4983 if (uid != 0 && uid != target.mSession.mUid) {
4984 if (mContext.checkPermission(
4985 android.Manifest.permission.INJECT_EVENTS, pid, uid)
4986 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004987 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 + pid + " uid " + uid + " to window " + target
4989 + " owned by uid " + target.mSession.mUid);
4990 if (qev != null) {
4991 mQueue.recycleEvent(qev);
4992 }
4993 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07004994 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
4996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004997
Michael Chan53071d62009-05-13 17:29:48 -07004998 if (MEASURE_LATENCY) {
4999 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5000 }
5001
Romain Guy06882f82009-06-10 13:36:04 -07005002 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005003 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5004 //target wants to ignore fat touch events
5005 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5006 //explicit flag to return without processing event further
5007 boolean returnFlag = false;
5008 if((action == MotionEvent.ACTION_DOWN)) {
5009 mFatTouch = false;
5010 if(cheekPress) {
5011 mFatTouch = true;
5012 returnFlag = true;
5013 }
5014 } else {
5015 if(action == MotionEvent.ACTION_UP) {
5016 if(mFatTouch) {
5017 //earlier even was invalid doesnt matter if current up is cheekpress or not
5018 mFatTouch = false;
5019 returnFlag = true;
5020 } else if(cheekPress) {
5021 //cancel the earlier event
5022 ev.setAction(MotionEvent.ACTION_CANCEL);
5023 action = MotionEvent.ACTION_CANCEL;
5024 }
5025 } else if(action == MotionEvent.ACTION_MOVE) {
5026 if(mFatTouch) {
5027 //two cases here
5028 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005029 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 returnFlag = true;
5031 } else if(cheekPress) {
5032 //valid down followed by invalid moves
5033 //an invalid move have to cancel earlier action
5034 ev.setAction(MotionEvent.ACTION_CANCEL);
5035 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005036 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 //note that the subsequent invalid moves will not get here
5038 mFatTouch = true;
5039 }
5040 }
5041 } //else if action
5042 if(returnFlag) {
5043 //recycle que, ev
5044 if (qev != null) {
5045 mQueue.recycleEvent(qev);
5046 }
5047 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005048 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 }
5050 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005051
Michael Chan9f028e62009-08-04 17:37:46 -07005052 // Enable this for testing the "right" value
5053 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005054 int max_events_per_sec = 35;
5055 try {
5056 max_events_per_sec = Integer.parseInt(SystemProperties
5057 .get("windowsmgr.max_events_per_sec"));
5058 if (max_events_per_sec < 1) {
5059 max_events_per_sec = 35;
5060 }
5061 } catch (NumberFormatException e) {
5062 }
5063 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5064 }
5065
5066 /*
5067 * Throttle events to minimize CPU usage when there's a flood of events
5068 * e.g. constant contact with the screen
5069 */
5070 if (action == MotionEvent.ACTION_MOVE) {
5071 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5072 long now = SystemClock.uptimeMillis();
5073 if (now < nextEventTime) {
5074 try {
5075 Thread.sleep(nextEventTime - now);
5076 } catch (InterruptedException e) {
5077 }
5078 mLastTouchEventTime = nextEventTime;
5079 } else {
5080 mLastTouchEventTime = now;
5081 }
5082 }
5083
Michael Chan53071d62009-05-13 17:29:48 -07005084 if (MEASURE_LATENCY) {
5085 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5086 }
5087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005089 if (!target.isVisibleLw()) {
5090 // During this motion dispatch, the target window has become
5091 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005092 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005093 if (qev != null) {
5094 mQueue.recycleEvent(qev);
5095 }
5096 ev.recycle();
5097 return INJECT_SUCCEEDED;
5098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5101 mKeyWaiter.bindTargetWindowLocked(target,
5102 KeyWaiter.RETURN_PENDING_POINTER, qev);
5103 ev = null;
5104 } else {
5105 if (action == MotionEvent.ACTION_DOWN) {
5106 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5107 if (out != null) {
5108 MotionEvent oev = MotionEvent.obtain(ev);
5109 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5110 do {
5111 final Rect frame = out.mFrame;
5112 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5113 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005114 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005116 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 }
5118 oev.offsetLocation((float)frame.left, (float)frame.top);
5119 out = out.mNextOutsideTouch;
5120 } while (out != null);
5121 mKeyWaiter.mOutsideTouchTargets = null;
5122 }
5123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005124
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005125 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005126
Dianne Hackborn6adba242009-11-10 11:10:09 -08005127 final Rect frame = target.mFrame;
5128 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5129 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131 }
Romain Guy06882f82009-06-10 13:36:04 -07005132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 // finally offset the event to the target's coordinate system and
5134 // dispatch the event.
5135 try {
5136 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005137 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005139
Michael Chan53071d62009-05-13 17:29:48 -07005140 if (MEASURE_LATENCY) {
5141 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5142 }
5143
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005144 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005145
5146 if (MEASURE_LATENCY) {
5147 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5148 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005149 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005151 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 mKeyWaiter.mMotionTarget = null;
5153 try {
5154 removeWindow(target.mSession, target.mClient);
5155 } catch (java.util.NoSuchElementException ex) {
5156 // This will happen if the window has already been
5157 // removed.
5158 }
5159 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005160 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005161 }
Romain Guy06882f82009-06-10 13:36:04 -07005162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 /**
5164 * @return Returns true if event was dispatched, false if it was dropped for any reason
5165 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005166 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005167 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005171 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005173 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 if (qev != null) {
5175 mQueue.recycleEvent(qev);
5176 }
5177 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005178 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
5180 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5181 if (qev != null) {
5182 mQueue.recycleEvent(qev);
5183 }
5184 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005185 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 }
Romain Guy06882f82009-06-10 13:36:04 -07005187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 if (uid != 0 && uid != focus.mSession.mUid) {
5191 if (mContext.checkPermission(
5192 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5193 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005194 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 + pid + " uid " + uid + " to window " + focus
5196 + " owned by uid " + focus.mSession.mUid);
5197 if (qev != null) {
5198 mQueue.recycleEvent(qev);
5199 }
5200 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005201 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 }
5203 }
Romain Guy06882f82009-06-10 13:36:04 -07005204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 synchronized(mWindowMap) {
5208 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5209 mKeyWaiter.bindTargetWindowLocked(focus,
5210 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5211 // We don't deliver movement events to the client, we hold
5212 // them and wait for them to call back.
5213 ev = null;
5214 } else {
5215 mKeyWaiter.bindTargetWindowLocked(focus);
5216 }
5217 }
Romain Guy06882f82009-06-10 13:36:04 -07005218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005220 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005221 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005223 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 try {
5225 removeWindow(focus.mSession, focus.mClient);
5226 } catch (java.util.NoSuchElementException ex) {
5227 // This will happen if the window has already been
5228 // removed.
5229 }
5230 }
Romain Guy06882f82009-06-10 13:36:04 -07005231
Dianne Hackborncfaef692009-06-15 14:24:44 -07005232 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 }
Romain Guy06882f82009-06-10 13:36:04 -07005234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 /**
5236 * @return Returns true if event was dispatched, false if it was dropped for any reason
5237 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005238 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005239 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240
5241 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005242 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005244 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005245 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 }
5247 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005248 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 }
Romain Guy06882f82009-06-10 13:36:04 -07005250
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005251 // Okay we have finished waiting for the last event to be processed.
5252 // First off, if this is a repeat event, check to see if there is
5253 // a corresponding up event in the queue. If there is, we will
5254 // just drop the repeat, because it makes no sense to repeat after
5255 // the user has released a key. (This is especially important for
5256 // long presses.)
5257 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5258 return INJECT_SUCCEEDED;
5259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005262
Joe Onorato8a9b2202010-02-26 18:56:32 -08005263 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 TAG, "Dispatching to " + focus + ": " + event);
5265
5266 if (uid != 0 && uid != focus.mSession.mUid) {
5267 if (mContext.checkPermission(
5268 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5269 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005270 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 + pid + " uid " + uid + " to window " + focus
5272 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005273 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 }
5275 }
Romain Guy06882f82009-06-10 13:36:04 -07005276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 synchronized(mWindowMap) {
5278 mKeyWaiter.bindTargetWindowLocked(focus);
5279 }
5280
5281 // NOSHIP extra state logging
5282 mKeyWaiter.recordDispatchState(event, focus);
5283 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 try {
5286 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005287 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 + " to " + focus);
5289 }
5290 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005291 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005293 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 try {
5295 removeWindow(focus.mSession, focus.mClient);
5296 } catch (java.util.NoSuchElementException ex) {
5297 // This will happen if the window has already been
5298 // removed.
5299 }
5300 }
Romain Guy06882f82009-06-10 13:36:04 -07005301
Dianne Hackborncfaef692009-06-15 14:24:44 -07005302 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 }
Romain Guy06882f82009-06-10 13:36:04 -07005304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 public void pauseKeyDispatching(IBinder _token) {
5306 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5307 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005308 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 }
5310
5311 synchronized (mWindowMap) {
5312 WindowToken token = mTokenMap.get(_token);
5313 if (token != null) {
5314 mKeyWaiter.pauseDispatchingLocked(token);
5315 }
5316 }
5317 }
5318
5319 public void resumeKeyDispatching(IBinder _token) {
5320 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5321 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005322 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 }
5324
5325 synchronized (mWindowMap) {
5326 WindowToken token = mTokenMap.get(_token);
5327 if (token != null) {
5328 mKeyWaiter.resumeDispatchingLocked(token);
5329 }
5330 }
5331 }
5332
5333 public void setEventDispatching(boolean enabled) {
5334 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5335 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005336 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 }
5338
5339 synchronized (mWindowMap) {
5340 mKeyWaiter.setEventDispatchingLocked(enabled);
5341 }
5342 }
Romain Guy06882f82009-06-10 13:36:04 -07005343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 /**
5345 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005346 *
5347 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 * {@link SystemClock#uptimeMillis()} as the timebase.)
5349 * @param sync If true, wait for the event to be completed before returning to the caller.
5350 * @return Returns true if event was dispatched, false if it was dropped for any reason
5351 */
5352 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5353 long downTime = ev.getDownTime();
5354 long eventTime = ev.getEventTime();
5355
5356 int action = ev.getAction();
5357 int code = ev.getKeyCode();
5358 int repeatCount = ev.getRepeatCount();
5359 int metaState = ev.getMetaState();
5360 int deviceId = ev.getDeviceId();
5361 int scancode = ev.getScanCode();
5362
5363 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5364 if (downTime == 0) downTime = eventTime;
5365
5366 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005367 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005369 final int pid = Binder.getCallingPid();
5370 final int uid = Binder.getCallingUid();
5371 final long ident = Binder.clearCallingIdentity();
5372 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005374 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005376 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005377 switch (result) {
5378 case INJECT_NO_PERMISSION:
5379 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305380 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005381 case INJECT_SUCCEEDED:
5382 return true;
5383 }
5384 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005385 }
5386
5387 /**
5388 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005389 *
5390 * @param ev A motion event describing the pointer (touch) action. (As noted in
5391 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 * {@link SystemClock#uptimeMillis()} as the timebase.)
5393 * @param sync If true, wait for the event to be completed before returning to the caller.
5394 * @return Returns true if event was dispatched, false if it was dropped for any reason
5395 */
5396 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005397 final int pid = Binder.getCallingPid();
5398 final int uid = Binder.getCallingUid();
5399 final long ident = Binder.clearCallingIdentity();
5400 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005402 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005404 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005405 switch (result) {
5406 case INJECT_NO_PERMISSION:
5407 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305408 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005409 case INJECT_SUCCEEDED:
5410 return true;
5411 }
5412 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005413 }
Romain Guy06882f82009-06-10 13:36:04 -07005414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 /**
5416 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005417 *
5418 * @param ev A motion event describing the trackball action. (As noted in
5419 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 * {@link SystemClock#uptimeMillis()} as the timebase.)
5421 * @param sync If true, wait for the event to be completed before returning to the caller.
5422 * @return Returns true if event was dispatched, false if it was dropped for any reason
5423 */
5424 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005425 final int pid = Binder.getCallingPid();
5426 final int uid = Binder.getCallingUid();
5427 final long ident = Binder.clearCallingIdentity();
5428 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005430 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005431 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005432 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005433 switch (result) {
5434 case INJECT_NO_PERMISSION:
5435 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305436 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005437 case INJECT_SUCCEEDED:
5438 return true;
5439 }
5440 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 }
Romain Guy06882f82009-06-10 13:36:04 -07005442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 private WindowState getFocusedWindow() {
5444 synchronized (mWindowMap) {
5445 return getFocusedWindowLocked();
5446 }
5447 }
5448
5449 private WindowState getFocusedWindowLocked() {
5450 return mCurrentFocus;
5451 }
Romain Guy06882f82009-06-10 13:36:04 -07005452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 /**
5454 * This class holds the state for dispatching key events. This state
5455 * is protected by the KeyWaiter instance, NOT by the window lock. You
5456 * can be holding the main window lock while acquire the KeyWaiter lock,
5457 * but not the other way around.
5458 */
5459 final class KeyWaiter {
5460 // NOSHIP debugging
5461 public class DispatchState {
5462 private KeyEvent event;
5463 private WindowState focus;
5464 private long time;
5465 private WindowState lastWin;
5466 private IBinder lastBinder;
5467 private boolean finished;
5468 private boolean gotFirstWindow;
5469 private boolean eventDispatching;
5470 private long timeToSwitch;
5471 private boolean wasFrozen;
5472 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005473 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5476 focus = theFocus;
5477 event = theEvent;
5478 time = System.currentTimeMillis();
5479 // snapshot KeyWaiter state
5480 lastWin = mLastWin;
5481 lastBinder = mLastBinder;
5482 finished = mFinished;
5483 gotFirstWindow = mGotFirstWindow;
5484 eventDispatching = mEventDispatching;
5485 timeToSwitch = mTimeToSwitch;
5486 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005487 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 // cache the paused state at ctor time as well
5489 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 focusPaused = false;
5491 } else {
5492 focusPaused = theFocus.mToken.paused;
5493 }
5494 }
Romain Guy06882f82009-06-10 13:36:04 -07005495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 public String toString() {
5497 return "{{" + event + " to " + focus + " @ " + time
5498 + " lw=" + lastWin + " lb=" + lastBinder
5499 + " fin=" + finished + " gfw=" + gotFirstWindow
5500 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005501 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005502 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 }
5504 };
5505 private DispatchState mDispatchState = null;
5506 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5507 mDispatchState = new DispatchState(theEvent, theFocus);
5508 }
5509 // END NOSHIP
5510
5511 public static final int RETURN_NOTHING = 0;
5512 public static final int RETURN_PENDING_POINTER = 1;
5513 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 final Object SKIP_TARGET_TOKEN = new Object();
5516 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 private WindowState mLastWin = null;
5519 private IBinder mLastBinder = null;
5520 private boolean mFinished = true;
5521 private boolean mGotFirstWindow = false;
5522 private boolean mEventDispatching = true;
5523 private long mTimeToSwitch = 0;
5524 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 // Target of Motion events
5527 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 // Windows above the target who would like to receive an "outside"
5530 // touch event for any down events outside of them.
5531 WindowState mOutsideTouchTargets;
5532
5533 /**
5534 * Wait for the last event dispatch to complete, then find the next
5535 * target that should receive the given event and wait for that one
5536 * to be ready to receive it.
5537 */
5538 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5539 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005540 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 long startTime = SystemClock.uptimeMillis();
5542 long keyDispatchingTimeout = 5 * 1000;
5543 long waitedFor = 0;
5544
5545 while (true) {
5546 // Figure out which window we care about. It is either the
5547 // last window we are waiting to have process the event or,
5548 // if none, then the next window we think the event should go
5549 // to. Note: we retrieve mLastWin outside of the lock, so
5550 // it may change before we lock. Thus we must check it again.
5551 WindowState targetWin = mLastWin;
5552 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005553 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 TAG, "waitForLastKey: mFinished=" + mFinished +
5555 ", mLastWin=" + mLastWin);
5556 if (targetIsNew) {
5557 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005558 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 if (target == SKIP_TARGET_TOKEN) {
5560 // The user has pressed a special key, and we are
5561 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005562 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 + " " + nextMotion);
5564 return null;
5565 }
5566 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005567 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 + " " + nextMotion);
5569 return target;
5570 }
5571 targetWin = (WindowState)target;
5572 }
Romain Guy06882f82009-06-10 13:36:04 -07005573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 // Now: is it okay to send the next event to this window?
5577 synchronized (this) {
5578 // First: did we come here based on the last window not
5579 // being null, but it changed by the time we got here?
5580 // If so, try again.
5581 if (!targetIsNew && mLastWin == null) {
5582 continue;
5583 }
Romain Guy06882f82009-06-10 13:36:04 -07005584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 // We never dispatch events if not finished with the
5586 // last one, or the display is frozen.
5587 if (mFinished && !mDisplayFrozen) {
5588 // If event dispatching is disabled, then we
5589 // just consume the events.
5590 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005591 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005592 "Skipping event; dispatching disabled: "
5593 + nextKey + " " + nextMotion);
5594 return null;
5595 }
5596 if (targetWin != null) {
5597 // If this is a new target, and that target is not
5598 // paused or unresponsive, then all looks good to
5599 // handle the event.
5600 if (targetIsNew && !targetWin.mToken.paused) {
5601 return targetWin;
5602 }
Romain Guy06882f82009-06-10 13:36:04 -07005603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 // If we didn't find a target window, and there is no
5605 // focused app window, then just eat the events.
5606 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005607 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 "Skipping event; no focused app: "
5609 + nextKey + " " + nextMotion);
5610 return null;
5611 }
5612 }
Romain Guy06882f82009-06-10 13:36:04 -07005613
Joe Onorato8a9b2202010-02-26 18:56:32 -08005614 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 TAG, "Waiting for last key in " + mLastBinder
5616 + " target=" + targetWin
5617 + " mFinished=" + mFinished
5618 + " mDisplayFrozen=" + mDisplayFrozen
5619 + " targetIsNew=" + targetIsNew
5620 + " paused="
5621 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005622 + " mFocusedApp=" + mFocusedApp
5623 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 targetApp = targetWin != null
5626 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 long curTimeout = keyDispatchingTimeout;
5629 if (mTimeToSwitch != 0) {
5630 long now = SystemClock.uptimeMillis();
5631 if (mTimeToSwitch <= now) {
5632 // If an app switch key has been pressed, and we have
5633 // waited too long for the current app to finish
5634 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005635 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 continue;
5637 }
5638 long switchTimeout = mTimeToSwitch - now;
5639 if (curTimeout > switchTimeout) {
5640 curTimeout = switchTimeout;
5641 }
5642 }
Romain Guy06882f82009-06-10 13:36:04 -07005643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 try {
5645 // after that continue
5646 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005647 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 TAG, "Waiting for key dispatch: " + curTimeout);
5649 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005650 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005652 + startTime + " switchTime=" + mTimeToSwitch
5653 + " target=" + targetWin + " mLW=" + mLastWin
5654 + " mLB=" + mLastBinder + " fin=" + mFinished
5655 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 } catch (InterruptedException e) {
5657 }
5658 }
5659
5660 // If we were frozen during configuration change, restart the
5661 // timeout checks from now; otherwise look at whether we timed
5662 // out before awakening.
5663 if (mWasFrozen) {
5664 waitedFor = 0;
5665 mWasFrozen = false;
5666 } else {
5667 waitedFor = SystemClock.uptimeMillis() - startTime;
5668 }
5669
5670 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5671 IApplicationToken at = null;
5672 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005673 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005675 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005677 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5678 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005679 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 // END NOSHIP
5681 //dump();
5682 if (targetWin != null) {
5683 at = targetWin.getAppToken();
5684 } else if (targetApp != null) {
5685 at = targetApp.appToken;
5686 }
5687 }
5688
5689 boolean abort = true;
5690 if (at != null) {
5691 try {
5692 long timeout = at.getKeyDispatchingTimeout();
5693 if (timeout > waitedFor) {
5694 // we did not wait the proper amount of time for this application.
5695 // set the timeout to be the real timeout and wait again.
5696 keyDispatchingTimeout = timeout - waitedFor;
5697 continue;
5698 } else {
5699 abort = at.keyDispatchingTimedOut();
5700 }
5701 } catch (RemoteException ex) {
5702 }
5703 }
5704
5705 synchronized (this) {
5706 if (abort && (mLastWin == targetWin || targetWin == null)) {
5707 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005708 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005709 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 "Window " + mLastWin +
5711 " timed out on key input");
5712 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005713 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 mLastWin.mToken.paused = false;
5715 }
5716 }
5717 if (mMotionTarget == targetWin) {
5718 mMotionTarget = null;
5719 }
5720 mLastWin = null;
5721 mLastBinder = null;
5722 if (failIfTimeout || targetWin == null) {
5723 return null;
5724 }
5725 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005726 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 startTime = SystemClock.uptimeMillis();
5728 }
5729 }
5730 }
5731 }
5732 }
Romain Guy06882f82009-06-10 13:36:04 -07005733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005735 MotionEvent nextMotion, boolean isPointerEvent,
5736 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 if (nextKey != null) {
5740 // Find the target window for a normal key event.
5741 final int keycode = nextKey.getKeyCode();
5742 final int repeatCount = nextKey.getRepeatCount();
5743 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5744 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005747 if (callingUid == 0 ||
5748 mContext.checkPermission(
5749 android.Manifest.permission.INJECT_EVENTS,
5750 callingPid, callingUid)
5751 == PackageManager.PERMISSION_GRANTED) {
5752 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005753 nextKey.getMetaState(), down, repeatCount,
5754 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005755 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005756 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 + nextKey);
5758 return SKIP_TARGET_TOKEN;
5759 }
Romain Guy06882f82009-06-10 13:36:04 -07005760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 WindowState focus = null;
5764 synchronized(mWindowMap) {
5765 focus = getFocusedWindowLocked();
5766 }
Romain Guy06882f82009-06-10 13:36:04 -07005767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005769
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005770 if (callingUid == 0 ||
5771 (focus != null && callingUid == focus.mSession.mUid) ||
5772 mContext.checkPermission(
5773 android.Manifest.permission.INJECT_EVENTS,
5774 callingPid, callingUid)
5775 == PackageManager.PERMISSION_GRANTED) {
5776 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005777 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005778 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005779 return CONSUMED_EVENT_TOKEN;
5780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 }
Romain Guy06882f82009-06-10 13:36:04 -07005782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 } else if (!isPointerEvent) {
5786 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5787 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005788 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 + nextMotion);
5790 return SKIP_TARGET_TOKEN;
5791 }
Romain Guy06882f82009-06-10 13:36:04 -07005792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 WindowState focus = null;
5794 synchronized(mWindowMap) {
5795 focus = getFocusedWindowLocked();
5796 }
Romain Guy06882f82009-06-10 13:36:04 -07005797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5799 return focus;
5800 }
Romain Guy06882f82009-06-10 13:36:04 -07005801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 if (nextMotion == null) {
5803 return SKIP_TARGET_TOKEN;
5804 }
Romain Guy06882f82009-06-10 13:36:04 -07005805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5807 KeyEvent.KEYCODE_UNKNOWN);
5808 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005809 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 + nextMotion);
5811 return SKIP_TARGET_TOKEN;
5812 }
Romain Guy06882f82009-06-10 13:36:04 -07005813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 // Find the target window for a pointer event.
5815 int action = nextMotion.getAction();
5816 final float xf = nextMotion.getX();
5817 final float yf = nextMotion.getY();
5818 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 final boolean screenWasOff = qev != null
5821 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 synchronized(mWindowMap) {
5826 synchronized (this) {
5827 if (action == MotionEvent.ACTION_DOWN) {
5828 if (mMotionTarget != null) {
5829 // this is weird, we got a pen down, but we thought it was
5830 // already down!
5831 // XXX: We should probably send an ACTION_UP to the current
5832 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005833 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 + mMotionTarget);
5835 mMotionTarget = null;
5836 }
Romain Guy06882f82009-06-10 13:36:04 -07005837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 // ACTION_DOWN is special, because we need to lock next events to
5839 // the window we'll land onto.
5840 final int x = (int)xf;
5841 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 final ArrayList windows = mWindows;
5844 final int N = windows.size();
5845 WindowState topErrWindow = null;
5846 final Rect tmpRect = mTempRect;
5847 for (int i=N-1; i>=0; i--) {
5848 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005849 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 final int flags = child.mAttrs.flags;
5851 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5852 if (topErrWindow == null) {
5853 topErrWindow = child;
5854 }
5855 }
5856 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005857 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 continue;
5859 }
5860 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005861 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 if ((flags & WindowManager.LayoutParams
5863 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5864 child.mNextOutsideTouch = mOutsideTouchTargets;
5865 mOutsideTouchTargets = child;
5866 }
5867 continue;
5868 }
5869 tmpRect.set(child.mFrame);
5870 if (child.mTouchableInsets == ViewTreeObserver
5871 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5872 // The touch is inside of the window if it is
5873 // inside the frame, AND the content part of that
5874 // frame that was given by the application.
5875 tmpRect.left += child.mGivenContentInsets.left;
5876 tmpRect.top += child.mGivenContentInsets.top;
5877 tmpRect.right -= child.mGivenContentInsets.right;
5878 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5879 } else if (child.mTouchableInsets == ViewTreeObserver
5880 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5881 // The touch is inside of the window if it is
5882 // inside the frame, AND the visible part of that
5883 // frame that was given by the application.
5884 tmpRect.left += child.mGivenVisibleInsets.left;
5885 tmpRect.top += child.mGivenVisibleInsets.top;
5886 tmpRect.right -= child.mGivenVisibleInsets.right;
5887 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5888 }
5889 final int touchFlags = flags &
5890 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5891 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5892 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005893 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 if (!screenWasOff || (flags &
5895 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5896 mMotionTarget = child;
5897 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005898 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 mMotionTarget = null;
5900 }
5901 break;
5902 }
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 if ((flags & WindowManager.LayoutParams
5905 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5906 child.mNextOutsideTouch = mOutsideTouchTargets;
5907 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005908 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 }
5910 }
5911
5912 // if there's an error window but it's not accepting
5913 // focus (typically because it is not yet visible) just
5914 // wait for it -- any other focused window may in fact
5915 // be in ANR state.
5916 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5917 mMotionTarget = null;
5918 }
5919 }
Romain Guy06882f82009-06-10 13:36:04 -07005920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 target = mMotionTarget;
5922 }
5923 }
Romain Guy06882f82009-06-10 13:36:04 -07005924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 // Pointer events are a little different -- if there isn't a
5928 // target found for any event, then just drop it.
5929 return target != null ? target : SKIP_TARGET_TOKEN;
5930 }
Romain Guy06882f82009-06-10 13:36:04 -07005931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 boolean checkShouldDispatchKey(int keycode) {
5933 synchronized (this) {
5934 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5935 mTimeToSwitch = 0;
5936 return true;
5937 }
5938 if (mTimeToSwitch != 0
5939 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5940 return false;
5941 }
5942 return true;
5943 }
5944 }
Romain Guy06882f82009-06-10 13:36:04 -07005945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 void bindTargetWindowLocked(WindowState win,
5947 int pendingWhat, QueuedEvent pendingMotion) {
5948 synchronized (this) {
5949 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
5950 }
5951 }
Romain Guy06882f82009-06-10 13:36:04 -07005952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 void bindTargetWindowLocked(WindowState win) {
5954 synchronized (this) {
5955 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
5956 }
5957 }
5958
5959 void bindTargetWindowLockedLocked(WindowState win,
5960 int pendingWhat, QueuedEvent pendingMotion) {
5961 mLastWin = win;
5962 mLastBinder = win.mClient.asBinder();
5963 mFinished = false;
5964 if (pendingMotion != null) {
5965 final Session s = win.mSession;
5966 if (pendingWhat == RETURN_PENDING_POINTER) {
5967 releasePendingPointerLocked(s);
5968 s.mPendingPointerMove = pendingMotion;
5969 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005970 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 "bindTargetToWindow " + s.mPendingPointerMove);
5972 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
5973 releasePendingTrackballLocked(s);
5974 s.mPendingTrackballMove = pendingMotion;
5975 s.mPendingTrackballWindow = win;
5976 }
5977 }
5978 }
Romain Guy06882f82009-06-10 13:36:04 -07005979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005981 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 "releasePendingPointer " + s.mPendingPointerMove);
5983 if (s.mPendingPointerMove != null) {
5984 mQueue.recycleEvent(s.mPendingPointerMove);
5985 s.mPendingPointerMove = null;
5986 }
5987 }
Romain Guy06882f82009-06-10 13:36:04 -07005988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 void releasePendingTrackballLocked(Session s) {
5990 if (s.mPendingTrackballMove != null) {
5991 mQueue.recycleEvent(s.mPendingTrackballMove);
5992 s.mPendingTrackballMove = null;
5993 }
5994 }
Romain Guy06882f82009-06-10 13:36:04 -07005995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 MotionEvent finishedKey(Session session, IWindow client, boolean force,
5997 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005998 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 TAG, "finishedKey: client=" + client + ", force=" + force);
6000
6001 if (client == null) {
6002 return null;
6003 }
6004
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006005 MotionEvent res = null;
6006 QueuedEvent qev = null;
6007 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006010 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 TAG, "finishedKey: client=" + client.asBinder()
6012 + ", force=" + force + ", last=" + mLastBinder
6013 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 if (returnWhat == RETURN_PENDING_POINTER) {
6016 qev = session.mPendingPointerMove;
6017 win = session.mPendingPointerWindow;
6018 session.mPendingPointerMove = null;
6019 session.mPendingPointerWindow = null;
6020 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6021 qev = session.mPendingTrackballMove;
6022 win = session.mPendingTrackballWindow;
6023 session.mPendingTrackballMove = null;
6024 session.mPendingTrackballWindow = null;
6025 }
Romain Guy06882f82009-06-10 13:36:04 -07006026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006028 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 TAG, "finishedKey: last paused="
6030 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6031 if (mLastWin != null && (!mLastWin.mToken.paused || force
6032 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006033 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 } else {
6035 // Make sure to wake up anyone currently waiting to
6036 // dispatch a key, so they can re-evaluate their
6037 // current situation.
6038 mFinished = true;
6039 notifyAll();
6040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
Romain Guy06882f82009-06-10 13:36:04 -07006042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006044 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006045 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 "Returning pending motion: " + res);
6047 mQueue.recycleEvent(qev);
6048 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6049 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006053
Christopher Tate2624fbc2009-12-11 12:11:31 -08006054 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6055 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006056 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006057 }
6058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006059
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006060 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 }
6062
6063 void tickle() {
6064 synchronized (this) {
6065 notifyAll();
6066 }
6067 }
Romain Guy06882f82009-06-10 13:36:04 -07006068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 void handleNewWindowLocked(WindowState newWindow) {
6070 if (!newWindow.canReceiveKeys()) {
6071 return;
6072 }
6073 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006074 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 TAG, "New key dispatch window: win="
6076 + newWindow.mClient.asBinder()
6077 + ", last=" + mLastBinder
6078 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6079 + "), finished=" + mFinished + ", paused="
6080 + newWindow.mToken.paused);
6081
6082 // Displaying a window implicitly causes dispatching to
6083 // be unpaused. (This is to protect against bugs if someone
6084 // pauses dispatching but forgets to resume.)
6085 newWindow.mToken.paused = false;
6086
6087 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088
6089 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006090 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 "New SYSTEM_ERROR window; resetting state");
6092 mLastWin = null;
6093 mLastBinder = null;
6094 mMotionTarget = null;
6095 mFinished = true;
6096 } else if (mLastWin != null) {
6097 // If the new window is above the window we are
6098 // waiting on, then stop waiting and let key dispatching
6099 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006100 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 TAG, "Last win layer=" + mLastWin.mLayer
6102 + ", new win layer=" + newWindow.mLayer);
6103 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006104 // The new window is above the old; finish pending input to the last
6105 // window and start directing it to the new one.
6106 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006107 doFinishedKeyLocked(false); // does a notifyAll()
6108 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 }
6110 }
6111
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006112 // Now that we've put a new window state in place, make the event waiter
6113 // take notice and retarget its attentions.
6114 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 }
6116 }
6117
6118 void pauseDispatchingLocked(WindowToken token) {
6119 synchronized (this)
6120 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006121 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 token.paused = true;
6123
6124 /*
6125 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6126 mPaused = true;
6127 } else {
6128 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006129 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006131 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006133 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 }
6135 }
6136 */
6137 }
6138 }
6139
6140 void resumeDispatchingLocked(WindowToken token) {
6141 synchronized (this) {
6142 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006143 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 TAG, "Resuming WindowToken " + token
6145 + ", last=" + mLastBinder
6146 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6147 + "), finished=" + mFinished + ", paused="
6148 + token.paused);
6149 token.paused = false;
6150 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006151 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 } else {
6153 notifyAll();
6154 }
6155 }
6156 }
6157 }
6158
6159 void setEventDispatchingLocked(boolean enabled) {
6160 synchronized (this) {
6161 mEventDispatching = enabled;
6162 notifyAll();
6163 }
6164 }
Romain Guy06882f82009-06-10 13:36:04 -07006165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 void appSwitchComing() {
6167 synchronized (this) {
6168 // Don't wait for more than .5 seconds for app to finish
6169 // processing the pending events.
6170 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006171 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6173 mTimeToSwitch = now;
6174 }
6175 notifyAll();
6176 }
6177 }
Romain Guy06882f82009-06-10 13:36:04 -07006178
Christopher Tate136b1f92010-02-11 17:51:24 -08006179 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 if (mLastWin != null) {
6181 releasePendingPointerLocked(mLastWin.mSession);
6182 releasePendingTrackballLocked(mLastWin.mSession);
6183 }
Romain Guy06882f82009-06-10 13:36:04 -07006184
Christopher Tate136b1f92010-02-11 17:51:24 -08006185 if (force || mLastWin == null || !mLastWin.mToken.paused
6186 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 // If the current window has been paused, we aren't -really-
6188 // finished... so let the waiters still wait.
6189 mLastWin = null;
6190 mLastBinder = null;
6191 }
6192 mFinished = true;
6193 notifyAll();
6194 }
6195 }
6196
6197 private class KeyQ extends KeyInputQueue
6198 implements KeyInputQueue.FilterCallback {
6199 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006202 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6204 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6205 "KEEP_SCREEN_ON_FLAG");
6206 mHoldingScreen.setReferenceCounted(false);
6207 }
6208
6209 @Override
6210 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6211 if (mPolicy.preprocessInputEventTq(event)) {
6212 return true;
6213 }
Romain Guy06882f82009-06-10 13:36:04 -07006214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 switch (event.type) {
6216 case RawInputEvent.EV_KEY: {
6217 // XXX begin hack
6218 if (DEBUG) {
6219 if (event.keycode == KeyEvent.KEYCODE_G) {
6220 if (event.value != 0) {
6221 // G down
6222 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6223 }
6224 return false;
6225 }
6226 if (event.keycode == KeyEvent.KEYCODE_D) {
6227 if (event.value != 0) {
6228 //dump();
6229 }
6230 return false;
6231 }
6232 }
6233 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006234
Charles Mendis322591c2009-10-29 11:06:59 -07006235 boolean screenIsOff = !mPowerManager.isScreenOn();
6236 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6240 mPowerManager.goToSleep(event.when);
6241 }
6242
6243 if (screenIsOff) {
6244 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6245 }
6246 if (screenIsDim) {
6247 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6248 }
6249 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6250 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006251 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 }
Romain Guy06882f82009-06-10 13:36:04 -07006253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6255 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6256 filterQueue(this);
6257 mKeyWaiter.appSwitchComing();
6258 }
6259 return true;
6260 } else {
6261 return false;
6262 }
6263 }
Romain Guy06882f82009-06-10 13:36:04 -07006264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006266 boolean screenIsOff = !mPowerManager.isScreenOn();
6267 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 if (screenIsOff) {
6269 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6270 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006271 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 return false;
6273 }
6274 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6275 }
6276 if (screenIsDim) {
6277 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6278 }
6279 return true;
6280 }
Romain Guy06882f82009-06-10 13:36:04 -07006281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006283 boolean screenIsOff = !mPowerManager.isScreenOn();
6284 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 if (screenIsOff) {
6286 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6287 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006288 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 return false;
6290 }
6291 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6292 }
6293 if (screenIsDim) {
6294 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6295 }
6296 return true;
6297 }
Romain Guy06882f82009-06-10 13:36:04 -07006298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 default:
6300 return true;
6301 }
6302 }
6303
6304 public int filterEvent(QueuedEvent ev) {
6305 switch (ev.classType) {
6306 case RawInputEvent.CLASS_KEYBOARD:
6307 KeyEvent ke = (KeyEvent)ev.event;
6308 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006309 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 + ke.getKeyCode() + ", action=" + ke.getAction());
6311 return FILTER_REMOVE;
6312 }
6313 return FILTER_ABORT;
6314 default:
6315 return FILTER_KEEP;
6316 }
6317 }
Romain Guy06882f82009-06-10 13:36:04 -07006318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 /**
6320 * Must be called with the main window manager lock held.
6321 */
6322 void setHoldScreenLocked(boolean holding) {
6323 boolean state = mHoldingScreen.isHeld();
6324 if (holding != state) {
6325 if (holding) {
6326 mHoldingScreen.acquire();
6327 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006328 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 mHoldingScreen.release();
6330 }
6331 }
6332 }
Michael Chan53071d62009-05-13 17:29:48 -07006333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334
6335 public boolean detectSafeMode() {
6336 mSafeMode = mPolicy.detectSafeMode();
6337 return mSafeMode;
6338 }
Romain Guy06882f82009-06-10 13:36:04 -07006339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 public void systemReady() {
6341 mPolicy.systemReady();
6342 }
Romain Guy06882f82009-06-10 13:36:04 -07006343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 private final class InputDispatcherThread extends Thread {
6345 // Time to wait when there is nothing to do: 9999 seconds.
6346 static final int LONG_WAIT=9999*1000;
6347
6348 public InputDispatcherThread() {
6349 super("InputDispatcher");
6350 }
Romain Guy06882f82009-06-10 13:36:04 -07006351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 @Override
6353 public void run() {
6354 while (true) {
6355 try {
6356 process();
6357 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006358 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 }
6360 }
6361 }
Romain Guy06882f82009-06-10 13:36:04 -07006362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 private void process() {
6364 android.os.Process.setThreadPriority(
6365 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 // The last key event we saw
6368 KeyEvent lastKey = null;
6369
6370 // Last keydown time for auto-repeating keys
6371 long lastKeyTime = SystemClock.uptimeMillis();
6372 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006373 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374
Romain Guy06882f82009-06-10 13:36:04 -07006375 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 int keyRepeatCount = 0;
6377
6378 // Need to report that configuration has changed?
6379 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 while (true) {
6382 long curTime = SystemClock.uptimeMillis();
6383
Joe Onorato8a9b2202010-02-26 18:56:32 -08006384 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 TAG, "Waiting for next key: now=" + curTime
6386 + ", repeat @ " + nextKeyTime);
6387
6388 // Retrieve next event, waiting only as long as the next
6389 // repeat timeout. If the configuration has changed, then
6390 // don't wait at all -- we'll report the change as soon as
6391 // we have processed all events.
6392 QueuedEvent ev = mQueue.getEvent(
6393 (int)((!configChanged && curTime < nextKeyTime)
6394 ? (nextKeyTime-curTime) : 0));
6395
Joe Onorato8a9b2202010-02-26 18:56:32 -08006396 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6398
Michael Chan53071d62009-05-13 17:29:48 -07006399 if (MEASURE_LATENCY) {
6400 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6401 }
6402
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006403 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6404 // cancel key repeat at the request of the policy.
6405 lastKey = null;
6406 downTime = 0;
6407 lastKeyTime = curTime;
6408 nextKeyTime = curTime + LONG_WAIT;
6409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 try {
6411 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006412 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413 int eventType;
6414 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6415 eventType = eventType((MotionEvent)ev.event);
6416 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6417 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6418 eventType = LocalPowerManager.BUTTON_EVENT;
6419 } else {
6420 eventType = LocalPowerManager.OTHER_EVENT;
6421 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006422 try {
Michael Chan53071d62009-05-13 17:29:48 -07006423 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006424 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006425 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006426 mBatteryStats.noteInputEvent();
6427 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006428 } catch (RemoteException e) {
6429 // Ignore
6430 }
Michael Chane10de972009-05-18 11:24:50 -07006431
Mike Lockwood5db42402009-11-30 14:51:51 -05006432 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6433 // do not wake screen in this case
6434 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006435 && eventType != LONG_TOUCH_EVENT
6436 && eventType != CHEEK_EVENT) {
6437 mPowerManager.userActivity(curTime, false,
6438 eventType, false);
6439 } else if (mLastTouchEventType != eventType
6440 || (curTime - mLastUserActivityCallTime)
6441 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6442 mLastUserActivityCallTime = curTime;
6443 mLastTouchEventType = eventType;
6444 mPowerManager.userActivity(curTime, false,
6445 eventType, false);
6446 }
6447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 switch (ev.classType) {
6449 case RawInputEvent.CLASS_KEYBOARD:
6450 KeyEvent ke = (KeyEvent)ev.event;
6451 if (ke.isDown()) {
6452 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006453 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 keyRepeatCount = 0;
6455 lastKeyTime = curTime;
6456 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006457 + ViewConfiguration.getLongPressTimeout();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006458 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 TAG, "Received key down: first repeat @ "
6460 + nextKeyTime);
6461 } else {
6462 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006463 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 // Arbitrary long timeout.
6465 lastKeyTime = curTime;
6466 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006467 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 TAG, "Received key up: ignore repeat @ "
6469 + nextKeyTime);
6470 }
6471 dispatchKey((KeyEvent)ev.event, 0, 0);
6472 mQueue.recycleEvent(ev);
6473 break;
6474 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006475 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6477 break;
6478 case RawInputEvent.CLASS_TRACKBALL:
6479 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6480 break;
6481 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6482 configChanged = true;
6483 break;
6484 default:
6485 mQueue.recycleEvent(ev);
6486 break;
6487 }
Romain Guy06882f82009-06-10 13:36:04 -07006488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 } else if (configChanged) {
6490 configChanged = false;
6491 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 } else if (lastKey != null) {
6494 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 // Timeout occurred while key was down. If it is at or
6497 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006498 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 TAG, "Key timeout: repeat=" + nextKeyTime
6500 + ", now=" + curTime);
6501 if (curTime < nextKeyTime) {
6502 continue;
6503 }
Romain Guy06882f82009-06-10 13:36:04 -07006504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 lastKeyTime = nextKeyTime;
6506 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6507 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006508 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 TAG, "Key repeat: count=" + keyRepeatCount
6510 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006511 KeyEvent newEvent;
6512 if (downTime != 0 && (downTime
6513 + ViewConfiguration.getLongPressTimeout())
6514 <= curTime) {
6515 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6516 curTime, keyRepeatCount,
6517 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6518 downTime = 0;
6519 } else {
6520 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6521 curTime, keyRepeatCount);
6522 }
6523 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 } else {
6526 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 lastKeyTime = curTime;
6529 nextKeyTime = curTime + LONG_WAIT;
6530 }
Romain Guy06882f82009-06-10 13:36:04 -07006531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006533 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 "Input thread received uncaught exception: " + e, e);
6535 }
6536 }
6537 }
6538 }
6539
6540 // -------------------------------------------------------------
6541 // Client Session State
6542 // -------------------------------------------------------------
6543
6544 private final class Session extends IWindowSession.Stub
6545 implements IBinder.DeathRecipient {
6546 final IInputMethodClient mClient;
6547 final IInputContext mInputContext;
6548 final int mUid;
6549 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006550 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 SurfaceSession mSurfaceSession;
6552 int mNumWindow = 0;
6553 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 /**
6556 * Current pointer move event being dispatched to client window... must
6557 * hold key lock to access.
6558 */
6559 QueuedEvent mPendingPointerMove;
6560 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 /**
6563 * Current trackball move event being dispatched to client window... must
6564 * hold key lock to access.
6565 */
6566 QueuedEvent mPendingTrackballMove;
6567 WindowState mPendingTrackballWindow;
6568
6569 public Session(IInputMethodClient client, IInputContext inputContext) {
6570 mClient = client;
6571 mInputContext = inputContext;
6572 mUid = Binder.getCallingUid();
6573 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006574 StringBuilder sb = new StringBuilder();
6575 sb.append("Session{");
6576 sb.append(Integer.toHexString(System.identityHashCode(this)));
6577 sb.append(" uid ");
6578 sb.append(mUid);
6579 sb.append("}");
6580 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 synchronized (mWindowMap) {
6583 if (mInputMethodManager == null && mHaveInputMethods) {
6584 IBinder b = ServiceManager.getService(
6585 Context.INPUT_METHOD_SERVICE);
6586 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6587 }
6588 }
6589 long ident = Binder.clearCallingIdentity();
6590 try {
6591 // Note: it is safe to call in to the input method manager
6592 // here because we are not holding our lock.
6593 if (mInputMethodManager != null) {
6594 mInputMethodManager.addClient(client, inputContext,
6595 mUid, mPid);
6596 } else {
6597 client.setUsingInputMethod(false);
6598 }
6599 client.asBinder().linkToDeath(this, 0);
6600 } catch (RemoteException e) {
6601 // The caller has died, so we can just forget about this.
6602 try {
6603 if (mInputMethodManager != null) {
6604 mInputMethodManager.removeClient(client);
6605 }
6606 } catch (RemoteException ee) {
6607 }
6608 } finally {
6609 Binder.restoreCallingIdentity(ident);
6610 }
6611 }
Romain Guy06882f82009-06-10 13:36:04 -07006612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 @Override
6614 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6615 throws RemoteException {
6616 try {
6617 return super.onTransact(code, data, reply, flags);
6618 } catch (RuntimeException e) {
6619 // Log all 'real' exceptions thrown to the caller
6620 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006621 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 }
6623 throw e;
6624 }
6625 }
6626
6627 public void binderDied() {
6628 // Note: it is safe to call in to the input method manager
6629 // here because we are not holding our lock.
6630 try {
6631 if (mInputMethodManager != null) {
6632 mInputMethodManager.removeClient(mClient);
6633 }
6634 } catch (RemoteException e) {
6635 }
6636 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006637 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 mClientDead = true;
6639 killSessionLocked();
6640 }
6641 }
6642
6643 public int add(IWindow window, WindowManager.LayoutParams attrs,
6644 int viewVisibility, Rect outContentInsets) {
6645 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6646 }
Romain Guy06882f82009-06-10 13:36:04 -07006647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 public void remove(IWindow window) {
6649 removeWindow(this, window);
6650 }
Romain Guy06882f82009-06-10 13:36:04 -07006651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6653 int requestedWidth, int requestedHeight, int viewFlags,
6654 boolean insetsPending, Rect outFrame, Rect outContentInsets,
6655 Rect outVisibleInsets, Surface outSurface) {
6656 return relayoutWindow(this, window, attrs,
6657 requestedWidth, requestedHeight, viewFlags, insetsPending,
6658 outFrame, outContentInsets, outVisibleInsets, outSurface);
6659 }
Romain Guy06882f82009-06-10 13:36:04 -07006660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 public void setTransparentRegion(IWindow window, Region region) {
6662 setTransparentRegionWindow(this, window, region);
6663 }
Romain Guy06882f82009-06-10 13:36:04 -07006664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 public void setInsets(IWindow window, int touchableInsets,
6666 Rect contentInsets, Rect visibleInsets) {
6667 setInsetsWindow(this, window, touchableInsets, contentInsets,
6668 visibleInsets);
6669 }
Romain Guy06882f82009-06-10 13:36:04 -07006670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6672 getWindowDisplayFrame(this, window, outDisplayFrame);
6673 }
Romain Guy06882f82009-06-10 13:36:04 -07006674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006676 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006677 TAG, "IWindow finishDrawing called for " + window);
6678 finishDrawingWindow(this, window);
6679 }
6680
6681 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006682 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 TAG, "IWindow finishKey called for " + window);
6684 mKeyWaiter.finishedKey(this, window, false,
6685 KeyWaiter.RETURN_NOTHING);
6686 }
6687
6688 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006689 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 TAG, "IWindow getPendingMotionEvent called for " + window);
6691 return mKeyWaiter.finishedKey(this, window, false,
6692 KeyWaiter.RETURN_PENDING_POINTER);
6693 }
Romain Guy06882f82009-06-10 13:36:04 -07006694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006696 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006697 TAG, "IWindow getPendingMotionEvent called for " + window);
6698 return mKeyWaiter.finishedKey(this, window, false,
6699 KeyWaiter.RETURN_PENDING_TRACKBALL);
6700 }
6701
6702 public void setInTouchMode(boolean mode) {
6703 synchronized(mWindowMap) {
6704 mInTouchMode = mode;
6705 }
6706 }
6707
6708 public boolean getInTouchMode() {
6709 synchronized(mWindowMap) {
6710 return mInTouchMode;
6711 }
6712 }
6713
6714 public boolean performHapticFeedback(IWindow window, int effectId,
6715 boolean always) {
6716 synchronized(mWindowMap) {
6717 long ident = Binder.clearCallingIdentity();
6718 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006719 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006720 windowForClientLocked(this, window, true),
6721 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 } finally {
6723 Binder.restoreCallingIdentity(ident);
6724 }
6725 }
6726 }
Romain Guy06882f82009-06-10 13:36:04 -07006727
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006728 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006729 synchronized(mWindowMap) {
6730 long ident = Binder.clearCallingIdentity();
6731 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006732 setWindowWallpaperPositionLocked(
6733 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006734 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006735 } finally {
6736 Binder.restoreCallingIdentity(ident);
6737 }
6738 }
6739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006740
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006741 public void wallpaperOffsetsComplete(IBinder window) {
6742 WindowManagerService.this.wallpaperOffsetsComplete(window);
6743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006744
Dianne Hackborn75804932009-10-20 20:15:20 -07006745 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6746 int z, Bundle extras, boolean sync) {
6747 synchronized(mWindowMap) {
6748 long ident = Binder.clearCallingIdentity();
6749 try {
6750 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006751 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006752 action, x, y, z, extras, sync);
6753 } finally {
6754 Binder.restoreCallingIdentity(ident);
6755 }
6756 }
6757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006758
Dianne Hackborn75804932009-10-20 20:15:20 -07006759 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6760 WindowManagerService.this.wallpaperCommandComplete(window, result);
6761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006763 void windowAddedLocked() {
6764 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006765 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 TAG, "First window added to " + this + ", creating SurfaceSession");
6767 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006768 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006769 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 mSessions.add(this);
6771 }
6772 mNumWindow++;
6773 }
6774
6775 void windowRemovedLocked() {
6776 mNumWindow--;
6777 killSessionLocked();
6778 }
Romain Guy06882f82009-06-10 13:36:04 -07006779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 void killSessionLocked() {
6781 if (mNumWindow <= 0 && mClientDead) {
6782 mSessions.remove(this);
6783 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006784 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 TAG, "Last window removed from " + this
6786 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006787 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006788 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 try {
6790 mSurfaceSession.kill();
6791 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006792 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 + mSurfaceSession + " in session " + this
6794 + ": " + e.toString());
6795 }
6796 mSurfaceSession = null;
6797 }
6798 }
6799 }
Romain Guy06882f82009-06-10 13:36:04 -07006800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006802 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6803 pw.print(" mClientDead="); pw.print(mClientDead);
6804 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6805 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6806 pw.print(prefix);
6807 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6808 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6809 }
6810 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6811 pw.print(prefix);
6812 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6813 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 }
6816
6817 @Override
6818 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006819 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 }
6821 }
6822
6823 // -------------------------------------------------------------
6824 // Client Window State
6825 // -------------------------------------------------------------
6826
6827 private final class WindowState implements WindowManagerPolicy.WindowState {
6828 final Session mSession;
6829 final IWindow mClient;
6830 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006831 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 AppWindowToken mAppToken;
6833 AppWindowToken mTargetAppToken;
6834 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6835 final DeathRecipient mDeathRecipient;
6836 final WindowState mAttachedWindow;
6837 final ArrayList mChildWindows = new ArrayList();
6838 final int mBaseLayer;
6839 final int mSubLayer;
6840 final boolean mLayoutAttached;
6841 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006842 final boolean mIsWallpaper;
6843 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 int mViewVisibility;
6845 boolean mPolicyVisibility = true;
6846 boolean mPolicyVisibilityAfterAnim = true;
6847 boolean mAppFreezing;
6848 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006849 boolean mReportDestroySurface;
6850 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 boolean mAttachedHidden; // is our parent window hidden?
6852 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006853 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006854 int mRequestedWidth;
6855 int mRequestedHeight;
6856 int mLastRequestedWidth;
6857 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 int mLayer;
6859 int mAnimLayer;
6860 int mLastLayer;
6861 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006862 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006863 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864
6865 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006866
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006867 int mLayoutSeq = -1;
6868
6869 Configuration mConfiguration = null;
6870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 // Actual frame shown on-screen (may be modified by animation)
6872 final Rect mShownFrame = new Rect();
6873 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 /**
6876 * Insets that determine the actually visible area
6877 */
6878 final Rect mVisibleInsets = new Rect();
6879 final Rect mLastVisibleInsets = new Rect();
6880 boolean mVisibleInsetsChanged;
6881
6882 /**
6883 * Insets that are covered by system windows
6884 */
6885 final Rect mContentInsets = new Rect();
6886 final Rect mLastContentInsets = new Rect();
6887 boolean mContentInsetsChanged;
6888
6889 /**
6890 * Set to true if we are waiting for this window to receive its
6891 * given internal insets before laying out other windows based on it.
6892 */
6893 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 /**
6896 * These are the content insets that were given during layout for
6897 * this window, to be applied to windows behind it.
6898 */
6899 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 /**
6902 * These are the visible insets that were given during layout for
6903 * this window, to be applied to windows behind it.
6904 */
6905 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 /**
6908 * Flag indicating whether the touchable region should be adjusted by
6909 * the visible insets; if false the area outside the visible insets is
6910 * NOT touchable, so we must use those to adjust the frame during hit
6911 * tests.
6912 */
6913 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 // Current transformation being applied.
6916 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6917 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6918 float mHScale=1, mVScale=1;
6919 float mLastHScale=1, mLastVScale=1;
6920 final Matrix mTmpMatrix = new Matrix();
6921
6922 // "Real" frame that the application sees.
6923 final Rect mFrame = new Rect();
6924 final Rect mLastFrame = new Rect();
6925
6926 final Rect mContainingFrame = new Rect();
6927 final Rect mDisplayFrame = new Rect();
6928 final Rect mContentFrame = new Rect();
6929 final Rect mVisibleFrame = new Rect();
6930
6931 float mShownAlpha = 1;
6932 float mAlpha = 1;
6933 float mLastAlpha = 1;
6934
6935 // Set to true if, when the window gets displayed, it should perform
6936 // an enter animation.
6937 boolean mEnterAnimationPending;
6938
6939 // Currently running animation.
6940 boolean mAnimating;
6941 boolean mLocalAnimating;
6942 Animation mAnimation;
6943 boolean mAnimationIsEntrance;
6944 boolean mHasTransformation;
6945 boolean mHasLocalTransformation;
6946 final Transformation mTransformation = new Transformation();
6947
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006948 // If a window showing a wallpaper: the requested offset for the
6949 // wallpaper; if a wallpaper window: the currently applied offset.
6950 float mWallpaperX = -1;
6951 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006952
6953 // If a window showing a wallpaper: what fraction of the offset
6954 // range corresponds to a full virtual screen.
6955 float mWallpaperXStep = -1;
6956 float mWallpaperYStep = -1;
6957
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006958 // Wallpaper windows: pixels offset based on above variables.
6959 int mXOffset;
6960 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 // This is set after IWindowSession.relayout() has been called at
6963 // least once for the window. It allows us to detect the situation
6964 // where we don't yet have a surface, but should have one soon, so
6965 // we can give the window focus before waiting for the relayout.
6966 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07006967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 // This is set after the Surface has been created but before the
6969 // window has been drawn. During this time the surface is hidden.
6970 boolean mDrawPending;
6971
6972 // This is set after the window has finished drawing for the first
6973 // time but before its surface is shown. The surface will be
6974 // displayed when the next layout is run.
6975 boolean mCommitDrawPending;
6976
6977 // This is set during the time after the window's drawing has been
6978 // committed, and before its surface is actually shown. It is used
6979 // to delay showing the surface until all windows in a token are ready
6980 // to be shown.
6981 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07006982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 // Set when the window has been shown in the screen the first time.
6984 boolean mHasDrawn;
6985
6986 // Currently running an exit animation?
6987 boolean mExiting;
6988
6989 // Currently on the mDestroySurface list?
6990 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07006991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 // Completely remove from window manager after exit animation?
6993 boolean mRemoveOnExit;
6994
6995 // Set when the orientation is changing and this window has not yet
6996 // been updated for the new orientation.
6997 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07006998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 // Is this window now (or just being) removed?
7000 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 WindowState(Session s, IWindow c, WindowToken token,
7003 WindowState attachedWindow, WindowManager.LayoutParams a,
7004 int viewVisibility) {
7005 mSession = s;
7006 mClient = c;
7007 mToken = token;
7008 mAttrs.copyFrom(a);
7009 mViewVisibility = viewVisibility;
7010 DeathRecipient deathRecipient = new DeathRecipient();
7011 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007012 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 TAG, "Window " + this + " client=" + c.asBinder()
7014 + " token=" + token + " (" + mAttrs.token + ")");
7015 try {
7016 c.asBinder().linkToDeath(deathRecipient, 0);
7017 } catch (RemoteException e) {
7018 mDeathRecipient = null;
7019 mAttachedWindow = null;
7020 mLayoutAttached = false;
7021 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007022 mIsWallpaper = false;
7023 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 mBaseLayer = 0;
7025 mSubLayer = 0;
7026 return;
7027 }
7028 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7031 mAttrs.type <= LAST_SUB_WINDOW)) {
7032 // The multiplier here is to reserve space for multiple
7033 // windows in the same type layer.
7034 mBaseLayer = mPolicy.windowTypeToLayerLw(
7035 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7036 + TYPE_LAYER_OFFSET;
7037 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7038 mAttachedWindow = attachedWindow;
7039 mAttachedWindow.mChildWindows.add(this);
7040 mLayoutAttached = mAttrs.type !=
7041 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7042 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7043 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007044 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7045 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 } else {
7047 // The multiplier here is to reserve space for multiple
7048 // windows in the same type layer.
7049 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7050 * TYPE_LAYER_MULTIPLIER
7051 + TYPE_LAYER_OFFSET;
7052 mSubLayer = 0;
7053 mAttachedWindow = null;
7054 mLayoutAttached = false;
7055 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7056 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007057 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7058 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 }
7060
7061 WindowState appWin = this;
7062 while (appWin.mAttachedWindow != null) {
7063 appWin = mAttachedWindow;
7064 }
7065 WindowToken appToken = appWin.mToken;
7066 while (appToken.appWindowToken == null) {
7067 WindowToken parent = mTokenMap.get(appToken.token);
7068 if (parent == null || appToken == parent) {
7069 break;
7070 }
7071 appToken = parent;
7072 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007073 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 mAppToken = appToken.appWindowToken;
7075
7076 mSurface = null;
7077 mRequestedWidth = 0;
7078 mRequestedHeight = 0;
7079 mLastRequestedWidth = 0;
7080 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007081 mXOffset = 0;
7082 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 mLayer = 0;
7084 mAnimLayer = 0;
7085 mLastLayer = 0;
7086 }
7087
7088 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007089 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 TAG, "Attaching " + this + " token=" + mToken
7091 + ", list=" + mToken.windows);
7092 mSession.windowAddedLocked();
7093 }
7094
7095 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7096 mHaveFrame = true;
7097
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007098 final Rect container = mContainingFrame;
7099 container.set(pf);
7100
7101 final Rect display = mDisplayFrame;
7102 display.set(df);
7103
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007104 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007105 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007106 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7107 display.intersect(mCompatibleScreenFrame);
7108 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007109 }
7110
7111 final int pw = container.right - container.left;
7112 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113
7114 int w,h;
7115 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7116 w = mAttrs.width < 0 ? pw : mAttrs.width;
7117 h = mAttrs.height< 0 ? ph : mAttrs.height;
7118 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007119 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7120 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 }
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 final Rect content = mContentFrame;
7124 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007126 final Rect visible = mVisibleFrame;
7127 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007130 final int fw = frame.width();
7131 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7134 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7135
7136 Gravity.apply(mAttrs.gravity, w, h, container,
7137 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7138 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7139
7140 //System.out.println("Out: " + mFrame);
7141
7142 // Now make sure the window fits in the overall display.
7143 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 // Make sure the content and visible frames are inside of the
7146 // final window frame.
7147 if (content.left < frame.left) content.left = frame.left;
7148 if (content.top < frame.top) content.top = frame.top;
7149 if (content.right > frame.right) content.right = frame.right;
7150 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7151 if (visible.left < frame.left) visible.left = frame.left;
7152 if (visible.top < frame.top) visible.top = frame.top;
7153 if (visible.right > frame.right) visible.right = frame.right;
7154 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 final Rect contentInsets = mContentInsets;
7157 contentInsets.left = content.left-frame.left;
7158 contentInsets.top = content.top-frame.top;
7159 contentInsets.right = frame.right-content.right;
7160 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 final Rect visibleInsets = mVisibleInsets;
7163 visibleInsets.left = visible.left-frame.left;
7164 visibleInsets.top = visible.top-frame.top;
7165 visibleInsets.right = frame.right-visible.right;
7166 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007167
Dianne Hackborn284ac932009-08-28 10:34:25 -07007168 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7169 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007170 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173 if (localLOGV) {
7174 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7175 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007176 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 + mRequestedWidth + ", mRequestedheight="
7178 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7179 + "): frame=" + mFrame.toShortString()
7180 + " ci=" + contentInsets.toShortString()
7181 + " vi=" + visibleInsets.toShortString());
7182 //}
7183 }
7184 }
Romain Guy06882f82009-06-10 13:36:04 -07007185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 public Rect getFrameLw() {
7187 return mFrame;
7188 }
7189
7190 public Rect getShownFrameLw() {
7191 return mShownFrame;
7192 }
7193
7194 public Rect getDisplayFrameLw() {
7195 return mDisplayFrame;
7196 }
7197
7198 public Rect getContentFrameLw() {
7199 return mContentFrame;
7200 }
7201
7202 public Rect getVisibleFrameLw() {
7203 return mVisibleFrame;
7204 }
7205
7206 public boolean getGivenInsetsPendingLw() {
7207 return mGivenInsetsPending;
7208 }
7209
7210 public Rect getGivenContentInsetsLw() {
7211 return mGivenContentInsets;
7212 }
Romain Guy06882f82009-06-10 13:36:04 -07007213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 public Rect getGivenVisibleInsetsLw() {
7215 return mGivenVisibleInsets;
7216 }
Romain Guy06882f82009-06-10 13:36:04 -07007217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 public WindowManager.LayoutParams getAttrs() {
7219 return mAttrs;
7220 }
7221
7222 public int getSurfaceLayer() {
7223 return mLayer;
7224 }
Romain Guy06882f82009-06-10 13:36:04 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 public IApplicationToken getAppToken() {
7227 return mAppToken != null ? mAppToken.appToken : null;
7228 }
7229
7230 public boolean hasAppShownWindows() {
7231 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7232 }
7233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007235 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 TAG, "Setting animation in " + this + ": " + anim);
7237 mAnimating = false;
7238 mLocalAnimating = false;
7239 mAnimation = anim;
7240 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7241 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7242 }
7243
7244 public void clearAnimation() {
7245 if (mAnimation != null) {
7246 mAnimating = true;
7247 mLocalAnimating = false;
7248 mAnimation = null;
7249 }
7250 }
Romain Guy06882f82009-06-10 13:36:04 -07007251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 Surface createSurfaceLocked() {
7253 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007254 mReportDestroySurface = false;
7255 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 mDrawPending = true;
7257 mCommitDrawPending = false;
7258 mReadyToShow = false;
7259 if (mAppToken != null) {
7260 mAppToken.allDrawn = false;
7261 }
7262
7263 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007264 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 flags |= Surface.PUSH_BUFFERS;
7266 }
7267
7268 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7269 flags |= Surface.SECURE;
7270 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007271 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 TAG, "Creating surface in session "
7273 + mSession.mSurfaceSession + " window " + this
7274 + " w=" + mFrame.width()
7275 + " h=" + mFrame.height() + " format="
7276 + mAttrs.format + " flags=" + flags);
7277
7278 int w = mFrame.width();
7279 int h = mFrame.height();
7280 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7281 // for a scaled surface, we always want the requested
7282 // size.
7283 w = mRequestedWidth;
7284 h = mRequestedHeight;
7285 }
7286
Romain Guy9825ec62009-10-01 00:58:09 -07007287 // Something is wrong and SurfaceFlinger will not like this,
7288 // try to revert to sane values
7289 if (w <= 0) w = 1;
7290 if (h <= 0) h = 1;
7291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 try {
7293 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007294 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007295 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007297 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007298 + mSurface + " IN SESSION "
7299 + mSession.mSurfaceSession
7300 + ": pid=" + mSession.mPid + " format="
7301 + mAttrs.format + " flags=0x"
7302 + Integer.toHexString(flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007304 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 reclaimSomeSurfaceMemoryLocked(this, "create");
7306 return null;
7307 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007308 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 return null;
7310 }
Romain Guy06882f82009-06-10 13:36:04 -07007311
Joe Onorato8a9b2202010-02-26 18:56:32 -08007312 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 TAG, "Got surface: " + mSurface
7314 + ", set left=" + mFrame.left + " top=" + mFrame.top
7315 + ", animLayer=" + mAnimLayer);
7316 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007317 Slog.i(TAG, ">>> OPEN TRANSACTION");
7318 Slog.i(TAG, " SURFACE " + mSurface + ": CREATE ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 + mAttrs.getTitle() + ") pos=(" +
7320 mFrame.left + "," + mFrame.top + ") (" +
7321 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7322 mAnimLayer + " HIDE");
7323 }
7324 Surface.openTransaction();
7325 try {
7326 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007327 mSurface.setPosition(mFrame.left + mXOffset,
7328 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 mSurface.setLayer(mAnimLayer);
7330 mSurface.hide();
7331 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007332 if (SHOW_TRANSACTIONS) Slog.i(TAG, " SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007333 + mSurface + ": DITHER");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 mSurface.setFlags(Surface.SURFACE_DITHER,
7335 Surface.SURFACE_DITHER);
7336 }
7337 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007338 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7340 }
7341 mLastHidden = true;
7342 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007343 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007344 Surface.closeTransaction();
7345 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007346 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007347 TAG, "Created surface " + this);
7348 }
7349 return mSurface;
7350 }
Romain Guy06882f82009-06-10 13:36:04 -07007351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352 void destroySurfaceLocked() {
7353 // Window is no longer on-screen, so can no longer receive
7354 // key events... if we were waiting for it to finish
7355 // handling a key event, the wait is over!
7356 mKeyWaiter.finishedKey(mSession, mClient, true,
7357 KeyWaiter.RETURN_NOTHING);
7358 mKeyWaiter.releasePendingPointerLocked(mSession);
7359 mKeyWaiter.releasePendingTrackballLocked(mSession);
7360
7361 if (mAppToken != null && this == mAppToken.startingWindow) {
7362 mAppToken.startingDisplayed = false;
7363 }
Romain Guy06882f82009-06-10 13:36:04 -07007364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007366 mDrawPending = false;
7367 mCommitDrawPending = false;
7368 mReadyToShow = false;
7369
7370 int i = mChildWindows.size();
7371 while (i > 0) {
7372 i--;
7373 WindowState c = (WindowState)mChildWindows.get(i);
7374 c.mAttachedHidden = true;
7375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007376
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007377 if (mReportDestroySurface) {
7378 mReportDestroySurface = false;
7379 mSurfacePendingDestroy = true;
7380 try {
7381 mClient.dispatchGetNewSurface();
7382 // We'll really destroy on the next time around.
7383 return;
7384 } catch (RemoteException e) {
7385 }
7386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007389 if (DEBUG_VISIBILITY) {
7390 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007391 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007392 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007393 + mSurface + ", session " + mSession, e);
7394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 if (SHOW_TRANSACTIONS) {
7396 RuntimeException ex = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007397 if (!HIDE_STACK_CRAWLS) ex.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007398 Slog.i(TAG, " SURFACE " + mSurface + ": DESTROY ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 + mAttrs.getTitle() + ")", ex);
7400 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007401 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007403 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007404 + " surface " + mSurface + " session " + mSession
7405 + ": " + e.toString());
7406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 }
7410 }
7411
7412 boolean finishDrawingLocked() {
7413 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007414 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 TAG, "finishDrawingLocked: " + mSurface);
7416 mCommitDrawPending = true;
7417 mDrawPending = false;
7418 return true;
7419 }
7420 return false;
7421 }
7422
7423 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007424 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007425 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007427 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 }
7429 mCommitDrawPending = false;
7430 mReadyToShow = true;
7431 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7432 final AppWindowToken atoken = mAppToken;
7433 if (atoken == null || atoken.allDrawn || starting) {
7434 performShowLocked();
7435 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007436 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 }
7438
7439 // This must be called while inside a transaction.
7440 boolean performShowLocked() {
7441 if (DEBUG_VISIBILITY) {
7442 RuntimeException e = new RuntimeException();
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07007443 if (!HIDE_STACK_CRAWLS) e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007444 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7446 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7447 }
7448 if (mReadyToShow && isReadyForDisplay()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007449 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 TAG, " SURFACE " + mSurface + ": SHOW (performShowLocked)");
Joe Onorato8a9b2202010-02-26 18:56:32 -08007451 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007452 + " during animation: policyVis=" + mPolicyVisibility
7453 + " attHidden=" + mAttachedHidden
7454 + " tok.hiddenRequested="
7455 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007456 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 + (mAppToken != null ? mAppToken.hidden : false)
7458 + " animating=" + mAnimating
7459 + " tok animating="
7460 + (mAppToken != null ? mAppToken.animating : false));
7461 if (!showSurfaceRobustlyLocked(this)) {
7462 return false;
7463 }
7464 mLastAlpha = -1;
7465 mHasDrawn = true;
7466 mLastHidden = false;
7467 mReadyToShow = false;
7468 enableScreenIfNeededLocked();
7469
7470 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 int i = mChildWindows.size();
7473 while (i > 0) {
7474 i--;
7475 WindowState c = (WindowState)mChildWindows.get(i);
7476 if (c.mSurface != null && c.mAttachedHidden) {
7477 c.mAttachedHidden = false;
7478 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007479 // It hadn't been shown, which means layout not
7480 // performed on it, so now we want to make sure to
7481 // do a layout. If called from within the transaction
7482 // loop, this will cause it to restart with a new
7483 // layout.
7484 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 }
7486 }
Romain Guy06882f82009-06-10 13:36:04 -07007487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 if (mAttrs.type != TYPE_APPLICATION_STARTING
7489 && mAppToken != null) {
7490 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007491
Dianne Hackborn248b1882009-09-16 16:46:44 -07007492 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007493 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007494 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007496 // If this initial window is animating, stop it -- we
7497 // will do an animation to reveal it from behind the
7498 // starting window, so there is no need for it to also
7499 // be doing its own stuff.
7500 if (mAnimation != null) {
7501 mAnimation = null;
7502 // Make sure we clean up the animation.
7503 mAnimating = true;
7504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 mFinishedStarting.add(mAppToken);
7506 mH.sendEmptyMessage(H.FINISHED_STARTING);
7507 }
7508 mAppToken.updateReportedVisibilityLocked();
7509 }
7510 }
7511 return true;
7512 }
Romain Guy06882f82009-06-10 13:36:04 -07007513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 // This must be called while inside a transaction. Returns true if
7515 // there is more animation to run.
7516 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007517 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7521 mHasTransformation = true;
7522 mHasLocalTransformation = true;
7523 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007524 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 TAG, "Starting animation in " + this +
7526 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7527 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7528 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7529 mAnimation.setStartTime(currentTime);
7530 mLocalAnimating = true;
7531 mAnimating = true;
7532 }
7533 mTransformation.clear();
7534 final boolean more = mAnimation.getTransformation(
7535 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007536 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 TAG, "Stepped animation in " + this +
7538 ": more=" + more + ", xform=" + mTransformation);
7539 if (more) {
7540 // we're not done!
7541 return true;
7542 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007543 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544 TAG, "Finished animation in " + this +
7545 " @ " + currentTime);
7546 mAnimation = null;
7547 //WindowManagerService.this.dump();
7548 }
7549 mHasLocalTransformation = false;
7550 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007551 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 // When our app token is animating, we kind-of pretend like
7553 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7554 // part of this check means that we will only do this if
7555 // our window is not currently exiting, or it is not
7556 // locally animating itself. The idea being that one that
7557 // is exiting and doing a local animation should be removed
7558 // once that animation is done.
7559 mAnimating = true;
7560 mHasTransformation = true;
7561 mTransformation.clear();
7562 return false;
7563 } else if (mHasTransformation) {
7564 // Little trick to get through the path below to act like
7565 // we have finished an animation.
7566 mAnimating = true;
7567 } else if (isAnimating()) {
7568 mAnimating = true;
7569 }
7570 } else if (mAnimation != null) {
7571 // If the display is frozen, and there is a pending animation,
7572 // clear it and make sure we run the cleanup code.
7573 mAnimating = true;
7574 mLocalAnimating = true;
7575 mAnimation = null;
7576 }
Romain Guy06882f82009-06-10 13:36:04 -07007577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 if (!mAnimating && !mLocalAnimating) {
7579 return false;
7580 }
7581
Joe Onorato8a9b2202010-02-26 18:56:32 -08007582 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 TAG, "Animation done in " + this + ": exiting=" + mExiting
7584 + ", reportedVisible="
7585 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 mAnimating = false;
7588 mLocalAnimating = false;
7589 mAnimation = null;
7590 mAnimLayer = mLayer;
7591 if (mIsImWindow) {
7592 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007593 } else if (mIsWallpaper) {
7594 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007596 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 + " anim layer: " + mAnimLayer);
7598 mHasTransformation = false;
7599 mHasLocalTransformation = false;
7600 mPolicyVisibility = mPolicyVisibilityAfterAnim;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007601 if (!mPolicyVisibility) {
7602 // Window is no longer visible -- make sure if we were waiting
7603 // for it to be displayed before enabling the display, that
7604 // we allow the display to be enabled now.
7605 enableScreenIfNeededLocked();
7606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 mTransformation.clear();
7608 if (mHasDrawn
7609 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7610 && mAppToken != null
7611 && mAppToken.firstWindowDrawn
7612 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007613 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 + mToken + ": first real window done animating");
7615 mFinishedStarting.add(mAppToken);
7616 mH.sendEmptyMessage(H.FINISHED_STARTING);
7617 }
Romain Guy06882f82009-06-10 13:36:04 -07007618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 finishExit();
7620
7621 if (mAppToken != null) {
7622 mAppToken.updateReportedVisibilityLocked();
7623 }
7624
7625 return false;
7626 }
7627
7628 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007629 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007630 TAG, "finishExit in " + this
7631 + ": exiting=" + mExiting
7632 + " remove=" + mRemoveOnExit
7633 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 final int N = mChildWindows.size();
7636 for (int i=0; i<N; i++) {
7637 ((WindowState)mChildWindows.get(i)).finishExit();
7638 }
Romain Guy06882f82009-06-10 13:36:04 -07007639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 if (!mExiting) {
7641 return;
7642 }
Romain Guy06882f82009-06-10 13:36:04 -07007643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 if (isWindowAnimating()) {
7645 return;
7646 }
7647
Joe Onorato8a9b2202010-02-26 18:56:32 -08007648 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 TAG, "Exit animation finished in " + this
7650 + ": remove=" + mRemoveOnExit);
7651 if (mSurface != null) {
7652 mDestroySurface.add(this);
7653 mDestroying = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007654 if (SHOW_TRANSACTIONS) Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 TAG, " SURFACE " + mSurface + ": HIDE (finishExit)");
7656 try {
7657 mSurface.hide();
7658 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007659 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 }
7661 mLastHidden = true;
7662 mKeyWaiter.releasePendingPointerLocked(mSession);
7663 }
7664 mExiting = false;
7665 if (mRemoveOnExit) {
7666 mPendingRemove.add(this);
7667 mRemoveOnExit = false;
7668 }
7669 }
Romain Guy06882f82009-06-10 13:36:04 -07007670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007671 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7672 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7673 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7674 if (dtdx < -.000001f || dtdx > .000001f) return false;
7675 if (dsdy < -.000001f || dsdy > .000001f) return false;
7676 return true;
7677 }
Romain Guy06882f82009-06-10 13:36:04 -07007678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679 void computeShownFrameLocked() {
7680 final boolean selfTransformation = mHasLocalTransformation;
7681 Transformation attachedTransformation =
7682 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7683 ? mAttachedWindow.mTransformation : null;
7684 Transformation appTransformation =
7685 (mAppToken != null && mAppToken.hasTransformation)
7686 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007687
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007688 // Wallpapers are animated based on the "real" window they
7689 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007690 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007691 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007692 if (mWallpaperTarget.mHasLocalTransformation &&
7693 mWallpaperTarget.mAnimation != null &&
7694 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007695 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007696 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007697 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007698 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007699 }
7700 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007701 mWallpaperTarget.mAppToken.hasTransformation &&
7702 mWallpaperTarget.mAppToken.animation != null &&
7703 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007704 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007705 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007706 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007707 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007708 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 if (selfTransformation || attachedTransformation != null
7712 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007713 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007714 final Rect frame = mFrame;
7715 final float tmpFloats[] = mTmpFloats;
7716 final Matrix tmpMatrix = mTmpMatrix;
7717
7718 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007719 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007721 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007723 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007725 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 }
7727 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007728 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 }
7730
7731 // "convert" it into SurfaceFlinger's format
7732 // (a 2x2 matrix + an offset)
7733 // Here we must not transform the position of the surface
7734 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007735 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737 tmpMatrix.getValues(tmpFloats);
7738 mDsDx = tmpFloats[Matrix.MSCALE_X];
7739 mDtDx = tmpFloats[Matrix.MSKEW_X];
7740 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7741 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007742 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7743 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 int w = frame.width();
7745 int h = frame.height();
7746 mShownFrame.set(x, y, x+w, y+h);
7747
7748 // Now set the alpha... but because our current hardware
7749 // can't do alpha transformation on a non-opaque surface,
7750 // turn it off if we are running an animation that is also
7751 // transforming since it is more important to have that
7752 // animation be smooth.
7753 mShownAlpha = mAlpha;
7754 if (!mLimitedAlphaCompositing
7755 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7756 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7757 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007758 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 if (selfTransformation) {
7760 mShownAlpha *= mTransformation.getAlpha();
7761 }
7762 if (attachedTransformation != null) {
7763 mShownAlpha *= attachedTransformation.getAlpha();
7764 }
7765 if (appTransformation != null) {
7766 mShownAlpha *= appTransformation.getAlpha();
7767 }
7768 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007769 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 }
Romain Guy06882f82009-06-10 13:36:04 -07007771
Joe Onorato8a9b2202010-02-26 18:56:32 -08007772 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 TAG, "Continuing animation in " + this +
7774 ": " + mShownFrame +
7775 ", alpha=" + mTransformation.getAlpha());
7776 return;
7777 }
Romain Guy06882f82009-06-10 13:36:04 -07007778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007780 if (mXOffset != 0 || mYOffset != 0) {
7781 mShownFrame.offset(mXOffset, mYOffset);
7782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 mShownAlpha = mAlpha;
7784 mDsDx = 1;
7785 mDtDx = 0;
7786 mDsDy = 0;
7787 mDtDy = 1;
7788 }
Romain Guy06882f82009-06-10 13:36:04 -07007789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 /**
7791 * Is this window visible? It is not visible if there is no
7792 * surface, or we are in the process of running an exit animation
7793 * that will remove the surface, or its app token has been hidden.
7794 */
7795 public boolean isVisibleLw() {
7796 final AppWindowToken atoken = mAppToken;
7797 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7798 && (atoken == null || !atoken.hiddenRequested)
7799 && !mExiting && !mDestroying;
7800 }
7801
7802 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007803 * Like {@link #isVisibleLw}, but also counts a window that is currently
7804 * "hidden" behind the keyguard as visible. This allows us to apply
7805 * things like window flags that impact the keyguard.
7806 * XXX I am starting to think we need to have ANOTHER visibility flag
7807 * for this "hidden behind keyguard" state rather than overloading
7808 * mPolicyVisibility. Ungh.
7809 */
7810 public boolean isVisibleOrBehindKeyguardLw() {
7811 final AppWindowToken atoken = mAppToken;
7812 return mSurface != null && !mAttachedHidden
7813 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
7814 && !mExiting && !mDestroying;
7815 }
7816
7817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 * Is this window visible, ignoring its app token? It is not visible
7819 * if there is no surface, or we are in the process of running an exit animation
7820 * that will remove the surface.
7821 */
7822 public boolean isWinVisibleLw() {
7823 final AppWindowToken atoken = mAppToken;
7824 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7825 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7826 && !mExiting && !mDestroying;
7827 }
7828
7829 /**
7830 * The same as isVisible(), but follows the current hidden state of
7831 * the associated app token, not the pending requested hidden state.
7832 */
7833 boolean isVisibleNow() {
7834 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007835 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 }
7837
7838 /**
7839 * Same as isVisible(), but we also count it as visible between the
7840 * call to IWindowSession.add() and the first relayout().
7841 */
7842 boolean isVisibleOrAdding() {
7843 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007844 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7846 && mPolicyVisibility && !mAttachedHidden
7847 && (atoken == null || !atoken.hiddenRequested)
7848 && !mExiting && !mDestroying;
7849 }
7850
7851 /**
7852 * Is this window currently on-screen? It is on-screen either if it
7853 * is visible or it is currently running an animation before no longer
7854 * being visible.
7855 */
7856 boolean isOnScreen() {
7857 final AppWindowToken atoken = mAppToken;
7858 if (atoken != null) {
7859 return mSurface != null && mPolicyVisibility && !mDestroying
7860 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007861 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 } else {
7863 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007864 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 }
7866 }
Romain Guy06882f82009-06-10 13:36:04 -07007867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 /**
7869 * Like isOnScreen(), but we don't return true if the window is part
7870 * of a transition that has not yet been started.
7871 */
7872 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007873 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007874 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007875 return false;
7876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007878 final boolean animating = atoken != null
7879 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007881 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7882 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007883 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 }
7885
7886 /** Is the window or its container currently animating? */
7887 boolean isAnimating() {
7888 final WindowState attached = mAttachedWindow;
7889 final AppWindowToken atoken = mAppToken;
7890 return mAnimation != null
7891 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007892 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 (atoken.animation != null
7894 || atoken.inPendingTransaction));
7895 }
7896
7897 /** Is this window currently animating? */
7898 boolean isWindowAnimating() {
7899 return mAnimation != null;
7900 }
7901
7902 /**
7903 * Like isOnScreen, but returns false if the surface hasn't yet
7904 * been drawn.
7905 */
7906 public boolean isDisplayedLw() {
7907 final AppWindowToken atoken = mAppToken;
7908 return mSurface != null && mPolicyVisibility && !mDestroying
7909 && !mDrawPending && !mCommitDrawPending
7910 && ((!mAttachedHidden &&
7911 (atoken == null || !atoken.hiddenRequested))
7912 || mAnimating);
7913 }
7914
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007915 /**
7916 * Returns true if the window has a surface that it has drawn a
7917 * complete UI in to.
7918 */
7919 public boolean isDrawnLw() {
7920 final AppWindowToken atoken = mAppToken;
7921 return mSurface != null && !mDestroying
7922 && !mDrawPending && !mCommitDrawPending;
7923 }
7924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 public boolean fillsScreenLw(int screenWidth, int screenHeight,
7926 boolean shownFrame, boolean onlyOpaque) {
7927 if (mSurface == null) {
7928 return false;
7929 }
7930 if (mAppToken != null && !mAppToken.appFullscreen) {
7931 return false;
7932 }
7933 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
7934 return false;
7935 }
7936 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007937
7938 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
7939 return frame.left <= mCompatibleScreenFrame.left &&
7940 frame.top <= mCompatibleScreenFrame.top &&
7941 frame.right >= mCompatibleScreenFrame.right &&
7942 frame.bottom >= mCompatibleScreenFrame.bottom;
7943 } else {
7944 return frame.left <= 0 && frame.top <= 0
7945 && frame.right >= screenWidth
7946 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 }
Romain Guy06882f82009-06-10 13:36:04 -07007949
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007950 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07007951 * Return true if the window is opaque and fully drawn. This indicates
7952 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007953 */
7954 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07007955 return (mAttrs.format == PixelFormat.OPAQUE
7956 || mAttrs.type == TYPE_WALLPAPER)
7957 && mSurface != null && mAnimation == null
7958 && (mAppToken == null || mAppToken.animation == null)
7959 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007960 }
7961
7962 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
7963 return
7964 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007965 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
7966 // only if it's visible
7967 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007968 // and only if the application fills the compatible screen
7969 mFrame.left <= mCompatibleScreenFrame.left &&
7970 mFrame.top <= mCompatibleScreenFrame.top &&
7971 mFrame.right >= mCompatibleScreenFrame.right &&
7972 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007973 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007974 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007975 }
7976
7977 boolean isFullscreen(int screenWidth, int screenHeight) {
7978 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007979 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007980 }
7981
7982 void removeLocked() {
7983 if (mAttachedWindow != null) {
7984 mAttachedWindow.mChildWindows.remove(this);
7985 }
7986 destroySurfaceLocked();
7987 mSession.windowRemovedLocked();
7988 try {
7989 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
7990 } catch (RuntimeException e) {
7991 // Ignore if it has already been removed (usually because
7992 // we are doing this as part of processing a death note.)
7993 }
7994 }
7995
7996 private class DeathRecipient implements IBinder.DeathRecipient {
7997 public void binderDied() {
7998 try {
7999 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008000 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008001 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 if (win != null) {
8003 removeWindowLocked(mSession, win);
8004 }
8005 }
8006 } catch (IllegalArgumentException ex) {
8007 // This will happen if the window has already been
8008 // removed.
8009 }
8010 }
8011 }
8012
8013 /** Returns true if this window desires key events. */
8014 public final boolean canReceiveKeys() {
8015 return isVisibleOrAdding()
8016 && (mViewVisibility == View.VISIBLE)
8017 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8018 }
8019
8020 public boolean hasDrawnLw() {
8021 return mHasDrawn;
8022 }
8023
8024 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008025 return showLw(doAnimation, true);
8026 }
8027
8028 boolean showLw(boolean doAnimation, boolean requestAnim) {
8029 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8030 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008031 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008032 mPolicyVisibility = true;
8033 mPolicyVisibilityAfterAnim = true;
8034 if (doAnimation) {
8035 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8036 }
8037 if (requestAnim) {
8038 requestAnimationLocked(0);
8039 }
8040 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 }
8042
8043 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008044 return hideLw(doAnimation, true);
8045 }
8046
8047 boolean hideLw(boolean doAnimation, boolean requestAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8049 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008050 if (!current) {
8051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008053 if (doAnimation) {
8054 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8055 if (mAnimation == null) {
8056 doAnimation = false;
8057 }
8058 }
8059 if (doAnimation) {
8060 mPolicyVisibilityAfterAnim = false;
8061 } else {
8062 mPolicyVisibilityAfterAnim = false;
8063 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008064 // Window is no longer visible -- make sure if we were waiting
8065 // for it to be displayed before enabling the display, that
8066 // we allow the display to be enabled now.
8067 enableScreenIfNeededLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008068 }
8069 if (requestAnim) {
8070 requestAnimationLocked(0);
8071 }
8072 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074
8075 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008076 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8077 pw.print(" mClient="); pw.println(mClient.asBinder());
8078 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8079 if (mAttachedWindow != null || mLayoutAttached) {
8080 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8081 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8082 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008083 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8084 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8085 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008086 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8087 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008088 }
8089 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8090 pw.print(" mSubLayer="); pw.print(mSubLayer);
8091 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8092 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8093 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8094 pw.print("="); pw.print(mAnimLayer);
8095 pw.print(" mLastLayer="); pw.println(mLastLayer);
8096 if (mSurface != null) {
8097 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8098 }
8099 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8100 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8101 if (mAppToken != null) {
8102 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8103 }
8104 if (mTargetAppToken != null) {
8105 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8106 }
8107 pw.print(prefix); pw.print("mViewVisibility=0x");
8108 pw.print(Integer.toHexString(mViewVisibility));
8109 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008110 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8111 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008112 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8113 pw.print(prefix); pw.print("mPolicyVisibility=");
8114 pw.print(mPolicyVisibility);
8115 pw.print(" mPolicyVisibilityAfterAnim=");
8116 pw.print(mPolicyVisibilityAfterAnim);
8117 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8118 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008119 if (!mRelayoutCalled) {
8120 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8121 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008122 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008123 pw.print(" h="); pw.print(mRequestedHeight);
8124 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008125 if (mXOffset != 0 || mYOffset != 0) {
8126 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8127 pw.print(" y="); pw.println(mYOffset);
8128 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008129 pw.print(prefix); pw.print("mGivenContentInsets=");
8130 mGivenContentInsets.printShortString(pw);
8131 pw.print(" mGivenVisibleInsets=");
8132 mGivenVisibleInsets.printShortString(pw);
8133 pw.println();
8134 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8135 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8136 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8137 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008138 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008139 pw.print(prefix); pw.print("mShownFrame=");
8140 mShownFrame.printShortString(pw);
8141 pw.print(" last="); mLastShownFrame.printShortString(pw);
8142 pw.println();
8143 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8144 pw.print(" last="); mLastFrame.printShortString(pw);
8145 pw.println();
8146 pw.print(prefix); pw.print("mContainingFrame=");
8147 mContainingFrame.printShortString(pw);
8148 pw.print(" mDisplayFrame=");
8149 mDisplayFrame.printShortString(pw);
8150 pw.println();
8151 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8152 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8153 pw.println();
8154 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8155 pw.print(" last="); mLastContentInsets.printShortString(pw);
8156 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8157 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8158 pw.println();
8159 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8160 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8161 pw.print(" mAlpha="); pw.print(mAlpha);
8162 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8163 }
8164 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8165 || mAnimation != null) {
8166 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8167 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8168 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8169 pw.print(" mAnimation="); pw.println(mAnimation);
8170 }
8171 if (mHasTransformation || mHasLocalTransformation) {
8172 pw.print(prefix); pw.print("XForm: has=");
8173 pw.print(mHasTransformation);
8174 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8175 pw.print(" "); mTransformation.printShortString(pw);
8176 pw.println();
8177 }
8178 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8179 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8180 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8181 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8182 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8183 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8184 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8185 pw.print(" mDestroying="); pw.print(mDestroying);
8186 pw.print(" mRemoved="); pw.println(mRemoved);
8187 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008188 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008189 pw.print(prefix); pw.print("mOrientationChanging=");
8190 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008191 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8192 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008193 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008194 if (mHScale != 1 || mVScale != 1) {
8195 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8196 pw.print(" mVScale="); pw.println(mVScale);
8197 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008198 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008199 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8200 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8201 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008202 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8203 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8204 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 }
8207
8208 @Override
8209 public String toString() {
8210 return "Window{"
8211 + Integer.toHexString(System.identityHashCode(this))
8212 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8213 }
8214 }
Romain Guy06882f82009-06-10 13:36:04 -07008215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 // -------------------------------------------------------------
8217 // Window Token State
8218 // -------------------------------------------------------------
8219
8220 class WindowToken {
8221 // The actual token.
8222 final IBinder token;
8223
8224 // The type of window this token is for, as per WindowManager.LayoutParams.
8225 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 // Set if this token was explicitly added by a client, so should
8228 // not be removed when all windows are removed.
8229 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008230
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008231 // For printing.
8232 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234 // If this is an AppWindowToken, this is non-null.
8235 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 // All of the windows associated with this token.
8238 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8239
8240 // Is key dispatching paused for this token?
8241 boolean paused = false;
8242
8243 // Should this token's windows be hidden?
8244 boolean hidden;
8245
8246 // Temporary for finding which tokens no longer have visible windows.
8247 boolean hasVisible;
8248
Dianne Hackborna8f60182009-09-01 19:01:50 -07008249 // Set to true when this token is in a pending transaction where it
8250 // will be shown.
8251 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008252
Dianne Hackborna8f60182009-09-01 19:01:50 -07008253 // Set to true when this token is in a pending transaction where it
8254 // will be hidden.
8255 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008256
Dianne Hackborna8f60182009-09-01 19:01:50 -07008257 // Set to true when this token is in a pending transaction where its
8258 // windows will be put to the bottom of the list.
8259 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008260
Dianne Hackborna8f60182009-09-01 19:01:50 -07008261 // Set to true when this token is in a pending transaction where its
8262 // windows will be put to the top of the list.
8263 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008265 WindowToken(IBinder _token, int type, boolean _explicit) {
8266 token = _token;
8267 windowType = type;
8268 explicit = _explicit;
8269 }
8270
8271 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008272 pw.print(prefix); pw.print("token="); pw.println(token);
8273 pw.print(prefix); pw.print("windows="); pw.println(windows);
8274 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8275 pw.print(" hidden="); pw.print(hidden);
8276 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008277 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8278 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8279 pw.print(" waitingToHide="); pw.print(waitingToHide);
8280 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8281 pw.print(" sendingToTop="); pw.println(sendingToTop);
8282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008283 }
8284
8285 @Override
8286 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008287 if (stringName == null) {
8288 StringBuilder sb = new StringBuilder();
8289 sb.append("WindowToken{");
8290 sb.append(Integer.toHexString(System.identityHashCode(this)));
8291 sb.append(" token="); sb.append(token); sb.append('}');
8292 stringName = sb.toString();
8293 }
8294 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 }
8296 };
8297
8298 class AppWindowToken extends WindowToken {
8299 // Non-null only for application tokens.
8300 final IApplicationToken appToken;
8301
8302 // All of the windows and child windows that are included in this
8303 // application token. Note this list is NOT sorted!
8304 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8305
8306 int groupId = -1;
8307 boolean appFullscreen;
8308 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310 // These are used for determining when all windows associated with
8311 // an activity have been drawn, so they can be made visible together
8312 // at the same time.
8313 int lastTransactionSequence = mTransactionSequence-1;
8314 int numInterestingWindows;
8315 int numDrawnWindows;
8316 boolean inPendingTransaction;
8317 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008319 // Is this token going to be hidden in a little while? If so, it
8320 // won't be taken into account for setting the screen orientation.
8321 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 // Is this window's surface needed? This is almost like hidden, except
8324 // it will sometimes be true a little earlier: when the token has
8325 // been shown, but is still waiting for its app transition to execute
8326 // before making its windows shown.
8327 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 // Have we told the window clients to hide themselves?
8330 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 // Last visibility state we reported to the app token.
8333 boolean reportedVisible;
8334
8335 // Set to true when the token has been removed from the window mgr.
8336 boolean removed;
8337
8338 // Have we been asked to have this token keep the screen frozen?
8339 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 boolean animating;
8342 Animation animation;
8343 boolean hasTransformation;
8344 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 // Offset to the window of all layers in the token, for use by
8347 // AppWindowToken animations.
8348 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 // Information about an application starting window if displayed.
8351 StartingData startingData;
8352 WindowState startingWindow;
8353 View startingView;
8354 boolean startingDisplayed;
8355 boolean startingMoved;
8356 boolean firstWindowDrawn;
8357
8358 AppWindowToken(IApplicationToken _token) {
8359 super(_token.asBinder(),
8360 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8361 appWindowToken = this;
8362 appToken = _token;
8363 }
Romain Guy06882f82009-06-10 13:36:04 -07008364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008366 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 TAG, "Setting animation in " + this + ": " + anim);
8368 animation = anim;
8369 animating = false;
8370 anim.restrictDuration(MAX_ANIMATION_DURATION);
8371 anim.scaleCurrentDuration(mTransitionAnimationScale);
8372 int zorder = anim.getZAdjustment();
8373 int adj = 0;
8374 if (zorder == Animation.ZORDER_TOP) {
8375 adj = TYPE_LAYER_OFFSET;
8376 } else if (zorder == Animation.ZORDER_BOTTOM) {
8377 adj = -TYPE_LAYER_OFFSET;
8378 }
Romain Guy06882f82009-06-10 13:36:04 -07008379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 if (animLayerAdjustment != adj) {
8381 animLayerAdjustment = adj;
8382 updateLayers();
8383 }
8384 }
Romain Guy06882f82009-06-10 13:36:04 -07008385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 public void setDummyAnimation() {
8387 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008388 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008389 TAG, "Setting dummy animation in " + this);
8390 animation = sDummyAnimation;
8391 }
8392 }
8393
8394 public void clearAnimation() {
8395 if (animation != null) {
8396 animation = null;
8397 animating = true;
8398 }
8399 }
Romain Guy06882f82009-06-10 13:36:04 -07008400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 void updateLayers() {
8402 final int N = allAppWindows.size();
8403 final int adj = animLayerAdjustment;
8404 for (int i=0; i<N; i++) {
8405 WindowState w = allAppWindows.get(i);
8406 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008407 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 + w.mAnimLayer);
8409 if (w == mInputMethodTarget) {
8410 setInputMethodAnimLayerAdjustment(adj);
8411 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008412 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008413 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 }
8416 }
Romain Guy06882f82009-06-10 13:36:04 -07008417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 void sendAppVisibilityToClients() {
8419 final int N = allAppWindows.size();
8420 for (int i=0; i<N; i++) {
8421 WindowState win = allAppWindows.get(i);
8422 if (win == startingWindow && clientHidden) {
8423 // Don't hide the starting window.
8424 continue;
8425 }
8426 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008427 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 "Setting visibility of " + win + ": " + (!clientHidden));
8429 win.mClient.dispatchAppVisibility(!clientHidden);
8430 } catch (RemoteException e) {
8431 }
8432 }
8433 }
Romain Guy06882f82009-06-10 13:36:04 -07008434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 void showAllWindowsLocked() {
8436 final int NW = allAppWindows.size();
8437 for (int i=0; i<NW; i++) {
8438 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008439 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 "performing show on: " + w);
8441 w.performShowLocked();
8442 }
8443 }
Romain Guy06882f82009-06-10 13:36:04 -07008444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 // This must be called while inside a transaction.
8446 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008447 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 if (animation == sDummyAnimation) {
8451 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008452 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453 // when it is really time to animate, this will be set to
8454 // a real animation and the next call will execute normally.
8455 return false;
8456 }
Romain Guy06882f82009-06-10 13:36:04 -07008457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8459 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008460 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 TAG, "Starting animation in " + this +
8462 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8463 + " scale=" + mTransitionAnimationScale
8464 + " allDrawn=" + allDrawn + " animating=" + animating);
8465 animation.initialize(dw, dh, dw, dh);
8466 animation.setStartTime(currentTime);
8467 animating = true;
8468 }
8469 transformation.clear();
8470 final boolean more = animation.getTransformation(
8471 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008472 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 TAG, "Stepped animation in " + this +
8474 ": more=" + more + ", xform=" + transformation);
8475 if (more) {
8476 // we're done!
8477 hasTransformation = true;
8478 return true;
8479 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008480 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 TAG, "Finished animation in " + this +
8482 " @ " + currentTime);
8483 animation = null;
8484 }
8485 } else if (animation != null) {
8486 // If the display is frozen, and there is a pending animation,
8487 // clear it and make sure we run the cleanup code.
8488 animating = true;
8489 animation = null;
8490 }
8491
8492 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 if (!animating) {
8495 return false;
8496 }
8497
8498 clearAnimation();
8499 animating = false;
8500 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8501 moveInputMethodWindowsIfNeededLocked(true);
8502 }
Romain Guy06882f82009-06-10 13:36:04 -07008503
Joe Onorato8a9b2202010-02-26 18:56:32 -08008504 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 TAG, "Animation done in " + this
8506 + ": reportedVisible=" + reportedVisible);
8507
8508 transformation.clear();
8509 if (animLayerAdjustment != 0) {
8510 animLayerAdjustment = 0;
8511 updateLayers();
8512 }
Romain Guy06882f82009-06-10 13:36:04 -07008513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008514 final int N = windows.size();
8515 for (int i=0; i<N; i++) {
8516 ((WindowState)windows.get(i)).finishExit();
8517 }
8518 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 return false;
8521 }
8522
8523 void updateReportedVisibilityLocked() {
8524 if (appToken == null) {
8525 return;
8526 }
Romain Guy06882f82009-06-10 13:36:04 -07008527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008528 int numInteresting = 0;
8529 int numVisible = 0;
8530 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008531
Joe Onorato8a9b2202010-02-26 18:56:32 -08008532 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008533 final int N = allAppWindows.size();
8534 for (int i=0; i<N; i++) {
8535 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008536 if (win == startingWindow || win.mAppFreezing
8537 || win.mViewVisibility != View.VISIBLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538 continue;
8539 }
8540 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008541 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008542 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008544 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008545 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 + " pv=" + win.mPolicyVisibility
8547 + " dp=" + win.mDrawPending
8548 + " cdp=" + win.mCommitDrawPending
8549 + " ah=" + win.mAttachedHidden
8550 + " th="
8551 + (win.mAppToken != null
8552 ? win.mAppToken.hiddenRequested : false)
8553 + " a=" + win.mAnimating);
8554 }
8555 }
8556 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008557 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 if (!win.isAnimating()) {
8559 numVisible++;
8560 }
8561 nowGone = false;
8562 } else if (win.isAnimating()) {
8563 nowGone = false;
8564 }
8565 }
Romain Guy06882f82009-06-10 13:36:04 -07008566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008568 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 + numInteresting + " visible=" + numVisible);
8570 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008571 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 TAG, "Visibility changed in " + this
8573 + ": vis=" + nowVisible);
8574 reportedVisible = nowVisible;
8575 Message m = mH.obtainMessage(
8576 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8577 nowVisible ? 1 : 0,
8578 nowGone ? 1 : 0,
8579 this);
8580 mH.sendMessage(m);
8581 }
8582 }
Romain Guy06882f82009-06-10 13:36:04 -07008583
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008584 WindowState findMainWindow() {
8585 int j = windows.size();
8586 while (j > 0) {
8587 j--;
8588 WindowState win = windows.get(j);
8589 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8590 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8591 return win;
8592 }
8593 }
8594 return null;
8595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 void dump(PrintWriter pw, String prefix) {
8598 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008599 if (appToken != null) {
8600 pw.print(prefix); pw.println("app=true");
8601 }
8602 if (allAppWindows.size() > 0) {
8603 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8604 }
8605 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008606 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008607 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8608 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8609 pw.print(" clientHidden="); pw.print(clientHidden);
8610 pw.print(" willBeHidden="); pw.print(willBeHidden);
8611 pw.print(" reportedVisible="); pw.println(reportedVisible);
8612 if (paused || freezingScreen) {
8613 pw.print(prefix); pw.print("paused="); pw.print(paused);
8614 pw.print(" freezingScreen="); pw.println(freezingScreen);
8615 }
8616 if (numInterestingWindows != 0 || numDrawnWindows != 0
8617 || inPendingTransaction || allDrawn) {
8618 pw.print(prefix); pw.print("numInterestingWindows=");
8619 pw.print(numInterestingWindows);
8620 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8621 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8622 pw.print(" allDrawn="); pw.println(allDrawn);
8623 }
8624 if (animating || animation != null) {
8625 pw.print(prefix); pw.print("animating="); pw.print(animating);
8626 pw.print(" animation="); pw.println(animation);
8627 }
8628 if (animLayerAdjustment != 0) {
8629 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8630 }
8631 if (hasTransformation) {
8632 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8633 pw.print(" transformation="); transformation.printShortString(pw);
8634 pw.println();
8635 }
8636 if (startingData != null || removed || firstWindowDrawn) {
8637 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8638 pw.print(" removed="); pw.print(removed);
8639 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8640 }
8641 if (startingWindow != null || startingView != null
8642 || startingDisplayed || startingMoved) {
8643 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8644 pw.print(" startingView="); pw.print(startingView);
8645 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8646 pw.print(" startingMoved"); pw.println(startingMoved);
8647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 }
8649
8650 @Override
8651 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008652 if (stringName == null) {
8653 StringBuilder sb = new StringBuilder();
8654 sb.append("AppWindowToken{");
8655 sb.append(Integer.toHexString(System.identityHashCode(this)));
8656 sb.append(" token="); sb.append(token); sb.append('}');
8657 stringName = sb.toString();
8658 }
8659 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 }
8661 }
Romain Guy06882f82009-06-10 13:36:04 -07008662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 // -------------------------------------------------------------
8664 // DummyAnimation
8665 // -------------------------------------------------------------
8666
8667 // This is an animation that does nothing: it just immediately finishes
8668 // itself every time it is called. It is used as a stub animation in cases
8669 // where we want to synchronize multiple things that may be animating.
8670 static final class DummyAnimation extends Animation {
8671 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8672 return false;
8673 }
8674 }
8675 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 // -------------------------------------------------------------
8678 // Async Handler
8679 // -------------------------------------------------------------
8680
8681 static final class StartingData {
8682 final String pkg;
8683 final int theme;
8684 final CharSequence nonLocalizedLabel;
8685 final int labelRes;
8686 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8689 int _labelRes, int _icon) {
8690 pkg = _pkg;
8691 theme = _theme;
8692 nonLocalizedLabel = _nonLocalizedLabel;
8693 labelRes = _labelRes;
8694 icon = _icon;
8695 }
8696 }
8697
8698 private final class H extends Handler {
8699 public static final int REPORT_FOCUS_CHANGE = 2;
8700 public static final int REPORT_LOSING_FOCUS = 3;
8701 public static final int ANIMATE = 4;
8702 public static final int ADD_STARTING = 5;
8703 public static final int REMOVE_STARTING = 6;
8704 public static final int FINISHED_STARTING = 7;
8705 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8707 public static final int HOLD_SCREEN_CHANGED = 12;
8708 public static final int APP_TRANSITION_TIMEOUT = 13;
8709 public static final int PERSIST_ANIMATION_SCALE = 14;
8710 public static final int FORCE_GC = 15;
8711 public static final int ENABLE_SCREEN = 16;
8712 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008713 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 public H() {
8718 }
Romain Guy06882f82009-06-10 13:36:04 -07008719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 @Override
8721 public void handleMessage(Message msg) {
8722 switch (msg.what) {
8723 case REPORT_FOCUS_CHANGE: {
8724 WindowState lastFocus;
8725 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008727 synchronized(mWindowMap) {
8728 lastFocus = mLastFocus;
8729 newFocus = mCurrentFocus;
8730 if (lastFocus == newFocus) {
8731 // Focus is not changing, so nothing to do.
8732 return;
8733 }
8734 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008735 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 // + " to " + newFocus);
8737 if (newFocus != null && lastFocus != null
8738 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008739 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 mLosingFocus.add(lastFocus);
8741 lastFocus = null;
8742 }
8743 }
8744
8745 if (lastFocus != newFocus) {
8746 //System.out.println("Changing focus from " + lastFocus
8747 // + " to " + newFocus);
8748 if (newFocus != null) {
8749 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008750 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008751 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8752 } catch (RemoteException e) {
8753 // Ignore if process has died.
8754 }
8755 }
8756
8757 if (lastFocus != null) {
8758 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008759 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8761 } catch (RemoteException e) {
8762 // Ignore if process has died.
8763 }
8764 }
8765 }
8766 } break;
8767
8768 case REPORT_LOSING_FOCUS: {
8769 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 synchronized(mWindowMap) {
8772 losers = mLosingFocus;
8773 mLosingFocus = new ArrayList<WindowState>();
8774 }
8775
8776 final int N = losers.size();
8777 for (int i=0; i<N; i++) {
8778 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008779 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8781 } catch (RemoteException e) {
8782 // Ignore if process has died.
8783 }
8784 }
8785 } break;
8786
8787 case ANIMATE: {
8788 synchronized(mWindowMap) {
8789 mAnimationPending = false;
8790 performLayoutAndPlaceSurfacesLocked();
8791 }
8792 } break;
8793
8794 case ADD_STARTING: {
8795 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8796 final StartingData sd = wtoken.startingData;
8797
8798 if (sd == null) {
8799 // Animation has been canceled... do nothing.
8800 return;
8801 }
Romain Guy06882f82009-06-10 13:36:04 -07008802
Joe Onorato8a9b2202010-02-26 18:56:32 -08008803 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 View view = null;
8807 try {
8808 view = mPolicy.addStartingWindow(
8809 wtoken.token, sd.pkg,
8810 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8811 sd.icon);
8812 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008813 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 }
8815
8816 if (view != null) {
8817 boolean abort = false;
8818
8819 synchronized(mWindowMap) {
8820 if (wtoken.removed || wtoken.startingData == null) {
8821 // If the window was successfully added, then
8822 // we need to remove it.
8823 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008824 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 "Aborted starting " + wtoken
8826 + ": removed=" + wtoken.removed
8827 + " startingData=" + wtoken.startingData);
8828 wtoken.startingWindow = null;
8829 wtoken.startingData = null;
8830 abort = true;
8831 }
8832 } else {
8833 wtoken.startingView = view;
8834 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008835 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 "Added starting " + wtoken
8837 + ": startingWindow="
8838 + wtoken.startingWindow + " startingView="
8839 + wtoken.startingView);
8840 }
8841
8842 if (abort) {
8843 try {
8844 mPolicy.removeStartingWindow(wtoken.token, view);
8845 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008846 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008847 }
8848 }
8849 }
8850 } break;
8851
8852 case REMOVE_STARTING: {
8853 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8854 IBinder token = null;
8855 View view = null;
8856 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008857 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 + wtoken + ": startingWindow="
8859 + wtoken.startingWindow + " startingView="
8860 + wtoken.startingView);
8861 if (wtoken.startingWindow != null) {
8862 view = wtoken.startingView;
8863 token = wtoken.token;
8864 wtoken.startingData = null;
8865 wtoken.startingView = null;
8866 wtoken.startingWindow = null;
8867 }
8868 }
8869 if (view != null) {
8870 try {
8871 mPolicy.removeStartingWindow(token, view);
8872 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008873 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 }
8875 }
8876 } break;
8877
8878 case FINISHED_STARTING: {
8879 IBinder token = null;
8880 View view = null;
8881 while (true) {
8882 synchronized (mWindowMap) {
8883 final int N = mFinishedStarting.size();
8884 if (N <= 0) {
8885 break;
8886 }
8887 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8888
Joe Onorato8a9b2202010-02-26 18:56:32 -08008889 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008890 "Finished starting " + wtoken
8891 + ": startingWindow=" + wtoken.startingWindow
8892 + " startingView=" + wtoken.startingView);
8893
8894 if (wtoken.startingWindow == null) {
8895 continue;
8896 }
8897
8898 view = wtoken.startingView;
8899 token = wtoken.token;
8900 wtoken.startingData = null;
8901 wtoken.startingView = null;
8902 wtoken.startingWindow = null;
8903 }
8904
8905 try {
8906 mPolicy.removeStartingWindow(token, view);
8907 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008908 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909 }
8910 }
8911 } break;
8912
8913 case REPORT_APPLICATION_TOKEN_WINDOWS: {
8914 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8915
8916 boolean nowVisible = msg.arg1 != 0;
8917 boolean nowGone = msg.arg2 != 0;
8918
8919 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008920 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 TAG, "Reporting visible in " + wtoken
8922 + " visible=" + nowVisible
8923 + " gone=" + nowGone);
8924 if (nowVisible) {
8925 wtoken.appToken.windowsVisible();
8926 } else {
8927 wtoken.appToken.windowsGone();
8928 }
8929 } catch (RemoteException ex) {
8930 }
8931 } break;
Romain Guy06882f82009-06-10 13:36:04 -07008932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 case WINDOW_FREEZE_TIMEOUT: {
8934 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008935 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 int i = mWindows.size();
8937 while (i > 0) {
8938 i--;
8939 WindowState w = (WindowState)mWindows.get(i);
8940 if (w.mOrientationChanging) {
8941 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008942 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 }
8944 }
8945 performLayoutAndPlaceSurfacesLocked();
8946 }
8947 break;
8948 }
Romain Guy06882f82009-06-10 13:36:04 -07008949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 case HOLD_SCREEN_CHANGED: {
8951 Session oldHold;
8952 Session newHold;
8953 synchronized (mWindowMap) {
8954 oldHold = mLastReportedHold;
8955 newHold = (Session)msg.obj;
8956 mLastReportedHold = newHold;
8957 }
Romain Guy06882f82009-06-10 13:36:04 -07008958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 if (oldHold != newHold) {
8960 try {
8961 if (oldHold != null) {
8962 mBatteryStats.noteStopWakelock(oldHold.mUid,
8963 "window",
8964 BatteryStats.WAKE_TYPE_WINDOW);
8965 }
8966 if (newHold != null) {
8967 mBatteryStats.noteStartWakelock(newHold.mUid,
8968 "window",
8969 BatteryStats.WAKE_TYPE_WINDOW);
8970 }
8971 } catch (RemoteException e) {
8972 }
8973 }
8974 break;
8975 }
Romain Guy06882f82009-06-10 13:36:04 -07008976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 case APP_TRANSITION_TIMEOUT: {
8978 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008979 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008980 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008981 "*** APP TRANSITION TIMEOUT");
8982 mAppTransitionReady = true;
8983 mAppTransitionTimeout = true;
8984 performLayoutAndPlaceSurfacesLocked();
8985 }
8986 }
8987 break;
8988 }
Romain Guy06882f82009-06-10 13:36:04 -07008989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008990 case PERSIST_ANIMATION_SCALE: {
8991 Settings.System.putFloat(mContext.getContentResolver(),
8992 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
8993 Settings.System.putFloat(mContext.getContentResolver(),
8994 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
8995 break;
8996 }
Romain Guy06882f82009-06-10 13:36:04 -07008997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 case FORCE_GC: {
8999 synchronized(mWindowMap) {
9000 if (mAnimationPending) {
9001 // If we are animating, don't do the gc now but
9002 // delay a bit so we don't interrupt the animation.
9003 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9004 2000);
9005 return;
9006 }
9007 // If we are currently rotating the display, it will
9008 // schedule a new message when done.
9009 if (mDisplayFrozen) {
9010 return;
9011 }
9012 mFreezeGcPending = 0;
9013 }
9014 Runtime.getRuntime().gc();
9015 break;
9016 }
Romain Guy06882f82009-06-10 13:36:04 -07009017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009018 case ENABLE_SCREEN: {
9019 performEnableScreen();
9020 break;
9021 }
Romain Guy06882f82009-06-10 13:36:04 -07009022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 case APP_FREEZE_TIMEOUT: {
9024 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009025 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 int i = mAppTokens.size();
9027 while (i > 0) {
9028 i--;
9029 AppWindowToken tok = mAppTokens.get(i);
9030 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009031 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 unsetAppFreezingScreenLocked(tok, true, true);
9033 }
9034 }
9035 }
9036 break;
9037 }
Romain Guy06882f82009-06-10 13:36:04 -07009038
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009039 case SEND_NEW_CONFIGURATION: {
9040 removeMessages(SEND_NEW_CONFIGURATION);
9041 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009042 break;
9043 }
Romain Guy06882f82009-06-10 13:36:04 -07009044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 }
9046 }
9047 }
9048
9049 // -------------------------------------------------------------
9050 // IWindowManager API
9051 // -------------------------------------------------------------
9052
9053 public IWindowSession openSession(IInputMethodClient client,
9054 IInputContext inputContext) {
9055 if (client == null) throw new IllegalArgumentException("null client");
9056 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9057 return new Session(client, inputContext);
9058 }
9059
9060 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9061 synchronized (mWindowMap) {
9062 // The focus for the client is the window immediately below
9063 // where we would place the input method window.
9064 int idx = findDesiredInputMethodWindowIndexLocked(false);
9065 WindowState imFocus;
9066 if (idx > 0) {
9067 imFocus = (WindowState)mWindows.get(idx-1);
9068 if (imFocus != null) {
9069 if (imFocus.mSession.mClient != null &&
9070 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9071 return true;
9072 }
9073 }
9074 }
9075 }
9076 return false;
9077 }
Romain Guy06882f82009-06-10 13:36:04 -07009078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 // -------------------------------------------------------------
9080 // Internals
9081 // -------------------------------------------------------------
9082
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009083 final WindowState windowForClientLocked(Session session, IWindow client,
9084 boolean throwOnError) {
9085 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 }
Romain Guy06882f82009-06-10 13:36:04 -07009087
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009088 final WindowState windowForClientLocked(Session session, IBinder client,
9089 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009091 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 TAG, "Looking up client " + client + ": " + win);
9093 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009094 RuntimeException ex = new IllegalArgumentException(
9095 "Requested window " + client + " does not exist");
9096 if (throwOnError) {
9097 throw ex;
9098 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009099 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100 return null;
9101 }
9102 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009103 RuntimeException ex = new IllegalArgumentException(
9104 "Requested window " + client + " is in session " +
9105 win.mSession + ", not " + session);
9106 if (throwOnError) {
9107 throw ex;
9108 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009109 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009110 return null;
9111 }
9112
9113 return win;
9114 }
9115
Dianne Hackborna8f60182009-09-01 19:01:50 -07009116 final void rebuildAppWindowListLocked() {
9117 int NW = mWindows.size();
9118 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009119 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009120 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009121
Dianne Hackborna8f60182009-09-01 19:01:50 -07009122 // First remove all existing app windows.
9123 i=0;
9124 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009125 WindowState w = (WindowState)mWindows.get(i);
9126 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009127 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009128 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009129 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009130 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009131 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009132 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009133 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9134 && lastWallpaper == i-1) {
9135 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009136 }
9137 i++;
9138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009139
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009140 // The wallpaper window(s) typically live at the bottom of the stack,
9141 // so skip them before adding app tokens.
9142 lastWallpaper++;
9143 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009144
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009145 // First add all of the exiting app tokens... these are no longer
9146 // in the main app list, but still have windows shown. We put them
9147 // in the back because now that the animation is over we no longer
9148 // will care about them.
9149 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009150 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009151 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009153
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009154 // And add in the still active app tokens in Z order.
9155 NT = mAppTokens.size();
9156 for (int j=0; j<NT; j++) {
9157 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009159
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009160 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009161 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009162 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009163 + " windows but added " + i);
9164 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009167 private final void assignLayersLocked() {
9168 int N = mWindows.size();
9169 int curBaseLayer = 0;
9170 int curLayer = 0;
9171 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 for (i=0; i<N; i++) {
9174 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009175 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9176 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 curLayer += WINDOW_LAYER_MULTIPLIER;
9178 w.mLayer = curLayer;
9179 } else {
9180 curBaseLayer = curLayer = w.mBaseLayer;
9181 w.mLayer = curLayer;
9182 }
9183 if (w.mTargetAppToken != null) {
9184 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9185 } else if (w.mAppToken != null) {
9186 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9187 } else {
9188 w.mAnimLayer = w.mLayer;
9189 }
9190 if (w.mIsImWindow) {
9191 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009192 } else if (w.mIsWallpaper) {
9193 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009194 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009195 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009196 + w.mAnimLayer);
9197 //System.out.println(
9198 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9199 }
9200 }
9201
9202 private boolean mInLayout = false;
9203 private final void performLayoutAndPlaceSurfacesLocked() {
9204 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009205 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 throw new RuntimeException("Recursive call!");
9207 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009208 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 return;
9210 }
9211
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009212 if (mWaitingForConfig) {
9213 // Our configuration has changed (most likely rotation), but we
9214 // don't yet have the complete configuration to report to
9215 // applications. Don't do any window layout until we have it.
9216 return;
9217 }
9218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 boolean recoveringMemory = false;
9220 if (mForceRemoves != null) {
9221 recoveringMemory = true;
9222 // Wait a little it for things to settle down, and off we go.
9223 for (int i=0; i<mForceRemoves.size(); i++) {
9224 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009225 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009226 removeWindowInnerLocked(ws.mSession, ws);
9227 }
9228 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009229 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 Object tmp = new Object();
9231 synchronized (tmp) {
9232 try {
9233 tmp.wait(250);
9234 } catch (InterruptedException e) {
9235 }
9236 }
9237 }
Romain Guy06882f82009-06-10 13:36:04 -07009238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 mInLayout = true;
9240 try {
9241 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 int i = mPendingRemove.size()-1;
9244 if (i >= 0) {
9245 while (i >= 0) {
9246 WindowState w = mPendingRemove.get(i);
9247 removeWindowInnerLocked(w.mSession, w);
9248 i--;
9249 }
9250 mPendingRemove.clear();
9251
9252 mInLayout = false;
9253 assignLayersLocked();
9254 mLayoutNeeded = true;
9255 performLayoutAndPlaceSurfacesLocked();
9256
9257 } else {
9258 mInLayout = false;
9259 if (mLayoutNeeded) {
9260 requestAnimationLocked(0);
9261 }
9262 }
9263 } catch (RuntimeException e) {
9264 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009265 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 }
9267 }
9268
9269 private final void performLayoutLockedInner() {
9270 final int dw = mDisplay.getWidth();
9271 final int dh = mDisplay.getHeight();
9272
9273 final int N = mWindows.size();
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009274 int repeats = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 int i;
9276
Joe Onorato8a9b2202010-02-26 18:56:32 -08009277 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009278 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 // FIRST LOOP: Perform a layout, if needed.
Romain Guy06882f82009-06-10 13:36:04 -07009281
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009282 while (mLayoutNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 mPolicy.beginLayoutLw(dw, dh);
9284
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009285 int seq = mLayoutSeq+1;
9286 if (seq < 0) seq = 0;
9287 mLayoutSeq = seq;
9288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 // First perform layout of any root windows (not attached
9290 // to another window).
9291 int topAttached = -1;
9292 for (i = N-1; i >= 0; i--) {
9293 WindowState win = (WindowState) mWindows.get(i);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009294
9295 // Don't do layout of a window if it is not visible, or
9296 // soon won't be visible, to avoid wasting time and funky
9297 // changes while a window is animating away.
9298 final AppWindowToken atoken = win.mAppToken;
9299 final boolean gone = win.mViewVisibility == View.GONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 || !win.mRelayoutCalled
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009301 || win.mRootToken.hidden
9302 || (atoken != null && atoken.hiddenRequested)
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009303 || win.mAttachedHidden
9304 || win.mExiting || win.mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009306 if (!win.mLayoutAttached) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009307 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009308 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9309 + " mLayoutAttached=" + win.mLayoutAttached);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009310 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009311 + win.mViewVisibility + " mRelayoutCalled="
9312 + win.mRelayoutCalled + " hidden="
9313 + win.mRootToken.hidden + " hiddenRequested="
9314 + (atoken != null && atoken.hiddenRequested)
9315 + " mAttachedHidden=" + win.mAttachedHidden);
9316 }
9317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 // If this view is GONE, then skip it -- keep the current
9319 // frame, and let the caller know so they can ignore it
9320 // if they want. (We do the normal layout for INVISIBLE
9321 // windows, since that means "perform layout as normal,
9322 // just don't display").
9323 if (!gone || !win.mHaveFrame) {
9324 if (!win.mLayoutAttached) {
9325 mPolicy.layoutWindowLw(win, win.mAttrs, null);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009326 win.mLayoutSeq = seq;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009327 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009328 + win.mFrame + " mContainingFrame="
9329 + win.mContainingFrame + " mDisplayFrame="
9330 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 } else {
9332 if (topAttached < 0) topAttached = i;
9333 }
9334 }
9335 }
Romain Guy06882f82009-06-10 13:36:04 -07009336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009337 // Now perform layout of attached windows, which usually
9338 // depend on the position of the window they are attached to.
9339 // XXX does not deal with windows that are attached to windows
9340 // that are themselves attached.
9341 for (i = topAttached; i >= 0; i--) {
9342 WindowState win = (WindowState) mWindows.get(i);
9343
9344 // If this view is GONE, then skip it -- keep the current
9345 // frame, and let the caller know so they can ignore it
9346 // if they want. (We do the normal layout for INVISIBLE
9347 // windows, since that means "perform layout as normal,
9348 // just don't display").
9349 if (win.mLayoutAttached) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009350 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009351 + " mHaveFrame=" + win.mHaveFrame
9352 + " mViewVisibility=" + win.mViewVisibility
9353 + " mRelayoutCalled=" + win.mRelayoutCalled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9355 || !win.mHaveFrame) {
9356 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009357 win.mLayoutSeq = seq;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009358 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009359 + win.mFrame + " mContainingFrame="
9360 + win.mContainingFrame + " mDisplayFrame="
9361 + win.mDisplayFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009362 }
9363 }
9364 }
9365
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009366 int changes = mPolicy.finishLayoutLw();
9367 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9368 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9369 assignLayersLocked();
9370 }
9371 }
9372 if (changes == 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009373 mLayoutNeeded = false;
9374 } else if (repeats > 2) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009375 Slog.w(TAG, "Layout repeat aborted after too many iterations");
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009376 mLayoutNeeded = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009377 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009378 if (updateOrientationFromAppTokensLocked()) {
9379 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009380 }
9381 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009382 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009383 if (DEBUG_LAYOUT) Slog.v(TAG, "Repeating layout because changes=0x"
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009384 + Integer.toHexString(changes));
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009385 repeats++;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009386 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009387 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009388 if (updateOrientationFromAppTokensLocked()) {
9389 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009390 }
9391 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 }
9394 }
Romain Guy06882f82009-06-10 13:36:04 -07009395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 private final void performLayoutAndPlaceSurfacesLockedInner(
9397 boolean recoveringMemory) {
9398 final long currentTime = SystemClock.uptimeMillis();
9399 final int dw = mDisplay.getWidth();
9400 final int dh = mDisplay.getHeight();
9401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 int i;
9403
9404 // FIRST LOOP: Perform a layout, if needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 performLayoutLockedInner();
Romain Guy06882f82009-06-10 13:36:04 -07009406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 if (mFxSession == null) {
9408 mFxSession = new SurfaceSession();
9409 }
Romain Guy06882f82009-06-10 13:36:04 -07009410
Joe Onorato8a9b2202010-02-26 18:56:32 -08009411 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009412
9413 // Initialize state of exiting tokens.
9414 for (i=mExitingTokens.size()-1; i>=0; i--) {
9415 mExitingTokens.get(i).hasVisible = false;
9416 }
9417
9418 // Initialize state of exiting applications.
9419 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9420 mExitingAppTokens.get(i).hasVisible = false;
9421 }
9422
9423 // SECOND LOOP: Execute animations and update visibility of windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009424 boolean orientationChangeComplete = true;
9425 Session holdScreen = null;
9426 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009427 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009428 boolean focusDisplayed = false;
9429 boolean animating = false;
9430
9431 Surface.openTransaction();
9432 try {
9433 boolean restart;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009434 boolean forceHiding = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009435 boolean wallpaperForceHidingChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009436
9437 do {
9438 final int transactionSequence = ++mTransactionSequence;
9439
9440 // Update animations of all applications, including those
9441 // associated with exiting/removed apps
9442 boolean tokensAnimating = false;
9443 final int NAT = mAppTokens.size();
9444 for (i=0; i<NAT; i++) {
9445 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9446 tokensAnimating = true;
9447 }
9448 }
9449 final int NEAT = mExitingAppTokens.size();
9450 for (i=0; i<NEAT; i++) {
9451 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9452 tokensAnimating = true;
9453 }
9454 }
9455
Joe Onorato8a9b2202010-02-26 18:56:32 -08009456 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009457 + transactionSequence + " tokensAnimating="
9458 + tokensAnimating);
9459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460 animating = tokensAnimating;
9461 restart = false;
9462
9463 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009464 boolean wallpaperMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009465 boolean focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466
9467 mPolicy.beginAnimationLw(dw, dh);
9468
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009469 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 for (i=N-1; i>=0; i--) {
9472 WindowState w = (WindowState)mWindows.get(i);
9473
9474 final WindowManager.LayoutParams attrs = w.mAttrs;
9475
9476 if (w.mSurface != null) {
9477 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009478 if (w.commitFinishDrawingLocked(currentTime)) {
9479 if ((w.mAttrs.flags
9480 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009481 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009482 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009483 wallpaperMayChange = true;
9484 }
9485 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009486
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009487 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009488 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9489 animating = true;
9490 //w.dump(" ");
9491 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009492 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9493 wallpaperMayChange = true;
9494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009495
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009496 if (mPolicy.doesForceHide(w, attrs)) {
9497 if (!wasAnimating && animating) {
9498 wallpaperForceHidingChanged = true;
9499 focusMayChange = true;
9500 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9501 forceHiding = true;
9502 }
9503 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9504 boolean changed;
9505 if (forceHiding) {
9506 changed = w.hideLw(false, false);
9507 } else {
9508 changed = w.showLw(false, false);
9509 if (changed && wallpaperForceHidingChanged
9510 && w.isReadyForDisplay()) {
9511 // Assume we will need to animate. If
9512 // we don't (because the wallpaper will
9513 // stay with the lock screen), then we will
9514 // clean up later.
9515 Animation a = mPolicy.createForceHideEnterAnimation();
9516 if (a != null) {
9517 w.setAnimation(a);
9518 }
9519 }
9520 }
9521 if (changed && (attrs.flags
9522 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9523 wallpaperMayChange = true;
9524 }
9525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009527 mPolicy.animatingWindowLw(w, attrs);
9528 }
9529
9530 final AppWindowToken atoken = w.mAppToken;
9531 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9532 if (atoken.lastTransactionSequence != transactionSequence) {
9533 atoken.lastTransactionSequence = transactionSequence;
9534 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9535 atoken.startingDisplayed = false;
9536 }
9537 if ((w.isOnScreen() || w.mAttrs.type
9538 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9539 && !w.mExiting && !w.mDestroying) {
9540 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009541 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009542 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009544 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009545 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 + " pv=" + w.mPolicyVisibility
9547 + " dp=" + w.mDrawPending
9548 + " cdp=" + w.mCommitDrawPending
9549 + " ah=" + w.mAttachedHidden
9550 + " th=" + atoken.hiddenRequested
9551 + " a=" + w.mAnimating);
9552 }
9553 }
9554 if (w != atoken.startingWindow) {
9555 if (!atoken.freezingScreen || !w.mAppFreezing) {
9556 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009557 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009559 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 "tokenMayBeDrawn: " + atoken
9561 + " freezingScreen=" + atoken.freezingScreen
9562 + " mAppFreezing=" + w.mAppFreezing);
9563 tokenMayBeDrawn = true;
9564 }
9565 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009566 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 atoken.startingDisplayed = true;
9568 }
9569 }
9570 } else if (w.mReadyToShow) {
9571 w.performShowLocked();
9572 }
9573 }
9574
9575 if (mPolicy.finishAnimationLw()) {
9576 restart = true;
9577 }
9578
9579 if (tokenMayBeDrawn) {
9580 // See if any windows have been drawn, so they (and others
9581 // associated with them) can now be shown.
9582 final int NT = mTokenList.size();
9583 for (i=0; i<NT; i++) {
9584 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9585 if (wtoken == null) {
9586 continue;
9587 }
9588 if (wtoken.freezingScreen) {
9589 int numInteresting = wtoken.numInterestingWindows;
9590 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009591 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 "allDrawn: " + wtoken
9593 + " interesting=" + numInteresting
9594 + " drawn=" + wtoken.numDrawnWindows);
9595 wtoken.showAllWindowsLocked();
9596 unsetAppFreezingScreenLocked(wtoken, false, true);
9597 orientationChangeComplete = true;
9598 }
9599 } else if (!wtoken.allDrawn) {
9600 int numInteresting = wtoken.numInterestingWindows;
9601 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009602 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 "allDrawn: " + wtoken
9604 + " interesting=" + numInteresting
9605 + " drawn=" + wtoken.numDrawnWindows);
9606 wtoken.allDrawn = true;
9607 restart = true;
9608
9609 // We can now show all of the drawn windows!
9610 if (!mOpeningApps.contains(wtoken)) {
9611 wtoken.showAllWindowsLocked();
9612 }
9613 }
9614 }
9615 }
9616 }
9617
9618 // If we are ready to perform an app transition, check through
9619 // all of the app tokens to be shown and see if they are ready
9620 // to go.
9621 if (mAppTransitionReady) {
9622 int NN = mOpeningApps.size();
9623 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009624 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 "Checking " + NN + " opening apps (frozen="
9626 + mDisplayFrozen + " timeout="
9627 + mAppTransitionTimeout + ")...");
9628 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9629 // If the display isn't frozen, wait to do anything until
9630 // all of the apps are ready. Otherwise just go because
9631 // we'll unfreeze the display when everyone is ready.
9632 for (i=0; i<NN && goodToGo; i++) {
9633 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009634 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635 "Check opening app" + wtoken + ": allDrawn="
9636 + wtoken.allDrawn + " startingDisplayed="
9637 + wtoken.startingDisplayed);
9638 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9639 && !wtoken.startingMoved) {
9640 goodToGo = false;
9641 }
9642 }
9643 }
9644 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009645 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 int transit = mNextAppTransition;
9647 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009648 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009649 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009650 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009652 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 mAppTransitionTimeout = false;
9654 mStartingIconInTransition = false;
9655 mSkipAppTransitionAnimation = false;
9656
9657 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9658
Dianne Hackborna8f60182009-09-01 19:01:50 -07009659 // If there are applications waiting to come to the
9660 // top of the stack, now is the time to move their windows.
9661 // (Note that we don't do apps going to the bottom
9662 // here -- we want to keep their windows in the old
9663 // Z-order until the animation completes.)
9664 if (mToTopApps.size() > 0) {
9665 NN = mAppTokens.size();
9666 for (i=0; i<NN; i++) {
9667 AppWindowToken wtoken = mAppTokens.get(i);
9668 if (wtoken.sendingToTop) {
9669 wtoken.sendingToTop = false;
9670 moveAppWindowsLocked(wtoken, NN, false);
9671 }
9672 }
9673 mToTopApps.clear();
9674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009675
Dianne Hackborn25994b42009-09-04 14:21:19 -07009676 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009677
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009678 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009679 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009680
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009681 // The top-most window will supply the layout params,
9682 // and we will determine it below.
9683 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009684 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009685 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009686
Joe Onorato8a9b2202010-02-26 18:56:32 -08009687 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009688 "New wallpaper target=" + mWallpaperTarget
9689 + ", lower target=" + mLowerWallpaperTarget
9690 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009691 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009692 // Do a first pass through the tokens for two
9693 // things:
9694 // (1) Determine if both the closing and opening
9695 // app token sets are wallpaper targets, in which
9696 // case special animations are needed
9697 // (since the wallpaper needs to stay static
9698 // behind them).
9699 // (2) Find the layout params of the top-most
9700 // application window in the tokens, which is
9701 // what will control the animation theme.
9702 final int NC = mClosingApps.size();
9703 NN = NC + mOpeningApps.size();
9704 for (i=0; i<NN; i++) {
9705 AppWindowToken wtoken;
9706 int mode;
9707 if (i < NC) {
9708 wtoken = mClosingApps.get(i);
9709 mode = 1;
9710 } else {
9711 wtoken = mOpeningApps.get(i-NC);
9712 mode = 2;
9713 }
9714 if (mLowerWallpaperTarget != null) {
9715 if (mLowerWallpaperTarget.mAppToken == wtoken
9716 || mUpperWallpaperTarget.mAppToken == wtoken) {
9717 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009718 }
9719 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009720 if (wtoken.appFullscreen) {
9721 WindowState ws = wtoken.findMainWindow();
9722 if (ws != null) {
9723 // If this is a compatibility mode
9724 // window, we will always use its anim.
9725 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9726 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009727 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009728 bestAnimLayer = Integer.MAX_VALUE;
9729 } else if (ws.mLayer > bestAnimLayer) {
9730 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009731 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009732 bestAnimLayer = ws.mLayer;
9733 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009734 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009735 }
9736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009737
Dianne Hackborn25994b42009-09-04 14:21:19 -07009738 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009739 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009740 "Wallpaper animation!");
9741 switch (transit) {
9742 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9743 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9744 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9745 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9746 break;
9747 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9748 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9749 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9750 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9751 break;
9752 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009753 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009754 "New transit: " + transit);
9755 } else if (oldWallpaper != null) {
9756 // We are transitioning from an activity with
9757 // a wallpaper to one without.
9758 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009759 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009760 "New transit away from wallpaper: " + transit);
9761 } else if (mWallpaperTarget != null) {
9762 // We are transitioning from an activity without
9763 // a wallpaper to now showing the wallpaper
9764 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009765 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009766 "New transit into wallpaper: " + transit);
9767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009768
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009769 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9770 mLastEnterAnimToken = animToken;
9771 mLastEnterAnimParams = animLp;
9772 } else if (mLastEnterAnimParams != null) {
9773 animLp = mLastEnterAnimParams;
9774 mLastEnterAnimToken = null;
9775 mLastEnterAnimParams = null;
9776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009777
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009778 // If all closing windows are obscured, then there is
9779 // no need to do an animation. This is the case, for
9780 // example, when this transition is being done behind
9781 // the lock screen.
9782 if (!mPolicy.allowAppAnimationsLw()) {
9783 animLp = null;
9784 }
9785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 NN = mOpeningApps.size();
9787 for (i=0; i<NN; i++) {
9788 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009789 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 "Now opening app" + wtoken);
9791 wtoken.reportedVisible = false;
9792 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009793 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009794 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009795 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009796 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 wtoken.showAllWindowsLocked();
9798 }
9799 NN = mClosingApps.size();
9800 for (i=0; i<NN; i++) {
9801 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009802 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 "Now closing app" + wtoken);
9804 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009805 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009806 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009808 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 // Force the allDrawn flag, because we want to start
9810 // this guy's animations regardless of whether it's
9811 // gotten drawn.
9812 wtoken.allDrawn = true;
9813 }
9814
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009815 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 mOpeningApps.clear();
9818 mClosingApps.clear();
9819
9820 // This has changed the visibility of windows, so perform
9821 // a new layout to get them all up-to-date.
9822 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009823 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9824 assignLayersLocked();
9825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 performLayoutLockedInner();
9827 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009828 focusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829
9830 restart = true;
9831 }
9832 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009833
Dianne Hackborna8f60182009-09-01 19:01:50 -07009834 if (!animating && mAppTransitionRunning) {
9835 // We have finished the animation of an app transition. To do
9836 // this, we have delayed a lot of operations like showing and
9837 // hiding apps, moving apps in Z-order, etc. The app token list
9838 // reflects the correct Z-order, but the window list may now
9839 // be out of sync with it. So here we will just rebuild the
9840 // entire app window list. Fun!
9841 mAppTransitionRunning = false;
9842 // Clear information about apps that were moving.
9843 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009844
Dianne Hackborna8f60182009-09-01 19:01:50 -07009845 rebuildAppWindowListLocked();
9846 restart = true;
9847 moveInputMethodWindowsIfNeededLocked(false);
9848 wallpaperMayChange = true;
9849 mLayoutNeeded = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009850 // Since the window list has been rebuilt, focus might
9851 // have to be recomputed since the actual order of windows
9852 // might have changed again.
9853 focusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009855
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009856 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009857
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009858 if (wallpaperForceHidingChanged && !restart && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009859 // At this point, there was a window with a wallpaper that
9860 // was force hiding other windows behind it, but now it
9861 // is going away. This may be simple -- just animate
9862 // away the wallpaper and its window -- or it may be
9863 // hard -- the wallpaper now needs to be shown behind
9864 // something that was hidden.
9865 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009866 if (mLowerWallpaperTarget != null
9867 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009868 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009869 "wallpaperForceHiding changed with lower="
9870 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009871 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009872 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
9873 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
9874 if (mLowerWallpaperTarget.mAppToken.hidden) {
9875 // The lower target has become hidden before we
9876 // actually started the animation... let's completely
9877 // re-evaluate everything.
9878 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
9879 restart = true;
9880 }
9881 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009882 adjResult = adjustWallpaperWindowsLocked();
9883 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009884 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009885 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009886 + " NEW: " + mWallpaperTarget
9887 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009888 if (mLowerWallpaperTarget == null) {
9889 // Whoops, we don't need a special wallpaper animation.
9890 // Clear them out.
9891 forceHiding = false;
9892 for (i=N-1; i>=0; i--) {
9893 WindowState w = (WindowState)mWindows.get(i);
9894 if (w.mSurface != null) {
9895 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -07009896 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009897 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009898 forceHiding = true;
9899 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9900 if (!w.mAnimating) {
9901 // We set the animation above so it
9902 // is not yet running.
9903 w.clearAnimation();
9904 }
9905 }
9906 }
9907 }
9908 }
9909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009910
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009911 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009912 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009913 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009914 adjResult = adjustWallpaperWindowsLocked();
9915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009916
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009917 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009918 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009919 "Wallpaper layer changed: assigning layers + relayout");
9920 restart = true;
9921 mLayoutNeeded = true;
9922 assignLayersLocked();
9923 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009924 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009925 "Wallpaper visibility changed: relayout");
9926 restart = true;
9927 mLayoutNeeded = true;
9928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009929
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009930 if (focusMayChange) {
9931 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009932 restart = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009933 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009934 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009935 }
9936
9937 if (mLayoutNeeded) {
9938 restart = true;
9939 performLayoutLockedInner();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009941
Joe Onorato8a9b2202010-02-26 18:56:32 -08009942 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: restart="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009943 + restart);
9944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 } while (restart);
9946
9947 // THIRD LOOP: Update the surfaces of all windows.
9948
9949 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
9950
9951 boolean obscured = false;
9952 boolean blurring = false;
9953 boolean dimming = false;
9954 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07009955 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07009956 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009958 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 for (i=N-1; i>=0; i--) {
9961 WindowState w = (WindowState)mWindows.get(i);
9962
9963 boolean displayed = false;
9964 final WindowManager.LayoutParams attrs = w.mAttrs;
9965 final int attrFlags = attrs.flags;
9966
9967 if (w.mSurface != null) {
9968 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08009969 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 TAG, "Placing surface #" + i + " " + w.mSurface
9971 + ": new=" + w.mShownFrame + ", old="
9972 + w.mLastShownFrame);
9973
9974 boolean resize;
9975 int width, height;
9976 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
9977 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
9978 w.mLastRequestedHeight != w.mRequestedHeight;
9979 // for a scaled surface, we just want to use
9980 // the requested size.
9981 width = w.mRequestedWidth;
9982 height = w.mRequestedHeight;
9983 w.mLastRequestedWidth = width;
9984 w.mLastRequestedHeight = height;
9985 w.mLastShownFrame.set(w.mShownFrame);
9986 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009987 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009988 TAG, " SURFACE " + w.mSurface
9989 + ": POS " + w.mShownFrame.left
9990 + ", " + w.mShownFrame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
9992 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009993 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994 if (!recoveringMemory) {
9995 reclaimSomeSurfaceMemoryLocked(w, "position");
9996 }
9997 }
9998 } else {
9999 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10000 width = w.mShownFrame.width();
10001 height = w.mShownFrame.height();
10002 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 }
10004
10005 if (resize) {
10006 if (width < 1) width = 1;
10007 if (height < 1) height = 1;
10008 if (w.mSurface != null) {
10009 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010010 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010011 TAG, " SURFACE " + w.mSurface + ": POS "
10012 + w.mShownFrame.left + ","
10013 + w.mShownFrame.top + " SIZE "
10014 + w.mShownFrame.width() + "x"
10015 + w.mShownFrame.height());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 w.mSurface.setSize(width, height);
10017 w.mSurface.setPosition(w.mShownFrame.left,
10018 w.mShownFrame.top);
10019 } catch (RuntimeException e) {
10020 // If something goes wrong with the surface (such
10021 // as running out of memory), don't take down the
10022 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010023 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 + "size=(" + width + "x" + height
10025 + "), pos=(" + w.mShownFrame.left
10026 + "," + w.mShownFrame.top + ")", e);
10027 if (!recoveringMemory) {
10028 reclaimSomeSurfaceMemoryLocked(w, "size");
10029 }
10030 }
10031 }
10032 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010033 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 w.mContentInsetsChanged =
10035 !w.mLastContentInsets.equals(w.mContentInsets);
10036 w.mVisibleInsetsChanged =
10037 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010038 boolean configChanged =
10039 w.mConfiguration != mCurConfiguration
10040 && (w.mConfiguration == null
10041 || mCurConfiguration.diff(w.mConfiguration) != 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010042 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010043 + ": configChanged=" + configChanged
10044 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010045 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010047 || w.mVisibleInsetsChanged
10048 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049 w.mLastFrame.set(w.mFrame);
10050 w.mLastContentInsets.set(w.mContentInsets);
10051 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010052 // If the screen is currently frozen, then keep
10053 // it frozen until this window draws at its new
10054 // orientation.
10055 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010056 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010057 "Resizing while display frozen: " + w);
10058 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010059 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010060 mWindowsFreezingScreen = true;
10061 // XXX should probably keep timeout from
10062 // when we first froze the display.
10063 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10064 mH.sendMessageDelayed(mH.obtainMessage(
10065 H.WINDOW_FREEZE_TIMEOUT), 2000);
10066 }
10067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 // If the orientation is changing, then we need to
10069 // hold off on unfreezing the display until this
10070 // window has been redrawn; to do that, we need
10071 // to go through the process of getting informed
10072 // by the application when it has finished drawing.
10073 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010074 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075 "Orientation start waiting for draw in "
10076 + w + ", surface " + w.mSurface);
10077 w.mDrawPending = true;
10078 w.mCommitDrawPending = false;
10079 w.mReadyToShow = false;
10080 if (w.mAppToken != null) {
10081 w.mAppToken.allDrawn = false;
10082 }
10083 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010084 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 "Resizing window " + w + " to " + w.mFrame);
10086 mResizingWindows.add(w);
10087 } else if (w.mOrientationChanging) {
10088 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010089 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 "Orientation not waiting for draw in "
10091 + w + ", surface " + w.mSurface);
10092 w.mOrientationChanging = false;
10093 }
10094 }
10095 }
10096
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010097 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 if (!w.mLastHidden) {
10099 //dump();
10100 w.mLastHidden = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010101 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010102 TAG, " SURFACE " + w.mSurface + ": HIDE (performLayout)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 if (w.mSurface != null) {
10104 try {
10105 w.mSurface.hide();
10106 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010107 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 }
10109 }
10110 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10111 }
10112 // If we are waiting for this window to handle an
10113 // orientation change, well, it is hidden, so
10114 // doesn't really matter. Note that this does
10115 // introduce a potential glitch if the window
10116 // becomes unhidden before it has drawn for the
10117 // new orientation.
10118 if (w.mOrientationChanging) {
10119 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010120 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 "Orientation change skips hidden " + w);
10122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010123 } else if (w.mLastLayer != w.mAnimLayer
10124 || w.mLastAlpha != w.mShownAlpha
10125 || w.mLastDsDx != w.mDsDx
10126 || w.mLastDtDx != w.mDtDx
10127 || w.mLastDsDy != w.mDsDy
10128 || w.mLastDtDy != w.mDtDy
10129 || w.mLastHScale != w.mHScale
10130 || w.mLastVScale != w.mVScale
10131 || w.mLastHidden) {
10132 displayed = true;
10133 w.mLastAlpha = w.mShownAlpha;
10134 w.mLastLayer = w.mAnimLayer;
10135 w.mLastDsDx = w.mDsDx;
10136 w.mLastDtDx = w.mDtDx;
10137 w.mLastDsDy = w.mDsDy;
10138 w.mLastDtDy = w.mDtDy;
10139 w.mLastHScale = w.mHScale;
10140 w.mLastVScale = w.mVScale;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010141 if (SHOW_TRANSACTIONS) Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 TAG, " SURFACE " + w.mSurface + ": alpha="
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010143 + w.mShownAlpha + " layer=" + w.mAnimLayer
10144 + " matrix=[" + (w.mDsDx*w.mHScale)
10145 + "," + (w.mDtDx*w.mVScale)
10146 + "][" + (w.mDsDy*w.mHScale)
10147 + "," + (w.mDtDy*w.mVScale) + "]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 if (w.mSurface != null) {
10149 try {
10150 w.mSurface.setAlpha(w.mShownAlpha);
10151 w.mSurface.setLayer(w.mAnimLayer);
10152 w.mSurface.setMatrix(
10153 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10154 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10155 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010156 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010157 if (!recoveringMemory) {
10158 reclaimSomeSurfaceMemoryLocked(w, "update");
10159 }
10160 }
10161 }
10162
10163 if (w.mLastHidden && !w.mDrawPending
10164 && !w.mCommitDrawPending
10165 && !w.mReadyToShow) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010166 if (SHOW_TRANSACTIONS) Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 TAG, " SURFACE " + w.mSurface + ": SHOW (performLayout)");
Joe Onorato8a9b2202010-02-26 18:56:32 -080010168 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 + " during relayout");
10170 if (showSurfaceRobustlyLocked(w)) {
10171 w.mHasDrawn = true;
10172 w.mLastHidden = false;
10173 } else {
10174 w.mOrientationChanging = false;
10175 }
10176 }
10177 if (w.mSurface != null) {
10178 w.mToken.hasVisible = true;
10179 }
10180 } else {
10181 displayed = true;
10182 }
10183
10184 if (displayed) {
10185 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010186 if (attrs.width == LayoutParams.MATCH_PARENT
10187 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 covered = true;
10189 }
10190 }
10191 if (w.mOrientationChanging) {
10192 if (w.mDrawPending || w.mCommitDrawPending) {
10193 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010194 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 "Orientation continue waiting for draw in " + w);
10196 } else {
10197 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010198 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 "Orientation change complete in " + w);
10200 }
10201 }
10202 w.mToken.hasVisible = true;
10203 }
10204 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010205 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 "Orientation change skips hidden " + w);
10207 w.mOrientationChanging = false;
10208 }
10209
10210 final boolean canBeSeen = w.isDisplayedLw();
10211
10212 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10213 focusDisplayed = true;
10214 }
10215
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010216 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010219 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 if (w.mSurface != null) {
10221 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10222 holdScreen = w.mSession;
10223 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010224 if (!syswin && w.mAttrs.screenBrightness >= 0
10225 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010226 screenBrightness = w.mAttrs.screenBrightness;
10227 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010228 if (!syswin && w.mAttrs.buttonBrightness >= 0
10229 && buttonBrightness < 0) {
10230 buttonBrightness = w.mAttrs.buttonBrightness;
10231 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010232 if (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10233 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10234 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR) {
10235 syswin = true;
10236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010238
Dianne Hackborn25994b42009-09-04 14:21:19 -070010239 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10240 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 // This window completely covers everything behind it,
10242 // so we want to leave all of them as unblurred (for
10243 // performance reasons).
10244 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010245 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010246 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010247 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010248 obscured = true;
10249 if (mBackgroundFillerSurface == null) {
10250 try {
10251 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010252 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010253 0, dw, dh,
10254 PixelFormat.OPAQUE,
10255 Surface.FX_SURFACE_NORMAL);
10256 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010257 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010258 }
10259 }
10260 try {
10261 mBackgroundFillerSurface.setPosition(0, 0);
10262 mBackgroundFillerSurface.setSize(dw, dh);
10263 // Using the same layer as Dim because they will never be shown at the
10264 // same time.
10265 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10266 mBackgroundFillerSurface.show();
10267 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010268 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010269 }
10270 backgroundFillerShown = true;
10271 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010272 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010274 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010275 + ": blurring=" + blurring
10276 + " obscured=" + obscured
10277 + " displayed=" + displayed);
10278 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10279 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010280 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010282 if (mDimAnimator == null) {
10283 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010284 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010285 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010287 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 }
10289 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10290 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010291 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 blurring = true;
10293 mBlurShown = true;
10294 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010295 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 + mBlurSurface + ": CREATE");
10297 try {
Romain Guy06882f82009-06-10 13:36:04 -070010298 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010299 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010300 -1, 16, 16,
10301 PixelFormat.OPAQUE,
10302 Surface.FX_SURFACE_BLUR);
10303 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010304 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 }
10306 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010307 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 + mBlurSurface + ": SHOW pos=(0,0) (" +
10309 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10310 if (mBlurSurface != null) {
10311 mBlurSurface.setPosition(0, 0);
10312 mBlurSurface.setSize(dw, dh);
10313 try {
10314 mBlurSurface.show();
10315 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010316 Slog.w(TAG, "Failure showing blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 }
10318 }
10319 }
10320 mBlurSurface.setLayer(w.mAnimLayer-2);
10321 }
10322 }
10323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010324
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010325 if (obscuredChanged && mWallpaperTarget == w) {
10326 // This is the wallpaper target and its obscured state
10327 // changed... make sure the current wallaper's visibility
10328 // has been updated accordingly.
10329 updateWallpaperVisibilityLocked();
10330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010332
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010333 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10334 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010335 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010336 try {
10337 mBackgroundFillerSurface.hide();
10338 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010339 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010340 }
10341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010343 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010344 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10345 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 }
Romain Guy06882f82009-06-10 13:36:04 -070010347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010349 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 + ": HIDE");
10351 try {
10352 mBlurSurface.hide();
10353 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010354 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 }
10356 mBlurShown = false;
10357 }
10358
Joe Onorato8a9b2202010-02-26 18:56:32 -080010359 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010361 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 }
10363
10364 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010365
Joe Onorato8a9b2202010-02-26 18:56:32 -080010366 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 "With display frozen, orientationChangeComplete="
10368 + orientationChangeComplete);
10369 if (orientationChangeComplete) {
10370 if (mWindowsFreezingScreen) {
10371 mWindowsFreezingScreen = false;
10372 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10373 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010374 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 }
Romain Guy06882f82009-06-10 13:36:04 -070010376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 i = mResizingWindows.size();
10378 if (i > 0) {
10379 do {
10380 i--;
10381 WindowState win = mResizingWindows.get(i);
10382 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010383 if (DEBUG_ORIENTATION) Slog.v(TAG, "Reporting new frame to "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010384 + win + ": " + win.mFrame);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010385 boolean configChanged =
10386 win.mConfiguration != mCurConfiguration
10387 && (win.mConfiguration == null
10388 || mCurConfiguration.diff(win.mConfiguration) != 0);
10389 win.mConfiguration = mCurConfiguration;
10390 if (DEBUG_ORIENTATION && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010391 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010392 + win.mFrame.width() + "x" + win.mFrame.height()
10393 + " / " + win.mConfiguration);
10394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 win.mClient.resized(win.mFrame.width(),
10396 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010397 win.mLastVisibleInsets, win.mDrawPending,
10398 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 win.mContentInsetsChanged = false;
10400 win.mVisibleInsetsChanged = false;
10401 } catch (RemoteException e) {
10402 win.mOrientationChanging = false;
10403 }
10404 } while (i > 0);
10405 mResizingWindows.clear();
10406 }
Romain Guy06882f82009-06-10 13:36:04 -070010407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010409 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 i = mDestroySurface.size();
10411 if (i > 0) {
10412 do {
10413 i--;
10414 WindowState win = mDestroySurface.get(i);
10415 win.mDestroying = false;
10416 if (mInputMethodWindow == win) {
10417 mInputMethodWindow = null;
10418 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010419 if (win == mWallpaperTarget) {
10420 wallpaperDestroyed = true;
10421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 win.destroySurfaceLocked();
10423 } while (i > 0);
10424 mDestroySurface.clear();
10425 }
10426
10427 // Time to remove any exiting tokens?
10428 for (i=mExitingTokens.size()-1; i>=0; i--) {
10429 WindowToken token = mExitingTokens.get(i);
10430 if (!token.hasVisible) {
10431 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010432 if (token.windowType == TYPE_WALLPAPER) {
10433 mWallpaperTokens.remove(token);
10434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 }
10436 }
10437
10438 // Time to remove any exiting applications?
10439 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10440 AppWindowToken token = mExitingAppTokens.get(i);
10441 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010442 // Make sure there is no animation running on this token,
10443 // so any windows associated with it will be removed as
10444 // soon as their animations are complete
10445 token.animation = null;
10446 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 mAppTokens.remove(token);
10448 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010449 if (mLastEnterAnimToken == token) {
10450 mLastEnterAnimToken = null;
10451 mLastEnterAnimParams = null;
10452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 }
10454 }
10455
Dianne Hackborna8f60182009-09-01 19:01:50 -070010456 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010457
Dianne Hackborna8f60182009-09-01 19:01:50 -070010458 if (!animating && mAppTransitionRunning) {
10459 // We have finished the animation of an app transition. To do
10460 // this, we have delayed a lot of operations like showing and
10461 // hiding apps, moving apps in Z-order, etc. The app token list
10462 // reflects the correct Z-order, but the window list may now
10463 // be out of sync with it. So here we will just rebuild the
10464 // entire app window list. Fun!
10465 mAppTransitionRunning = false;
10466 needRelayout = true;
10467 rebuildAppWindowListLocked();
10468 // Clear information about apps that were moving.
10469 mToBottomApps.clear();
10470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 if (focusDisplayed) {
10473 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10474 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010475 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010476 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010477 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010478 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010479 requestAnimationLocked(0);
10480 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10482 }
10483 mQueue.setHoldScreenLocked(holdScreen != null);
10484 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10485 mPowerManager.setScreenBrightnessOverride(-1);
10486 } else {
10487 mPowerManager.setScreenBrightnessOverride((int)
10488 (screenBrightness * Power.BRIGHTNESS_ON));
10489 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010490 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10491 mPowerManager.setButtonBrightnessOverride(-1);
10492 } else {
10493 mPowerManager.setButtonBrightnessOverride((int)
10494 (buttonBrightness * Power.BRIGHTNESS_ON));
10495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 if (holdScreen != mHoldingScreenOn) {
10497 mHoldingScreenOn = holdScreen;
10498 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10499 mH.sendMessage(m);
10500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010501
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010502 if (mTurnOnScreen) {
10503 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10504 LocalPowerManager.BUTTON_EVENT, true);
10505 mTurnOnScreen = false;
10506 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010507
10508 // Check to see if we are now in a state where the screen should
10509 // be enabled, because the window obscured flags have changed.
10510 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 }
10512
10513 void requestAnimationLocked(long delay) {
10514 if (!mAnimationPending) {
10515 mAnimationPending = true;
10516 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10517 }
10518 }
Romain Guy06882f82009-06-10 13:36:04 -070010519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 /**
10521 * Have the surface flinger show a surface, robustly dealing with
10522 * error conditions. In particular, if there is not enough memory
10523 * to show the surface, then we will try to get rid of other surfaces
10524 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010525 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 * @return Returns true if the surface was successfully shown.
10527 */
10528 boolean showSurfaceRobustlyLocked(WindowState win) {
10529 try {
10530 if (win.mSurface != null) {
10531 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010532 if (win.mTurnOnScreen) {
10533 win.mTurnOnScreen = false;
10534 mTurnOnScreen = true;
10535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537 return true;
10538 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010539 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 }
Romain Guy06882f82009-06-10 13:36:04 -070010541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 return false;
10545 }
Romain Guy06882f82009-06-10 13:36:04 -070010546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10548 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010549
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010550 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 if (mForceRemoves == null) {
10554 mForceRemoves = new ArrayList<WindowState>();
10555 }
Romain Guy06882f82009-06-10 13:36:04 -070010556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 long callingIdentity = Binder.clearCallingIdentity();
10558 try {
10559 // There was some problem... first, do a sanity check of the
10560 // window list to make sure we haven't left any dangling surfaces
10561 // around.
10562 int N = mWindows.size();
10563 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010564 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 for (int i=0; i<N; i++) {
10566 WindowState ws = (WindowState)mWindows.get(i);
10567 if (ws.mSurface != null) {
10568 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010569 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 + ws + " surface=" + ws.mSurface
10571 + " token=" + win.mToken
10572 + " pid=" + ws.mSession.mPid
10573 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010574 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 ws.mSurface = null;
10576 mForceRemoves.add(ws);
10577 i--;
10578 N--;
10579 leakedSurface = true;
10580 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010581 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 + ws + " surface=" + ws.mSurface
10583 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010584 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 ws.mSurface = null;
10586 leakedSurface = true;
10587 }
10588 }
10589 }
Romain Guy06882f82009-06-10 13:36:04 -070010590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 boolean killedApps = false;
10592 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010593 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 SparseIntArray pidCandidates = new SparseIntArray();
10595 for (int i=0; i<N; i++) {
10596 WindowState ws = (WindowState)mWindows.get(i);
10597 if (ws.mSurface != null) {
10598 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10599 }
10600 }
10601 if (pidCandidates.size() > 0) {
10602 int[] pids = new int[pidCandidates.size()];
10603 for (int i=0; i<pids.length; i++) {
10604 pids[i] = pidCandidates.keyAt(i);
10605 }
10606 try {
10607 if (mActivityManager.killPidsForMemory(pids)) {
10608 killedApps = true;
10609 }
10610 } catch (RemoteException e) {
10611 }
10612 }
10613 }
Romain Guy06882f82009-06-10 13:36:04 -070010614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 if (leakedSurface || killedApps) {
10616 // We managed to reclaim some memory, so get rid of the trouble
10617 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010618 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010620 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621 win.mSurface = null;
10622 }
Romain Guy06882f82009-06-10 13:36:04 -070010623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 try {
10625 win.mClient.dispatchGetNewSurface();
10626 } catch (RemoteException e) {
10627 }
10628 }
10629 } finally {
10630 Binder.restoreCallingIdentity(callingIdentity);
10631 }
10632 }
Romain Guy06882f82009-06-10 13:36:04 -070010633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 private boolean updateFocusedWindowLocked(int mode) {
10635 WindowState newFocus = computeFocusedWindowLocked();
10636 if (mCurrentFocus != newFocus) {
10637 // This check makes sure that we don't already have the focus
10638 // change message pending.
10639 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10640 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010641 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10643 final WindowState oldFocus = mCurrentFocus;
10644 mCurrentFocus = newFocus;
10645 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 final WindowState imWindow = mInputMethodWindow;
10648 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010649 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010651 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10652 mLayoutNeeded = true;
10653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10655 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010656 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10657 // Client will do the layout, but we need to assign layers
10658 // for handleNewWindowLocked() below.
10659 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 }
10661 }
Romain Guy06882f82009-06-10 13:36:04 -070010662
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010663 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10664 mKeyWaiter.handleNewWindowLocked(newFocus);
10665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 return true;
10667 }
10668 return false;
10669 }
10670
10671 private WindowState computeFocusedWindowLocked() {
10672 WindowState result = null;
10673 WindowState win;
10674
10675 int i = mWindows.size() - 1;
10676 int nextAppIndex = mAppTokens.size()-1;
10677 WindowToken nextApp = nextAppIndex >= 0
10678 ? mAppTokens.get(nextAppIndex) : null;
10679
10680 while (i >= 0) {
10681 win = (WindowState)mWindows.get(i);
10682
Joe Onorato8a9b2202010-02-26 18:56:32 -080010683 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 TAG, "Looking for focus: " + i
10685 + " = " + win
10686 + ", flags=" + win.mAttrs.flags
10687 + ", canReceive=" + win.canReceiveKeys());
10688
10689 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 // If this window's application has been removed, just skip it.
10692 if (thisApp != null && thisApp.removed) {
10693 i--;
10694 continue;
10695 }
Romain Guy06882f82009-06-10 13:36:04 -070010696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 // If there is a focused app, don't allow focus to go to any
10698 // windows below it. If this is an application window, step
10699 // through the app tokens until we find its app.
10700 if (thisApp != null && nextApp != null && thisApp != nextApp
10701 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10702 int origAppIndex = nextAppIndex;
10703 while (nextAppIndex > 0) {
10704 if (nextApp == mFocusedApp) {
10705 // Whoops, we are below the focused app... no focus
10706 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080010707 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 TAG, "Reached focused app: " + mFocusedApp);
10709 return null;
10710 }
10711 nextAppIndex--;
10712 nextApp = mAppTokens.get(nextAppIndex);
10713 if (nextApp == thisApp) {
10714 break;
10715 }
10716 }
10717 if (thisApp != nextApp) {
10718 // Uh oh, the app token doesn't exist! This shouldn't
10719 // happen, but if it does we can get totally hosed...
10720 // so restart at the original app.
10721 nextAppIndex = origAppIndex;
10722 nextApp = mAppTokens.get(nextAppIndex);
10723 }
10724 }
10725
10726 // Dispatch to this window if it is wants key events.
10727 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010728 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 TAG, "Found focus @ " + i + " = " + win);
10730 result = win;
10731 break;
10732 }
10733
10734 i--;
10735 }
10736
10737 return result;
10738 }
10739
10740 private void startFreezingDisplayLocked() {
10741 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010742 // Freezing the display also suspends key event delivery, to
10743 // keep events from going astray while the display is reconfigured.
10744 // If someone has changed orientation again while the screen is
10745 // still frozen, the events will continue to be blocked while the
10746 // successive orientation change is processed. To prevent spurious
10747 // ANRs, we reset the event dispatch timeout in this case.
10748 synchronized (mKeyWaiter) {
10749 mKeyWaiter.mWasFrozen = true;
10750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 return;
10752 }
Romain Guy06882f82009-06-10 13:36:04 -070010753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010754 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010757 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 if (mFreezeGcPending != 0) {
10759 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010760 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 mH.removeMessages(H.FORCE_GC);
10762 Runtime.getRuntime().gc();
10763 mFreezeGcPending = now;
10764 }
10765 } else {
10766 mFreezeGcPending = now;
10767 }
Romain Guy06882f82009-06-10 13:36:04 -070010768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010770 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10771 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010772 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 mAppTransitionReady = true;
10774 }
Romain Guy06882f82009-06-10 13:36:04 -070010775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010776 if (PROFILE_ORIENTATION) {
10777 File file = new File("/data/system/frozen");
10778 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10779 }
10780 Surface.freezeDisplay(0);
10781 }
Romain Guy06882f82009-06-10 13:36:04 -070010782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 private void stopFreezingDisplayLocked() {
10784 if (!mDisplayFrozen) {
10785 return;
10786 }
Romain Guy06882f82009-06-10 13:36:04 -070010787
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010788 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
10789 return;
10790 }
10791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010792 mDisplayFrozen = false;
10793 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10794 if (PROFILE_ORIENTATION) {
10795 Debug.stopMethodTracing();
10796 }
10797 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010798
Chris Tate2ad63a92009-03-25 17:36:48 -070010799 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10800 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 synchronized (mKeyWaiter) {
10802 mKeyWaiter.mWasFrozen = true;
10803 mKeyWaiter.notifyAll();
10804 }
10805
10806 // A little kludge: a lot could have happened while the
10807 // display was frozen, so now that we are coming back we
10808 // do a gc so that any remote references the system
10809 // processes holds on others can be released if they are
10810 // no longer needed.
10811 mH.removeMessages(H.FORCE_GC);
10812 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10813 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 mScreenFrozenLock.release();
10816 }
Romain Guy06882f82009-06-10 13:36:04 -070010817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 @Override
10819 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10820 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10821 != PackageManager.PERMISSION_GRANTED) {
10822 pw.println("Permission Denial: can't dump WindowManager from from pid="
10823 + Binder.getCallingPid()
10824 + ", uid=" + Binder.getCallingUid());
10825 return;
10826 }
Romain Guy06882f82009-06-10 13:36:04 -070010827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828 synchronized(mWindowMap) {
10829 pw.println("Current Window Manager state:");
10830 for (int i=mWindows.size()-1; i>=0; i--) {
10831 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010832 pw.print(" Window #"); pw.print(i); pw.print(' ');
10833 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 w.dump(pw, " ");
10835 }
10836 if (mInputMethodDialogs.size() > 0) {
10837 pw.println(" ");
10838 pw.println(" Input method dialogs:");
10839 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10840 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010841 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 }
10843 }
10844 if (mPendingRemove.size() > 0) {
10845 pw.println(" ");
10846 pw.println(" Remove pending for:");
10847 for (int i=mPendingRemove.size()-1; i>=0; i--) {
10848 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010849 pw.print(" Remove #"); pw.print(i); pw.print(' ');
10850 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 w.dump(pw, " ");
10852 }
10853 }
10854 if (mForceRemoves != null && mForceRemoves.size() > 0) {
10855 pw.println(" ");
10856 pw.println(" Windows force removing:");
10857 for (int i=mForceRemoves.size()-1; i>=0; i--) {
10858 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010859 pw.print(" Removing #"); pw.print(i); pw.print(' ');
10860 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861 w.dump(pw, " ");
10862 }
10863 }
10864 if (mDestroySurface.size() > 0) {
10865 pw.println(" ");
10866 pw.println(" Windows waiting to destroy their surface:");
10867 for (int i=mDestroySurface.size()-1; i>=0; i--) {
10868 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010869 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
10870 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 w.dump(pw, " ");
10872 }
10873 }
10874 if (mLosingFocus.size() > 0) {
10875 pw.println(" ");
10876 pw.println(" Windows losing focus:");
10877 for (int i=mLosingFocus.size()-1; i>=0; i--) {
10878 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010879 pw.print(" Losing #"); pw.print(i); pw.print(' ');
10880 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010881 w.dump(pw, " ");
10882 }
10883 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010884 if (mResizingWindows.size() > 0) {
10885 pw.println(" ");
10886 pw.println(" Windows waiting to resize:");
10887 for (int i=mResizingWindows.size()-1; i>=0; i--) {
10888 WindowState w = mResizingWindows.get(i);
10889 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
10890 pw.print(w); pw.println(":");
10891 w.dump(pw, " ");
10892 }
10893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 if (mSessions.size() > 0) {
10895 pw.println(" ");
10896 pw.println(" All active sessions:");
10897 Iterator<Session> it = mSessions.iterator();
10898 while (it.hasNext()) {
10899 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010900 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 s.dump(pw, " ");
10902 }
10903 }
10904 if (mTokenMap.size() > 0) {
10905 pw.println(" ");
10906 pw.println(" All tokens:");
10907 Iterator<WindowToken> it = mTokenMap.values().iterator();
10908 while (it.hasNext()) {
10909 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010910 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010911 token.dump(pw, " ");
10912 }
10913 }
10914 if (mTokenList.size() > 0) {
10915 pw.println(" ");
10916 pw.println(" Window token list:");
10917 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010918 pw.print(" #"); pw.print(i); pw.print(": ");
10919 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 }
10921 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010922 if (mWallpaperTokens.size() > 0) {
10923 pw.println(" ");
10924 pw.println(" Wallpaper tokens:");
10925 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
10926 WindowToken token = mWallpaperTokens.get(i);
10927 pw.print(" Wallpaper #"); pw.print(i);
10928 pw.print(' '); pw.print(token); pw.println(':');
10929 token.dump(pw, " ");
10930 }
10931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 if (mAppTokens.size() > 0) {
10933 pw.println(" ");
10934 pw.println(" Application tokens in Z order:");
10935 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010936 pw.print(" App #"); pw.print(i); pw.print(": ");
10937 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938 }
10939 }
10940 if (mFinishedStarting.size() > 0) {
10941 pw.println(" ");
10942 pw.println(" Finishing start of application tokens:");
10943 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
10944 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010945 pw.print(" Finished Starting #"); pw.print(i);
10946 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 token.dump(pw, " ");
10948 }
10949 }
10950 if (mExitingTokens.size() > 0) {
10951 pw.println(" ");
10952 pw.println(" Exiting tokens:");
10953 for (int i=mExitingTokens.size()-1; i>=0; i--) {
10954 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010955 pw.print(" Exiting #"); pw.print(i);
10956 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010957 token.dump(pw, " ");
10958 }
10959 }
10960 if (mExitingAppTokens.size() > 0) {
10961 pw.println(" ");
10962 pw.println(" Exiting application tokens:");
10963 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
10964 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010965 pw.print(" Exiting App #"); pw.print(i);
10966 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967 token.dump(pw, " ");
10968 }
10969 }
10970 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010971 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
10972 pw.print(" mLastFocus="); pw.println(mLastFocus);
10973 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
10974 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
10975 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070010976 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010977 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
10978 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
10979 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
10980 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010981 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
10982 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
10983 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010984 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
10985 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
10986 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
10987 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010988 if (mDimAnimator != null) {
10989 mDimAnimator.printTo(pw);
10990 } else {
10991 pw.print( " no DimAnimator ");
10992 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010993 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070010994 pw.print(mInputMethodAnimLayerAdjustment);
10995 pw.print(" mWallpaperAnimLayerAdjustment=");
10996 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070010997 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
10998 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010999 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11000 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011001 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11002 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011003 pw.print(" mRotation="); pw.print(mRotation);
11004 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11005 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11006 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11007 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11008 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11009 pw.print(" mNextAppTransition=0x");
11010 pw.print(Integer.toHexString(mNextAppTransition));
11011 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011012 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011013 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011014 if (mNextAppTransitionPackage != null) {
11015 pw.print(" mNextAppTransitionPackage=");
11016 pw.print(mNextAppTransitionPackage);
11017 pw.print(", mNextAppTransitionEnter=0x");
11018 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11019 pw.print(", mNextAppTransitionExit=0x");
11020 pw.print(Integer.toHexString(mNextAppTransitionExit));
11021 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011022 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11023 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011024 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11025 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11026 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11027 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011028 if (mOpeningApps.size() > 0) {
11029 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11030 }
11031 if (mClosingApps.size() > 0) {
11032 pw.print(" mClosingApps="); pw.println(mClosingApps);
11033 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011034 if (mToTopApps.size() > 0) {
11035 pw.print(" mToTopApps="); pw.println(mToTopApps);
11036 }
11037 if (mToBottomApps.size() > 0) {
11038 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11039 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011040 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11041 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011043 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11044 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11045 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11046 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11047 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11048 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011049 }
11050 }
11051
11052 public void monitor() {
11053 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011054 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 synchronized (mKeyWaiter) { }
11056 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011057
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011058 public void virtualKeyFeedback(KeyEvent event) {
11059 mPolicy.keyFeedbackFromInput(event);
11060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011061
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011062 /**
11063 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011064 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011065 */
11066 private static class DimAnimator {
11067 Surface mDimSurface;
11068 boolean mDimShown = false;
11069 float mDimCurrentAlpha;
11070 float mDimTargetAlpha;
11071 float mDimDeltaPerMs;
11072 long mLastDimAnimTime;
11073
11074 DimAnimator (SurfaceSession session) {
11075 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011076 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011077 + mDimSurface + ": CREATE");
11078 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011079 mDimSurface = new Surface(session, 0,
11080 "DimSurface",
11081 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011082 Surface.FX_SURFACE_DIM);
11083 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011084 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011085 }
11086 }
11087 }
11088
11089 /**
11090 * Show the dim surface.
11091 */
11092 void show(int dw, int dh) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011093 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011094 dw + "x" + dh + ")");
11095 mDimShown = true;
11096 try {
11097 mDimSurface.setPosition(0, 0);
11098 mDimSurface.setSize(dw, dh);
11099 mDimSurface.show();
11100 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011101 Slog.w(TAG, "Failure showing dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011102 }
11103 }
11104
11105 /**
11106 * Set's the dim surface's layer and update dim parameters that will be used in
11107 * {@link updateSurface} after all windows are examined.
11108 */
11109 void updateParameters(WindowState w, long currentTime) {
11110 mDimSurface.setLayer(w.mAnimLayer-1);
11111
11112 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011113 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011114 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011115 if (mDimTargetAlpha != target) {
11116 // If the desired dim level has changed, then
11117 // start an animation to it.
11118 mLastDimAnimTime = currentTime;
11119 long duration = (w.mAnimating && w.mAnimation != null)
11120 ? w.mAnimation.computeDurationHint()
11121 : DEFAULT_DIM_DURATION;
11122 if (target > mDimTargetAlpha) {
11123 // This is happening behind the activity UI,
11124 // so we can make it run a little longer to
11125 // give a stronger impression without disrupting
11126 // the user.
11127 duration *= DIM_DURATION_MULTIPLIER;
11128 }
11129 if (duration < 1) {
11130 // Don't divide by zero
11131 duration = 1;
11132 }
11133 mDimTargetAlpha = target;
11134 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11135 }
11136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011137
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011138 /**
11139 * Updating the surface's alpha. Returns true if the animation continues, or returns
11140 * false when the animation is finished and the dim surface is hidden.
11141 */
11142 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11143 if (!dimming) {
11144 if (mDimTargetAlpha != 0) {
11145 mLastDimAnimTime = currentTime;
11146 mDimTargetAlpha = 0;
11147 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11148 }
11149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011150
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011151 boolean animating = false;
11152 if (mLastDimAnimTime != 0) {
11153 mDimCurrentAlpha += mDimDeltaPerMs
11154 * (currentTime-mLastDimAnimTime);
11155 boolean more = true;
11156 if (displayFrozen) {
11157 // If the display is frozen, there is no reason to animate.
11158 more = false;
11159 } else if (mDimDeltaPerMs > 0) {
11160 if (mDimCurrentAlpha > mDimTargetAlpha) {
11161 more = false;
11162 }
11163 } else if (mDimDeltaPerMs < 0) {
11164 if (mDimCurrentAlpha < mDimTargetAlpha) {
11165 more = false;
11166 }
11167 } else {
11168 more = false;
11169 }
11170
11171 // Do we need to continue animating?
11172 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011173 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011174 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11175 mLastDimAnimTime = currentTime;
11176 mDimSurface.setAlpha(mDimCurrentAlpha);
11177 animating = true;
11178 } else {
11179 mDimCurrentAlpha = mDimTargetAlpha;
11180 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011181 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011182 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11183 mDimSurface.setAlpha(mDimCurrentAlpha);
11184 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011185 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011186 + ": HIDE");
11187 try {
11188 mDimSurface.hide();
11189 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011190 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011191 }
11192 mDimShown = false;
11193 }
11194 }
11195 }
11196 return animating;
11197 }
11198
11199 public void printTo(PrintWriter pw) {
11200 pw.print(" mDimShown="); pw.print(mDimShown);
11201 pw.print(" current="); pw.print(mDimCurrentAlpha);
11202 pw.print(" target="); pw.print(mDimTargetAlpha);
11203 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11204 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11205 }
11206 }
11207
11208 /**
11209 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11210 * This is used for opening/closing transition for apps in compatible mode.
11211 */
11212 private static class FadeInOutAnimation extends Animation {
11213 int mWidth;
11214 boolean mFadeIn;
11215
11216 public FadeInOutAnimation(boolean fadeIn) {
11217 setInterpolator(new AccelerateInterpolator());
11218 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11219 mFadeIn = fadeIn;
11220 }
11221
11222 @Override
11223 protected void applyTransformation(float interpolatedTime, Transformation t) {
11224 float x = interpolatedTime;
11225 if (!mFadeIn) {
11226 x = 1.0f - x; // reverse the interpolation for fade out
11227 }
11228 if (x < 0.5) {
11229 // move the window out of the screen.
11230 t.getMatrix().setTranslate(mWidth, 0);
11231 } else {
11232 t.getMatrix().setTranslate(0, 0);// show
11233 t.setAlpha((x - 0.5f) * 2);
11234 }
11235 }
11236
11237 @Override
11238 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11239 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11240 mWidth = width;
11241 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011242
11243 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011244 public int getZAdjustment() {
11245 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011246 }
11247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248}