blob: 93e45fc08039ab17e8d32f26813932e17addcb63 [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;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080046import com.android.internal.policy.impl.PhoneWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import com.android.internal.view.IInputContext;
48import com.android.internal.view.IInputMethodClient;
49import com.android.internal.view.IInputMethodManager;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080050import com.android.internal.view.WindowManagerPolicyThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import com.android.server.KeyInputQueue.QueuedEvent;
52import com.android.server.am.BatteryStatsService;
53
54import android.Manifest;
55import android.app.ActivityManagerNative;
56import android.app.IActivityManager;
57import android.content.Context;
58import android.content.pm.ActivityInfo;
59import android.content.pm.PackageManager;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070060import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Configuration;
62import android.graphics.Matrix;
63import android.graphics.PixelFormat;
64import android.graphics.Rect;
65import android.graphics.Region;
66import android.os.BatteryStats;
67import android.os.Binder;
Dianne Hackborn75804932009-10-20 20:15:20 -070068import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Debug;
70import android.os.Handler;
71import android.os.IBinder;
Michael Chan53071d62009-05-13 17:29:48 -070072import android.os.LatencyTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.LocalPowerManager;
74import android.os.Looper;
75import android.os.Message;
76import android.os.Parcel;
77import android.os.ParcelFileDescriptor;
78import android.os.Power;
79import android.os.PowerManager;
80import android.os.Process;
81import android.os.RemoteException;
82import android.os.ServiceManager;
83import android.os.SystemClock;
84import android.os.SystemProperties;
85import android.os.TokenWatcher;
86import android.provider.Settings;
Dianne Hackborn723738c2009-06-25 19:48:04 -070087import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080089import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.SparseIntArray;
91import android.view.Display;
92import android.view.Gravity;
93import android.view.IApplicationToken;
94import android.view.IOnKeyguardExitResult;
95import android.view.IRotationWatcher;
96import android.view.IWindow;
97import android.view.IWindowManager;
98import android.view.IWindowSession;
99import android.view.KeyEvent;
100import android.view.MotionEvent;
101import android.view.RawInputEvent;
102import android.view.Surface;
103import android.view.SurfaceSession;
104import android.view.View;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700105import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.ViewTreeObserver;
107import android.view.WindowManager;
108import android.view.WindowManagerImpl;
109import android.view.WindowManagerPolicy;
110import android.view.WindowManager.LayoutParams;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700111import android.view.animation.AccelerateInterpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.animation.Animation;
113import android.view.animation.AnimationUtils;
114import android.view.animation.Transformation;
115
116import java.io.BufferedWriter;
117import java.io.File;
118import java.io.FileDescriptor;
119import java.io.IOException;
120import java.io.OutputStream;
121import java.io.OutputStreamWriter;
122import java.io.PrintWriter;
123import java.io.StringWriter;
124import java.net.Socket;
125import java.util.ArrayList;
126import java.util.HashMap;
127import java.util.HashSet;
128import java.util.Iterator;
129import java.util.List;
130
131/** {@hide} */
Dianne Hackbornddca3ee2009-07-23 19:01:31 -0700132public class WindowManagerService extends IWindowManager.Stub
133 implements Watchdog.Monitor, KeyInputQueue.HapticFeedbackCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 static final String TAG = "WindowManager";
135 static final boolean DEBUG = false;
136 static final boolean DEBUG_FOCUS = false;
137 static final boolean DEBUG_ANIM = false;
Dianne Hackborn9b52a212009-12-11 14:51:35 -0800138 static final boolean DEBUG_LAYOUT = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800139 static final boolean DEBUG_RESIZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final boolean DEBUG_LAYERS = false;
141 static final boolean DEBUG_INPUT = false;
142 static final boolean DEBUG_INPUT_METHOD = false;
143 static final boolean DEBUG_VISIBILITY = false;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -0700144 static final boolean DEBUG_WINDOW_MOVEMENT = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean DEBUG_ORIENTATION = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700146 static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean DEBUG_APP_TRANSITIONS = false;
148 static final boolean DEBUG_STARTING_WINDOW = false;
149 static final boolean DEBUG_REORDER = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700150 static final boolean DEBUG_WALLPAPER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700152 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700153 static final boolean MEASURE_LATENCY = false;
154 static private LatencyTimer lt;
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 static final boolean PROFILE_ORIENTATION = false;
157 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700158 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 /** How long to wait for subsequent key repeats, in milliseconds */
161 static final int KEY_REPEAT_DELAY = 50;
162
163 /** How much to multiply the policy's type layer, to reserve room
164 * for multiple windows of the same type and Z-ordering adjustment
165 * with TYPE_LAYER_OFFSET. */
166 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
169 * or below others in the same layer. */
170 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /** How much to increment the layer for each window, to reserve room
173 * for effect surfaces between them.
174 */
175 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 /** The maximum length we will accept for a loaded animation duration:
178 * this is 10 seconds.
179 */
180 static final int MAX_ANIMATION_DURATION = 10*1000;
181
182 /** Amount of time (in milliseconds) to animate the dim surface from one
183 * value to another, when no window animation is driving it.
184 */
185 static final int DEFAULT_DIM_DURATION = 200;
186
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700187 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
188 * compatible windows.
189 */
190 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /** Adjustment to time to perform a dim, to make it more dramatic.
193 */
194 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700195
Dianne Hackborncfaef692009-06-15 14:24:44 -0700196 static final int INJECT_FAILED = 0;
197 static final int INJECT_SUCCEEDED = 1;
198 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 static final int UPDATE_FOCUS_NORMAL = 0;
201 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
202 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
203 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700204
Michael Chane96440f2009-05-06 10:27:36 -0700205 /** The minimum time between dispatching touch events. */
206 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
207
208 // Last touch event time
209 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700210
Michael Chane96440f2009-05-06 10:27:36 -0700211 // Last touch event type
212 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700213
Michael Chane96440f2009-05-06 10:27:36 -0700214 // Time to wait before calling useractivity again. This saves CPU usage
215 // when we get a flood of touch events.
216 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
217
218 // Last time we call user activity
219 long mLastUserActivityCallTime = 0;
220
Romain Guy06882f82009-06-10 13:36:04 -0700221 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700222 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700225 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 /**
228 * Condition waited on by {@link #reenableKeyguard} to know the call to
229 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500230 * This is set to true only if mKeyguardTokenWatcher.acquired() has
231 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500233 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Mike Lockwood983ee092009-11-22 01:42:24 -0500235 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
236 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 public void acquired() {
238 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500239 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700242 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500243 synchronized (mKeyguardTokenWatcher) {
244 mKeyguardDisabled = false;
245 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247 }
248 };
249
250 final Context mContext;
251
252 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
257
258 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * All currently active sessions with clients.
264 */
265 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
268 * Mapping from an IWindow IBinder to the server's Window object.
269 * This is also used as the lock for all of our state.
270 */
271 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
272
273 /**
274 * Mapping from a token IBinder to a WindowToken object.
275 */
276 final HashMap<IBinder, WindowToken> mTokenMap =
277 new HashMap<IBinder, WindowToken>();
278
279 /**
280 * The same tokens as mTokenMap, stored in a list for efficient iteration
281 * over them.
282 */
283 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * Window tokens that are in the process of exiting, but still
287 * on screen for animations.
288 */
289 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
290
291 /**
292 * Z-ordered (bottom-most first) list of all application tokens, for
293 * controlling the ordering of windows in different applications. This
294 * contains WindowToken objects.
295 */
296 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
297
298 /**
299 * Application tokens that are in the process of exiting, but still
300 * on screen for animations.
301 */
302 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
303
304 /**
305 * List of window tokens that have finished starting their application,
306 * and now need to have the policy remove their windows.
307 */
308 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
309
310 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700311 * This was the app token that was used to retrieve the last enter
312 * animation. It will be used for the next exit animation.
313 */
314 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700316 /**
317 * These were the layout params used to retrieve the last enter animation.
318 * They will be used for the next exit animation.
319 */
320 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * Z-ordered (bottom-most first) list of all Window objects.
324 */
325 final ArrayList mWindows = new ArrayList();
326
327 /**
328 * Windows that are being resized. Used so we can tell the client about
329 * the resize after closing the transaction in which we resized the
330 * underlying surface.
331 */
332 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
333
334 /**
335 * Windows whose animations have ended and now must be removed.
336 */
337 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
338
339 /**
340 * Windows whose surface should be destroyed.
341 */
342 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
343
344 /**
345 * Windows that have lost input focus and are waiting for the new
346 * focus window to be displayed before they are told about this.
347 */
348 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
349
350 /**
351 * This is set when we have run out of memory, and will either be an empty
352 * list or contain windows that need to be force removed.
353 */
354 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700359 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 Surface mBlurSurface;
361 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 final float[] mTmpFloats = new float[9];
366
367 boolean mSafeMode;
368 boolean mDisplayEnabled = false;
369 boolean mSystemBooted = false;
370 int mRotation = 0;
371 int mRequestedRotation = 0;
372 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700373 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 ArrayList<IRotationWatcher> mRotationWatchers
375 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 boolean mLayoutNeeded = true;
378 boolean mAnimationPending = false;
379 boolean mDisplayFrozen = false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800380 boolean mWaitingForConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 boolean mWindowsFreezingScreen = false;
382 long mFreezeGcPending = 0;
383 int mAppsFreezingScreen = 0;
384
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800385 int mLayoutSeq = 0;
386
Dianne Hackbornb601ce12010-03-01 23:36:02 -0800387 // State while inside of layoutAndPlaceSurfacesLocked().
388 boolean mFocusMayChange;
389
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800390 Configuration mCurConfiguration = new Configuration();
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 // This is held as long as we have the screen frozen, to give us time to
393 // perform a rotation animation when turning off shows the lock screen which
394 // changes the orientation.
395 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 // State management of app transitions. When we are preparing for a
398 // transition, mNextAppTransition will be the kind of transition to
399 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
400 // mOpeningApps and mClosingApps are the lists of tokens that will be
401 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700402 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700403 String mNextAppTransitionPackage;
404 int mNextAppTransitionEnter;
405 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700407 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 boolean mAppTransitionTimeout = false;
409 boolean mStartingIconInTransition = false;
410 boolean mSkipAppTransitionAnimation = false;
411 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
412 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700413 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
414 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 //flag to detect fat touch events
417 boolean mFatTouch = false;
418 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 H mH = new H();
421
422 WindowState mCurrentFocus = null;
423 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 // This just indicates the window the input method is on top of, not
426 // necessarily the window its input is going to.
427 WindowState mInputMethodTarget = null;
428 WindowState mUpcomingInputMethodTarget = null;
429 boolean mInputMethodTargetWaitingAnim;
430 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 WindowState mInputMethodWindow = null;
433 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
434
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700435 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700437 // If non-null, this is the currently visible window that is associated
438 // with the wallpaper.
439 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700440 // If non-null, we are in the middle of animating from one wallpaper target
441 // to another, and this is the lower one in Z-order.
442 WindowState mLowerWallpaperTarget = null;
443 // If non-null, we are in the middle of animating from one wallpaper target
444 // to another, and this is the higher one in Z-order.
445 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700446 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700447 float mLastWallpaperX = -1;
448 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800449 float mLastWallpaperXStep = -1;
450 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800451 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700452 // This is set when we are waiting for a wallpaper to tell us it is done
453 // changing its scroll position.
454 WindowState mWaitingOnWallpaper;
455 // The last time we had a timeout when waiting for a wallpaper.
456 long mLastWallpaperTimeoutTime;
457 // We give a wallpaper up to 150ms to finish scrolling.
458 static final long WALLPAPER_TIMEOUT = 150;
459 // Time we wait after a timeout before trying to wait again.
460 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 AppWindowToken mFocusedApp = null;
463
464 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 float mWindowAnimationScale = 1.0f;
467 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 final KeyWaiter mKeyWaiter = new KeyWaiter();
470 final KeyQ mQueue;
471 final InputDispatcherThread mInputThread;
472
473 // Who is holding the screen on.
474 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700475
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700476 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 /**
479 * Whether the UI is currently running in touch mode (not showing
480 * navigational focus because the user is directly pressing the screen).
481 */
482 boolean mInTouchMode = false;
483
484 private ViewServer mViewServer;
485
486 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700487
Dianne Hackbornc485a602009-03-24 22:39:49 -0700488 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700489 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700490
491 // The frame use to limit the size of the app running in compatibility mode.
492 Rect mCompatibleScreenFrame = new Rect();
493 // The surface used to fill the outer rim of the app running in compatibility mode.
494 Surface mBackgroundFillerSurface = null;
495 boolean mBackgroundFillerShown = false;
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 public static WindowManagerService main(Context context,
498 PowerManagerService pm, boolean haveInputMethods) {
499 WMThread thr = new WMThread(context, pm, haveInputMethods);
500 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 synchronized (thr) {
503 while (thr.mService == null) {
504 try {
505 thr.wait();
506 } catch (InterruptedException e) {
507 }
508 }
509 }
Romain Guy06882f82009-06-10 13:36:04 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 return thr.mService;
512 }
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 static class WMThread extends Thread {
515 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 private final Context mContext;
518 private final PowerManagerService mPM;
519 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public WMThread(Context context, PowerManagerService pm,
522 boolean haveInputMethods) {
523 super("WindowManager");
524 mContext = context;
525 mPM = pm;
526 mHaveInputMethods = haveInputMethods;
527 }
Romain Guy06882f82009-06-10 13:36:04 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 public void run() {
530 Looper.prepare();
531 WindowManagerService s = new WindowManagerService(mContext, mPM,
532 mHaveInputMethods);
533 android.os.Process.setThreadPriority(
534 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 synchronized (this) {
537 mService = s;
538 notifyAll();
539 }
Romain Guy06882f82009-06-10 13:36:04 -0700540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 Looper.loop();
542 }
543 }
544
545 static class PolicyThread extends Thread {
546 private final WindowManagerPolicy mPolicy;
547 private final WindowManagerService mService;
548 private final Context mContext;
549 private final PowerManagerService mPM;
550 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 public PolicyThread(WindowManagerPolicy policy,
553 WindowManagerService service, Context context,
554 PowerManagerService pm) {
555 super("WindowManagerPolicy");
556 mPolicy = policy;
557 mService = service;
558 mContext = context;
559 mPM = pm;
560 }
Romain Guy06882f82009-06-10 13:36:04 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public void run() {
563 Looper.prepare();
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800564 WindowManagerPolicyThread.set(this, Looper.myLooper());
565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 //Looper.myLooper().setMessageLogging(new LogPrinter(
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 // Log.VERBOSE, "WindowManagerPolicy", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 android.os.Process.setThreadPriority(
569 android.os.Process.THREAD_PRIORITY_FOREGROUND);
570 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 synchronized (this) {
573 mRunning = true;
574 notifyAll();
575 }
Romain Guy06882f82009-06-10 13:36:04 -0700576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 Looper.loop();
578 }
579 }
580
581 private WindowManagerService(Context context, PowerManagerService pm,
582 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700583 if (MEASURE_LATENCY) {
584 lt = new LatencyTimer(100, 1000);
585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 mContext = context;
588 mHaveInputMethods = haveInputMethods;
589 mLimitedAlphaCompositing = context.getResources().getBoolean(
590 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mPowerManager = pm;
593 mPowerManager.setPolicy(mPolicy);
594 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
595 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
596 "SCREEN_FROZEN");
597 mScreenFrozenLock.setReferenceCounted(false);
598
599 mActivityManager = ActivityManagerNative.getDefault();
600 mBatteryStats = BatteryStatsService.getService();
601
602 // Get persisted window scale setting
603 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
604 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
605 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
606 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700607
Michael Chan9f028e62009-08-04 17:37:46 -0700608 int max_events_per_sec = 35;
609 try {
610 max_events_per_sec = Integer.parseInt(SystemProperties
611 .get("windowsmgr.max_events_per_sec"));
612 if (max_events_per_sec < 1) {
613 max_events_per_sec = 35;
614 }
615 } catch (NumberFormatException e) {
616 }
617 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 mQueue = new KeyQ();
620
621 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
624 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 synchronized (thr) {
627 while (!thr.mRunning) {
628 try {
629 thr.wait();
630 } catch (InterruptedException e) {
631 }
632 }
633 }
Romain Guy06882f82009-06-10 13:36:04 -0700634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 // Add ourself to the Watchdog monitors.
638 Watchdog.getInstance().addMonitor(this);
639 }
640
641 @Override
642 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
643 throws RemoteException {
644 try {
645 return super.onTransact(code, data, reply, flags);
646 } catch (RuntimeException e) {
647 // The window manager only throws security exceptions, so let's
648 // log all others.
649 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800650 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
652 throw e;
653 }
654 }
655
656 private void placeWindowAfter(Object pos, WindowState window) {
657 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800658 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 TAG, "Adding window " + window + " at "
660 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
661 mWindows.add(i+1, window);
662 }
663
664 private void placeWindowBefore(Object pos, WindowState window) {
665 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800666 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 TAG, "Adding window " + window + " at "
668 + i + " of " + mWindows.size() + " (before " + pos + ")");
669 mWindows.add(i, window);
670 }
671
672 //This method finds out the index of a window that has the same app token as
673 //win. used for z ordering the windows in mWindows
674 private int findIdxBasedOnAppTokens(WindowState win) {
675 //use a local variable to cache mWindows
676 ArrayList localmWindows = mWindows;
677 int jmax = localmWindows.size();
678 if(jmax == 0) {
679 return -1;
680 }
681 for(int j = (jmax-1); j >= 0; j--) {
682 WindowState wentry = (WindowState)localmWindows.get(j);
683 if(wentry.mAppToken == win.mAppToken) {
684 return j;
685 }
686 }
687 return -1;
688 }
Romain Guy06882f82009-06-10 13:36:04 -0700689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
691 final IWindow client = win.mClient;
692 final WindowToken token = win.mToken;
693 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 final int N = localmWindows.size();
696 final WindowState attached = win.mAttachedWindow;
697 int i;
698 if (attached == null) {
699 int tokenWindowsPos = token.windows.size();
700 if (token.appWindowToken != null) {
701 int index = tokenWindowsPos-1;
702 if (index >= 0) {
703 // If this application has existing windows, we
704 // simply place the new window on top of them... but
705 // keep the starting window on top.
706 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
707 // Base windows go behind everything else.
708 placeWindowBefore(token.windows.get(0), win);
709 tokenWindowsPos = 0;
710 } else {
711 AppWindowToken atoken = win.mAppToken;
712 if (atoken != null &&
713 token.windows.get(index) == atoken.startingWindow) {
714 placeWindowBefore(token.windows.get(index), win);
715 tokenWindowsPos--;
716 } else {
717 int newIdx = findIdxBasedOnAppTokens(win);
718 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700719 //there is a window above this one associated with the same
720 //apptoken note that the window could be a floating window
721 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800723 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700724 TAG, "Adding window " + win + " at "
725 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 }
730 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800731 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 TAG, "Figuring out where to add app window "
733 + client.asBinder() + " (token=" + token + ")");
734 // Figure out where the window should go, based on the
735 // order of applications.
736 final int NA = mAppTokens.size();
737 Object pos = null;
738 for (i=NA-1; i>=0; i--) {
739 AppWindowToken t = mAppTokens.get(i);
740 if (t == token) {
741 i--;
742 break;
743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800744
Dianne Hackborna8f60182009-09-01 19:01:50 -0700745 // We haven't reached the token yet; if this token
746 // is not going to the bottom and has windows, we can
747 // use it as an anchor for when we do reach the token.
748 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 pos = t.windows.get(0);
750 }
751 }
752 // We now know the index into the apps. If we found
753 // an app window above, that gives us the position; else
754 // we need to look some more.
755 if (pos != null) {
756 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700757 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 mTokenMap.get(((WindowState)pos).mClient.asBinder());
759 if (atoken != null) {
760 final int NC = atoken.windows.size();
761 if (NC > 0) {
762 WindowState bottom = atoken.windows.get(0);
763 if (bottom.mSubLayer < 0) {
764 pos = bottom;
765 }
766 }
767 }
768 placeWindowBefore(pos, win);
769 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700770 // Continue looking down until we find the first
771 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 while (i >= 0) {
773 AppWindowToken t = mAppTokens.get(i);
774 final int NW = t.windows.size();
775 if (NW > 0) {
776 pos = t.windows.get(NW-1);
777 break;
778 }
779 i--;
780 }
781 if (pos != null) {
782 // Move in front of any windows attached to this
783 // one.
784 WindowToken atoken =
785 mTokenMap.get(((WindowState)pos).mClient.asBinder());
786 if (atoken != null) {
787 final int NC = atoken.windows.size();
788 if (NC > 0) {
789 WindowState top = atoken.windows.get(NC-1);
790 if (top.mSubLayer >= 0) {
791 pos = top;
792 }
793 }
794 }
795 placeWindowAfter(pos, win);
796 } else {
797 // Just search for the start of this layer.
798 final int myLayer = win.mBaseLayer;
799 for (i=0; i<N; i++) {
800 WindowState w = (WindowState)localmWindows.get(i);
801 if (w.mBaseLayer > myLayer) {
802 break;
803 }
804 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800805 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700806 TAG, "Adding window " + win + " at "
807 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 localmWindows.add(i, win);
809 }
810 }
811 }
812 } else {
813 // Figure out where window should go, based on layer.
814 final int myLayer = win.mBaseLayer;
815 for (i=N-1; i>=0; i--) {
816 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
817 i++;
818 break;
819 }
820 }
821 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800822 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700823 TAG, "Adding window " + win + " at "
824 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 localmWindows.add(i, win);
826 }
827 if (addToToken) {
828 token.windows.add(tokenWindowsPos, win);
829 }
830
831 } else {
832 // Figure out this window's ordering relative to the window
833 // it is attached to.
834 final int NA = token.windows.size();
835 final int sublayer = win.mSubLayer;
836 int largestSublayer = Integer.MIN_VALUE;
837 WindowState windowWithLargestSublayer = null;
838 for (i=0; i<NA; i++) {
839 WindowState w = token.windows.get(i);
840 final int wSublayer = w.mSubLayer;
841 if (wSublayer >= largestSublayer) {
842 largestSublayer = wSublayer;
843 windowWithLargestSublayer = w;
844 }
845 if (sublayer < 0) {
846 // For negative sublayers, we go below all windows
847 // in the same sublayer.
848 if (wSublayer >= sublayer) {
849 if (addToToken) {
850 token.windows.add(i, win);
851 }
852 placeWindowBefore(
853 wSublayer >= 0 ? attached : w, win);
854 break;
855 }
856 } else {
857 // For positive sublayers, we go above all windows
858 // in the same sublayer.
859 if (wSublayer > sublayer) {
860 if (addToToken) {
861 token.windows.add(i, win);
862 }
863 placeWindowBefore(w, win);
864 break;
865 }
866 }
867 }
868 if (i >= NA) {
869 if (addToToken) {
870 token.windows.add(win);
871 }
872 if (sublayer < 0) {
873 placeWindowBefore(attached, win);
874 } else {
875 placeWindowAfter(largestSublayer >= 0
876 ? windowWithLargestSublayer
877 : attached,
878 win);
879 }
880 }
881 }
Romain Guy06882f82009-06-10 13:36:04 -0700882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 if (win.mAppToken != null && addToToken) {
884 win.mAppToken.allAppWindows.add(win);
885 }
886 }
Romain Guy06882f82009-06-10 13:36:04 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 static boolean canBeImeTarget(WindowState w) {
889 final int fl = w.mAttrs.flags
890 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
891 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
892 return w.isVisibleOrAdding();
893 }
894 return false;
895 }
Romain Guy06882f82009-06-10 13:36:04 -0700896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
898 final ArrayList localmWindows = mWindows;
899 final int N = localmWindows.size();
900 WindowState w = null;
901 int i = N;
902 while (i > 0) {
903 i--;
904 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700905
Joe Onorato8a9b2202010-02-26 18:56:32 -0800906 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // + Integer.toHexString(w.mAttrs.flags));
908 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800909 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Yet more tricksyness! If this window is a "starting"
912 // window, we do actually want to be on top of it, but
913 // it is not -really- where input will go. So if the caller
914 // is not actually looking to move the IME, look down below
915 // for a real window to target...
916 if (!willMove
917 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
918 && i > 0) {
919 WindowState wb = (WindowState)localmWindows.get(i-1);
920 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
921 i--;
922 w = wb;
923 }
924 }
925 break;
926 }
927 }
Romain Guy06882f82009-06-10 13:36:04 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700930
Joe Onorato8a9b2202010-02-26 18:56:32 -0800931 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 if (willMove && w != null) {
935 final WindowState curTarget = mInputMethodTarget;
936 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 // Now some fun for dealing with window animations that
939 // modify the Z order. We need to look at all windows below
940 // the current target that are in this app, finding the highest
941 // visible one in layering.
942 AppWindowToken token = curTarget.mAppToken;
943 WindowState highestTarget = null;
944 int highestPos = 0;
945 if (token.animating || token.animation != null) {
946 int pos = 0;
947 pos = localmWindows.indexOf(curTarget);
948 while (pos >= 0) {
949 WindowState win = (WindowState)localmWindows.get(pos);
950 if (win.mAppToken != token) {
951 break;
952 }
953 if (!win.mRemoved) {
954 if (highestTarget == null || win.mAnimLayer >
955 highestTarget.mAnimLayer) {
956 highestTarget = win;
957 highestPos = pos;
958 }
959 }
960 pos--;
961 }
962 }
Romain Guy06882f82009-06-10 13:36:04 -0700963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800965 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 + mNextAppTransition + " " + highestTarget
967 + " animating=" + highestTarget.isAnimating()
968 + " layer=" + highestTarget.mAnimLayer
969 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700970
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700971 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 // If we are currently setting up for an animation,
973 // hold everything until we can find out what will happen.
974 mInputMethodTargetWaitingAnim = true;
975 mInputMethodTarget = highestTarget;
976 return highestPos + 1;
977 } else if (highestTarget.isAnimating() &&
978 highestTarget.mAnimLayer > w.mAnimLayer) {
979 // If the window we are currently targeting is involved
980 // with an animation, and it is on top of the next target
981 // we will be over, then hold off on moving until
982 // that is done.
983 mInputMethodTarget = highestTarget;
984 return highestPos + 1;
985 }
986 }
987 }
988 }
Romain Guy06882f82009-06-10 13:36:04 -0700989
Joe Onorato8a9b2202010-02-26 18:56:32 -0800990 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 if (w != null) {
992 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800993 if (DEBUG_INPUT_METHOD) {
994 RuntimeException e = null;
995 if (!HIDE_STACK_CRAWLS) {
996 e = new RuntimeException();
997 e.fillInStackTrace();
998 }
999 Slog.w(TAG, "Moving IM target from "
1000 + mInputMethodTarget + " to " + w, e);
1001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 mInputMethodTarget = w;
1003 if (w.mAppToken != null) {
1004 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
1005 } else {
1006 setInputMethodAnimLayerAdjustment(0);
1007 }
1008 }
1009 return i+1;
1010 }
1011 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001012 if (DEBUG_INPUT_METHOD) {
1013 RuntimeException e = null;
1014 if (!HIDE_STACK_CRAWLS) {
1015 e = new RuntimeException();
1016 e.fillInStackTrace();
1017 }
1018 Slog.w(TAG, "Moving IM target from "
1019 + mInputMethodTarget + " to null", e);
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 mInputMethodTarget = null;
1022 setInputMethodAnimLayerAdjustment(0);
1023 }
1024 return -1;
1025 }
Romain Guy06882f82009-06-10 13:36:04 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 void addInputMethodWindowToListLocked(WindowState win) {
1028 int pos = findDesiredInputMethodWindowIndexLocked(true);
1029 if (pos >= 0) {
1030 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001031 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001032 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 mWindows.add(pos, win);
1034 moveInputMethodDialogsLocked(pos+1);
1035 return;
1036 }
1037 win.mTargetAppToken = null;
1038 addWindowToListInOrderLocked(win, true);
1039 moveInputMethodDialogsLocked(pos);
1040 }
Romain Guy06882f82009-06-10 13:36:04 -07001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001043 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 mInputMethodAnimLayerAdjustment = adj;
1045 WindowState imw = mInputMethodWindow;
1046 if (imw != null) {
1047 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 + " anim layer: " + imw.mAnimLayer);
1050 int wi = imw.mChildWindows.size();
1051 while (wi > 0) {
1052 wi--;
1053 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1054 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001055 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 + " anim layer: " + cw.mAnimLayer);
1057 }
1058 }
1059 int di = mInputMethodDialogs.size();
1060 while (di > 0) {
1061 di --;
1062 imw = mInputMethodDialogs.get(di);
1063 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 + " anim layer: " + imw.mAnimLayer);
1066 }
1067 }
Romain Guy06882f82009-06-10 13:36:04 -07001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1070 int wpos = mWindows.indexOf(win);
1071 if (wpos >= 0) {
1072 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001073 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 mWindows.remove(wpos);
1075 int NC = win.mChildWindows.size();
1076 while (NC > 0) {
1077 NC--;
1078 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1079 int cpos = mWindows.indexOf(cw);
1080 if (cpos >= 0) {
1081 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001083 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 mWindows.remove(cpos);
1085 }
1086 }
1087 }
1088 return interestingPos;
1089 }
Romain Guy06882f82009-06-10 13:36:04 -07001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 private void reAddWindowToListInOrderLocked(WindowState win) {
1092 addWindowToListInOrderLocked(win, false);
1093 // This is a hack to get all of the child windows added as well
1094 // at the right position. Child windows should be rare and
1095 // this case should be rare, so it shouldn't be that big a deal.
1096 int wpos = mWindows.indexOf(win);
1097 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001098 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001099 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 mWindows.remove(wpos);
1101 reAddWindowLocked(wpos, win);
1102 }
1103 }
Romain Guy06882f82009-06-10 13:36:04 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 void logWindowList(String prefix) {
1106 int N = mWindows.size();
1107 while (N > 0) {
1108 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001109 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111 }
Romain Guy06882f82009-06-10 13:36:04 -07001112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 void moveInputMethodDialogsLocked(int pos) {
1114 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 for (int i=0; i<N; i++) {
1119 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1120 }
1121 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001122 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 logWindowList(" ");
1124 }
Romain Guy06882f82009-06-10 13:36:04 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (pos >= 0) {
1127 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1128 if (pos < mWindows.size()) {
1129 WindowState wp = (WindowState)mWindows.get(pos);
1130 if (wp == mInputMethodWindow) {
1131 pos++;
1132 }
1133 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 for (int i=0; i<N; i++) {
1136 WindowState win = dialogs.get(i);
1137 win.mTargetAppToken = targetAppToken;
1138 pos = reAddWindowLocked(pos, win);
1139 }
1140 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 logWindowList(" ");
1143 }
1144 return;
1145 }
1146 for (int i=0; i<N; i++) {
1147 WindowState win = dialogs.get(i);
1148 win.mTargetAppToken = null;
1149 reAddWindowToListInOrderLocked(win);
1150 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001151 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 logWindowList(" ");
1153 }
1154 }
1155 }
Romain Guy06882f82009-06-10 13:36:04 -07001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1158 final WindowState imWin = mInputMethodWindow;
1159 final int DN = mInputMethodDialogs.size();
1160 if (imWin == null && DN == 0) {
1161 return false;
1162 }
Romain Guy06882f82009-06-10 13:36:04 -07001163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1165 if (imPos >= 0) {
1166 // In this case, the input method windows are to be placed
1167 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // First check to see if the input method windows are already
1170 // located here, and contiguous.
1171 final int N = mWindows.size();
1172 WindowState firstImWin = imPos < N
1173 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Figure out the actual input method window that should be
1176 // at the bottom of their stack.
1177 WindowState baseImWin = imWin != null
1178 ? imWin : mInputMethodDialogs.get(0);
1179 if (baseImWin.mChildWindows.size() > 0) {
1180 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1181 if (cw.mSubLayer < 0) baseImWin = cw;
1182 }
Romain Guy06882f82009-06-10 13:36:04 -07001183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 if (firstImWin == baseImWin) {
1185 // The windows haven't moved... but are they still contiguous?
1186 // First find the top IM window.
1187 int pos = imPos+1;
1188 while (pos < N) {
1189 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1190 break;
1191 }
1192 pos++;
1193 }
1194 pos++;
1195 // Now there should be no more input method windows above.
1196 while (pos < N) {
1197 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1198 break;
1199 }
1200 pos++;
1201 }
1202 if (pos >= N) {
1203 // All is good!
1204 return false;
1205 }
1206 }
Romain Guy06882f82009-06-10 13:36:04 -07001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 if (imWin != null) {
1209 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 logWindowList(" ");
1212 }
1213 imPos = tmpRemoveWindowLocked(imPos, imWin);
1214 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001215 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 logWindowList(" ");
1217 }
1218 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1219 reAddWindowLocked(imPos, imWin);
1220 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 logWindowList(" ");
1223 }
1224 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1225 } else {
1226 moveInputMethodDialogsLocked(imPos);
1227 }
Romain Guy06882f82009-06-10 13:36:04 -07001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 } else {
1230 // In this case, the input method windows go in a fixed layer,
1231 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001234 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 tmpRemoveWindowLocked(0, imWin);
1236 imWin.mTargetAppToken = null;
1237 reAddWindowToListInOrderLocked(imWin);
1238 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 logWindowList(" ");
1241 }
1242 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1243 } else {
1244 moveInputMethodDialogsLocked(-1);;
1245 }
Romain Guy06882f82009-06-10 13:36:04 -07001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
Romain Guy06882f82009-06-10 13:36:04 -07001248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (needAssignLayers) {
1250 assignLayersLocked();
1251 }
Romain Guy06882f82009-06-10 13:36:04 -07001252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 return true;
1254 }
Romain Guy06882f82009-06-10 13:36:04 -07001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 void adjustInputMethodDialogsLocked() {
1257 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1258 }
Romain Guy06882f82009-06-10 13:36:04 -07001259
Dianne Hackborn25994b42009-09-04 14:21:19 -07001260 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001261 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001262 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1263 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1264 ? wallpaperTarget.mAppToken.animation : null)
1265 + " upper=" + mUpperWallpaperTarget
1266 + " lower=" + mLowerWallpaperTarget);
1267 return (wallpaperTarget != null
1268 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1269 && wallpaperTarget.mAppToken.animation != null)))
1270 || mUpperWallpaperTarget != null
1271 || mLowerWallpaperTarget != null;
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001274 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1275 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001277 int adjustWallpaperWindowsLocked() {
1278 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001279
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001280 final int dw = mDisplay.getWidth();
1281 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001283 // First find top-most window that has asked to be on top of the
1284 // wallpaper; all wallpapers go behind it.
1285 final ArrayList localmWindows = mWindows;
1286 int N = localmWindows.size();
1287 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001288 WindowState foundW = null;
1289 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001290 WindowState topCurW = null;
1291 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001292 int i = N;
1293 while (i > 0) {
1294 i--;
1295 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001296 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1297 if (topCurW == null) {
1298 topCurW = w;
1299 topCurI = i;
1300 }
1301 continue;
1302 }
1303 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001304 if (w.mAppToken != null) {
1305 // If this window's app token is hidden and not animating,
1306 // it is of no interest to us.
1307 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001309 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001310 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001311 continue;
1312 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001313 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001314 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001315 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1316 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001317 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001318 && (mWallpaperTarget == w
1319 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001320 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001321 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001322 foundW = w;
1323 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001324 if (w == mWallpaperTarget && ((w.mAppToken != null
1325 && w.mAppToken.animation != null)
1326 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001327 // The current wallpaper target is animating, so we'll
1328 // look behind it for another possible target and figure
1329 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001330 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001331 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001332 continue;
1333 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001334 break;
1335 }
1336 }
1337
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001338 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001339 // If we are currently waiting for an app transition, and either
1340 // the current target or the next target are involved with it,
1341 // then hold off on doing anything with the wallpaper.
1342 // Note that we are checking here for just whether the target
1343 // is part of an app token... which is potentially overly aggressive
1344 // (the app token may not be involved in the transition), but good
1345 // enough (we'll just wait until whatever transition is pending
1346 // executes).
1347 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001349 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001350 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001351 }
1352 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001353 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001354 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001355 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001356 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001359 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001360 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001361 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001362 + " oldTarget: " + mWallpaperTarget);
1363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001365 mLowerWallpaperTarget = null;
1366 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001368 WindowState oldW = mWallpaperTarget;
1369 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001371 // Now what is happening... if the current and new targets are
1372 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001373 if (foundW != null && oldW != null) {
1374 boolean oldAnim = oldW.mAnimation != null
1375 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1376 boolean foundAnim = foundW.mAnimation != null
1377 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001378 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001380 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001381 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001382 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001383 int oldI = localmWindows.indexOf(oldW);
1384 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001385 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001386 }
1387 if (oldI >= 0) {
1388 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001389 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001390 + "=" + oldW + "; new#" + foundI
1391 + "=" + foundW);
1392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001394 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001395 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001396 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001397 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001398 }
1399 mWallpaperTarget = oldW;
1400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001402 // Now set the upper and lower wallpaper targets
1403 // correctly, and make sure that we are positioning
1404 // the wallpaper below the lower.
1405 if (foundI > oldI) {
1406 // The new target is on top of the old one.
1407 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001409 }
1410 mUpperWallpaperTarget = foundW;
1411 mLowerWallpaperTarget = oldW;
1412 foundW = oldW;
1413 foundI = oldI;
1414 } else {
1415 // The new target is below the old one.
1416 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001418 }
1419 mUpperWallpaperTarget = oldW;
1420 mLowerWallpaperTarget = foundW;
1421 }
1422 }
1423 }
1424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001426 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001427 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001428 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1429 || (mLowerWallpaperTarget.mAppToken != null
1430 && mLowerWallpaperTarget.mAppToken.animation != null);
1431 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1432 || (mUpperWallpaperTarget.mAppToken != null
1433 && mUpperWallpaperTarget.mAppToken.animation != null);
1434 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001435 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001436 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001437 }
1438 mLowerWallpaperTarget = null;
1439 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001440 }
1441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001443 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001444 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001445 // The window is visible to the compositor... but is it visible
1446 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001447 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001448 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001450 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001451 // its layer adjustment. Only do this if we are not transfering
1452 // between two wallpaper targets.
1453 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001454 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001455 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001457 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1458 * TYPE_LAYER_MULTIPLIER
1459 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001461 // Now w is the window we are supposed to be behind... but we
1462 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001463 // AND any starting window associated with it, AND below the
1464 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001465 while (foundI > 0) {
1466 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001467 if (wb.mBaseLayer < maxLayer &&
1468 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001469 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001470 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001471 // This window is not related to the previous one in any
1472 // interesting way, so stop here.
1473 break;
1474 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001475 foundW = wb;
1476 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001477 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001478 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001479 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001482 if (foundW == null && topCurW != null) {
1483 // There is no wallpaper target, so it goes at the bottom.
1484 // We will assume it is the same place as last time, if known.
1485 foundW = topCurW;
1486 foundI = topCurI+1;
1487 } else {
1488 // Okay i is the position immediately above the wallpaper. Look at
1489 // what is below it for later.
1490 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
Dianne Hackborn284ac932009-08-28 10:34:25 -07001493 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001494 if (mWallpaperTarget.mWallpaperX >= 0) {
1495 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001496 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001497 }
1498 if (mWallpaperTarget.mWallpaperY >= 0) {
1499 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001500 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001501 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001504 // Start stepping backwards from here, ensuring that our wallpaper windows
1505 // are correctly placed.
1506 int curTokenIndex = mWallpaperTokens.size();
1507 while (curTokenIndex > 0) {
1508 curTokenIndex--;
1509 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001510 if (token.hidden == visible) {
1511 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1512 token.hidden = !visible;
1513 // Need to do a layout to ensure the wallpaper now has the
1514 // correct size.
1515 mLayoutNeeded = true;
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001518 int curWallpaperIndex = token.windows.size();
1519 while (curWallpaperIndex > 0) {
1520 curWallpaperIndex--;
1521 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001522
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001523 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001524 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001527 // First, make sure the client has the current visibility
1528 // state.
1529 if (wallpaper.mWallpaperVisible != visible) {
1530 wallpaper.mWallpaperVisible = visible;
1531 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001532 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001533 "Setting visibility of wallpaper " + wallpaper
1534 + ": " + visible);
1535 wallpaper.mClient.dispatchAppVisibility(visible);
1536 } catch (RemoteException e) {
1537 }
1538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001540 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001541 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001542 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001544 // First, if this window is at the current index, then all
1545 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001546 if (wallpaper == foundW) {
1547 foundI--;
1548 foundW = foundI > 0
1549 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001550 continue;
1551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001552
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001553 // The window didn't match... the current wallpaper window,
1554 // wherever it is, is in the wrong place, so make sure it is
1555 // not in the list.
1556 int oldIndex = localmWindows.indexOf(wallpaper);
1557 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001558 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001559 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001560 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001561 if (oldIndex < foundI) {
1562 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001563 }
1564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001566 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001568 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001569 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001571 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001572 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001576 return changed;
1577 }
1578
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001579 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001580 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001581 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001582 mWallpaperAnimLayerAdjustment = adj;
1583 int curTokenIndex = mWallpaperTokens.size();
1584 while (curTokenIndex > 0) {
1585 curTokenIndex--;
1586 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1587 int curWallpaperIndex = token.windows.size();
1588 while (curWallpaperIndex > 0) {
1589 curWallpaperIndex--;
1590 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1591 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001592 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001593 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001594 }
1595 }
1596 }
1597
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001598 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1599 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001600 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001601 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001602 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001603 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001604 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1605 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1606 changed = wallpaperWin.mXOffset != offset;
1607 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001608 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001609 + wallpaperWin + " x: " + offset);
1610 wallpaperWin.mXOffset = offset;
1611 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001612 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001613 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001614 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001615 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001618 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001619 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001620 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1621 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1622 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001623 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001624 + wallpaperWin + " y: " + offset);
1625 changed = true;
1626 wallpaperWin.mYOffset = offset;
1627 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001628 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001629 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001630 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001631 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001634 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001635 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001637 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1638 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001639 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001640 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001641 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001642 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001643 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1644 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001645 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001646 if (mWaitingOnWallpaper != null) {
1647 long start = SystemClock.uptimeMillis();
1648 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1649 < start) {
1650 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001651 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001652 "Waiting for offset complete...");
1653 mWindowMap.wait(WALLPAPER_TIMEOUT);
1654 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001655 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001656 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001657 if ((start+WALLPAPER_TIMEOUT)
1658 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001660 + wallpaperWin);
1661 mLastWallpaperTimeoutTime = start;
1662 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001663 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001664 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001665 }
1666 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001667 } catch (RemoteException e) {
1668 }
1669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001671 return changed;
1672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001674 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001675 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001676 if (mWaitingOnWallpaper != null &&
1677 mWaitingOnWallpaper.mClient.asBinder() == window) {
1678 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001679 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001680 }
1681 }
1682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001683
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001684 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001685 final int dw = mDisplay.getWidth();
1686 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001687
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001688 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001690 WindowState target = mWallpaperTarget;
1691 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001692 if (target.mWallpaperX >= 0) {
1693 mLastWallpaperX = target.mWallpaperX;
1694 } else if (changingTarget.mWallpaperX >= 0) {
1695 mLastWallpaperX = changingTarget.mWallpaperX;
1696 }
1697 if (target.mWallpaperY >= 0) {
1698 mLastWallpaperY = target.mWallpaperY;
1699 } else if (changingTarget.mWallpaperY >= 0) {
1700 mLastWallpaperY = changingTarget.mWallpaperY;
1701 }
1702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001704 int curTokenIndex = mWallpaperTokens.size();
1705 while (curTokenIndex > 0) {
1706 curTokenIndex--;
1707 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1708 int curWallpaperIndex = token.windows.size();
1709 while (curWallpaperIndex > 0) {
1710 curWallpaperIndex--;
1711 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1712 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1713 wallpaper.computeShownFrameLocked();
1714 changed = true;
1715 // We only want to be synchronous with one wallpaper.
1716 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001717 }
1718 }
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001721 return changed;
1722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001724 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001725 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001726 final int dw = mDisplay.getWidth();
1727 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001729 int curTokenIndex = mWallpaperTokens.size();
1730 while (curTokenIndex > 0) {
1731 curTokenIndex--;
1732 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001733 if (token.hidden == visible) {
1734 token.hidden = !visible;
1735 // Need to do a layout to ensure the wallpaper now has the
1736 // correct size.
1737 mLayoutNeeded = true;
1738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001740 int curWallpaperIndex = token.windows.size();
1741 while (curWallpaperIndex > 0) {
1742 curWallpaperIndex--;
1743 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1744 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001745 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001748 if (wallpaper.mWallpaperVisible != visible) {
1749 wallpaper.mWallpaperVisible = visible;
1750 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001751 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001752 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001753 + ": " + visible);
1754 wallpaper.mClient.dispatchAppVisibility(visible);
1755 } catch (RemoteException e) {
1756 }
1757 }
1758 }
1759 }
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001762 void sendPointerToWallpaperLocked(WindowState srcWin,
1763 MotionEvent pointer, long eventTime) {
1764 int curTokenIndex = mWallpaperTokens.size();
1765 while (curTokenIndex > 0) {
1766 curTokenIndex--;
1767 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1768 int curWallpaperIndex = token.windows.size();
1769 while (curWallpaperIndex > 0) {
1770 curWallpaperIndex--;
1771 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1772 if ((wallpaper.mAttrs.flags &
1773 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1774 continue;
1775 }
1776 try {
1777 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001778 if (srcWin != null) {
1779 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1780 srcWin.mFrame.top-wallpaper.mFrame.top);
1781 } else {
1782 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1783 }
1784 switch (pointer.getAction()) {
1785 case MotionEvent.ACTION_DOWN:
1786 mSendingPointersToWallpaper = true;
1787 break;
1788 case MotionEvent.ACTION_UP:
1789 mSendingPointersToWallpaper = false;
1790 break;
1791 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001792 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1793 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001794 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001795 }
1796 }
1797 }
1798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001800 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1801 MotionEvent pointer, long eventTime, boolean skipped) {
1802 if (relWin != null) {
1803 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1804 } else {
1805 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1806 }
1807
1808 // If we sent an initial down to the wallpaper, then continue
1809 // sending events until the final up.
1810 if (mSendingPointersToWallpaper) {
1811 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001812 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001813 }
1814 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1815
1816 // If we are on top of the wallpaper, then the wallpaper also
1817 // gets to see this movement.
1818 } else if (srcWin != null
1819 && pointer.getAction() == MotionEvent.ACTION_DOWN
1820 && mWallpaperTarget == srcWin
1821 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1822 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1823 }
1824 }
1825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 public int addWindow(Session session, IWindow client,
1827 WindowManager.LayoutParams attrs, int viewVisibility,
1828 Rect outContentInsets) {
1829 int res = mPolicy.checkAddPermission(attrs);
1830 if (res != WindowManagerImpl.ADD_OKAY) {
1831 return res;
1832 }
Romain Guy06882f82009-06-10 13:36:04 -07001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 boolean reportNewConfig = false;
1835 WindowState attachedWindow = null;
1836 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 synchronized(mWindowMap) {
1839 // Instantiating a Display requires talking with the simulator,
1840 // so don't do it until we know the system is mostly up and
1841 // running.
1842 if (mDisplay == null) {
1843 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1844 mDisplay = wm.getDefaultDisplay();
1845 mQueue.setDisplay(mDisplay);
1846 reportNewConfig = true;
1847 }
Romain Guy06882f82009-06-10 13:36:04 -07001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001850 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1852 }
1853
1854 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001855 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001857 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 + attrs.token + ". Aborting.");
1859 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1860 }
1861 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1862 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001863 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 + attrs.token + ". Aborting.");
1865 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1866 }
1867 }
1868
1869 boolean addToken = false;
1870 WindowToken token = mTokenMap.get(attrs.token);
1871 if (token == null) {
1872 if (attrs.type >= FIRST_APPLICATION_WINDOW
1873 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001874 Slog.w(TAG, "Attempted to add application window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 + attrs.token + ". Aborting.");
1876 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1877 }
1878 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 + attrs.token + ". Aborting.");
1881 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1882 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001883 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001884 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001885 + attrs.token + ". Aborting.");
1886 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 token = new WindowToken(attrs.token, -1, false);
1889 addToken = true;
1890 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1891 && attrs.type <= LAST_APPLICATION_WINDOW) {
1892 AppWindowToken atoken = token.appWindowToken;
1893 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001894 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 + token + ". Aborting.");
1896 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1897 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001898 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 + token + ". Aborting.");
1900 return WindowManagerImpl.ADD_APP_EXITING;
1901 }
1902 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1903 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001904 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1906 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1907 }
1908 } else if (attrs.type == TYPE_INPUT_METHOD) {
1909 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 + attrs.token + ". Aborting.");
1912 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1913 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001914 } else if (attrs.type == TYPE_WALLPAPER) {
1915 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001916 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001917 + attrs.token + ". Aborting.");
1918 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921
1922 win = new WindowState(session, client, token,
1923 attachedWindow, attrs, viewVisibility);
1924 if (win.mDeathRecipient == null) {
1925 // Client has apparently died, so there is no reason to
1926 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001927 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 + " that is dead, aborting.");
1929 return WindowManagerImpl.ADD_APP_EXITING;
1930 }
1931
1932 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 res = mPolicy.prepareAddWindowLw(win, attrs);
1935 if (res != WindowManagerImpl.ADD_OKAY) {
1936 return res;
1937 }
1938
1939 // From now on, no exceptions or errors allowed!
1940
1941 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (addToken) {
1946 mTokenMap.put(attrs.token, token);
1947 mTokenList.add(token);
1948 }
1949 win.attach();
1950 mWindowMap.put(client.asBinder(), win);
1951
1952 if (attrs.type == TYPE_APPLICATION_STARTING &&
1953 token.appWindowToken != null) {
1954 token.appWindowToken.startingWindow = win;
1955 }
1956
1957 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 if (attrs.type == TYPE_INPUT_METHOD) {
1960 mInputMethodWindow = win;
1961 addInputMethodWindowToListLocked(win);
1962 imMayMove = false;
1963 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1964 mInputMethodDialogs.add(win);
1965 addWindowToListInOrderLocked(win, true);
1966 adjustInputMethodDialogsLocked();
1967 imMayMove = false;
1968 } else {
1969 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001970 if (attrs.type == TYPE_WALLPAPER) {
1971 mLastWallpaperTimeoutTime = 0;
1972 adjustWallpaperWindowsLocked();
1973 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001974 adjustWallpaperWindowsLocked();
1975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Romain Guy06882f82009-06-10 13:36:04 -07001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if (mInTouchMode) {
1983 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1984 }
1985 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1986 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1987 }
Romain Guy06882f82009-06-10 13:36:04 -07001988
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001989 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001991 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1992 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 imMayMove = false;
1994 }
1995 }
Romain Guy06882f82009-06-10 13:36:04 -07001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07001998 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
Romain Guy06882f82009-06-10 13:36:04 -07002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 assignLayersLocked();
2002 // Don't do layout here, the window must call
2003 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 //dump();
2006
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002007 if (focusChanged) {
2008 if (mCurrentFocus != null) {
2009 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2010 }
2011 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002012 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 TAG, "New client " + client.asBinder()
2014 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002015
2016 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2017 reportNewConfig = true;
2018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020
2021 // sendNewConfiguration() checks caller permissions so we must call it with
2022 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2023 // identity anyway, so it's safe to just clear & restore around this whole
2024 // block.
2025 final long origId = Binder.clearCallingIdentity();
2026 if (reportNewConfig) {
2027 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
2029 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 return res;
2032 }
Romain Guy06882f82009-06-10 13:36:04 -07002033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 public void removeWindow(Session session, IWindow client) {
2035 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002036 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 if (win == null) {
2038 return;
2039 }
2040 removeWindowLocked(session, win);
2041 }
2042 }
Romain Guy06882f82009-06-10 13:36:04 -07002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 public void removeWindowLocked(Session session, WindowState win) {
2045
Joe Onorato8a9b2202010-02-26 18:56:32 -08002046 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 TAG, "Remove " + win + " client="
2048 + Integer.toHexString(System.identityHashCode(
2049 win.mClient.asBinder()))
2050 + ", surface=" + win.mSurface);
2051
2052 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002053
Joe Onorato8a9b2202010-02-26 18:56:32 -08002054 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2056 + " mExiting=" + win.mExiting
2057 + " isAnimating=" + win.isAnimating()
2058 + " app-animation="
2059 + (win.mAppToken != null ? win.mAppToken.animation : null)
2060 + " inPendingTransaction="
2061 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2062 + " mDisplayFrozen=" + mDisplayFrozen);
2063 // Visibility of the removed window. Will be used later to update orientation later on.
2064 boolean wasVisible = false;
2065 // First, see if we need to run an animation. If we do, we have
2066 // to hold off on removing the window until the animation is done.
2067 // If the display is frozen, just remove immediately, since the
2068 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002069 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 // If we are not currently running the exit animation, we
2071 // need to see about starting one.
2072 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2075 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2076 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2077 }
2078 // Try starting an animation.
2079 if (applyAnimationLocked(win, transit, false)) {
2080 win.mExiting = true;
2081 }
2082 }
2083 if (win.mExiting || win.isAnimating()) {
2084 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002085 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 win.mExiting = true;
2087 win.mRemoveOnExit = true;
2088 mLayoutNeeded = true;
2089 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2090 performLayoutAndPlaceSurfacesLocked();
2091 if (win.mAppToken != null) {
2092 win.mAppToken.updateReportedVisibilityLocked();
2093 }
2094 //dump();
2095 Binder.restoreCallingIdentity(origId);
2096 return;
2097 }
2098 }
2099
2100 removeWindowInnerLocked(session, win);
2101 // Removing a visible window will effect the computed orientation
2102 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002103 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002104 != mForcedAppOrientation
2105 && updateOrientationFromAppTokensLocked()) {
2106 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2109 Binder.restoreCallingIdentity(origId);
2110 }
Romain Guy06882f82009-06-10 13:36:04 -07002111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002113 mKeyWaiter.finishedKey(session, win.mClient, true,
2114 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2116 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 if (mInputMethodTarget == win) {
2121 moveInputMethodWindowsIfNeededLocked(false);
2122 }
Romain Guy06882f82009-06-10 13:36:04 -07002123
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002124 if (false) {
2125 RuntimeException e = new RuntimeException("here");
2126 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002127 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 mPolicy.removeWindowLw(win);
2131 win.removeLocked();
2132
2133 mWindowMap.remove(win.mClient.asBinder());
2134 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002135 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136
2137 if (mInputMethodWindow == win) {
2138 mInputMethodWindow = null;
2139 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2140 mInputMethodDialogs.remove(win);
2141 }
Romain Guy06882f82009-06-10 13:36:04 -07002142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 final WindowToken token = win.mToken;
2144 final AppWindowToken atoken = win.mAppToken;
2145 token.windows.remove(win);
2146 if (atoken != null) {
2147 atoken.allAppWindows.remove(win);
2148 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002149 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 TAG, "**** Removing window " + win + ": count="
2151 + token.windows.size());
2152 if (token.windows.size() == 0) {
2153 if (!token.explicit) {
2154 mTokenMap.remove(token.token);
2155 mTokenList.remove(token);
2156 } else if (atoken != null) {
2157 atoken.firstWindowDrawn = false;
2158 }
2159 }
2160
2161 if (atoken != null) {
2162 if (atoken.startingWindow == win) {
2163 atoken.startingWindow = null;
2164 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2165 // If this is the last window and we had requested a starting
2166 // transition window, well there is no point now.
2167 atoken.startingData = null;
2168 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2169 // If this is the last window except for a starting transition
2170 // window, we need to get rid of the starting transition.
2171 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002172 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 + ": no more real windows");
2174 }
2175 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2176 mH.sendMessage(m);
2177 }
2178 }
Romain Guy06882f82009-06-10 13:36:04 -07002179
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002180 if (win.mAttrs.type == TYPE_WALLPAPER) {
2181 mLastWallpaperTimeoutTime = 0;
2182 adjustWallpaperWindowsLocked();
2183 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002184 adjustWallpaperWindowsLocked();
2185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 if (!mInLayout) {
2188 assignLayersLocked();
2189 mLayoutNeeded = true;
2190 performLayoutAndPlaceSurfacesLocked();
2191 if (win.mAppToken != null) {
2192 win.mAppToken.updateReportedVisibilityLocked();
2193 }
2194 }
2195 }
2196
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002197 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2198 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2199 + ": " + msg + " / " + w.mAttrs.getTitle();
2200 if (where != null) {
2201 Slog.i(TAG, str, where);
2202 } else {
2203 Slog.i(TAG, str);
2204 }
2205 }
2206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2208 long origId = Binder.clearCallingIdentity();
2209 try {
2210 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002211 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002213 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 Surface.openTransaction();
2215 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002216 if (SHOW_TRANSACTIONS) logSurface(w,
2217 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 w.mSurface.setTransparentRegionHint(region);
2219 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002220 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 Surface.closeTransaction();
2222 }
2223 }
2224 }
2225 } finally {
2226 Binder.restoreCallingIdentity(origId);
2227 }
2228 }
2229
2230 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002231 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 Rect visibleInsets) {
2233 long origId = Binder.clearCallingIdentity();
2234 try {
2235 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002236 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 if (w != null) {
2238 w.mGivenInsetsPending = false;
2239 w.mGivenContentInsets.set(contentInsets);
2240 w.mGivenVisibleInsets.set(visibleInsets);
2241 w.mTouchableInsets = touchableInsets;
2242 mLayoutNeeded = true;
2243 performLayoutAndPlaceSurfacesLocked();
2244 }
2245 }
2246 } finally {
2247 Binder.restoreCallingIdentity(origId);
2248 }
2249 }
Romain Guy06882f82009-06-10 13:36:04 -07002250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 public void getWindowDisplayFrame(Session session, IWindow client,
2252 Rect outDisplayFrame) {
2253 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002254 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 if (win == null) {
2256 outDisplayFrame.setEmpty();
2257 return;
2258 }
2259 outDisplayFrame.set(win.mDisplayFrame);
2260 }
2261 }
2262
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002263 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2264 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002265 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2266 window.mWallpaperX = x;
2267 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002268 window.mWallpaperXStep = xStep;
2269 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002270 if (updateWallpaperOffsetLocked(window, true)) {
2271 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002272 }
2273 }
2274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002275
Dianne Hackborn75804932009-10-20 20:15:20 -07002276 void wallpaperCommandComplete(IBinder window, Bundle result) {
2277 synchronized (mWindowMap) {
2278 if (mWaitingOnWallpaper != null &&
2279 mWaitingOnWallpaper.mClient.asBinder() == window) {
2280 mWaitingOnWallpaper = null;
2281 mWindowMap.notifyAll();
2282 }
2283 }
2284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
Dianne Hackborn75804932009-10-20 20:15:20 -07002286 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2287 String action, int x, int y, int z, Bundle extras, boolean sync) {
2288 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2289 || window == mUpperWallpaperTarget) {
2290 boolean doWait = sync;
2291 int curTokenIndex = mWallpaperTokens.size();
2292 while (curTokenIndex > 0) {
2293 curTokenIndex--;
2294 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2295 int curWallpaperIndex = token.windows.size();
2296 while (curWallpaperIndex > 0) {
2297 curWallpaperIndex--;
2298 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2299 try {
2300 wallpaper.mClient.dispatchWallpaperCommand(action,
2301 x, y, z, extras, sync);
2302 // We only want to be synchronous with one wallpaper.
2303 sync = false;
2304 } catch (RemoteException e) {
2305 }
2306 }
2307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002308
Dianne Hackborn75804932009-10-20 20:15:20 -07002309 if (doWait) {
2310 // XXX Need to wait for result.
2311 }
2312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002313
Dianne Hackborn75804932009-10-20 20:15:20 -07002314 return null;
2315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 public int relayoutWindow(Session session, IWindow client,
2318 WindowManager.LayoutParams attrs, int requestedWidth,
2319 int requestedHeight, int viewVisibility, boolean insetsPending,
2320 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002321 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 boolean displayed = false;
2323 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002324 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002328 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 if (win == null) {
2330 return 0;
2331 }
2332 win.mRequestedWidth = requestedWidth;
2333 win.mRequestedHeight = requestedHeight;
2334
2335 if (attrs != null) {
2336 mPolicy.adjustWindowParamsLw(attrs);
2337 }
Romain Guy06882f82009-06-10 13:36:04 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 int attrChanges = 0;
2340 int flagChanges = 0;
2341 if (attrs != null) {
2342 flagChanges = win.mAttrs.flags ^= attrs.flags;
2343 attrChanges = win.mAttrs.copyFrom(attrs);
2344 }
2345
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347
2348 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2349 win.mAlpha = attrs.alpha;
2350 }
2351
2352 final boolean scaledWindow =
2353 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2354
2355 if (scaledWindow) {
2356 // requested{Width|Height} Surface's physical size
2357 // attrs.{width|height} Size on screen
2358 win.mHScale = (attrs.width != requestedWidth) ?
2359 (attrs.width / (float)requestedWidth) : 1.0f;
2360 win.mVScale = (attrs.height != requestedHeight) ?
2361 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002362 } else {
2363 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365
2366 boolean imMayMove = (flagChanges&(
2367 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2368 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 boolean focusMayChange = win.mViewVisibility != viewVisibility
2371 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2372 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002373
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002374 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2375 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 win.mRelayoutCalled = true;
2378 final int oldVisibility = win.mViewVisibility;
2379 win.mViewVisibility = viewVisibility;
2380 if (viewVisibility == View.VISIBLE &&
2381 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2382 displayed = !win.isVisibleLw();
2383 if (win.mExiting) {
2384 win.mExiting = false;
2385 win.mAnimation = null;
2386 }
2387 if (win.mDestroying) {
2388 win.mDestroying = false;
2389 mDestroySurface.remove(win);
2390 }
2391 if (oldVisibility == View.GONE) {
2392 win.mEnterAnimationPending = true;
2393 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002394 if (displayed) {
2395 if (win.mSurface != null && !win.mDrawPending
2396 && !win.mCommitDrawPending && !mDisplayFrozen
2397 && mPolicy.isScreenOn()) {
2398 applyEnterAnimationLocked(win);
2399 }
2400 if ((win.mAttrs.flags
2401 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2402 if (DEBUG_VISIBILITY) Slog.v(TAG,
2403 "Relayout window turning screen on: " + win);
2404 win.mTurnOnScreen = true;
2405 }
2406 int diff = 0;
2407 if (win.mConfiguration != mCurConfiguration
2408 && (win.mConfiguration == null
2409 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2410 win.mConfiguration = mCurConfiguration;
2411 if (DEBUG_CONFIGURATION) {
2412 Slog.i(TAG, "Window " + win + " visible with new config: "
2413 + win.mConfiguration + " / 0x"
2414 + Integer.toHexString(diff));
2415 }
2416 outConfig.setTo(mCurConfiguration);
2417 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2420 // To change the format, we need to re-build the surface.
2421 win.destroySurfaceLocked();
2422 displayed = true;
2423 }
2424 try {
2425 Surface surface = win.createSurfaceLocked();
2426 if (surface != null) {
2427 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002428 win.mReportDestroySurface = false;
2429 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002430 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002431 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002433 // For some reason there isn't a surface. Clear the
2434 // caller's object so they see the same state.
2435 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002438 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002439 + client + " (" + win.mAttrs.getTitle() + ")",
2440 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 Binder.restoreCallingIdentity(origId);
2442 return 0;
2443 }
2444 if (displayed) {
2445 focusMayChange = true;
2446 }
2447 if (win.mAttrs.type == TYPE_INPUT_METHOD
2448 && mInputMethodWindow == null) {
2449 mInputMethodWindow = win;
2450 imMayMove = true;
2451 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002452 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2453 && win.mAppToken != null
2454 && win.mAppToken.startingWindow != null) {
2455 // Special handling of starting window over the base
2456 // window of the app: propagate lock screen flags to it,
2457 // to provide the correct semantics while starting.
2458 final int mask =
2459 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002460 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2461 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002462 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2463 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 } else {
2466 win.mEnterAnimationPending = false;
2467 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002468 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002469 + ": mExiting=" + win.mExiting
2470 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 // If we are not currently running the exit animation, we
2472 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002473 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 // Try starting an animation; if there isn't one, we
2475 // can destroy the surface right away.
2476 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2477 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2478 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2479 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002480 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002482 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 win.mExiting = true;
2484 mKeyWaiter.finishedKey(session, client, true,
2485 KeyWaiter.RETURN_NOTHING);
2486 } else if (win.isAnimating()) {
2487 // Currently in a hide animation... turn this into
2488 // an exit.
2489 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002490 } else if (win == mWallpaperTarget) {
2491 // If the wallpaper is currently behind this
2492 // window, we need to change both of them inside
2493 // of a transaction to avoid artifacts.
2494 win.mExiting = true;
2495 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 } else {
2497 if (mInputMethodWindow == win) {
2498 mInputMethodWindow = null;
2499 }
2500 win.destroySurfaceLocked();
2501 }
2502 }
2503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002504
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002505 if (win.mSurface == null || (win.getAttrs().flags
2506 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2507 || win.mSurfacePendingDestroy) {
2508 // We are being called from a local process, which
2509 // means outSurface holds its current surface. Ensure the
2510 // surface object is cleared, but we don't want it actually
2511 // destroyed at this point.
2512 win.mSurfacePendingDestroy = false;
2513 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002514 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002515 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002516 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002517 "Keeping surface, will report destroy: " + win);
2518 win.mReportDestroySurface = true;
2519 outSurface.copyFrom(win.mSurface);
2520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
2522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 if (focusMayChange) {
2524 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2525 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 imMayMove = false;
2527 }
2528 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2529 }
Romain Guy06882f82009-06-10 13:36:04 -07002530
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002531 // updateFocusedWindowLocked() already assigned layers so we only need to
2532 // reassign them at this point if the IM window state gets shuffled
2533 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002536 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2537 // Little hack here -- we -should- be able to rely on the
2538 // function to return true if the IME has moved and needs
2539 // its layer recomputed. However, if the IME was hidden
2540 // and isn't actually moved in the list, its layer may be
2541 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 assignLayers = true;
2543 }
2544 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002545 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002546 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002547 assignLayers = true;
2548 }
2549 }
Romain Guy06882f82009-06-10 13:36:04 -07002550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 mLayoutNeeded = true;
2552 win.mGivenInsetsPending = insetsPending;
2553 if (assignLayers) {
2554 assignLayersLocked();
2555 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002556 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002558 if (displayed && win.mIsWallpaper) {
2559 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002560 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 if (win.mAppToken != null) {
2563 win.mAppToken.updateReportedVisibilityLocked();
2564 }
2565 outFrame.set(win.mFrame);
2566 outContentInsets.set(win.mContentInsets);
2567 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002568 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002570 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 + ", requestedHeight=" + requestedHeight
2572 + ", viewVisibility=" + viewVisibility
2573 + "\nRelayout returning frame=" + outFrame
2574 + ", surface=" + outSurface);
2575
Joe Onorato8a9b2202010-02-26 18:56:32 -08002576 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2578
2579 inTouchMode = mInTouchMode;
2580 }
2581
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002582 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 sendNewConfiguration();
2584 }
Romain Guy06882f82009-06-10 13:36:04 -07002585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2589 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2590 }
2591
2592 public void finishDrawingWindow(Session session, IWindow client) {
2593 final long origId = Binder.clearCallingIdentity();
2594 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002595 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002597 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2598 adjustWallpaperWindowsLocked();
2599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 mLayoutNeeded = true;
2601 performLayoutAndPlaceSurfacesLocked();
2602 }
2603 }
2604 Binder.restoreCallingIdentity(origId);
2605 }
2606
2607 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002608 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 + (lp != null ? lp.packageName : null)
2610 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2611 if (lp != null && lp.windowAnimations != 0) {
2612 // If this is a system resource, don't try to load it from the
2613 // application resources. It is nice to avoid loading application
2614 // resources if we can.
2615 String packageName = lp.packageName != null ? lp.packageName : "android";
2616 int resId = lp.windowAnimations;
2617 if ((resId&0xFF000000) == 0x01000000) {
2618 packageName = "android";
2619 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002620 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 + packageName);
2622 return AttributeCache.instance().get(packageName, resId,
2623 com.android.internal.R.styleable.WindowAnimation);
2624 }
2625 return null;
2626 }
Romain Guy06882f82009-06-10 13:36:04 -07002627
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002628 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002629 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002630 + packageName + " resId=0x" + Integer.toHexString(resId));
2631 if (packageName != null) {
2632 if ((resId&0xFF000000) == 0x01000000) {
2633 packageName = "android";
2634 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002635 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002636 + packageName);
2637 return AttributeCache.instance().get(packageName, resId,
2638 com.android.internal.R.styleable.WindowAnimation);
2639 }
2640 return null;
2641 }
2642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 private void applyEnterAnimationLocked(WindowState win) {
2644 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2645 if (win.mEnterAnimationPending) {
2646 win.mEnterAnimationPending = false;
2647 transit = WindowManagerPolicy.TRANSIT_ENTER;
2648 }
2649
2650 applyAnimationLocked(win, transit, true);
2651 }
2652
2653 private boolean applyAnimationLocked(WindowState win,
2654 int transit, boolean isEntrance) {
2655 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2656 // If we are trying to apply an animation, but already running
2657 // an animation of the same type, then just leave that one alone.
2658 return true;
2659 }
Romain Guy06882f82009-06-10 13:36:04 -07002660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 // Only apply an animation if the display isn't frozen. If it is
2662 // frozen, there is no reason to animate and it can cause strange
2663 // artifacts when we unfreeze the display if some different animation
2664 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002665 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 int anim = mPolicy.selectAnimationLw(win, transit);
2667 int attr = -1;
2668 Animation a = null;
2669 if (anim != 0) {
2670 a = AnimationUtils.loadAnimation(mContext, anim);
2671 } else {
2672 switch (transit) {
2673 case WindowManagerPolicy.TRANSIT_ENTER:
2674 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2675 break;
2676 case WindowManagerPolicy.TRANSIT_EXIT:
2677 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2678 break;
2679 case WindowManagerPolicy.TRANSIT_SHOW:
2680 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2681 break;
2682 case WindowManagerPolicy.TRANSIT_HIDE:
2683 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2684 break;
2685 }
2686 if (attr >= 0) {
2687 a = loadAnimation(win.mAttrs, attr);
2688 }
2689 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002690 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2692 + " mAnimation=" + win.mAnimation
2693 + " isEntrance=" + isEntrance);
2694 if (a != null) {
2695 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002696 RuntimeException e = null;
2697 if (!HIDE_STACK_CRAWLS) {
2698 e = new RuntimeException();
2699 e.fillInStackTrace();
2700 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002701 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
2703 win.setAnimation(a);
2704 win.mAnimationIsEntrance = isEntrance;
2705 }
2706 } else {
2707 win.clearAnimation();
2708 }
2709
2710 return win.mAnimation != null;
2711 }
2712
2713 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2714 int anim = 0;
2715 Context context = mContext;
2716 if (animAttr >= 0) {
2717 AttributeCache.Entry ent = getCachedAnimations(lp);
2718 if (ent != null) {
2719 context = ent.context;
2720 anim = ent.array.getResourceId(animAttr, 0);
2721 }
2722 }
2723 if (anim != 0) {
2724 return AnimationUtils.loadAnimation(context, anim);
2725 }
2726 return null;
2727 }
Romain Guy06882f82009-06-10 13:36:04 -07002728
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002729 private Animation loadAnimation(String packageName, int resId) {
2730 int anim = 0;
2731 Context context = mContext;
2732 if (resId >= 0) {
2733 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2734 if (ent != null) {
2735 context = ent.context;
2736 anim = resId;
2737 }
2738 }
2739 if (anim != 0) {
2740 return AnimationUtils.loadAnimation(context, anim);
2741 }
2742 return null;
2743 }
2744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 private boolean applyAnimationLocked(AppWindowToken wtoken,
2746 WindowManager.LayoutParams lp, int transit, boolean enter) {
2747 // Only apply an animation if the display isn't frozen. If it is
2748 // frozen, there is no reason to animate and it can cause strange
2749 // artifacts when we unfreeze the display if some different animation
2750 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002751 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002752 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002753 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002754 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002755 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002756 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002757 } else if (mNextAppTransitionPackage != null) {
2758 a = loadAnimation(mNextAppTransitionPackage, enter ?
2759 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002760 } else {
2761 int animAttr = 0;
2762 switch (transit) {
2763 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2764 animAttr = enter
2765 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2766 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2767 break;
2768 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2769 animAttr = enter
2770 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2771 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2772 break;
2773 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2774 animAttr = enter
2775 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2776 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2777 break;
2778 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2779 animAttr = enter
2780 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2781 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2782 break;
2783 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2784 animAttr = enter
2785 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2786 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2787 break;
2788 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2789 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002790 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002791 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2792 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002793 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002794 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002795 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2796 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002797 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002798 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002799 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002800 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2801 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2802 break;
2803 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2804 animAttr = enter
2805 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2806 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2807 break;
2808 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2809 animAttr = enter
2810 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2811 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002812 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002813 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002814 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002815 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002816 + " anim=" + a
2817 + " animAttr=0x" + Integer.toHexString(animAttr)
2818 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 if (a != null) {
2821 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002822 RuntimeException e = null;
2823 if (!HIDE_STACK_CRAWLS) {
2824 e = new RuntimeException();
2825 e.fillInStackTrace();
2826 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002827 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
2829 wtoken.setAnimation(a);
2830 }
2831 } else {
2832 wtoken.clearAnimation();
2833 }
2834
2835 return wtoken.animation != null;
2836 }
2837
2838 // -------------------------------------------------------------
2839 // Application Window Tokens
2840 // -------------------------------------------------------------
2841
2842 public void validateAppTokens(List tokens) {
2843 int v = tokens.size()-1;
2844 int m = mAppTokens.size()-1;
2845 while (v >= 0 && m >= 0) {
2846 AppWindowToken wtoken = mAppTokens.get(m);
2847 if (wtoken.removed) {
2848 m--;
2849 continue;
2850 }
2851 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002852 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2854 }
2855 v--;
2856 m--;
2857 }
2858 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002859 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 v--;
2861 }
2862 while (m >= 0) {
2863 AppWindowToken wtoken = mAppTokens.get(m);
2864 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002865 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 }
2867 m--;
2868 }
2869 }
2870
2871 boolean checkCallingPermission(String permission, String func) {
2872 // Quick check: if the calling permission is me, it's all okay.
2873 if (Binder.getCallingPid() == Process.myPid()) {
2874 return true;
2875 }
Romain Guy06882f82009-06-10 13:36:04 -07002876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 if (mContext.checkCallingPermission(permission)
2878 == PackageManager.PERMISSION_GRANTED) {
2879 return true;
2880 }
2881 String msg = "Permission Denial: " + func + " from pid="
2882 + Binder.getCallingPid()
2883 + ", uid=" + Binder.getCallingUid()
2884 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002885 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 return false;
2887 }
Romain Guy06882f82009-06-10 13:36:04 -07002888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 AppWindowToken findAppWindowToken(IBinder token) {
2890 WindowToken wtoken = mTokenMap.get(token);
2891 if (wtoken == null) {
2892 return null;
2893 }
2894 return wtoken.appWindowToken;
2895 }
Romain Guy06882f82009-06-10 13:36:04 -07002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 public void addWindowToken(IBinder token, int type) {
2898 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2899 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002900 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Romain Guy06882f82009-06-10 13:36:04 -07002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 synchronized(mWindowMap) {
2904 WindowToken wtoken = mTokenMap.get(token);
2905 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002906 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 return;
2908 }
2909 wtoken = new WindowToken(token, type, true);
2910 mTokenMap.put(token, wtoken);
2911 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002912 if (type == TYPE_WALLPAPER) {
2913 mWallpaperTokens.add(wtoken);
2914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 }
2916 }
Romain Guy06882f82009-06-10 13:36:04 -07002917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 public void removeWindowToken(IBinder token) {
2919 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2920 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002921 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
2923
2924 final long origId = Binder.clearCallingIdentity();
2925 synchronized(mWindowMap) {
2926 WindowToken wtoken = mTokenMap.remove(token);
2927 mTokenList.remove(wtoken);
2928 if (wtoken != null) {
2929 boolean delayed = false;
2930 if (!wtoken.hidden) {
2931 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 final int N = wtoken.windows.size();
2934 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 for (int i=0; i<N; i++) {
2937 WindowState win = wtoken.windows.get(i);
2938
2939 if (win.isAnimating()) {
2940 delayed = true;
2941 }
Romain Guy06882f82009-06-10 13:36:04 -07002942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 if (win.isVisibleNow()) {
2944 applyAnimationLocked(win,
2945 WindowManagerPolicy.TRANSIT_EXIT, false);
2946 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2947 KeyWaiter.RETURN_NOTHING);
2948 changed = true;
2949 }
2950 }
2951
2952 if (changed) {
2953 mLayoutNeeded = true;
2954 performLayoutAndPlaceSurfacesLocked();
2955 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2956 }
Romain Guy06882f82009-06-10 13:36:04 -07002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 if (delayed) {
2959 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002960 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2961 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 }
2963 }
Romain Guy06882f82009-06-10 13:36:04 -07002964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002966 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
2968 }
2969 Binder.restoreCallingIdentity(origId);
2970 }
2971
2972 public void addAppToken(int addPos, IApplicationToken token,
2973 int groupId, int requestedOrientation, boolean fullscreen) {
2974 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2975 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002976 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 }
Romain Guy06882f82009-06-10 13:36:04 -07002978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 synchronized(mWindowMap) {
2980 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2981 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002982 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 return;
2984 }
2985 wtoken = new AppWindowToken(token);
2986 wtoken.groupId = groupId;
2987 wtoken.appFullscreen = fullscreen;
2988 wtoken.requestedOrientation = requestedOrientation;
2989 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002990 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 mTokenMap.put(token.asBinder(), wtoken);
2992 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 // Application tokens start out hidden.
2995 wtoken.hidden = true;
2996 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07002997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 //dump();
2999 }
3000 }
Romain Guy06882f82009-06-10 13:36:04 -07003001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 public void setAppGroupId(IBinder token, int groupId) {
3003 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3004 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003005 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
3007
3008 synchronized(mWindowMap) {
3009 AppWindowToken wtoken = findAppWindowToken(token);
3010 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003011 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 return;
3013 }
3014 wtoken.groupId = groupId;
3015 }
3016 }
Romain Guy06882f82009-06-10 13:36:04 -07003017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 public int getOrientationFromWindowsLocked() {
3019 int pos = mWindows.size() - 1;
3020 while (pos >= 0) {
3021 WindowState wtoken = (WindowState) mWindows.get(pos);
3022 pos--;
3023 if (wtoken.mAppToken != null) {
3024 // We hit an application window. so the orientation will be determined by the
3025 // app window. No point in continuing further.
3026 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3027 }
3028 if (!wtoken.isVisibleLw()) {
3029 continue;
3030 }
3031 int req = wtoken.mAttrs.screenOrientation;
3032 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3033 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3034 continue;
3035 } else {
3036 return req;
3037 }
3038 }
3039 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3040 }
Romain Guy06882f82009-06-10 13:36:04 -07003041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003043 int pos = mAppTokens.size() - 1;
3044 int curGroup = 0;
3045 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3046 boolean findingBehind = false;
3047 boolean haveGroup = false;
3048 boolean lastFullscreen = false;
3049 while (pos >= 0) {
3050 AppWindowToken wtoken = mAppTokens.get(pos);
3051 pos--;
3052 // if we're about to tear down this window and not seek for
3053 // the behind activity, don't use it for orientation
3054 if (!findingBehind
3055 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3056 continue;
3057 }
3058
3059 if (!haveGroup) {
3060 // We ignore any hidden applications on the top.
3061 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003062 continue;
3063 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003064 haveGroup = true;
3065 curGroup = wtoken.groupId;
3066 lastOrientation = wtoken.requestedOrientation;
3067 } else if (curGroup != wtoken.groupId) {
3068 // If we have hit a new application group, and the bottom
3069 // of the previous group didn't explicitly say to use
3070 // the orientation behind it, and the last app was
3071 // full screen, then we'll stick with the
3072 // user's orientation.
3073 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3074 && lastFullscreen) {
3075 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003078 int or = wtoken.requestedOrientation;
3079 // If this application is fullscreen, and didn't explicitly say
3080 // to use the orientation behind it, then just take whatever
3081 // orientation it has and ignores whatever is under it.
3082 lastFullscreen = wtoken.appFullscreen;
3083 if (lastFullscreen
3084 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3085 return or;
3086 }
3087 // If this application has requested an explicit orientation,
3088 // then use it.
3089 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3090 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3091 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3092 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3093 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3094 return or;
3095 }
3096 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3097 }
3098 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
Romain Guy06882f82009-06-10 13:36:04 -07003100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003102 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003103 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3104 "updateOrientationFromAppTokens()")) {
3105 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003107
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003108 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003110
3111 synchronized(mWindowMap) {
3112 if (updateOrientationFromAppTokensLocked()) {
3113 if (freezeThisOneIfNeeded != null) {
3114 AppWindowToken wtoken = findAppWindowToken(
3115 freezeThisOneIfNeeded);
3116 if (wtoken != null) {
3117 startAppFreezingScreenLocked(wtoken,
3118 ActivityInfo.CONFIG_ORIENTATION);
3119 }
3120 }
3121 config = computeNewConfigurationLocked();
3122
3123 } else if (currentConfig != null) {
3124 // No obvious action we need to take, but if our current
3125 // state mismatches the activity maanager's, update it
3126 mTempConfiguration.setToDefaults();
3127 if (computeNewConfigurationLocked(mTempConfiguration)) {
3128 if (currentConfig.diff(mTempConfiguration) != 0) {
3129 mWaitingForConfig = true;
3130 mLayoutNeeded = true;
3131 startFreezingDisplayLocked();
3132 config = new Configuration(mTempConfiguration);
3133 }
3134 }
3135 }
3136 }
3137
Dianne Hackborncfaef692009-06-15 14:24:44 -07003138 Binder.restoreCallingIdentity(ident);
3139 return config;
3140 }
3141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003143 * Determine the new desired orientation of the display, returning
3144 * a non-null new Configuration if it has changed from the current
3145 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3146 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3147 * SCREEN. This will typically be done for you if you call
3148 * sendNewConfiguration().
3149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 * The orientation is computed from non-application windows first. If none of
3151 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003152 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3154 * android.os.IBinder)
3155 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003156 boolean updateOrientationFromAppTokensLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 long ident = Binder.clearCallingIdentity();
3159 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003160 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 mForcedAppOrientation = req;
3164 //send a message to Policy indicating orientation change to take
3165 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003166 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003167 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3168 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3169 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
3171 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003172
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003173 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 } finally {
3175 Binder.restoreCallingIdentity(ident);
3176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 }
Romain Guy06882f82009-06-10 13:36:04 -07003178
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003179 int computeForcedAppOrientationLocked() {
3180 int req = getOrientationFromWindowsLocked();
3181 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3182 req = getOrientationFromAppTokensLocked();
3183 }
3184 return req;
3185 }
Romain Guy06882f82009-06-10 13:36:04 -07003186
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003187 public void setNewConfiguration(Configuration config) {
3188 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3189 "setNewConfiguration()")) {
3190 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3191 }
3192
3193 synchronized(mWindowMap) {
3194 mCurConfiguration = new Configuration(config);
3195 mWaitingForConfig = false;
3196 performLayoutAndPlaceSurfacesLocked();
3197 }
3198 }
3199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3201 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3202 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003203 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 }
Romain Guy06882f82009-06-10 13:36:04 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 synchronized(mWindowMap) {
3207 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3208 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003209 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 return;
3211 }
Romain Guy06882f82009-06-10 13:36:04 -07003212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 wtoken.requestedOrientation = requestedOrientation;
3214 }
3215 }
Romain Guy06882f82009-06-10 13:36:04 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 public int getAppOrientation(IApplicationToken token) {
3218 synchronized(mWindowMap) {
3219 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3220 if (wtoken == null) {
3221 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3222 }
Romain Guy06882f82009-06-10 13:36:04 -07003223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 return wtoken.requestedOrientation;
3225 }
3226 }
Romain Guy06882f82009-06-10 13:36:04 -07003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3229 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3230 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003231 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
3233
3234 synchronized(mWindowMap) {
3235 boolean changed = false;
3236 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003237 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 changed = mFocusedApp != null;
3239 mFocusedApp = null;
3240 mKeyWaiter.tickle();
3241 } else {
3242 AppWindowToken newFocus = findAppWindowToken(token);
3243 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003244 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 return;
3246 }
3247 changed = mFocusedApp != newFocus;
3248 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003249 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 mKeyWaiter.tickle();
3251 }
3252
3253 if (moveFocusNow && changed) {
3254 final long origId = Binder.clearCallingIdentity();
3255 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3256 Binder.restoreCallingIdentity(origId);
3257 }
3258 }
3259 }
3260
3261 public void prepareAppTransition(int transit) {
3262 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3263 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003264 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
Romain Guy06882f82009-06-10 13:36:04 -07003266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003268 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 TAG, "Prepare app transition: transit=" + transit
3270 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003271 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003272 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3273 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003275 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3276 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3277 // Opening a new task always supersedes a close for the anim.
3278 mNextAppTransition = transit;
3279 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3280 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3281 // Opening a new activity always supersedes a close for the anim.
3282 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 }
3284 mAppTransitionReady = false;
3285 mAppTransitionTimeout = false;
3286 mStartingIconInTransition = false;
3287 mSkipAppTransitionAnimation = false;
3288 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3289 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3290 5000);
3291 }
3292 }
3293 }
3294
3295 public int getPendingAppTransition() {
3296 return mNextAppTransition;
3297 }
Romain Guy06882f82009-06-10 13:36:04 -07003298
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003299 public void overridePendingAppTransition(String packageName,
3300 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003301 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003302 mNextAppTransitionPackage = packageName;
3303 mNextAppTransitionEnter = enterAnim;
3304 mNextAppTransitionExit = exitAnim;
3305 }
3306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 public void executeAppTransition() {
3309 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3310 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003311 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 }
Romain Guy06882f82009-06-10 13:36:04 -07003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003315 if (DEBUG_APP_TRANSITIONS) {
3316 RuntimeException e = new RuntimeException("here");
3317 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003318 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003319 + mNextAppTransition, e);
3320 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003321 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 mAppTransitionReady = true;
3323 final long origId = Binder.clearCallingIdentity();
3324 performLayoutAndPlaceSurfacesLocked();
3325 Binder.restoreCallingIdentity(origId);
3326 }
3327 }
3328 }
3329
3330 public void setAppStartingWindow(IBinder token, String pkg,
3331 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3332 IBinder transferFrom, boolean createIfNeeded) {
3333 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3334 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003335 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
3337
3338 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003339 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3341 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 AppWindowToken wtoken = findAppWindowToken(token);
3344 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003345 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 return;
3347 }
3348
3349 // If the display is frozen, we won't do anything until the
3350 // actual window is displayed so there is no reason to put in
3351 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003352 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 return;
3354 }
Romain Guy06882f82009-06-10 13:36:04 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 if (wtoken.startingData != null) {
3357 return;
3358 }
Romain Guy06882f82009-06-10 13:36:04 -07003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 if (transferFrom != null) {
3361 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3362 if (ttoken != null) {
3363 WindowState startingWindow = ttoken.startingWindow;
3364 if (startingWindow != null) {
3365 if (mStartingIconInTransition) {
3366 // In this case, the starting icon has already
3367 // been displayed, so start letting windows get
3368 // shown immediately without any more transitions.
3369 mSkipAppTransitionAnimation = true;
3370 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003371 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 "Moving existing starting from " + ttoken
3373 + " to " + wtoken);
3374 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 // Transfer the starting window over to the new
3377 // token.
3378 wtoken.startingData = ttoken.startingData;
3379 wtoken.startingView = ttoken.startingView;
3380 wtoken.startingWindow = startingWindow;
3381 ttoken.startingData = null;
3382 ttoken.startingView = null;
3383 ttoken.startingWindow = null;
3384 ttoken.startingMoved = true;
3385 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003386 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003388 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003389 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 mWindows.remove(startingWindow);
3391 ttoken.windows.remove(startingWindow);
3392 ttoken.allAppWindows.remove(startingWindow);
3393 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 // Propagate other interesting state between the
3396 // tokens. If the old token is displayed, we should
3397 // immediately force the new one to be displayed. If
3398 // it is animating, we need to move that animation to
3399 // the new one.
3400 if (ttoken.allDrawn) {
3401 wtoken.allDrawn = true;
3402 }
3403 if (ttoken.firstWindowDrawn) {
3404 wtoken.firstWindowDrawn = true;
3405 }
3406 if (!ttoken.hidden) {
3407 wtoken.hidden = false;
3408 wtoken.hiddenRequested = false;
3409 wtoken.willBeHidden = false;
3410 }
3411 if (wtoken.clientHidden != ttoken.clientHidden) {
3412 wtoken.clientHidden = ttoken.clientHidden;
3413 wtoken.sendAppVisibilityToClients();
3414 }
3415 if (ttoken.animation != null) {
3416 wtoken.animation = ttoken.animation;
3417 wtoken.animating = ttoken.animating;
3418 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3419 ttoken.animation = null;
3420 ttoken.animLayerAdjustment = 0;
3421 wtoken.updateLayers();
3422 ttoken.updateLayers();
3423 }
Romain Guy06882f82009-06-10 13:36:04 -07003424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 mLayoutNeeded = true;
3427 performLayoutAndPlaceSurfacesLocked();
3428 Binder.restoreCallingIdentity(origId);
3429 return;
3430 } else if (ttoken.startingData != null) {
3431 // The previous app was getting ready to show a
3432 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003433 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 "Moving pending starting from " + ttoken
3435 + " to " + wtoken);
3436 wtoken.startingData = ttoken.startingData;
3437 ttoken.startingData = null;
3438 ttoken.startingMoved = true;
3439 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3440 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3441 // want to process the message ASAP, before any other queued
3442 // messages.
3443 mH.sendMessageAtFrontOfQueue(m);
3444 return;
3445 }
3446 }
3447 }
3448
3449 // There is no existing starting window, and the caller doesn't
3450 // want us to create one, so that's it!
3451 if (!createIfNeeded) {
3452 return;
3453 }
Romain Guy06882f82009-06-10 13:36:04 -07003454
Dianne Hackborn284ac932009-08-28 10:34:25 -07003455 // If this is a translucent or wallpaper window, then don't
3456 // show a starting window -- the current effect (a full-screen
3457 // opaque starting window that fades away to the real contents
3458 // when it is ready) does not work for this.
3459 if (theme != 0) {
3460 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3461 com.android.internal.R.styleable.Window);
3462 if (ent.array.getBoolean(
3463 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3464 return;
3465 }
3466 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003467 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3468 return;
3469 }
3470 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003471 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3472 return;
3473 }
3474 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 mStartingIconInTransition = true;
3477 wtoken.startingData = new StartingData(
3478 pkg, theme, nonLocalizedLabel,
3479 labelRes, icon);
3480 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3481 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3482 // want to process the message ASAP, before any other queued
3483 // messages.
3484 mH.sendMessageAtFrontOfQueue(m);
3485 }
3486 }
3487
3488 public void setAppWillBeHidden(IBinder token) {
3489 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3490 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003491 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 }
3493
3494 AppWindowToken wtoken;
3495
3496 synchronized(mWindowMap) {
3497 wtoken = findAppWindowToken(token);
3498 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003499 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 -08003500 return;
3501 }
3502 wtoken.willBeHidden = true;
3503 }
3504 }
Romain Guy06882f82009-06-10 13:36:04 -07003505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3507 boolean visible, int transit, boolean performLayout) {
3508 boolean delayed = false;
3509
3510 if (wtoken.clientHidden == visible) {
3511 wtoken.clientHidden = !visible;
3512 wtoken.sendAppVisibilityToClients();
3513 }
Romain Guy06882f82009-06-10 13:36:04 -07003514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 wtoken.willBeHidden = false;
3516 if (wtoken.hidden == visible) {
3517 final int N = wtoken.allAppWindows.size();
3518 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003519 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3521 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003524
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003525 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 if (wtoken.animation == sDummyAnimation) {
3527 wtoken.animation = null;
3528 }
3529 applyAnimationLocked(wtoken, lp, transit, visible);
3530 changed = true;
3531 if (wtoken.animation != null) {
3532 delayed = runningAppAnimation = true;
3533 }
3534 }
Romain Guy06882f82009-06-10 13:36:04 -07003535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 for (int i=0; i<N; i++) {
3537 WindowState win = wtoken.allAppWindows.get(i);
3538 if (win == wtoken.startingWindow) {
3539 continue;
3540 }
3541
3542 if (win.isAnimating()) {
3543 delayed = true;
3544 }
Romain Guy06882f82009-06-10 13:36:04 -07003545
Joe Onorato8a9b2202010-02-26 18:56:32 -08003546 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 //win.dump(" ");
3548 if (visible) {
3549 if (!win.isVisibleNow()) {
3550 if (!runningAppAnimation) {
3551 applyAnimationLocked(win,
3552 WindowManagerPolicy.TRANSIT_ENTER, true);
3553 }
3554 changed = true;
3555 }
3556 } else if (win.isVisibleNow()) {
3557 if (!runningAppAnimation) {
3558 applyAnimationLocked(win,
3559 WindowManagerPolicy.TRANSIT_EXIT, false);
3560 }
3561 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3562 KeyWaiter.RETURN_NOTHING);
3563 changed = true;
3564 }
3565 }
3566
3567 wtoken.hidden = wtoken.hiddenRequested = !visible;
3568 if (!visible) {
3569 unsetAppFreezingScreenLocked(wtoken, true, true);
3570 } else {
3571 // If we are being set visible, and the starting window is
3572 // not yet displayed, then make sure it doesn't get displayed.
3573 WindowState swin = wtoken.startingWindow;
3574 if (swin != null && (swin.mDrawPending
3575 || swin.mCommitDrawPending)) {
3576 swin.mPolicyVisibility = false;
3577 swin.mPolicyVisibilityAfterAnim = false;
3578 }
3579 }
Romain Guy06882f82009-06-10 13:36:04 -07003580
Joe Onorato8a9b2202010-02-26 18:56:32 -08003581 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3583 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003584
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003585 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003587 if (performLayout) {
3588 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3589 performLayoutAndPlaceSurfacesLocked();
3590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 }
3592 }
3593
3594 if (wtoken.animation != null) {
3595 delayed = true;
3596 }
Romain Guy06882f82009-06-10 13:36:04 -07003597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 return delayed;
3599 }
3600
3601 public void setAppVisibility(IBinder token, boolean visible) {
3602 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3603 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003604 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 }
3606
3607 AppWindowToken wtoken;
3608
3609 synchronized(mWindowMap) {
3610 wtoken = findAppWindowToken(token);
3611 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003612 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 return;
3614 }
3615
3616 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003617 RuntimeException e = null;
3618 if (!HIDE_STACK_CRAWLS) {
3619 e = new RuntimeException();
3620 e.fillInStackTrace();
3621 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003622 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 + "): mNextAppTransition=" + mNextAppTransition
3624 + " hidden=" + wtoken.hidden
3625 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3626 }
Romain Guy06882f82009-06-10 13:36:04 -07003627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 // If we are preparing an app transition, then delay changing
3629 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003630 if (!mDisplayFrozen && mPolicy.isScreenOn()
3631 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 // Already in requested state, don't do anything more.
3633 if (wtoken.hiddenRequested != visible) {
3634 return;
3635 }
3636 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003637
Joe Onorato8a9b2202010-02-26 18:56:32 -08003638 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 TAG, "Setting dummy animation on: " + wtoken);
3640 wtoken.setDummyAnimation();
3641 mOpeningApps.remove(wtoken);
3642 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003643 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 wtoken.inPendingTransaction = true;
3645 if (visible) {
3646 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 wtoken.startingDisplayed = false;
3648 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003649
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003650 // If the token is currently hidden (should be the
3651 // common case), then we need to set up to wait for
3652 // its windows to be ready.
3653 if (wtoken.hidden) {
3654 wtoken.allDrawn = false;
3655 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003656
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003657 if (wtoken.clientHidden) {
3658 // In the case where we are making an app visible
3659 // but holding off for a transition, we still need
3660 // to tell the client to make its windows visible so
3661 // they get drawn. Otherwise, we will wait on
3662 // performing the transition until all windows have
3663 // been drawn, they never will be, and we are sad.
3664 wtoken.clientHidden = false;
3665 wtoken.sendAppVisibilityToClients();
3666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 }
3668 } else {
3669 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003670
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003671 // If the token is currently visible (should be the
3672 // common case), then set up to wait for it to be hidden.
3673 if (!wtoken.hidden) {
3674 wtoken.waitingToHide = true;
3675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 }
3677 return;
3678 }
Romain Guy06882f82009-06-10 13:36:04 -07003679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003681 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 wtoken.updateReportedVisibilityLocked();
3683 Binder.restoreCallingIdentity(origId);
3684 }
3685 }
3686
3687 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3688 boolean unfreezeSurfaceNow, boolean force) {
3689 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003690 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 + " force=" + force);
3692 final int N = wtoken.allAppWindows.size();
3693 boolean unfrozeWindows = false;
3694 for (int i=0; i<N; i++) {
3695 WindowState w = wtoken.allAppWindows.get(i);
3696 if (w.mAppFreezing) {
3697 w.mAppFreezing = false;
3698 if (w.mSurface != null && !w.mOrientationChanging) {
3699 w.mOrientationChanging = true;
3700 }
3701 unfrozeWindows = true;
3702 }
3703 }
3704 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003705 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 wtoken.freezingScreen = false;
3707 mAppsFreezingScreen--;
3708 }
3709 if (unfreezeSurfaceNow) {
3710 if (unfrozeWindows) {
3711 mLayoutNeeded = true;
3712 performLayoutAndPlaceSurfacesLocked();
3713 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003714 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
3716 }
3717 }
Romain Guy06882f82009-06-10 13:36:04 -07003718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3720 int configChanges) {
3721 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003722 RuntimeException e = null;
3723 if (!HIDE_STACK_CRAWLS) {
3724 e = new RuntimeException();
3725 e.fillInStackTrace();
3726 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003727 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 + ": hidden=" + wtoken.hidden + " freezing="
3729 + wtoken.freezingScreen, e);
3730 }
3731 if (!wtoken.hiddenRequested) {
3732 if (!wtoken.freezingScreen) {
3733 wtoken.freezingScreen = true;
3734 mAppsFreezingScreen++;
3735 if (mAppsFreezingScreen == 1) {
3736 startFreezingDisplayLocked();
3737 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3738 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3739 5000);
3740 }
3741 }
3742 final int N = wtoken.allAppWindows.size();
3743 for (int i=0; i<N; i++) {
3744 WindowState w = wtoken.allAppWindows.get(i);
3745 w.mAppFreezing = true;
3746 }
3747 }
3748 }
Romain Guy06882f82009-06-10 13:36:04 -07003749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 public void startAppFreezingScreen(IBinder token, int configChanges) {
3751 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3752 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003753 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 }
3755
3756 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003757 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003758 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 return;
3760 }
Romain Guy06882f82009-06-10 13:36:04 -07003761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 AppWindowToken wtoken = findAppWindowToken(token);
3763 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003764 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 return;
3766 }
3767 final long origId = Binder.clearCallingIdentity();
3768 startAppFreezingScreenLocked(wtoken, configChanges);
3769 Binder.restoreCallingIdentity(origId);
3770 }
3771 }
Romain Guy06882f82009-06-10 13:36:04 -07003772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 public void stopAppFreezingScreen(IBinder token, boolean force) {
3774 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3775 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003776 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 }
3778
3779 synchronized(mWindowMap) {
3780 AppWindowToken wtoken = findAppWindowToken(token);
3781 if (wtoken == null || wtoken.appToken == null) {
3782 return;
3783 }
3784 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003785 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3787 unsetAppFreezingScreenLocked(wtoken, true, force);
3788 Binder.restoreCallingIdentity(origId);
3789 }
3790 }
Romain Guy06882f82009-06-10 13:36:04 -07003791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 public void removeAppToken(IBinder token) {
3793 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3794 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003795 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 }
3797
3798 AppWindowToken wtoken = null;
3799 AppWindowToken startingToken = null;
3800 boolean delayed = false;
3801
3802 final long origId = Binder.clearCallingIdentity();
3803 synchronized(mWindowMap) {
3804 WindowToken basewtoken = mTokenMap.remove(token);
3805 mTokenList.remove(basewtoken);
3806 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003807 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003808 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 wtoken.inPendingTransaction = false;
3810 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003811 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 if (mClosingApps.contains(wtoken)) {
3813 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003814 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003816 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 delayed = true;
3818 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003819 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 TAG, "Removing app " + wtoken + " delayed=" + delayed
3821 + " animation=" + wtoken.animation
3822 + " animating=" + wtoken.animating);
3823 if (delayed) {
3824 // set the token aside because it has an active animation to be finished
3825 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003826 } else {
3827 // Make sure there is no animation running on this token,
3828 // so any windows associated with it will be removed as
3829 // soon as their animations are complete
3830 wtoken.animation = null;
3831 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 }
3833 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003834 if (mLastEnterAnimToken == wtoken) {
3835 mLastEnterAnimToken = null;
3836 mLastEnterAnimParams = null;
3837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 wtoken.removed = true;
3839 if (wtoken.startingData != null) {
3840 startingToken = wtoken;
3841 }
3842 unsetAppFreezingScreenLocked(wtoken, true, true);
3843 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003844 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 mFocusedApp = null;
3846 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3847 mKeyWaiter.tickle();
3848 }
3849 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003850 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
Romain Guy06882f82009-06-10 13:36:04 -07003852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 if (!delayed && wtoken != null) {
3854 wtoken.updateReportedVisibilityLocked();
3855 }
3856 }
3857 Binder.restoreCallingIdentity(origId);
3858
3859 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003860 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 + startingToken + ": app token removed");
3862 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3863 mH.sendMessage(m);
3864 }
3865 }
3866
3867 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3868 final int NW = token.windows.size();
3869 for (int i=0; i<NW; i++) {
3870 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003871 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 mWindows.remove(win);
3873 int j = win.mChildWindows.size();
3874 while (j > 0) {
3875 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003876 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003877 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003878 "Tmp removing child window " + cwin);
3879 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881 }
3882 return NW > 0;
3883 }
3884
3885 void dumpAppTokensLocked() {
3886 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003887 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889 }
Romain Guy06882f82009-06-10 13:36:04 -07003890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 void dumpWindowsLocked() {
3892 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003893 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895 }
Romain Guy06882f82009-06-10 13:36:04 -07003896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 private int findWindowOffsetLocked(int tokenPos) {
3898 final int NW = mWindows.size();
3899
3900 if (tokenPos >= mAppTokens.size()) {
3901 int i = NW;
3902 while (i > 0) {
3903 i--;
3904 WindowState win = (WindowState)mWindows.get(i);
3905 if (win.getAppToken() != null) {
3906 return i+1;
3907 }
3908 }
3909 }
3910
3911 while (tokenPos > 0) {
3912 // Find the first app token below the new position that has
3913 // a window displayed.
3914 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003915 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003917 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003918 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003919 "Skipping token -- currently sending to bottom");
3920 tokenPos--;
3921 continue;
3922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 int i = wtoken.windows.size();
3924 while (i > 0) {
3925 i--;
3926 WindowState win = wtoken.windows.get(i);
3927 int j = win.mChildWindows.size();
3928 while (j > 0) {
3929 j--;
3930 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003931 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 for (int pos=NW-1; pos>=0; pos--) {
3933 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003934 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 "Found child win @" + (pos+1));
3936 return pos+1;
3937 }
3938 }
3939 }
3940 }
3941 for (int pos=NW-1; pos>=0; pos--) {
3942 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003943 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 return pos+1;
3945 }
3946 }
3947 }
3948 tokenPos--;
3949 }
3950
3951 return 0;
3952 }
3953
3954 private final int reAddWindowLocked(int index, WindowState win) {
3955 final int NCW = win.mChildWindows.size();
3956 boolean added = false;
3957 for (int j=0; j<NCW; j++) {
3958 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3959 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003960 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003961 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 mWindows.add(index, win);
3963 index++;
3964 added = true;
3965 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003966 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003967 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 mWindows.add(index, cwin);
3969 index++;
3970 }
3971 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003972 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003973 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 mWindows.add(index, win);
3975 index++;
3976 }
3977 return index;
3978 }
Romain Guy06882f82009-06-10 13:36:04 -07003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3981 final int NW = token.windows.size();
3982 for (int i=0; i<NW; i++) {
3983 index = reAddWindowLocked(index, token.windows.get(i));
3984 }
3985 return index;
3986 }
3987
3988 public void moveAppToken(int index, IBinder token) {
3989 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3990 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003991 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 }
3993
3994 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003995 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 if (DEBUG_REORDER) dumpAppTokensLocked();
3997 final AppWindowToken wtoken = findAppWindowToken(token);
3998 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003999 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 + token + " (" + wtoken + ")");
4001 return;
4002 }
4003 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004004 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004008 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 if (DEBUG_REORDER) dumpWindowsLocked();
4010 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004011 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 if (DEBUG_REORDER) dumpWindowsLocked();
4013 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004014 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 if (DEBUG_REORDER) dumpWindowsLocked();
4016 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 mLayoutNeeded = true;
4018 performLayoutAndPlaceSurfacesLocked();
4019 }
4020 Binder.restoreCallingIdentity(origId);
4021 }
4022 }
4023
4024 private void removeAppTokensLocked(List<IBinder> tokens) {
4025 // XXX This should be done more efficiently!
4026 // (take advantage of the fact that both lists should be
4027 // ordered in the same way.)
4028 int N = tokens.size();
4029 for (int i=0; i<N; i++) {
4030 IBinder token = tokens.get(i);
4031 final AppWindowToken wtoken = findAppWindowToken(token);
4032 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004033 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 + token + " (" + wtoken + ")");
4035 i--;
4036 N--;
4037 }
4038 }
4039 }
4040
Dianne Hackborna8f60182009-09-01 19:01:50 -07004041 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4042 boolean updateFocusAndLayout) {
4043 // First remove all of the windows from the list.
4044 tmpRemoveAppWindowsLocked(wtoken);
4045
4046 // Where to start adding?
4047 int pos = findWindowOffsetLocked(tokenPos);
4048
4049 // And now add them back at the correct place.
4050 pos = reAddAppWindowsLocked(pos, wtoken);
4051
4052 if (updateFocusAndLayout) {
4053 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4054 assignLayersLocked();
4055 }
4056 mLayoutNeeded = true;
4057 performLayoutAndPlaceSurfacesLocked();
4058 }
4059 }
4060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4062 // First remove all of the windows from the list.
4063 final int N = tokens.size();
4064 int i;
4065 for (i=0; i<N; i++) {
4066 WindowToken token = mTokenMap.get(tokens.get(i));
4067 if (token != null) {
4068 tmpRemoveAppWindowsLocked(token);
4069 }
4070 }
4071
4072 // Where to start adding?
4073 int pos = findWindowOffsetLocked(tokenPos);
4074
4075 // And now add them back at the correct place.
4076 for (i=0; i<N; i++) {
4077 WindowToken token = mTokenMap.get(tokens.get(i));
4078 if (token != null) {
4079 pos = reAddAppWindowsLocked(pos, token);
4080 }
4081 }
4082
Dianne Hackborna8f60182009-09-01 19:01:50 -07004083 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4084 assignLayersLocked();
4085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 mLayoutNeeded = true;
4087 performLayoutAndPlaceSurfacesLocked();
4088
4089 //dump();
4090 }
4091
4092 public void moveAppTokensToTop(List<IBinder> tokens) {
4093 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4094 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004095 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
4097
4098 final long origId = Binder.clearCallingIdentity();
4099 synchronized(mWindowMap) {
4100 removeAppTokensLocked(tokens);
4101 final int N = tokens.size();
4102 for (int i=0; i<N; i++) {
4103 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4104 if (wt != null) {
4105 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004106 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004107 mToTopApps.remove(wt);
4108 mToBottomApps.remove(wt);
4109 mToTopApps.add(wt);
4110 wt.sendingToBottom = false;
4111 wt.sendingToTop = true;
4112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 }
4114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004115
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004116 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004117 moveAppWindowsLocked(tokens, mAppTokens.size());
4118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 }
4120 Binder.restoreCallingIdentity(origId);
4121 }
4122
4123 public void moveAppTokensToBottom(List<IBinder> tokens) {
4124 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4125 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004126 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 }
4128
4129 final long origId = Binder.clearCallingIdentity();
4130 synchronized(mWindowMap) {
4131 removeAppTokensLocked(tokens);
4132 final int N = tokens.size();
4133 int pos = 0;
4134 for (int i=0; i<N; i++) {
4135 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4136 if (wt != null) {
4137 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004138 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004139 mToTopApps.remove(wt);
4140 mToBottomApps.remove(wt);
4141 mToBottomApps.add(i, wt);
4142 wt.sendingToTop = false;
4143 wt.sendingToBottom = true;
4144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 pos++;
4146 }
4147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004148
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004149 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004150 moveAppWindowsLocked(tokens, 0);
4151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153 Binder.restoreCallingIdentity(origId);
4154 }
4155
4156 // -------------------------------------------------------------
4157 // Misc IWindowSession methods
4158 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004161 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 != PackageManager.PERMISSION_GRANTED) {
4163 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4164 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004165 synchronized (mKeyguardTokenWatcher) {
4166 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 }
4169
4170 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004171 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 != PackageManager.PERMISSION_GRANTED) {
4173 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4174 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004175 synchronized (mKeyguardTokenWatcher) {
4176 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177
Mike Lockwood983ee092009-11-22 01:42:24 -05004178 if (!mKeyguardTokenWatcher.isAcquired()) {
4179 // If we are the last one to reenable the keyguard wait until
4180 // we have actaully finished reenabling until returning.
4181 // It is possible that reenableKeyguard() can be called before
4182 // the previous disableKeyguard() is handled, in which case
4183 // neither mKeyguardTokenWatcher.acquired() or released() would
4184 // be called. In that case mKeyguardDisabled will be false here
4185 // and we have nothing to wait for.
4186 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004188 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 } catch (InterruptedException e) {
4190 Thread.currentThread().interrupt();
4191 }
4192 }
4193 }
4194 }
4195 }
4196
4197 /**
4198 * @see android.app.KeyguardManager#exitKeyguardSecurely
4199 */
4200 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004201 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 != PackageManager.PERMISSION_GRANTED) {
4203 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4204 }
4205 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4206 public void onKeyguardExitResult(boolean success) {
4207 try {
4208 callback.onKeyguardExitResult(success);
4209 } catch (RemoteException e) {
4210 // Client has died, we don't care.
4211 }
4212 }
4213 });
4214 }
4215
4216 public boolean inKeyguardRestrictedInputMode() {
4217 return mPolicy.inKeyguardRestrictedKeyInputMode();
4218 }
Romain Guy06882f82009-06-10 13:36:04 -07004219
Dianne Hackbornffa42482009-09-23 22:20:11 -07004220 public void closeSystemDialogs(String reason) {
4221 synchronized(mWindowMap) {
4222 for (int i=mWindows.size()-1; i>=0; i--) {
4223 WindowState w = (WindowState)mWindows.get(i);
4224 if (w.mSurface != null) {
4225 try {
4226 w.mClient.closeSystemDialogs(reason);
4227 } catch (RemoteException e) {
4228 }
4229 }
4230 }
4231 }
4232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 static float fixScale(float scale) {
4235 if (scale < 0) scale = 0;
4236 else if (scale > 20) scale = 20;
4237 return Math.abs(scale);
4238 }
Romain Guy06882f82009-06-10 13:36:04 -07004239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 public void setAnimationScale(int which, float scale) {
4241 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4242 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004243 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245
4246 if (scale < 0) scale = 0;
4247 else if (scale > 20) scale = 20;
4248 scale = Math.abs(scale);
4249 switch (which) {
4250 case 0: mWindowAnimationScale = fixScale(scale); break;
4251 case 1: mTransitionAnimationScale = fixScale(scale); break;
4252 }
Romain Guy06882f82009-06-10 13:36:04 -07004253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 // Persist setting
4255 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4256 }
Romain Guy06882f82009-06-10 13:36:04 -07004257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 public void setAnimationScales(float[] scales) {
4259 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4260 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004261 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 }
4263
4264 if (scales != null) {
4265 if (scales.length >= 1) {
4266 mWindowAnimationScale = fixScale(scales[0]);
4267 }
4268 if (scales.length >= 2) {
4269 mTransitionAnimationScale = fixScale(scales[1]);
4270 }
4271 }
Romain Guy06882f82009-06-10 13:36:04 -07004272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 // Persist setting
4274 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4275 }
Romain Guy06882f82009-06-10 13:36:04 -07004276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 public float getAnimationScale(int which) {
4278 switch (which) {
4279 case 0: return mWindowAnimationScale;
4280 case 1: return mTransitionAnimationScale;
4281 }
4282 return 0;
4283 }
Romain Guy06882f82009-06-10 13:36:04 -07004284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 public float[] getAnimationScales() {
4286 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4287 }
Romain Guy06882f82009-06-10 13:36:04 -07004288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 public int getSwitchState(int sw) {
4290 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4291 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004292 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 }
4294 return KeyInputQueue.getSwitchState(sw);
4295 }
Romain Guy06882f82009-06-10 13:36:04 -07004296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 public int getSwitchStateForDevice(int devid, int sw) {
4298 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4299 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004300 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302 return KeyInputQueue.getSwitchState(devid, sw);
4303 }
Romain Guy06882f82009-06-10 13:36:04 -07004304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 public int getScancodeState(int sw) {
4306 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4307 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004308 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004310 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 }
Romain Guy06882f82009-06-10 13:36:04 -07004312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 public int getScancodeStateForDevice(int devid, int sw) {
4314 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4315 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004316 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004318 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
Romain Guy06882f82009-06-10 13:36:04 -07004320
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004321 public int getTrackballScancodeState(int sw) {
4322 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4323 "getTrackballScancodeState()")) {
4324 throw new SecurityException("Requires READ_INPUT_STATE permission");
4325 }
4326 return mQueue.getTrackballScancodeState(sw);
4327 }
4328
4329 public int getDPadScancodeState(int sw) {
4330 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4331 "getDPadScancodeState()")) {
4332 throw new SecurityException("Requires READ_INPUT_STATE permission");
4333 }
4334 return mQueue.getDPadScancodeState(sw);
4335 }
4336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 public int getKeycodeState(int sw) {
4338 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4339 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004340 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004342 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 }
Romain Guy06882f82009-06-10 13:36:04 -07004344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 public int getKeycodeStateForDevice(int devid, int sw) {
4346 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4347 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004348 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004350 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 }
Romain Guy06882f82009-06-10 13:36:04 -07004352
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004353 public int getTrackballKeycodeState(int sw) {
4354 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4355 "getTrackballKeycodeState()")) {
4356 throw new SecurityException("Requires READ_INPUT_STATE permission");
4357 }
4358 return mQueue.getTrackballKeycodeState(sw);
4359 }
4360
4361 public int getDPadKeycodeState(int sw) {
4362 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4363 "getDPadKeycodeState()")) {
4364 throw new SecurityException("Requires READ_INPUT_STATE permission");
4365 }
4366 return mQueue.getDPadKeycodeState(sw);
4367 }
4368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4370 return KeyInputQueue.hasKeys(keycodes, keyExists);
4371 }
Romain Guy06882f82009-06-10 13:36:04 -07004372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 public void enableScreenAfterBoot() {
4374 synchronized(mWindowMap) {
4375 if (mSystemBooted) {
4376 return;
4377 }
4378 mSystemBooted = true;
4379 }
Romain Guy06882f82009-06-10 13:36:04 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 performEnableScreen();
4382 }
Romain Guy06882f82009-06-10 13:36:04 -07004383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 public void enableScreenIfNeededLocked() {
4385 if (mDisplayEnabled) {
4386 return;
4387 }
4388 if (!mSystemBooted) {
4389 return;
4390 }
4391 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4392 }
Romain Guy06882f82009-06-10 13:36:04 -07004393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 public void performEnableScreen() {
4395 synchronized(mWindowMap) {
4396 if (mDisplayEnabled) {
4397 return;
4398 }
4399 if (!mSystemBooted) {
4400 return;
4401 }
Romain Guy06882f82009-06-10 13:36:04 -07004402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 // Don't enable the screen until all existing windows
4404 // have been drawn.
4405 final int N = mWindows.size();
4406 for (int i=0; i<N; i++) {
4407 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08004408 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 return;
4410 }
4411 }
Romain Guy06882f82009-06-10 13:36:04 -07004412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 mDisplayEnabled = true;
4414 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004415 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 StringWriter sw = new StringWriter();
4417 PrintWriter pw = new PrintWriter(sw);
4418 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004419 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 }
4421 try {
4422 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4423 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004424 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 Parcel data = Parcel.obtain();
4426 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4427 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4428 data, null, 0);
4429 data.recycle();
4430 }
4431 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004432 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 }
4434 }
Romain Guy06882f82009-06-10 13:36:04 -07004435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004439 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4440 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
Romain Guy06882f82009-06-10 13:36:04 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 public void setInTouchMode(boolean mode) {
4444 synchronized(mWindowMap) {
4445 mInTouchMode = mode;
4446 }
4447 }
4448
Romain Guy06882f82009-06-10 13:36:04 -07004449 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004450 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004452 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004453 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 }
4455
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004456 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 }
Romain Guy06882f82009-06-10 13:36:04 -07004458
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004459 public void setRotationUnchecked(int rotation,
4460 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004461 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 long origId = Binder.clearCallingIdentity();
4465 boolean changed;
4466 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004467 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 }
Romain Guy06882f82009-06-10 13:36:04 -07004469
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004470 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 sendNewConfiguration();
4472 }
Romain Guy06882f82009-06-10 13:36:04 -07004473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 Binder.restoreCallingIdentity(origId);
4475 }
Romain Guy06882f82009-06-10 13:36:04 -07004476
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004477 /**
4478 * Apply a new rotation to the screen, respecting the requests of
4479 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4480 * re-evaluate the desired rotation.
4481 *
4482 * Returns null if the rotation has been changed. In this case YOU
4483 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4484 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004485 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 boolean changed;
4487 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4488 rotation = mRequestedRotation;
4489 } else {
4490 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004491 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004493 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004494 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004496 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004500 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 "Rotation changed to " + rotation
4502 + " from " + mRotation
4503 + " (forceApp=" + mForcedAppOrientation
4504 + ", req=" + mRequestedRotation + ")");
4505 mRotation = rotation;
4506 mWindowsFreezingScreen = true;
4507 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4508 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4509 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004510 mWaitingForConfig = true;
4511 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004513 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 mQueue.setOrientation(rotation);
4515 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004516 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
4518 for (int i=mWindows.size()-1; i>=0; i--) {
4519 WindowState w = (WindowState)mWindows.get(i);
4520 if (w.mSurface != null) {
4521 w.mOrientationChanging = true;
4522 }
4523 }
4524 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4525 try {
4526 mRotationWatchers.get(i).onRotationChanged(rotation);
4527 } catch (RemoteException e) {
4528 }
4529 }
4530 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 return changed;
4533 }
Romain Guy06882f82009-06-10 13:36:04 -07004534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 public int getRotation() {
4536 return mRotation;
4537 }
4538
4539 public int watchRotation(IRotationWatcher watcher) {
4540 final IBinder watcherBinder = watcher.asBinder();
4541 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4542 public void binderDied() {
4543 synchronized (mWindowMap) {
4544 for (int i=0; i<mRotationWatchers.size(); i++) {
4545 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004546 IRotationWatcher removed = mRotationWatchers.remove(i);
4547 if (removed != null) {
4548 removed.asBinder().unlinkToDeath(this, 0);
4549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 i--;
4551 }
4552 }
4553 }
4554 }
4555 };
Romain Guy06882f82009-06-10 13:36:04 -07004556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 synchronized (mWindowMap) {
4558 try {
4559 watcher.asBinder().linkToDeath(dr, 0);
4560 mRotationWatchers.add(watcher);
4561 } catch (RemoteException e) {
4562 // Client died, no cleanup needed.
4563 }
Romain Guy06882f82009-06-10 13:36:04 -07004564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 return mRotation;
4566 }
4567 }
4568
4569 /**
4570 * Starts the view server on the specified port.
4571 *
4572 * @param port The port to listener to.
4573 *
4574 * @return True if the server was successfully started, false otherwise.
4575 *
4576 * @see com.android.server.ViewServer
4577 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4578 */
4579 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004580 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 return false;
4582 }
4583
4584 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4585 return false;
4586 }
4587
4588 if (port < 1024) {
4589 return false;
4590 }
4591
4592 if (mViewServer != null) {
4593 if (!mViewServer.isRunning()) {
4594 try {
4595 return mViewServer.start();
4596 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004597 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 }
4599 }
4600 return false;
4601 }
4602
4603 try {
4604 mViewServer = new ViewServer(this, port);
4605 return mViewServer.start();
4606 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004607 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 }
4609 return false;
4610 }
4611
Romain Guy06882f82009-06-10 13:36:04 -07004612 private boolean isSystemSecure() {
4613 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4614 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4615 }
4616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 /**
4618 * Stops the view server if it exists.
4619 *
4620 * @return True if the server stopped, false if it wasn't started or
4621 * couldn't be stopped.
4622 *
4623 * @see com.android.server.ViewServer
4624 */
4625 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004626 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 return false;
4628 }
4629
4630 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4631 return false;
4632 }
4633
4634 if (mViewServer != null) {
4635 return mViewServer.stop();
4636 }
4637 return false;
4638 }
4639
4640 /**
4641 * Indicates whether the view server is running.
4642 *
4643 * @return True if the server is running, false otherwise.
4644 *
4645 * @see com.android.server.ViewServer
4646 */
4647 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004648 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004649 return false;
4650 }
4651
4652 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4653 return false;
4654 }
4655
4656 return mViewServer != null && mViewServer.isRunning();
4657 }
4658
4659 /**
4660 * Lists all availble windows in the system. The listing is written in the
4661 * specified Socket's output stream with the following syntax:
4662 * windowHashCodeInHexadecimal windowName
4663 * Each line of the ouput represents a different window.
4664 *
4665 * @param client The remote client to send the listing to.
4666 * @return False if an error occured, true otherwise.
4667 */
4668 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004669 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 return false;
4671 }
4672
4673 boolean result = true;
4674
4675 Object[] windows;
4676 synchronized (mWindowMap) {
4677 windows = new Object[mWindows.size()];
4678 //noinspection unchecked
4679 windows = mWindows.toArray(windows);
4680 }
4681
4682 BufferedWriter out = null;
4683
4684 // Any uncaught exception will crash the system process
4685 try {
4686 OutputStream clientStream = client.getOutputStream();
4687 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4688
4689 final int count = windows.length;
4690 for (int i = 0; i < count; i++) {
4691 final WindowState w = (WindowState) windows[i];
4692 out.write(Integer.toHexString(System.identityHashCode(w)));
4693 out.write(' ');
4694 out.append(w.mAttrs.getTitle());
4695 out.write('\n');
4696 }
4697
4698 out.write("DONE.\n");
4699 out.flush();
4700 } catch (Exception e) {
4701 result = false;
4702 } finally {
4703 if (out != null) {
4704 try {
4705 out.close();
4706 } catch (IOException e) {
4707 result = false;
4708 }
4709 }
4710 }
4711
4712 return result;
4713 }
4714
4715 /**
4716 * Sends a command to a target window. The result of the command, if any, will be
4717 * written in the output stream of the specified socket.
4718 *
4719 * The parameters must follow this syntax:
4720 * windowHashcode extra
4721 *
4722 * Where XX is the length in characeters of the windowTitle.
4723 *
4724 * The first parameter is the target window. The window with the specified hashcode
4725 * will be the target. If no target can be found, nothing happens. The extra parameters
4726 * will be delivered to the target window and as parameters to the command itself.
4727 *
4728 * @param client The remote client to sent the result, if any, to.
4729 * @param command The command to execute.
4730 * @param parameters The command parameters.
4731 *
4732 * @return True if the command was successfully delivered, false otherwise. This does
4733 * not indicate whether the command itself was successful.
4734 */
4735 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004736 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 return false;
4738 }
4739
4740 boolean success = true;
4741 Parcel data = null;
4742 Parcel reply = null;
4743
4744 // Any uncaught exception will crash the system process
4745 try {
4746 // Find the hashcode of the window
4747 int index = parameters.indexOf(' ');
4748 if (index == -1) {
4749 index = parameters.length();
4750 }
4751 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004752 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004753
4754 // Extract the command's parameter after the window description
4755 if (index < parameters.length()) {
4756 parameters = parameters.substring(index + 1);
4757 } else {
4758 parameters = "";
4759 }
4760
4761 final WindowManagerService.WindowState window = findWindow(hashCode);
4762 if (window == null) {
4763 return false;
4764 }
4765
4766 data = Parcel.obtain();
4767 data.writeInterfaceToken("android.view.IWindow");
4768 data.writeString(command);
4769 data.writeString(parameters);
4770 data.writeInt(1);
4771 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4772
4773 reply = Parcel.obtain();
4774
4775 final IBinder binder = window.mClient.asBinder();
4776 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4777 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4778
4779 reply.readException();
4780
4781 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004782 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 success = false;
4784 } finally {
4785 if (data != null) {
4786 data.recycle();
4787 }
4788 if (reply != null) {
4789 reply.recycle();
4790 }
4791 }
4792
4793 return success;
4794 }
4795
4796 private WindowState findWindow(int hashCode) {
4797 if (hashCode == -1) {
4798 return getFocusedWindow();
4799 }
4800
4801 synchronized (mWindowMap) {
4802 final ArrayList windows = mWindows;
4803 final int count = windows.size();
4804
4805 for (int i = 0; i < count; i++) {
4806 WindowState w = (WindowState) windows.get(i);
4807 if (System.identityHashCode(w) == hashCode) {
4808 return w;
4809 }
4810 }
4811 }
4812
4813 return null;
4814 }
4815
4816 /*
4817 * Instruct the Activity Manager to fetch the current configuration and broadcast
4818 * that to config-changed listeners if appropriate.
4819 */
4820 void sendNewConfiguration() {
4821 try {
4822 mActivityManager.updateConfiguration(null);
4823 } catch (RemoteException e) {
4824 }
4825 }
Romain Guy06882f82009-06-10 13:36:04 -07004826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004827 public Configuration computeNewConfiguration() {
4828 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004829 return computeNewConfigurationLocked();
4830 }
4831 }
Romain Guy06882f82009-06-10 13:36:04 -07004832
Dianne Hackbornc485a602009-03-24 22:39:49 -07004833 Configuration computeNewConfigurationLocked() {
4834 Configuration config = new Configuration();
4835 if (!computeNewConfigurationLocked(config)) {
4836 return null;
4837 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004838 return config;
4839 }
Romain Guy06882f82009-06-10 13:36:04 -07004840
Dianne Hackbornc485a602009-03-24 22:39:49 -07004841 boolean computeNewConfigurationLocked(Configuration config) {
4842 if (mDisplay == null) {
4843 return false;
4844 }
4845 mQueue.getInputConfiguration(config);
4846 final int dw = mDisplay.getWidth();
4847 final int dh = mDisplay.getHeight();
4848 int orientation = Configuration.ORIENTATION_SQUARE;
4849 if (dw < dh) {
4850 orientation = Configuration.ORIENTATION_PORTRAIT;
4851 } else if (dw > dh) {
4852 orientation = Configuration.ORIENTATION_LANDSCAPE;
4853 }
4854 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004855
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004856 DisplayMetrics dm = new DisplayMetrics();
4857 mDisplay.getMetrics(dm);
4858 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4859
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004860 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004861 // Note we only do this once because at this point we don't
4862 // expect the screen to change in this way at runtime, and want
4863 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004864 int longSize = dw;
4865 int shortSize = dh;
4866 if (longSize < shortSize) {
4867 int tmp = longSize;
4868 longSize = shortSize;
4869 shortSize = tmp;
4870 }
4871 longSize = (int)(longSize/dm.density);
4872 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004873
Dianne Hackborn723738c2009-06-25 19:48:04 -07004874 // These semi-magic numbers define our compatibility modes for
4875 // applications with different screens. Don't change unless you
4876 // make sure to test lots and lots of apps!
4877 if (longSize < 470) {
4878 // This is shorter than an HVGA normal density screen (which
4879 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004880 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4881 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004882 } else {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004883 // Is this a large screen?
4884 if (longSize > 640 && shortSize >= 480) {
4885 // VGA or larger screens at medium density are the point
4886 // at which we consider it to be a large screen.
4887 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4888 } else {
4889 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004890
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004891 // If this screen is wider than normal HVGA, or taller
4892 // than FWVGA, then for old apps we want to run in size
4893 // compatibility mode.
4894 if (shortSize > 321 || longSize > 570) {
4895 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4896 }
4897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004898
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004899 // Is this a long screen?
4900 if (((longSize*3)/5) >= (shortSize-1)) {
4901 // Anything wider than WVGA (5:3) is considering to be long.
4902 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4903 } else {
4904 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4905 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004906 }
4907 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004908 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004909
Dianne Hackbornc485a602009-03-24 22:39:49 -07004910 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4911 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4912 mPolicy.adjustConfigurationLw(config);
4913 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 }
Romain Guy06882f82009-06-10 13:36:04 -07004915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 // -------------------------------------------------------------
4917 // Input Events and Focus Management
4918 // -------------------------------------------------------------
4919
4920 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004921 long curTime = SystemClock.uptimeMillis();
4922
Michael Chane10de972009-05-18 11:24:50 -07004923 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004924 if (mLastTouchEventType == eventType &&
4925 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4926 return;
4927 }
4928 mLastUserActivityCallTime = curTime;
4929 mLastTouchEventType = eventType;
4930 }
4931
4932 if (targetWin == null
4933 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4934 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 }
4936 }
4937
4938 // tells if it's a cheek event or not -- this function is stateful
4939 private static final int EVENT_NONE = 0;
4940 private static final int EVENT_UNKNOWN = 0;
4941 private static final int EVENT_CHEEK = 0;
4942 private static final int EVENT_IGNORE_DURATION = 300; // ms
4943 private static final float CHEEK_THRESHOLD = 0.6f;
4944 private int mEventState = EVENT_NONE;
4945 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 private int eventType(MotionEvent ev) {
4948 float size = ev.getSize();
4949 switch (ev.getAction()) {
4950 case MotionEvent.ACTION_DOWN:
4951 mEventSize = size;
4952 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4953 case MotionEvent.ACTION_UP:
4954 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004955 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 case MotionEvent.ACTION_MOVE:
4957 final int N = ev.getHistorySize();
4958 if (size > mEventSize) mEventSize = size;
4959 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4960 for (int i=0; i<N; i++) {
4961 size = ev.getHistoricalSize(i);
4962 if (size > mEventSize) mEventSize = size;
4963 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4964 }
4965 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4966 return TOUCH_EVENT;
4967 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004968 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 }
4970 default:
4971 // not good
4972 return OTHER_EVENT;
4973 }
4974 }
4975
4976 /**
4977 * @return Returns true if event was dispatched, false if it was dropped for any reason
4978 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07004979 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004980 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 "dispatchPointer " + ev);
4982
Michael Chan53071d62009-05-13 17:29:48 -07004983 if (MEASURE_LATENCY) {
4984 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
4985 }
4986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004987 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07004988 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004989
Michael Chan53071d62009-05-13 17:29:48 -07004990 if (MEASURE_LATENCY) {
4991 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
4992 }
4993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07004995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 if (action == MotionEvent.ACTION_UP) {
4997 // let go of our target
4998 mKeyWaiter.mMotionTarget = null;
4999 mPowerManager.logPointerUpEvent();
5000 } else if (action == MotionEvent.ACTION_DOWN) {
5001 mPowerManager.logPointerDownEvent();
5002 }
5003
5004 if (targetObj == null) {
5005 // In this case we are either dropping the event, or have received
5006 // a move or up without a down. It is common to receive move
5007 // events in such a way, since this means the user is moving the
5008 // pointer without actually pressing down. All other cases should
5009 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005010 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005011 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005013 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005014 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 if (qev != null) {
5017 mQueue.recycleEvent(qev);
5018 }
5019 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005020 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 }
5022 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005023 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005024 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 if (qev != null) {
5027 mQueue.recycleEvent(qev);
5028 }
5029 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005030 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 }
Romain Guy06882f82009-06-10 13:36:04 -07005032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005036 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005037
Joe Onorato8a9b2202010-02-26 18:56:32 -08005038 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039
5040 if (uid != 0 && uid != target.mSession.mUid) {
5041 if (mContext.checkPermission(
5042 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5043 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005044 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 + pid + " uid " + uid + " to window " + target
5046 + " owned by uid " + target.mSession.mUid);
5047 if (qev != null) {
5048 mQueue.recycleEvent(qev);
5049 }
5050 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005051 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 }
5053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005054
Michael Chan53071d62009-05-13 17:29:48 -07005055 if (MEASURE_LATENCY) {
5056 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5057 }
5058
Romain Guy06882f82009-06-10 13:36:04 -07005059 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5061 //target wants to ignore fat touch events
5062 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5063 //explicit flag to return without processing event further
5064 boolean returnFlag = false;
5065 if((action == MotionEvent.ACTION_DOWN)) {
5066 mFatTouch = false;
5067 if(cheekPress) {
5068 mFatTouch = true;
5069 returnFlag = true;
5070 }
5071 } else {
5072 if(action == MotionEvent.ACTION_UP) {
5073 if(mFatTouch) {
5074 //earlier even was invalid doesnt matter if current up is cheekpress or not
5075 mFatTouch = false;
5076 returnFlag = true;
5077 } else if(cheekPress) {
5078 //cancel the earlier event
5079 ev.setAction(MotionEvent.ACTION_CANCEL);
5080 action = MotionEvent.ACTION_CANCEL;
5081 }
5082 } else if(action == MotionEvent.ACTION_MOVE) {
5083 if(mFatTouch) {
5084 //two cases here
5085 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005086 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 returnFlag = true;
5088 } else if(cheekPress) {
5089 //valid down followed by invalid moves
5090 //an invalid move have to cancel earlier action
5091 ev.setAction(MotionEvent.ACTION_CANCEL);
5092 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005093 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 //note that the subsequent invalid moves will not get here
5095 mFatTouch = true;
5096 }
5097 }
5098 } //else if action
5099 if(returnFlag) {
5100 //recycle que, ev
5101 if (qev != null) {
5102 mQueue.recycleEvent(qev);
5103 }
5104 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005105 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 }
5107 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005108
Michael Chan9f028e62009-08-04 17:37:46 -07005109 // Enable this for testing the "right" value
5110 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005111 int max_events_per_sec = 35;
5112 try {
5113 max_events_per_sec = Integer.parseInt(SystemProperties
5114 .get("windowsmgr.max_events_per_sec"));
5115 if (max_events_per_sec < 1) {
5116 max_events_per_sec = 35;
5117 }
5118 } catch (NumberFormatException e) {
5119 }
5120 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5121 }
5122
5123 /*
5124 * Throttle events to minimize CPU usage when there's a flood of events
5125 * e.g. constant contact with the screen
5126 */
5127 if (action == MotionEvent.ACTION_MOVE) {
5128 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5129 long now = SystemClock.uptimeMillis();
5130 if (now < nextEventTime) {
5131 try {
5132 Thread.sleep(nextEventTime - now);
5133 } catch (InterruptedException e) {
5134 }
5135 mLastTouchEventTime = nextEventTime;
5136 } else {
5137 mLastTouchEventTime = now;
5138 }
5139 }
5140
Michael Chan53071d62009-05-13 17:29:48 -07005141 if (MEASURE_LATENCY) {
5142 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5143 }
5144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005146 if (!target.isVisibleLw()) {
5147 // During this motion dispatch, the target window has become
5148 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005149 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005150 if (qev != null) {
5151 mQueue.recycleEvent(qev);
5152 }
5153 ev.recycle();
5154 return INJECT_SUCCEEDED;
5155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5158 mKeyWaiter.bindTargetWindowLocked(target,
5159 KeyWaiter.RETURN_PENDING_POINTER, qev);
5160 ev = null;
5161 } else {
5162 if (action == MotionEvent.ACTION_DOWN) {
5163 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5164 if (out != null) {
5165 MotionEvent oev = MotionEvent.obtain(ev);
5166 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5167 do {
5168 final Rect frame = out.mFrame;
5169 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5170 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005171 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005173 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 }
5175 oev.offsetLocation((float)frame.left, (float)frame.top);
5176 out = out.mNextOutsideTouch;
5177 } while (out != null);
5178 mKeyWaiter.mOutsideTouchTargets = null;
5179 }
5180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005181
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005182 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005183
Dianne Hackborn6adba242009-11-10 11:10:09 -08005184 final Rect frame = target.mFrame;
5185 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5186 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 }
5188 }
Romain Guy06882f82009-06-10 13:36:04 -07005189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 // finally offset the event to the target's coordinate system and
5191 // dispatch the event.
5192 try {
5193 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005194 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005196
Michael Chan53071d62009-05-13 17:29:48 -07005197 if (MEASURE_LATENCY) {
5198 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5199 }
5200
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005201 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005202
5203 if (MEASURE_LATENCY) {
5204 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5205 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005206 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005208 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 mKeyWaiter.mMotionTarget = null;
5210 try {
5211 removeWindow(target.mSession, target.mClient);
5212 } catch (java.util.NoSuchElementException ex) {
5213 // This will happen if the window has already been
5214 // removed.
5215 }
5216 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005217 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 }
Romain Guy06882f82009-06-10 13:36:04 -07005219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 /**
5221 * @return Returns true if event was dispatched, false if it was dropped for any reason
5222 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005223 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005224 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005228 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005230 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 if (qev != null) {
5232 mQueue.recycleEvent(qev);
5233 }
5234 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005235 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5238 if (qev != null) {
5239 mQueue.recycleEvent(qev);
5240 }
5241 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005242 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 }
Romain Guy06882f82009-06-10 13:36:04 -07005244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 if (uid != 0 && uid != focus.mSession.mUid) {
5248 if (mContext.checkPermission(
5249 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5250 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005251 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 + pid + " uid " + uid + " to window " + focus
5253 + " owned by uid " + focus.mSession.mUid);
5254 if (qev != null) {
5255 mQueue.recycleEvent(qev);
5256 }
5257 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005258 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 }
5260 }
Romain Guy06882f82009-06-10 13:36:04 -07005261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 synchronized(mWindowMap) {
5265 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5266 mKeyWaiter.bindTargetWindowLocked(focus,
5267 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5268 // We don't deliver movement events to the client, we hold
5269 // them and wait for them to call back.
5270 ev = null;
5271 } else {
5272 mKeyWaiter.bindTargetWindowLocked(focus);
5273 }
5274 }
Romain Guy06882f82009-06-10 13:36:04 -07005275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005277 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005278 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005280 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 try {
5282 removeWindow(focus.mSession, focus.mClient);
5283 } catch (java.util.NoSuchElementException ex) {
5284 // This will happen if the window has already been
5285 // removed.
5286 }
5287 }
Romain Guy06882f82009-06-10 13:36:04 -07005288
Dianne Hackborncfaef692009-06-15 14:24:44 -07005289 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 }
Romain Guy06882f82009-06-10 13:36:04 -07005291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 /**
5293 * @return Returns true if event was dispatched, false if it was dropped for any reason
5294 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005295 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005296 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297
5298 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005299 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005301 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005302 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 }
5304 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005305 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 }
Romain Guy06882f82009-06-10 13:36:04 -07005307
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005308 // Okay we have finished waiting for the last event to be processed.
5309 // First off, if this is a repeat event, check to see if there is
5310 // a corresponding up event in the queue. If there is, we will
5311 // just drop the repeat, because it makes no sense to repeat after
5312 // the user has released a key. (This is especially important for
5313 // long presses.)
5314 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5315 return INJECT_SUCCEEDED;
5316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005319
Joe Onorato8a9b2202010-02-26 18:56:32 -08005320 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 TAG, "Dispatching to " + focus + ": " + event);
5322
5323 if (uid != 0 && uid != focus.mSession.mUid) {
5324 if (mContext.checkPermission(
5325 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5326 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005327 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 + pid + " uid " + uid + " to window " + focus
5329 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005330 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 }
5332 }
Romain Guy06882f82009-06-10 13:36:04 -07005333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 synchronized(mWindowMap) {
5335 mKeyWaiter.bindTargetWindowLocked(focus);
5336 }
5337
5338 // NOSHIP extra state logging
5339 mKeyWaiter.recordDispatchState(event, focus);
5340 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 try {
5343 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005344 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 + " to " + focus);
5346 }
5347 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005348 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005350 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 try {
5352 removeWindow(focus.mSession, focus.mClient);
5353 } catch (java.util.NoSuchElementException ex) {
5354 // This will happen if the window has already been
5355 // removed.
5356 }
5357 }
Romain Guy06882f82009-06-10 13:36:04 -07005358
Dianne Hackborncfaef692009-06-15 14:24:44 -07005359 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 }
Romain Guy06882f82009-06-10 13:36:04 -07005361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 public void pauseKeyDispatching(IBinder _token) {
5363 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5364 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005365 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 }
5367
5368 synchronized (mWindowMap) {
5369 WindowToken token = mTokenMap.get(_token);
5370 if (token != null) {
5371 mKeyWaiter.pauseDispatchingLocked(token);
5372 }
5373 }
5374 }
5375
5376 public void resumeKeyDispatching(IBinder _token) {
5377 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5378 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005379 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 }
5381
5382 synchronized (mWindowMap) {
5383 WindowToken token = mTokenMap.get(_token);
5384 if (token != null) {
5385 mKeyWaiter.resumeDispatchingLocked(token);
5386 }
5387 }
5388 }
5389
5390 public void setEventDispatching(boolean enabled) {
5391 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5392 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005393 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 }
5395
5396 synchronized (mWindowMap) {
5397 mKeyWaiter.setEventDispatchingLocked(enabled);
5398 }
5399 }
Romain Guy06882f82009-06-10 13:36:04 -07005400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 /**
5402 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005403 *
5404 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 * {@link SystemClock#uptimeMillis()} as the timebase.)
5406 * @param sync If true, wait for the event to be completed before returning to the caller.
5407 * @return Returns true if event was dispatched, false if it was dropped for any reason
5408 */
5409 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5410 long downTime = ev.getDownTime();
5411 long eventTime = ev.getEventTime();
5412
5413 int action = ev.getAction();
5414 int code = ev.getKeyCode();
5415 int repeatCount = ev.getRepeatCount();
5416 int metaState = ev.getMetaState();
5417 int deviceId = ev.getDeviceId();
5418 int scancode = ev.getScanCode();
5419
5420 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5421 if (downTime == 0) downTime = eventTime;
5422
5423 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005424 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005426 final int pid = Binder.getCallingPid();
5427 final int uid = Binder.getCallingUid();
5428 final long ident = Binder.clearCallingIdentity();
5429 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005431 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005433 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005434 switch (result) {
5435 case INJECT_NO_PERMISSION:
5436 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305437 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005438 case INJECT_SUCCEEDED:
5439 return true;
5440 }
5441 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 }
5443
5444 /**
5445 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005446 *
5447 * @param ev A motion event describing the pointer (touch) action. (As noted in
5448 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * {@link SystemClock#uptimeMillis()} as the timebase.)
5450 * @param sync If true, wait for the event to be completed before returning to the caller.
5451 * @return Returns true if event was dispatched, false if it was dropped for any reason
5452 */
5453 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005454 final int pid = Binder.getCallingPid();
5455 final int uid = Binder.getCallingUid();
5456 final long ident = Binder.clearCallingIdentity();
5457 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005459 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005461 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005462 switch (result) {
5463 case INJECT_NO_PERMISSION:
5464 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305465 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005466 case INJECT_SUCCEEDED:
5467 return true;
5468 }
5469 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 }
Romain Guy06882f82009-06-10 13:36:04 -07005471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 /**
5473 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005474 *
5475 * @param ev A motion event describing the trackball action. (As noted in
5476 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 * {@link SystemClock#uptimeMillis()} as the timebase.)
5478 * @param sync If true, wait for the event to be completed before returning to the caller.
5479 * @return Returns true if event was dispatched, false if it was dropped for any reason
5480 */
5481 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005482 final int pid = Binder.getCallingPid();
5483 final int uid = Binder.getCallingUid();
5484 final long ident = Binder.clearCallingIdentity();
5485 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005487 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005489 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005490 switch (result) {
5491 case INJECT_NO_PERMISSION:
5492 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305493 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005494 case INJECT_SUCCEEDED:
5495 return true;
5496 }
5497 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
Romain Guy06882f82009-06-10 13:36:04 -07005499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 private WindowState getFocusedWindow() {
5501 synchronized (mWindowMap) {
5502 return getFocusedWindowLocked();
5503 }
5504 }
5505
5506 private WindowState getFocusedWindowLocked() {
5507 return mCurrentFocus;
5508 }
Romain Guy06882f82009-06-10 13:36:04 -07005509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 /**
5511 * This class holds the state for dispatching key events. This state
5512 * is protected by the KeyWaiter instance, NOT by the window lock. You
5513 * can be holding the main window lock while acquire the KeyWaiter lock,
5514 * but not the other way around.
5515 */
5516 final class KeyWaiter {
5517 // NOSHIP debugging
5518 public class DispatchState {
5519 private KeyEvent event;
5520 private WindowState focus;
5521 private long time;
5522 private WindowState lastWin;
5523 private IBinder lastBinder;
5524 private boolean finished;
5525 private boolean gotFirstWindow;
5526 private boolean eventDispatching;
5527 private long timeToSwitch;
5528 private boolean wasFrozen;
5529 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005530 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5533 focus = theFocus;
5534 event = theEvent;
5535 time = System.currentTimeMillis();
5536 // snapshot KeyWaiter state
5537 lastWin = mLastWin;
5538 lastBinder = mLastBinder;
5539 finished = mFinished;
5540 gotFirstWindow = mGotFirstWindow;
5541 eventDispatching = mEventDispatching;
5542 timeToSwitch = mTimeToSwitch;
5543 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005544 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 // cache the paused state at ctor time as well
5546 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 focusPaused = false;
5548 } else {
5549 focusPaused = theFocus.mToken.paused;
5550 }
5551 }
Romain Guy06882f82009-06-10 13:36:04 -07005552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 public String toString() {
5554 return "{{" + event + " to " + focus + " @ " + time
5555 + " lw=" + lastWin + " lb=" + lastBinder
5556 + " fin=" + finished + " gfw=" + gotFirstWindow
5557 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005558 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005559 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 }
5561 };
5562 private DispatchState mDispatchState = null;
5563 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5564 mDispatchState = new DispatchState(theEvent, theFocus);
5565 }
5566 // END NOSHIP
5567
5568 public static final int RETURN_NOTHING = 0;
5569 public static final int RETURN_PENDING_POINTER = 1;
5570 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 final Object SKIP_TARGET_TOKEN = new Object();
5573 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 private WindowState mLastWin = null;
5576 private IBinder mLastBinder = null;
5577 private boolean mFinished = true;
5578 private boolean mGotFirstWindow = false;
5579 private boolean mEventDispatching = true;
5580 private long mTimeToSwitch = 0;
5581 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 // Target of Motion events
5584 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005586 // Windows above the target who would like to receive an "outside"
5587 // touch event for any down events outside of them.
5588 WindowState mOutsideTouchTargets;
5589
5590 /**
5591 * Wait for the last event dispatch to complete, then find the next
5592 * target that should receive the given event and wait for that one
5593 * to be ready to receive it.
5594 */
5595 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5596 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005597 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 long startTime = SystemClock.uptimeMillis();
5599 long keyDispatchingTimeout = 5 * 1000;
5600 long waitedFor = 0;
5601
5602 while (true) {
5603 // Figure out which window we care about. It is either the
5604 // last window we are waiting to have process the event or,
5605 // if none, then the next window we think the event should go
5606 // to. Note: we retrieve mLastWin outside of the lock, so
5607 // it may change before we lock. Thus we must check it again.
5608 WindowState targetWin = mLastWin;
5609 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005610 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 TAG, "waitForLastKey: mFinished=" + mFinished +
5612 ", mLastWin=" + mLastWin);
5613 if (targetIsNew) {
5614 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005615 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 if (target == SKIP_TARGET_TOKEN) {
5617 // The user has pressed a special key, and we are
5618 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005619 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 + " " + nextMotion);
5621 return null;
5622 }
5623 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005624 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 + " " + nextMotion);
5626 return target;
5627 }
5628 targetWin = (WindowState)target;
5629 }
Romain Guy06882f82009-06-10 13:36:04 -07005630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 // Now: is it okay to send the next event to this window?
5634 synchronized (this) {
5635 // First: did we come here based on the last window not
5636 // being null, but it changed by the time we got here?
5637 // If so, try again.
5638 if (!targetIsNew && mLastWin == null) {
5639 continue;
5640 }
Romain Guy06882f82009-06-10 13:36:04 -07005641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 // We never dispatch events if not finished with the
5643 // last one, or the display is frozen.
5644 if (mFinished && !mDisplayFrozen) {
5645 // If event dispatching is disabled, then we
5646 // just consume the events.
5647 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005648 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 "Skipping event; dispatching disabled: "
5650 + nextKey + " " + nextMotion);
5651 return null;
5652 }
5653 if (targetWin != null) {
5654 // If this is a new target, and that target is not
5655 // paused or unresponsive, then all looks good to
5656 // handle the event.
5657 if (targetIsNew && !targetWin.mToken.paused) {
5658 return targetWin;
5659 }
Romain Guy06882f82009-06-10 13:36:04 -07005660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 // If we didn't find a target window, and there is no
5662 // focused app window, then just eat the events.
5663 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005664 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 "Skipping event; no focused app: "
5666 + nextKey + " " + nextMotion);
5667 return null;
5668 }
5669 }
Romain Guy06882f82009-06-10 13:36:04 -07005670
Joe Onorato8a9b2202010-02-26 18:56:32 -08005671 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 TAG, "Waiting for last key in " + mLastBinder
5673 + " target=" + targetWin
5674 + " mFinished=" + mFinished
5675 + " mDisplayFrozen=" + mDisplayFrozen
5676 + " targetIsNew=" + targetIsNew
5677 + " paused="
5678 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005679 + " mFocusedApp=" + mFocusedApp
5680 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 targetApp = targetWin != null
5683 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 long curTimeout = keyDispatchingTimeout;
5686 if (mTimeToSwitch != 0) {
5687 long now = SystemClock.uptimeMillis();
5688 if (mTimeToSwitch <= now) {
5689 // If an app switch key has been pressed, and we have
5690 // waited too long for the current app to finish
5691 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005692 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 continue;
5694 }
5695 long switchTimeout = mTimeToSwitch - now;
5696 if (curTimeout > switchTimeout) {
5697 curTimeout = switchTimeout;
5698 }
5699 }
Romain Guy06882f82009-06-10 13:36:04 -07005700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 try {
5702 // after that continue
5703 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005704 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 TAG, "Waiting for key dispatch: " + curTimeout);
5706 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005707 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005709 + startTime + " switchTime=" + mTimeToSwitch
5710 + " target=" + targetWin + " mLW=" + mLastWin
5711 + " mLB=" + mLastBinder + " fin=" + mFinished
5712 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 } catch (InterruptedException e) {
5714 }
5715 }
5716
5717 // If we were frozen during configuration change, restart the
5718 // timeout checks from now; otherwise look at whether we timed
5719 // out before awakening.
5720 if (mWasFrozen) {
5721 waitedFor = 0;
5722 mWasFrozen = false;
5723 } else {
5724 waitedFor = SystemClock.uptimeMillis() - startTime;
5725 }
5726
5727 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5728 IApplicationToken at = null;
5729 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005730 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005731 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005732 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005734 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5735 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005736 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 // END NOSHIP
5738 //dump();
5739 if (targetWin != null) {
5740 at = targetWin.getAppToken();
5741 } else if (targetApp != null) {
5742 at = targetApp.appToken;
5743 }
5744 }
5745
5746 boolean abort = true;
5747 if (at != null) {
5748 try {
5749 long timeout = at.getKeyDispatchingTimeout();
5750 if (timeout > waitedFor) {
5751 // we did not wait the proper amount of time for this application.
5752 // set the timeout to be the real timeout and wait again.
5753 keyDispatchingTimeout = timeout - waitedFor;
5754 continue;
5755 } else {
5756 abort = at.keyDispatchingTimedOut();
5757 }
5758 } catch (RemoteException ex) {
5759 }
5760 }
5761
5762 synchronized (this) {
5763 if (abort && (mLastWin == targetWin || targetWin == null)) {
5764 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005765 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005766 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 "Window " + mLastWin +
5768 " timed out on key input");
5769 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005770 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771 mLastWin.mToken.paused = false;
5772 }
5773 }
5774 if (mMotionTarget == targetWin) {
5775 mMotionTarget = null;
5776 }
5777 mLastWin = null;
5778 mLastBinder = null;
5779 if (failIfTimeout || targetWin == null) {
5780 return null;
5781 }
5782 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005783 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 startTime = SystemClock.uptimeMillis();
5785 }
5786 }
5787 }
5788 }
5789 }
Romain Guy06882f82009-06-10 13:36:04 -07005790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005792 MotionEvent nextMotion, boolean isPointerEvent,
5793 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 if (nextKey != null) {
5797 // Find the target window for a normal key event.
5798 final int keycode = nextKey.getKeyCode();
5799 final int repeatCount = nextKey.getRepeatCount();
5800 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5801 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005804 if (callingUid == 0 ||
5805 mContext.checkPermission(
5806 android.Manifest.permission.INJECT_EVENTS,
5807 callingPid, callingUid)
5808 == PackageManager.PERMISSION_GRANTED) {
5809 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005810 nextKey.getMetaState(), down, repeatCount,
5811 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005812 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005813 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 + nextKey);
5815 return SKIP_TARGET_TOKEN;
5816 }
Romain Guy06882f82009-06-10 13:36:04 -07005817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 WindowState focus = null;
5821 synchronized(mWindowMap) {
5822 focus = getFocusedWindowLocked();
5823 }
Romain Guy06882f82009-06-10 13:36:04 -07005824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005825 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005826
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005827 if (callingUid == 0 ||
5828 (focus != null && callingUid == focus.mSession.mUid) ||
5829 mContext.checkPermission(
5830 android.Manifest.permission.INJECT_EVENTS,
5831 callingPid, callingUid)
5832 == PackageManager.PERMISSION_GRANTED) {
5833 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005834 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005835 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005836 return CONSUMED_EVENT_TOKEN;
5837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 }
Romain Guy06882f82009-06-10 13:36:04 -07005839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 } else if (!isPointerEvent) {
5843 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5844 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005845 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 + nextMotion);
5847 return SKIP_TARGET_TOKEN;
5848 }
Romain Guy06882f82009-06-10 13:36:04 -07005849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 WindowState focus = null;
5851 synchronized(mWindowMap) {
5852 focus = getFocusedWindowLocked();
5853 }
Romain Guy06882f82009-06-10 13:36:04 -07005854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5856 return focus;
5857 }
Romain Guy06882f82009-06-10 13:36:04 -07005858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 if (nextMotion == null) {
5860 return SKIP_TARGET_TOKEN;
5861 }
Romain Guy06882f82009-06-10 13:36:04 -07005862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5864 KeyEvent.KEYCODE_UNKNOWN);
5865 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005866 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 + nextMotion);
5868 return SKIP_TARGET_TOKEN;
5869 }
Romain Guy06882f82009-06-10 13:36:04 -07005870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 // Find the target window for a pointer event.
5872 int action = nextMotion.getAction();
5873 final float xf = nextMotion.getX();
5874 final float yf = nextMotion.getY();
5875 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 final boolean screenWasOff = qev != null
5878 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 synchronized(mWindowMap) {
5883 synchronized (this) {
5884 if (action == MotionEvent.ACTION_DOWN) {
5885 if (mMotionTarget != null) {
5886 // this is weird, we got a pen down, but we thought it was
5887 // already down!
5888 // XXX: We should probably send an ACTION_UP to the current
5889 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005890 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 + mMotionTarget);
5892 mMotionTarget = null;
5893 }
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 // ACTION_DOWN is special, because we need to lock next events to
5896 // the window we'll land onto.
5897 final int x = (int)xf;
5898 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005900 final ArrayList windows = mWindows;
5901 final int N = windows.size();
5902 WindowState topErrWindow = null;
5903 final Rect tmpRect = mTempRect;
5904 for (int i=N-1; i>=0; i--) {
5905 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005906 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 final int flags = child.mAttrs.flags;
5908 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5909 if (topErrWindow == null) {
5910 topErrWindow = child;
5911 }
5912 }
5913 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005914 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 continue;
5916 }
5917 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005918 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 if ((flags & WindowManager.LayoutParams
5920 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5921 child.mNextOutsideTouch = mOutsideTouchTargets;
5922 mOutsideTouchTargets = child;
5923 }
5924 continue;
5925 }
5926 tmpRect.set(child.mFrame);
5927 if (child.mTouchableInsets == ViewTreeObserver
5928 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5929 // The touch is inside of the window if it is
5930 // inside the frame, AND the content part of that
5931 // frame that was given by the application.
5932 tmpRect.left += child.mGivenContentInsets.left;
5933 tmpRect.top += child.mGivenContentInsets.top;
5934 tmpRect.right -= child.mGivenContentInsets.right;
5935 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5936 } else if (child.mTouchableInsets == ViewTreeObserver
5937 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5938 // The touch is inside of the window if it is
5939 // inside the frame, AND the visible part of that
5940 // frame that was given by the application.
5941 tmpRect.left += child.mGivenVisibleInsets.left;
5942 tmpRect.top += child.mGivenVisibleInsets.top;
5943 tmpRect.right -= child.mGivenVisibleInsets.right;
5944 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5945 }
5946 final int touchFlags = flags &
5947 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5948 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5949 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005950 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 if (!screenWasOff || (flags &
5952 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5953 mMotionTarget = child;
5954 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005955 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 mMotionTarget = null;
5957 }
5958 break;
5959 }
Romain Guy06882f82009-06-10 13:36:04 -07005960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 if ((flags & WindowManager.LayoutParams
5962 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5963 child.mNextOutsideTouch = mOutsideTouchTargets;
5964 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005965 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 }
5967 }
5968
5969 // if there's an error window but it's not accepting
5970 // focus (typically because it is not yet visible) just
5971 // wait for it -- any other focused window may in fact
5972 // be in ANR state.
5973 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5974 mMotionTarget = null;
5975 }
5976 }
Romain Guy06882f82009-06-10 13:36:04 -07005977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 target = mMotionTarget;
5979 }
5980 }
Romain Guy06882f82009-06-10 13:36:04 -07005981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07005983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 // Pointer events are a little different -- if there isn't a
5985 // target found for any event, then just drop it.
5986 return target != null ? target : SKIP_TARGET_TOKEN;
5987 }
Romain Guy06882f82009-06-10 13:36:04 -07005988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 boolean checkShouldDispatchKey(int keycode) {
5990 synchronized (this) {
5991 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
5992 mTimeToSwitch = 0;
5993 return true;
5994 }
5995 if (mTimeToSwitch != 0
5996 && mTimeToSwitch < SystemClock.uptimeMillis()) {
5997 return false;
5998 }
5999 return true;
6000 }
6001 }
Romain Guy06882f82009-06-10 13:36:04 -07006002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 void bindTargetWindowLocked(WindowState win,
6004 int pendingWhat, QueuedEvent pendingMotion) {
6005 synchronized (this) {
6006 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6007 }
6008 }
Romain Guy06882f82009-06-10 13:36:04 -07006009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 void bindTargetWindowLocked(WindowState win) {
6011 synchronized (this) {
6012 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6013 }
6014 }
6015
6016 void bindTargetWindowLockedLocked(WindowState win,
6017 int pendingWhat, QueuedEvent pendingMotion) {
6018 mLastWin = win;
6019 mLastBinder = win.mClient.asBinder();
6020 mFinished = false;
6021 if (pendingMotion != null) {
6022 final Session s = win.mSession;
6023 if (pendingWhat == RETURN_PENDING_POINTER) {
6024 releasePendingPointerLocked(s);
6025 s.mPendingPointerMove = pendingMotion;
6026 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006027 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 "bindTargetToWindow " + s.mPendingPointerMove);
6029 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6030 releasePendingTrackballLocked(s);
6031 s.mPendingTrackballMove = pendingMotion;
6032 s.mPendingTrackballWindow = win;
6033 }
6034 }
6035 }
Romain Guy06882f82009-06-10 13:36:04 -07006036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006038 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 "releasePendingPointer " + s.mPendingPointerMove);
6040 if (s.mPendingPointerMove != null) {
6041 mQueue.recycleEvent(s.mPendingPointerMove);
6042 s.mPendingPointerMove = null;
6043 }
6044 }
Romain Guy06882f82009-06-10 13:36:04 -07006045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 void releasePendingTrackballLocked(Session s) {
6047 if (s.mPendingTrackballMove != null) {
6048 mQueue.recycleEvent(s.mPendingTrackballMove);
6049 s.mPendingTrackballMove = null;
6050 }
6051 }
Romain Guy06882f82009-06-10 13:36:04 -07006052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6054 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006055 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 TAG, "finishedKey: client=" + client + ", force=" + force);
6057
6058 if (client == null) {
6059 return null;
6060 }
6061
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006062 MotionEvent res = null;
6063 QueuedEvent qev = null;
6064 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006067 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 TAG, "finishedKey: client=" + client.asBinder()
6069 + ", force=" + force + ", last=" + mLastBinder
6070 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 if (returnWhat == RETURN_PENDING_POINTER) {
6073 qev = session.mPendingPointerMove;
6074 win = session.mPendingPointerWindow;
6075 session.mPendingPointerMove = null;
6076 session.mPendingPointerWindow = null;
6077 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6078 qev = session.mPendingTrackballMove;
6079 win = session.mPendingTrackballWindow;
6080 session.mPendingTrackballMove = null;
6081 session.mPendingTrackballWindow = null;
6082 }
Romain Guy06882f82009-06-10 13:36:04 -07006083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006084 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006085 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 TAG, "finishedKey: last paused="
6087 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6088 if (mLastWin != null && (!mLastWin.mToken.paused || force
6089 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006090 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 } else {
6092 // Make sure to wake up anyone currently waiting to
6093 // dispatch a key, so they can re-evaluate their
6094 // current situation.
6095 mFinished = true;
6096 notifyAll();
6097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 }
Romain Guy06882f82009-06-10 13:36:04 -07006099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006101 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006102 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006103 "Returning pending motion: " + res);
6104 mQueue.recycleEvent(qev);
6105 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6106 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006110
Christopher Tate2624fbc2009-12-11 12:11:31 -08006111 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6112 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006113 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006114 }
6115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006116
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006117 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 }
6119
6120 void tickle() {
6121 synchronized (this) {
6122 notifyAll();
6123 }
6124 }
Romain Guy06882f82009-06-10 13:36:04 -07006125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 void handleNewWindowLocked(WindowState newWindow) {
6127 if (!newWindow.canReceiveKeys()) {
6128 return;
6129 }
6130 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006131 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 TAG, "New key dispatch window: win="
6133 + newWindow.mClient.asBinder()
6134 + ", last=" + mLastBinder
6135 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6136 + "), finished=" + mFinished + ", paused="
6137 + newWindow.mToken.paused);
6138
6139 // Displaying a window implicitly causes dispatching to
6140 // be unpaused. (This is to protect against bugs if someone
6141 // pauses dispatching but forgets to resume.)
6142 newWindow.mToken.paused = false;
6143
6144 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145
6146 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006147 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 "New SYSTEM_ERROR window; resetting state");
6149 mLastWin = null;
6150 mLastBinder = null;
6151 mMotionTarget = null;
6152 mFinished = true;
6153 } else if (mLastWin != null) {
6154 // If the new window is above the window we are
6155 // waiting on, then stop waiting and let key dispatching
6156 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006157 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 TAG, "Last win layer=" + mLastWin.mLayer
6159 + ", new win layer=" + newWindow.mLayer);
6160 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006161 // The new window is above the old; finish pending input to the last
6162 // window and start directing it to the new one.
6163 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006164 doFinishedKeyLocked(false); // does a notifyAll()
6165 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 }
6167 }
6168
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006169 // Now that we've put a new window state in place, make the event waiter
6170 // take notice and retarget its attentions.
6171 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 }
6173 }
6174
6175 void pauseDispatchingLocked(WindowToken token) {
6176 synchronized (this)
6177 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006178 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 token.paused = true;
6180
6181 /*
6182 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6183 mPaused = true;
6184 } else {
6185 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006186 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006188 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006190 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 }
6192 }
6193 */
6194 }
6195 }
6196
6197 void resumeDispatchingLocked(WindowToken token) {
6198 synchronized (this) {
6199 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006200 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 TAG, "Resuming WindowToken " + token
6202 + ", last=" + mLastBinder
6203 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6204 + "), finished=" + mFinished + ", paused="
6205 + token.paused);
6206 token.paused = false;
6207 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006208 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 } else {
6210 notifyAll();
6211 }
6212 }
6213 }
6214 }
6215
6216 void setEventDispatchingLocked(boolean enabled) {
6217 synchronized (this) {
6218 mEventDispatching = enabled;
6219 notifyAll();
6220 }
6221 }
Romain Guy06882f82009-06-10 13:36:04 -07006222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 void appSwitchComing() {
6224 synchronized (this) {
6225 // Don't wait for more than .5 seconds for app to finish
6226 // processing the pending events.
6227 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006228 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6230 mTimeToSwitch = now;
6231 }
6232 notifyAll();
6233 }
6234 }
Romain Guy06882f82009-06-10 13:36:04 -07006235
Christopher Tate136b1f92010-02-11 17:51:24 -08006236 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 if (mLastWin != null) {
6238 releasePendingPointerLocked(mLastWin.mSession);
6239 releasePendingTrackballLocked(mLastWin.mSession);
6240 }
Romain Guy06882f82009-06-10 13:36:04 -07006241
Christopher Tate136b1f92010-02-11 17:51:24 -08006242 if (force || mLastWin == null || !mLastWin.mToken.paused
6243 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 // If the current window has been paused, we aren't -really-
6245 // finished... so let the waiters still wait.
6246 mLastWin = null;
6247 mLastBinder = null;
6248 }
6249 mFinished = true;
6250 notifyAll();
6251 }
6252 }
6253
6254 private class KeyQ extends KeyInputQueue
6255 implements KeyInputQueue.FilterCallback {
6256 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006259 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6261 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6262 "KEEP_SCREEN_ON_FLAG");
6263 mHoldingScreen.setReferenceCounted(false);
6264 }
6265
6266 @Override
6267 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6268 if (mPolicy.preprocessInputEventTq(event)) {
6269 return true;
6270 }
Romain Guy06882f82009-06-10 13:36:04 -07006271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 switch (event.type) {
6273 case RawInputEvent.EV_KEY: {
6274 // XXX begin hack
6275 if (DEBUG) {
6276 if (event.keycode == KeyEvent.KEYCODE_G) {
6277 if (event.value != 0) {
6278 // G down
6279 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6280 }
6281 return false;
6282 }
6283 if (event.keycode == KeyEvent.KEYCODE_D) {
6284 if (event.value != 0) {
6285 //dump();
6286 }
6287 return false;
6288 }
6289 }
6290 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006291
Charles Mendis322591c2009-10-29 11:06:59 -07006292 boolean screenIsOff = !mPowerManager.isScreenOn();
6293 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006294 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6297 mPowerManager.goToSleep(event.when);
6298 }
6299
6300 if (screenIsOff) {
6301 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6302 }
6303 if (screenIsDim) {
6304 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6305 }
6306 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6307 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006308 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 }
Romain Guy06882f82009-06-10 13:36:04 -07006310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6312 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6313 filterQueue(this);
6314 mKeyWaiter.appSwitchComing();
6315 }
6316 return true;
6317 } else {
6318 return false;
6319 }
6320 }
Romain Guy06882f82009-06-10 13:36:04 -07006321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006323 boolean screenIsOff = !mPowerManager.isScreenOn();
6324 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 if (screenIsOff) {
6326 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6327 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006328 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 return false;
6330 }
6331 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6332 }
6333 if (screenIsDim) {
6334 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6335 }
6336 return true;
6337 }
Romain Guy06882f82009-06-10 13:36:04 -07006338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006340 boolean screenIsOff = !mPowerManager.isScreenOn();
6341 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 if (screenIsOff) {
6343 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6344 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006345 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 return false;
6347 }
6348 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6349 }
6350 if (screenIsDim) {
6351 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6352 }
6353 return true;
6354 }
Romain Guy06882f82009-06-10 13:36:04 -07006355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 default:
6357 return true;
6358 }
6359 }
6360
6361 public int filterEvent(QueuedEvent ev) {
6362 switch (ev.classType) {
6363 case RawInputEvent.CLASS_KEYBOARD:
6364 KeyEvent ke = (KeyEvent)ev.event;
6365 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006366 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 + ke.getKeyCode() + ", action=" + ke.getAction());
6368 return FILTER_REMOVE;
6369 }
6370 return FILTER_ABORT;
6371 default:
6372 return FILTER_KEEP;
6373 }
6374 }
Romain Guy06882f82009-06-10 13:36:04 -07006375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006376 /**
6377 * Must be called with the main window manager lock held.
6378 */
6379 void setHoldScreenLocked(boolean holding) {
6380 boolean state = mHoldingScreen.isHeld();
6381 if (holding != state) {
6382 if (holding) {
6383 mHoldingScreen.acquire();
6384 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006385 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 mHoldingScreen.release();
6387 }
6388 }
6389 }
Michael Chan53071d62009-05-13 17:29:48 -07006390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391
6392 public boolean detectSafeMode() {
6393 mSafeMode = mPolicy.detectSafeMode();
6394 return mSafeMode;
6395 }
Romain Guy06882f82009-06-10 13:36:04 -07006396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006397 public void systemReady() {
6398 mPolicy.systemReady();
6399 }
Romain Guy06882f82009-06-10 13:36:04 -07006400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 private final class InputDispatcherThread extends Thread {
6402 // Time to wait when there is nothing to do: 9999 seconds.
6403 static final int LONG_WAIT=9999*1000;
6404
6405 public InputDispatcherThread() {
6406 super("InputDispatcher");
6407 }
Romain Guy06882f82009-06-10 13:36:04 -07006408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 @Override
6410 public void run() {
6411 while (true) {
6412 try {
6413 process();
6414 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006415 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 }
6417 }
6418 }
Romain Guy06882f82009-06-10 13:36:04 -07006419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 private void process() {
6421 android.os.Process.setThreadPriority(
6422 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006424 // The last key event we saw
6425 KeyEvent lastKey = null;
6426
6427 // Last keydown time for auto-repeating keys
6428 long lastKeyTime = SystemClock.uptimeMillis();
6429 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006430 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431
Romain Guy06882f82009-06-10 13:36:04 -07006432 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 int keyRepeatCount = 0;
6434
6435 // Need to report that configuration has changed?
6436 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 while (true) {
6439 long curTime = SystemClock.uptimeMillis();
6440
Joe Onorato8a9b2202010-02-26 18:56:32 -08006441 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 TAG, "Waiting for next key: now=" + curTime
6443 + ", repeat @ " + nextKeyTime);
6444
6445 // Retrieve next event, waiting only as long as the next
6446 // repeat timeout. If the configuration has changed, then
6447 // don't wait at all -- we'll report the change as soon as
6448 // we have processed all events.
6449 QueuedEvent ev = mQueue.getEvent(
6450 (int)((!configChanged && curTime < nextKeyTime)
6451 ? (nextKeyTime-curTime) : 0));
6452
Joe Onorato8a9b2202010-02-26 18:56:32 -08006453 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6455
Michael Chan53071d62009-05-13 17:29:48 -07006456 if (MEASURE_LATENCY) {
6457 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6458 }
6459
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006460 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6461 // cancel key repeat at the request of the policy.
6462 lastKey = null;
6463 downTime = 0;
6464 lastKeyTime = curTime;
6465 nextKeyTime = curTime + LONG_WAIT;
6466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 try {
6468 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006469 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 int eventType;
6471 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6472 eventType = eventType((MotionEvent)ev.event);
6473 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6474 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6475 eventType = LocalPowerManager.BUTTON_EVENT;
6476 } else {
6477 eventType = LocalPowerManager.OTHER_EVENT;
6478 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006479 try {
Michael Chan53071d62009-05-13 17:29:48 -07006480 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006481 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006482 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006483 mBatteryStats.noteInputEvent();
6484 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006485 } catch (RemoteException e) {
6486 // Ignore
6487 }
Michael Chane10de972009-05-18 11:24:50 -07006488
Mike Lockwood5db42402009-11-30 14:51:51 -05006489 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6490 // do not wake screen in this case
6491 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006492 && eventType != LONG_TOUCH_EVENT
6493 && eventType != CHEEK_EVENT) {
6494 mPowerManager.userActivity(curTime, false,
6495 eventType, false);
6496 } else if (mLastTouchEventType != eventType
6497 || (curTime - mLastUserActivityCallTime)
6498 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6499 mLastUserActivityCallTime = curTime;
6500 mLastTouchEventType = eventType;
6501 mPowerManager.userActivity(curTime, false,
6502 eventType, false);
6503 }
6504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 switch (ev.classType) {
6506 case RawInputEvent.CLASS_KEYBOARD:
6507 KeyEvent ke = (KeyEvent)ev.event;
6508 if (ke.isDown()) {
6509 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006510 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 keyRepeatCount = 0;
6512 lastKeyTime = curTime;
6513 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006514 + ViewConfiguration.getLongPressTimeout();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006515 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 TAG, "Received key down: first repeat @ "
6517 + nextKeyTime);
6518 } else {
6519 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006520 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 // Arbitrary long timeout.
6522 lastKeyTime = curTime;
6523 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006524 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 TAG, "Received key up: ignore repeat @ "
6526 + nextKeyTime);
6527 }
6528 dispatchKey((KeyEvent)ev.event, 0, 0);
6529 mQueue.recycleEvent(ev);
6530 break;
6531 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006532 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6534 break;
6535 case RawInputEvent.CLASS_TRACKBALL:
6536 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6537 break;
6538 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6539 configChanged = true;
6540 break;
6541 default:
6542 mQueue.recycleEvent(ev);
6543 break;
6544 }
Romain Guy06882f82009-06-10 13:36:04 -07006545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 } else if (configChanged) {
6547 configChanged = false;
6548 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 } else if (lastKey != null) {
6551 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 // Timeout occurred while key was down. If it is at or
6554 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006555 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 TAG, "Key timeout: repeat=" + nextKeyTime
6557 + ", now=" + curTime);
6558 if (curTime < nextKeyTime) {
6559 continue;
6560 }
Romain Guy06882f82009-06-10 13:36:04 -07006561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 lastKeyTime = nextKeyTime;
6563 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6564 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006565 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 TAG, "Key repeat: count=" + keyRepeatCount
6567 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006568 KeyEvent newEvent;
6569 if (downTime != 0 && (downTime
6570 + ViewConfiguration.getLongPressTimeout())
6571 <= curTime) {
6572 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6573 curTime, keyRepeatCount,
6574 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6575 downTime = 0;
6576 } else {
6577 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6578 curTime, keyRepeatCount);
6579 }
6580 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 } else {
6583 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 lastKeyTime = curTime;
6586 nextKeyTime = curTime + LONG_WAIT;
6587 }
Romain Guy06882f82009-06-10 13:36:04 -07006588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006590 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 "Input thread received uncaught exception: " + e, e);
6592 }
6593 }
6594 }
6595 }
6596
6597 // -------------------------------------------------------------
6598 // Client Session State
6599 // -------------------------------------------------------------
6600
6601 private final class Session extends IWindowSession.Stub
6602 implements IBinder.DeathRecipient {
6603 final IInputMethodClient mClient;
6604 final IInputContext mInputContext;
6605 final int mUid;
6606 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006607 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 SurfaceSession mSurfaceSession;
6609 int mNumWindow = 0;
6610 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 /**
6613 * Current pointer move event being dispatched to client window... must
6614 * hold key lock to access.
6615 */
6616 QueuedEvent mPendingPointerMove;
6617 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 /**
6620 * Current trackball move event being dispatched to client window... must
6621 * hold key lock to access.
6622 */
6623 QueuedEvent mPendingTrackballMove;
6624 WindowState mPendingTrackballWindow;
6625
6626 public Session(IInputMethodClient client, IInputContext inputContext) {
6627 mClient = client;
6628 mInputContext = inputContext;
6629 mUid = Binder.getCallingUid();
6630 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006631 StringBuilder sb = new StringBuilder();
6632 sb.append("Session{");
6633 sb.append(Integer.toHexString(System.identityHashCode(this)));
6634 sb.append(" uid ");
6635 sb.append(mUid);
6636 sb.append("}");
6637 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006639 synchronized (mWindowMap) {
6640 if (mInputMethodManager == null && mHaveInputMethods) {
6641 IBinder b = ServiceManager.getService(
6642 Context.INPUT_METHOD_SERVICE);
6643 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6644 }
6645 }
6646 long ident = Binder.clearCallingIdentity();
6647 try {
6648 // Note: it is safe to call in to the input method manager
6649 // here because we are not holding our lock.
6650 if (mInputMethodManager != null) {
6651 mInputMethodManager.addClient(client, inputContext,
6652 mUid, mPid);
6653 } else {
6654 client.setUsingInputMethod(false);
6655 }
6656 client.asBinder().linkToDeath(this, 0);
6657 } catch (RemoteException e) {
6658 // The caller has died, so we can just forget about this.
6659 try {
6660 if (mInputMethodManager != null) {
6661 mInputMethodManager.removeClient(client);
6662 }
6663 } catch (RemoteException ee) {
6664 }
6665 } finally {
6666 Binder.restoreCallingIdentity(ident);
6667 }
6668 }
Romain Guy06882f82009-06-10 13:36:04 -07006669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 @Override
6671 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6672 throws RemoteException {
6673 try {
6674 return super.onTransact(code, data, reply, flags);
6675 } catch (RuntimeException e) {
6676 // Log all 'real' exceptions thrown to the caller
6677 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006678 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 }
6680 throw e;
6681 }
6682 }
6683
6684 public void binderDied() {
6685 // Note: it is safe to call in to the input method manager
6686 // here because we are not holding our lock.
6687 try {
6688 if (mInputMethodManager != null) {
6689 mInputMethodManager.removeClient(mClient);
6690 }
6691 } catch (RemoteException e) {
6692 }
6693 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006694 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 mClientDead = true;
6696 killSessionLocked();
6697 }
6698 }
6699
6700 public int add(IWindow window, WindowManager.LayoutParams attrs,
6701 int viewVisibility, Rect outContentInsets) {
6702 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6703 }
Romain Guy06882f82009-06-10 13:36:04 -07006704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 public void remove(IWindow window) {
6706 removeWindow(this, window);
6707 }
Romain Guy06882f82009-06-10 13:36:04 -07006708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6710 int requestedWidth, int requestedHeight, int viewFlags,
6711 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006712 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006713 return relayoutWindow(this, window, attrs,
6714 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006715 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006716 }
Romain Guy06882f82009-06-10 13:36:04 -07006717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 public void setTransparentRegion(IWindow window, Region region) {
6719 setTransparentRegionWindow(this, window, region);
6720 }
Romain Guy06882f82009-06-10 13:36:04 -07006721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 public void setInsets(IWindow window, int touchableInsets,
6723 Rect contentInsets, Rect visibleInsets) {
6724 setInsetsWindow(this, window, touchableInsets, contentInsets,
6725 visibleInsets);
6726 }
Romain Guy06882f82009-06-10 13:36:04 -07006727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6729 getWindowDisplayFrame(this, window, outDisplayFrame);
6730 }
Romain Guy06882f82009-06-10 13:36:04 -07006731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006733 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 TAG, "IWindow finishDrawing called for " + window);
6735 finishDrawingWindow(this, window);
6736 }
6737
6738 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006739 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 TAG, "IWindow finishKey called for " + window);
6741 mKeyWaiter.finishedKey(this, window, false,
6742 KeyWaiter.RETURN_NOTHING);
6743 }
6744
6745 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006746 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 TAG, "IWindow getPendingMotionEvent called for " + window);
6748 return mKeyWaiter.finishedKey(this, window, false,
6749 KeyWaiter.RETURN_PENDING_POINTER);
6750 }
Romain Guy06882f82009-06-10 13:36:04 -07006751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006753 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 TAG, "IWindow getPendingMotionEvent called for " + window);
6755 return mKeyWaiter.finishedKey(this, window, false,
6756 KeyWaiter.RETURN_PENDING_TRACKBALL);
6757 }
6758
6759 public void setInTouchMode(boolean mode) {
6760 synchronized(mWindowMap) {
6761 mInTouchMode = mode;
6762 }
6763 }
6764
6765 public boolean getInTouchMode() {
6766 synchronized(mWindowMap) {
6767 return mInTouchMode;
6768 }
6769 }
6770
6771 public boolean performHapticFeedback(IWindow window, int effectId,
6772 boolean always) {
6773 synchronized(mWindowMap) {
6774 long ident = Binder.clearCallingIdentity();
6775 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006776 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006777 windowForClientLocked(this, window, true),
6778 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779 } finally {
6780 Binder.restoreCallingIdentity(ident);
6781 }
6782 }
6783 }
Romain Guy06882f82009-06-10 13:36:04 -07006784
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006785 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006786 synchronized(mWindowMap) {
6787 long ident = Binder.clearCallingIdentity();
6788 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006789 setWindowWallpaperPositionLocked(
6790 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006791 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006792 } finally {
6793 Binder.restoreCallingIdentity(ident);
6794 }
6795 }
6796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006797
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006798 public void wallpaperOffsetsComplete(IBinder window) {
6799 WindowManagerService.this.wallpaperOffsetsComplete(window);
6800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006801
Dianne Hackborn75804932009-10-20 20:15:20 -07006802 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6803 int z, Bundle extras, boolean sync) {
6804 synchronized(mWindowMap) {
6805 long ident = Binder.clearCallingIdentity();
6806 try {
6807 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006808 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006809 action, x, y, z, extras, sync);
6810 } finally {
6811 Binder.restoreCallingIdentity(ident);
6812 }
6813 }
6814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006815
Dianne Hackborn75804932009-10-20 20:15:20 -07006816 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6817 WindowManagerService.this.wallpaperCommandComplete(window, result);
6818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 void windowAddedLocked() {
6821 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006822 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 TAG, "First window added to " + this + ", creating SurfaceSession");
6824 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006825 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006826 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006827 mSessions.add(this);
6828 }
6829 mNumWindow++;
6830 }
6831
6832 void windowRemovedLocked() {
6833 mNumWindow--;
6834 killSessionLocked();
6835 }
Romain Guy06882f82009-06-10 13:36:04 -07006836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 void killSessionLocked() {
6838 if (mNumWindow <= 0 && mClientDead) {
6839 mSessions.remove(this);
6840 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006841 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 TAG, "Last window removed from " + this
6843 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006844 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006845 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 try {
6847 mSurfaceSession.kill();
6848 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006849 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006850 + mSurfaceSession + " in session " + this
6851 + ": " + e.toString());
6852 }
6853 mSurfaceSession = null;
6854 }
6855 }
6856 }
Romain Guy06882f82009-06-10 13:36:04 -07006857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006859 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6860 pw.print(" mClientDead="); pw.print(mClientDead);
6861 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6862 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6863 pw.print(prefix);
6864 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6865 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6866 }
6867 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6868 pw.print(prefix);
6869 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6870 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 }
6873
6874 @Override
6875 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006876 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 }
6878 }
6879
6880 // -------------------------------------------------------------
6881 // Client Window State
6882 // -------------------------------------------------------------
6883
6884 private final class WindowState implements WindowManagerPolicy.WindowState {
6885 final Session mSession;
6886 final IWindow mClient;
6887 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006888 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 AppWindowToken mAppToken;
6890 AppWindowToken mTargetAppToken;
6891 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6892 final DeathRecipient mDeathRecipient;
6893 final WindowState mAttachedWindow;
6894 final ArrayList mChildWindows = new ArrayList();
6895 final int mBaseLayer;
6896 final int mSubLayer;
6897 final boolean mLayoutAttached;
6898 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006899 final boolean mIsWallpaper;
6900 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 int mViewVisibility;
6902 boolean mPolicyVisibility = true;
6903 boolean mPolicyVisibilityAfterAnim = true;
6904 boolean mAppFreezing;
6905 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006906 boolean mReportDestroySurface;
6907 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006908 boolean mAttachedHidden; // is our parent window hidden?
6909 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006910 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 int mRequestedWidth;
6912 int mRequestedHeight;
6913 int mLastRequestedWidth;
6914 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 int mLayer;
6916 int mAnimLayer;
6917 int mLastLayer;
6918 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006919 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006920 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921
6922 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006923
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006924 int mLayoutSeq = -1;
6925
6926 Configuration mConfiguration = null;
6927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928 // Actual frame shown on-screen (may be modified by animation)
6929 final Rect mShownFrame = new Rect();
6930 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08006933 * Set when we have changed the size of the surface, to know that
6934 * we must tell them application to resize (and thus redraw itself).
6935 */
6936 boolean mSurfaceResized;
6937
6938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 * Insets that determine the actually visible area
6940 */
6941 final Rect mVisibleInsets = new Rect();
6942 final Rect mLastVisibleInsets = new Rect();
6943 boolean mVisibleInsetsChanged;
6944
6945 /**
6946 * Insets that are covered by system windows
6947 */
6948 final Rect mContentInsets = new Rect();
6949 final Rect mLastContentInsets = new Rect();
6950 boolean mContentInsetsChanged;
6951
6952 /**
6953 * Set to true if we are waiting for this window to receive its
6954 * given internal insets before laying out other windows based on it.
6955 */
6956 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 /**
6959 * These are the content insets that were given during layout for
6960 * this window, to be applied to windows behind it.
6961 */
6962 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 /**
6965 * These are the visible insets that were given during layout for
6966 * this window, to be applied to windows behind it.
6967 */
6968 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 /**
6971 * Flag indicating whether the touchable region should be adjusted by
6972 * the visible insets; if false the area outside the visible insets is
6973 * NOT touchable, so we must use those to adjust the frame during hit
6974 * tests.
6975 */
6976 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07006977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 // Current transformation being applied.
6979 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
6980 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
6981 float mHScale=1, mVScale=1;
6982 float mLastHScale=1, mLastVScale=1;
6983 final Matrix mTmpMatrix = new Matrix();
6984
6985 // "Real" frame that the application sees.
6986 final Rect mFrame = new Rect();
6987 final Rect mLastFrame = new Rect();
6988
6989 final Rect mContainingFrame = new Rect();
6990 final Rect mDisplayFrame = new Rect();
6991 final Rect mContentFrame = new Rect();
6992 final Rect mVisibleFrame = new Rect();
6993
6994 float mShownAlpha = 1;
6995 float mAlpha = 1;
6996 float mLastAlpha = 1;
6997
6998 // Set to true if, when the window gets displayed, it should perform
6999 // an enter animation.
7000 boolean mEnterAnimationPending;
7001
7002 // Currently running animation.
7003 boolean mAnimating;
7004 boolean mLocalAnimating;
7005 Animation mAnimation;
7006 boolean mAnimationIsEntrance;
7007 boolean mHasTransformation;
7008 boolean mHasLocalTransformation;
7009 final Transformation mTransformation = new Transformation();
7010
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007011 // If a window showing a wallpaper: the requested offset for the
7012 // wallpaper; if a wallpaper window: the currently applied offset.
7013 float mWallpaperX = -1;
7014 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007015
7016 // If a window showing a wallpaper: what fraction of the offset
7017 // range corresponds to a full virtual screen.
7018 float mWallpaperXStep = -1;
7019 float mWallpaperYStep = -1;
7020
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007021 // Wallpaper windows: pixels offset based on above variables.
7022 int mXOffset;
7023 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 // This is set after IWindowSession.relayout() has been called at
7026 // least once for the window. It allows us to detect the situation
7027 // where we don't yet have a surface, but should have one soon, so
7028 // we can give the window focus before waiting for the relayout.
7029 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 // This is set after the Surface has been created but before the
7032 // window has been drawn. During this time the surface is hidden.
7033 boolean mDrawPending;
7034
7035 // This is set after the window has finished drawing for the first
7036 // time but before its surface is shown. The surface will be
7037 // displayed when the next layout is run.
7038 boolean mCommitDrawPending;
7039
7040 // This is set during the time after the window's drawing has been
7041 // committed, and before its surface is actually shown. It is used
7042 // to delay showing the surface until all windows in a token are ready
7043 // to be shown.
7044 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 // Set when the window has been shown in the screen the first time.
7047 boolean mHasDrawn;
7048
7049 // Currently running an exit animation?
7050 boolean mExiting;
7051
7052 // Currently on the mDestroySurface list?
7053 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 // Completely remove from window manager after exit animation?
7056 boolean mRemoveOnExit;
7057
7058 // Set when the orientation is changing and this window has not yet
7059 // been updated for the new orientation.
7060 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062 // Is this window now (or just being) removed?
7063 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 WindowState(Session s, IWindow c, WindowToken token,
7066 WindowState attachedWindow, WindowManager.LayoutParams a,
7067 int viewVisibility) {
7068 mSession = s;
7069 mClient = c;
7070 mToken = token;
7071 mAttrs.copyFrom(a);
7072 mViewVisibility = viewVisibility;
7073 DeathRecipient deathRecipient = new DeathRecipient();
7074 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007075 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 TAG, "Window " + this + " client=" + c.asBinder()
7077 + " token=" + token + " (" + mAttrs.token + ")");
7078 try {
7079 c.asBinder().linkToDeath(deathRecipient, 0);
7080 } catch (RemoteException e) {
7081 mDeathRecipient = null;
7082 mAttachedWindow = null;
7083 mLayoutAttached = false;
7084 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007085 mIsWallpaper = false;
7086 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 mBaseLayer = 0;
7088 mSubLayer = 0;
7089 return;
7090 }
7091 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7094 mAttrs.type <= LAST_SUB_WINDOW)) {
7095 // The multiplier here is to reserve space for multiple
7096 // windows in the same type layer.
7097 mBaseLayer = mPolicy.windowTypeToLayerLw(
7098 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7099 + TYPE_LAYER_OFFSET;
7100 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7101 mAttachedWindow = attachedWindow;
7102 mAttachedWindow.mChildWindows.add(this);
7103 mLayoutAttached = mAttrs.type !=
7104 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7105 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7106 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007107 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7108 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 } else {
7110 // The multiplier here is to reserve space for multiple
7111 // windows in the same type layer.
7112 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7113 * TYPE_LAYER_MULTIPLIER
7114 + TYPE_LAYER_OFFSET;
7115 mSubLayer = 0;
7116 mAttachedWindow = null;
7117 mLayoutAttached = false;
7118 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7119 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007120 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7121 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 }
7123
7124 WindowState appWin = this;
7125 while (appWin.mAttachedWindow != null) {
7126 appWin = mAttachedWindow;
7127 }
7128 WindowToken appToken = appWin.mToken;
7129 while (appToken.appWindowToken == null) {
7130 WindowToken parent = mTokenMap.get(appToken.token);
7131 if (parent == null || appToken == parent) {
7132 break;
7133 }
7134 appToken = parent;
7135 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007136 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 mAppToken = appToken.appWindowToken;
7138
7139 mSurface = null;
7140 mRequestedWidth = 0;
7141 mRequestedHeight = 0;
7142 mLastRequestedWidth = 0;
7143 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007144 mXOffset = 0;
7145 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 mLayer = 0;
7147 mAnimLayer = 0;
7148 mLastLayer = 0;
7149 }
7150
7151 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007152 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 TAG, "Attaching " + this + " token=" + mToken
7154 + ", list=" + mToken.windows);
7155 mSession.windowAddedLocked();
7156 }
7157
7158 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7159 mHaveFrame = true;
7160
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007161 final Rect container = mContainingFrame;
7162 container.set(pf);
7163
7164 final Rect display = mDisplayFrame;
7165 display.set(df);
7166
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007167 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007168 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007169 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7170 display.intersect(mCompatibleScreenFrame);
7171 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007172 }
7173
7174 final int pw = container.right - container.left;
7175 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176
7177 int w,h;
7178 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7179 w = mAttrs.width < 0 ? pw : mAttrs.width;
7180 h = mAttrs.height< 0 ? ph : mAttrs.height;
7181 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007182 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7183 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 }
Romain Guy06882f82009-06-10 13:36:04 -07007185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 final Rect content = mContentFrame;
7187 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 final Rect visible = mVisibleFrame;
7190 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007193 final int fw = frame.width();
7194 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7197 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7198
7199 Gravity.apply(mAttrs.gravity, w, h, container,
7200 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7201 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7202
7203 //System.out.println("Out: " + mFrame);
7204
7205 // Now make sure the window fits in the overall display.
7206 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 // Make sure the content and visible frames are inside of the
7209 // final window frame.
7210 if (content.left < frame.left) content.left = frame.left;
7211 if (content.top < frame.top) content.top = frame.top;
7212 if (content.right > frame.right) content.right = frame.right;
7213 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7214 if (visible.left < frame.left) visible.left = frame.left;
7215 if (visible.top < frame.top) visible.top = frame.top;
7216 if (visible.right > frame.right) visible.right = frame.right;
7217 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 final Rect contentInsets = mContentInsets;
7220 contentInsets.left = content.left-frame.left;
7221 contentInsets.top = content.top-frame.top;
7222 contentInsets.right = frame.right-content.right;
7223 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 final Rect visibleInsets = mVisibleInsets;
7226 visibleInsets.left = visible.left-frame.left;
7227 visibleInsets.top = visible.top-frame.top;
7228 visibleInsets.right = frame.right-visible.right;
7229 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007230
Dianne Hackborn284ac932009-08-28 10:34:25 -07007231 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7232 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007233 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 if (localLOGV) {
7237 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7238 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007239 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 + mRequestedWidth + ", mRequestedheight="
7241 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7242 + "): frame=" + mFrame.toShortString()
7243 + " ci=" + contentInsets.toShortString()
7244 + " vi=" + visibleInsets.toShortString());
7245 //}
7246 }
7247 }
Romain Guy06882f82009-06-10 13:36:04 -07007248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 public Rect getFrameLw() {
7250 return mFrame;
7251 }
7252
7253 public Rect getShownFrameLw() {
7254 return mShownFrame;
7255 }
7256
7257 public Rect getDisplayFrameLw() {
7258 return mDisplayFrame;
7259 }
7260
7261 public Rect getContentFrameLw() {
7262 return mContentFrame;
7263 }
7264
7265 public Rect getVisibleFrameLw() {
7266 return mVisibleFrame;
7267 }
7268
7269 public boolean getGivenInsetsPendingLw() {
7270 return mGivenInsetsPending;
7271 }
7272
7273 public Rect getGivenContentInsetsLw() {
7274 return mGivenContentInsets;
7275 }
Romain Guy06882f82009-06-10 13:36:04 -07007276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007277 public Rect getGivenVisibleInsetsLw() {
7278 return mGivenVisibleInsets;
7279 }
Romain Guy06882f82009-06-10 13:36:04 -07007280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 public WindowManager.LayoutParams getAttrs() {
7282 return mAttrs;
7283 }
7284
7285 public int getSurfaceLayer() {
7286 return mLayer;
7287 }
Romain Guy06882f82009-06-10 13:36:04 -07007288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007289 public IApplicationToken getAppToken() {
7290 return mAppToken != null ? mAppToken.appToken : null;
7291 }
7292
7293 public boolean hasAppShownWindows() {
7294 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7295 }
7296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007298 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 TAG, "Setting animation in " + this + ": " + anim);
7300 mAnimating = false;
7301 mLocalAnimating = false;
7302 mAnimation = anim;
7303 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7304 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7305 }
7306
7307 public void clearAnimation() {
7308 if (mAnimation != null) {
7309 mAnimating = true;
7310 mLocalAnimating = false;
7311 mAnimation = null;
7312 }
7313 }
Romain Guy06882f82009-06-10 13:36:04 -07007314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 Surface createSurfaceLocked() {
7316 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007317 mReportDestroySurface = false;
7318 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 mDrawPending = true;
7320 mCommitDrawPending = false;
7321 mReadyToShow = false;
7322 if (mAppToken != null) {
7323 mAppToken.allDrawn = false;
7324 }
7325
7326 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007327 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 flags |= Surface.PUSH_BUFFERS;
7329 }
7330
7331 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7332 flags |= Surface.SECURE;
7333 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007334 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 TAG, "Creating surface in session "
7336 + mSession.mSurfaceSession + " window " + this
7337 + " w=" + mFrame.width()
7338 + " h=" + mFrame.height() + " format="
7339 + mAttrs.format + " flags=" + flags);
7340
7341 int w = mFrame.width();
7342 int h = mFrame.height();
7343 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7344 // for a scaled surface, we always want the requested
7345 // size.
7346 w = mRequestedWidth;
7347 h = mRequestedHeight;
7348 }
7349
Romain Guy9825ec62009-10-01 00:58:09 -07007350 // Something is wrong and SurfaceFlinger will not like this,
7351 // try to revert to sane values
7352 if (w <= 0) w = 1;
7353 if (h <= 0) h = 1;
7354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 try {
7356 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007357 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007358 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007360 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007361 + mSurface + " IN SESSION "
7362 + mSession.mSurfaceSession
7363 + ": pid=" + mSession.mPid + " format="
7364 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007365 + Integer.toHexString(flags)
7366 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007368 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 reclaimSomeSurfaceMemoryLocked(this, "create");
7370 return null;
7371 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007372 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 return null;
7374 }
Romain Guy06882f82009-06-10 13:36:04 -07007375
Joe Onorato8a9b2202010-02-26 18:56:32 -08007376 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 TAG, "Got surface: " + mSurface
7378 + ", set left=" + mFrame.left + " top=" + mFrame.top
7379 + ", animLayer=" + mAnimLayer);
7380 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007381 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007382 if (SHOW_TRANSACTIONS) logSurface(this,
7383 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7384 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7385 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 }
7387 Surface.openTransaction();
7388 try {
7389 try {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007390 mSurface.setPosition(mFrame.left + mXOffset,
7391 mFrame.top + mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 mSurface.setLayer(mAnimLayer);
7393 mSurface.hide();
7394 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007395 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 mSurface.setFlags(Surface.SURFACE_DITHER,
7397 Surface.SURFACE_DITHER);
7398 }
7399 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007400 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007401 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7402 }
7403 mLastHidden = true;
7404 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007405 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 Surface.closeTransaction();
7407 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007408 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 TAG, "Created surface " + this);
7410 }
7411 return mSurface;
7412 }
Romain Guy06882f82009-06-10 13:36:04 -07007413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 void destroySurfaceLocked() {
7415 // Window is no longer on-screen, so can no longer receive
7416 // key events... if we were waiting for it to finish
7417 // handling a key event, the wait is over!
7418 mKeyWaiter.finishedKey(mSession, mClient, true,
7419 KeyWaiter.RETURN_NOTHING);
7420 mKeyWaiter.releasePendingPointerLocked(mSession);
7421 mKeyWaiter.releasePendingTrackballLocked(mSession);
7422
7423 if (mAppToken != null && this == mAppToken.startingWindow) {
7424 mAppToken.startingDisplayed = false;
7425 }
Romain Guy06882f82009-06-10 13:36:04 -07007426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007428 mDrawPending = false;
7429 mCommitDrawPending = false;
7430 mReadyToShow = false;
7431
7432 int i = mChildWindows.size();
7433 while (i > 0) {
7434 i--;
7435 WindowState c = (WindowState)mChildWindows.get(i);
7436 c.mAttachedHidden = true;
7437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007438
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007439 if (mReportDestroySurface) {
7440 mReportDestroySurface = false;
7441 mSurfacePendingDestroy = true;
7442 try {
7443 mClient.dispatchGetNewSurface();
7444 // We'll really destroy on the next time around.
7445 return;
7446 } catch (RemoteException e) {
7447 }
7448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007451 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007452 RuntimeException e = null;
7453 if (!HIDE_STACK_CRAWLS) {
7454 e = new RuntimeException();
7455 e.fillInStackTrace();
7456 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007457 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007458 + mSurface + ", session " + mSession, e);
7459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007461 RuntimeException e = null;
7462 if (!HIDE_STACK_CRAWLS) {
7463 e = new RuntimeException();
7464 e.fillInStackTrace();
7465 }
7466 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007468 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007470 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 + " surface " + mSurface + " session " + mSession
7472 + ": " + e.toString());
7473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 }
7477 }
7478
7479 boolean finishDrawingLocked() {
7480 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007481 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 TAG, "finishDrawingLocked: " + mSurface);
7483 mCommitDrawPending = true;
7484 mDrawPending = false;
7485 return true;
7486 }
7487 return false;
7488 }
7489
7490 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007491 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007492 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007494 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 }
7496 mCommitDrawPending = false;
7497 mReadyToShow = true;
7498 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7499 final AppWindowToken atoken = mAppToken;
7500 if (atoken == null || atoken.allDrawn || starting) {
7501 performShowLocked();
7502 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007503 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007504 }
7505
7506 // This must be called while inside a transaction.
7507 boolean performShowLocked() {
7508 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007509 RuntimeException e = null;
7510 if (!HIDE_STACK_CRAWLS) {
7511 e = new RuntimeException();
7512 e.fillInStackTrace();
7513 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007514 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7516 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7517 }
7518 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007519 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7520 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007521 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 + " during animation: policyVis=" + mPolicyVisibility
7523 + " attHidden=" + mAttachedHidden
7524 + " tok.hiddenRequested="
7525 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007526 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 + (mAppToken != null ? mAppToken.hidden : false)
7528 + " animating=" + mAnimating
7529 + " tok animating="
7530 + (mAppToken != null ? mAppToken.animating : false));
7531 if (!showSurfaceRobustlyLocked(this)) {
7532 return false;
7533 }
7534 mLastAlpha = -1;
7535 mHasDrawn = true;
7536 mLastHidden = false;
7537 mReadyToShow = false;
7538 enableScreenIfNeededLocked();
7539
7540 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 int i = mChildWindows.size();
7543 while (i > 0) {
7544 i--;
7545 WindowState c = (WindowState)mChildWindows.get(i);
7546 if (c.mSurface != null && c.mAttachedHidden) {
7547 c.mAttachedHidden = false;
7548 c.performShowLocked();
Dianne Hackborn9b52a212009-12-11 14:51:35 -08007549 // It hadn't been shown, which means layout not
7550 // performed on it, so now we want to make sure to
7551 // do a layout. If called from within the transaction
7552 // loop, this will cause it to restart with a new
7553 // layout.
7554 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 }
7556 }
Romain Guy06882f82009-06-10 13:36:04 -07007557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 if (mAttrs.type != TYPE_APPLICATION_STARTING
7559 && mAppToken != null) {
7560 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007561
Dianne Hackborn248b1882009-09-16 16:46:44 -07007562 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007563 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007564 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007566 // If this initial window is animating, stop it -- we
7567 // will do an animation to reveal it from behind the
7568 // starting window, so there is no need for it to also
7569 // be doing its own stuff.
7570 if (mAnimation != null) {
7571 mAnimation = null;
7572 // Make sure we clean up the animation.
7573 mAnimating = true;
7574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575 mFinishedStarting.add(mAppToken);
7576 mH.sendEmptyMessage(H.FINISHED_STARTING);
7577 }
7578 mAppToken.updateReportedVisibilityLocked();
7579 }
7580 }
7581 return true;
7582 }
Romain Guy06882f82009-06-10 13:36:04 -07007583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007584 // This must be called while inside a transaction. Returns true if
7585 // there is more animation to run.
7586 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007587 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7591 mHasTransformation = true;
7592 mHasLocalTransformation = true;
7593 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007594 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 TAG, "Starting animation in " + this +
7596 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7597 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7598 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7599 mAnimation.setStartTime(currentTime);
7600 mLocalAnimating = true;
7601 mAnimating = true;
7602 }
7603 mTransformation.clear();
7604 final boolean more = mAnimation.getTransformation(
7605 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007606 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 TAG, "Stepped animation in " + this +
7608 ": more=" + more + ", xform=" + mTransformation);
7609 if (more) {
7610 // we're not done!
7611 return true;
7612 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007613 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 TAG, "Finished animation in " + this +
7615 " @ " + currentTime);
7616 mAnimation = null;
7617 //WindowManagerService.this.dump();
7618 }
7619 mHasLocalTransformation = false;
7620 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007621 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622 // When our app token is animating, we kind-of pretend like
7623 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7624 // part of this check means that we will only do this if
7625 // our window is not currently exiting, or it is not
7626 // locally animating itself. The idea being that one that
7627 // is exiting and doing a local animation should be removed
7628 // once that animation is done.
7629 mAnimating = true;
7630 mHasTransformation = true;
7631 mTransformation.clear();
7632 return false;
7633 } else if (mHasTransformation) {
7634 // Little trick to get through the path below to act like
7635 // we have finished an animation.
7636 mAnimating = true;
7637 } else if (isAnimating()) {
7638 mAnimating = true;
7639 }
7640 } else if (mAnimation != null) {
7641 // If the display is frozen, and there is a pending animation,
7642 // clear it and make sure we run the cleanup code.
7643 mAnimating = true;
7644 mLocalAnimating = true;
7645 mAnimation = null;
7646 }
Romain Guy06882f82009-06-10 13:36:04 -07007647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 if (!mAnimating && !mLocalAnimating) {
7649 return false;
7650 }
7651
Joe Onorato8a9b2202010-02-26 18:56:32 -08007652 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 TAG, "Animation done in " + this + ": exiting=" + mExiting
7654 + ", reportedVisible="
7655 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 mAnimating = false;
7658 mLocalAnimating = false;
7659 mAnimation = null;
7660 mAnimLayer = mLayer;
7661 if (mIsImWindow) {
7662 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007663 } else if (mIsWallpaper) {
7664 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007666 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 + " anim layer: " + mAnimLayer);
7668 mHasTransformation = false;
7669 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007670 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7671 if (DEBUG_VISIBILITY) {
7672 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7673 + mPolicyVisibilityAfterAnim);
7674 }
7675 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7676 if (!mPolicyVisibility) {
7677 if (mCurrentFocus == this) {
7678 mFocusMayChange = true;
7679 }
7680 // Window is no longer visible -- make sure if we were waiting
7681 // for it to be displayed before enabling the display, that
7682 // we allow the display to be enabled now.
7683 enableScreenIfNeededLocked();
7684 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 mTransformation.clear();
7687 if (mHasDrawn
7688 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7689 && mAppToken != null
7690 && mAppToken.firstWindowDrawn
7691 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007692 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 + mToken + ": first real window done animating");
7694 mFinishedStarting.add(mAppToken);
7695 mH.sendEmptyMessage(H.FINISHED_STARTING);
7696 }
Romain Guy06882f82009-06-10 13:36:04 -07007697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 finishExit();
7699
7700 if (mAppToken != null) {
7701 mAppToken.updateReportedVisibilityLocked();
7702 }
7703
7704 return false;
7705 }
7706
7707 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007708 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 TAG, "finishExit in " + this
7710 + ": exiting=" + mExiting
7711 + " remove=" + mRemoveOnExit
7712 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007714 final int N = mChildWindows.size();
7715 for (int i=0; i<N; i++) {
7716 ((WindowState)mChildWindows.get(i)).finishExit();
7717 }
Romain Guy06882f82009-06-10 13:36:04 -07007718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 if (!mExiting) {
7720 return;
7721 }
Romain Guy06882f82009-06-10 13:36:04 -07007722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 if (isWindowAnimating()) {
7724 return;
7725 }
7726
Joe Onorato8a9b2202010-02-26 18:56:32 -08007727 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 TAG, "Exit animation finished in " + this
7729 + ": remove=" + mRemoveOnExit);
7730 if (mSurface != null) {
7731 mDestroySurface.add(this);
7732 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007733 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 try {
7735 mSurface.hide();
7736 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007737 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 }
7739 mLastHidden = true;
7740 mKeyWaiter.releasePendingPointerLocked(mSession);
7741 }
7742 mExiting = false;
7743 if (mRemoveOnExit) {
7744 mPendingRemove.add(this);
7745 mRemoveOnExit = false;
7746 }
7747 }
Romain Guy06882f82009-06-10 13:36:04 -07007748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7750 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7751 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7752 if (dtdx < -.000001f || dtdx > .000001f) return false;
7753 if (dsdy < -.000001f || dsdy > .000001f) return false;
7754 return true;
7755 }
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 void computeShownFrameLocked() {
7758 final boolean selfTransformation = mHasLocalTransformation;
7759 Transformation attachedTransformation =
7760 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7761 ? mAttachedWindow.mTransformation : null;
7762 Transformation appTransformation =
7763 (mAppToken != null && mAppToken.hasTransformation)
7764 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007765
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007766 // Wallpapers are animated based on the "real" window they
7767 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007768 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007769 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007770 if (mWallpaperTarget.mHasLocalTransformation &&
7771 mWallpaperTarget.mAnimation != null &&
7772 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007773 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007774 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007775 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007776 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007777 }
7778 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007779 mWallpaperTarget.mAppToken.hasTransformation &&
7780 mWallpaperTarget.mAppToken.animation != null &&
7781 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007782 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007783 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007784 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007785 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007786 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 if (selfTransformation || attachedTransformation != null
7790 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007791 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 final Rect frame = mFrame;
7793 final float tmpFloats[] = mTmpFloats;
7794 final Matrix tmpMatrix = mTmpMatrix;
7795
7796 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007797 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007799 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007801 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007803 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 }
7805 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007806 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 }
7808
7809 // "convert" it into SurfaceFlinger's format
7810 // (a 2x2 matrix + an offset)
7811 // Here we must not transform the position of the surface
7812 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007813 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 tmpMatrix.getValues(tmpFloats);
7816 mDsDx = tmpFloats[Matrix.MSCALE_X];
7817 mDtDx = tmpFloats[Matrix.MSKEW_X];
7818 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7819 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007820 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7821 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 int w = frame.width();
7823 int h = frame.height();
7824 mShownFrame.set(x, y, x+w, y+h);
7825
7826 // Now set the alpha... but because our current hardware
7827 // can't do alpha transformation on a non-opaque surface,
7828 // turn it off if we are running an animation that is also
7829 // transforming since it is more important to have that
7830 // animation be smooth.
7831 mShownAlpha = mAlpha;
7832 if (!mLimitedAlphaCompositing
7833 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7834 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7835 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007836 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 if (selfTransformation) {
7838 mShownAlpha *= mTransformation.getAlpha();
7839 }
7840 if (attachedTransformation != null) {
7841 mShownAlpha *= attachedTransformation.getAlpha();
7842 }
7843 if (appTransformation != null) {
7844 mShownAlpha *= appTransformation.getAlpha();
7845 }
7846 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007847 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
Romain Guy06882f82009-06-10 13:36:04 -07007849
Joe Onorato8a9b2202010-02-26 18:56:32 -08007850 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 TAG, "Continuing animation in " + this +
7852 ": " + mShownFrame +
7853 ", alpha=" + mTransformation.getAlpha());
7854 return;
7855 }
Romain Guy06882f82009-06-10 13:36:04 -07007856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007858 if (mXOffset != 0 || mYOffset != 0) {
7859 mShownFrame.offset(mXOffset, mYOffset);
7860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 mShownAlpha = mAlpha;
7862 mDsDx = 1;
7863 mDtDx = 0;
7864 mDsDy = 0;
7865 mDtDy = 1;
7866 }
Romain Guy06882f82009-06-10 13:36:04 -07007867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 /**
7869 * Is this window visible? It is not visible if there is no
7870 * surface, or we are in the process of running an exit animation
7871 * that will remove the surface, or its app token has been hidden.
7872 */
7873 public boolean isVisibleLw() {
7874 final AppWindowToken atoken = mAppToken;
7875 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7876 && (atoken == null || !atoken.hiddenRequested)
7877 && !mExiting && !mDestroying;
7878 }
7879
7880 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007881 * Like {@link #isVisibleLw}, but also counts a window that is currently
7882 * "hidden" behind the keyguard as visible. This allows us to apply
7883 * things like window flags that impact the keyguard.
7884 * XXX I am starting to think we need to have ANOTHER visibility flag
7885 * for this "hidden behind keyguard" state rather than overloading
7886 * mPolicyVisibility. Ungh.
7887 */
7888 public boolean isVisibleOrBehindKeyguardLw() {
7889 final AppWindowToken atoken = mAppToken;
7890 return mSurface != null && !mAttachedHidden
7891 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007892 && !mDrawPending && !mCommitDrawPending
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007893 && !mExiting && !mDestroying;
7894 }
7895
7896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 * Is this window visible, ignoring its app token? It is not visible
7898 * if there is no surface, or we are in the process of running an exit animation
7899 * that will remove the surface.
7900 */
7901 public boolean isWinVisibleLw() {
7902 final AppWindowToken atoken = mAppToken;
7903 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7904 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7905 && !mExiting && !mDestroying;
7906 }
7907
7908 /**
7909 * The same as isVisible(), but follows the current hidden state of
7910 * the associated app token, not the pending requested hidden state.
7911 */
7912 boolean isVisibleNow() {
7913 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007914 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 }
7916
7917 /**
7918 * Same as isVisible(), but we also count it as visible between the
7919 * call to IWindowSession.add() and the first relayout().
7920 */
7921 boolean isVisibleOrAdding() {
7922 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007923 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007924 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7925 && mPolicyVisibility && !mAttachedHidden
7926 && (atoken == null || !atoken.hiddenRequested)
7927 && !mExiting && !mDestroying;
7928 }
7929
7930 /**
7931 * Is this window currently on-screen? It is on-screen either if it
7932 * is visible or it is currently running an animation before no longer
7933 * being visible.
7934 */
7935 boolean isOnScreen() {
7936 final AppWindowToken atoken = mAppToken;
7937 if (atoken != null) {
7938 return mSurface != null && mPolicyVisibility && !mDestroying
7939 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007940 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 } else {
7942 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007943 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 }
7945 }
Romain Guy06882f82009-06-10 13:36:04 -07007946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 /**
7948 * Like isOnScreen(), but we don't return true if the window is part
7949 * of a transition that has not yet been started.
7950 */
7951 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007952 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007953 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007954 return false;
7955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007957 final boolean animating = atoken != null
7958 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007960 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
7961 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007962 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007963 }
7964
7965 /** Is the window or its container currently animating? */
7966 boolean isAnimating() {
7967 final WindowState attached = mAttachedWindow;
7968 final AppWindowToken atoken = mAppToken;
7969 return mAnimation != null
7970 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07007971 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 (atoken.animation != null
7973 || atoken.inPendingTransaction));
7974 }
7975
7976 /** Is this window currently animating? */
7977 boolean isWindowAnimating() {
7978 return mAnimation != null;
7979 }
7980
7981 /**
7982 * Like isOnScreen, but returns false if the surface hasn't yet
7983 * been drawn.
7984 */
7985 public boolean isDisplayedLw() {
7986 final AppWindowToken atoken = mAppToken;
7987 return mSurface != null && mPolicyVisibility && !mDestroying
7988 && !mDrawPending && !mCommitDrawPending
7989 && ((!mAttachedHidden &&
7990 (atoken == null || !atoken.hiddenRequested))
7991 || mAnimating);
7992 }
7993
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07007994 /**
7995 * Returns true if the window has a surface that it has drawn a
7996 * complete UI in to.
7997 */
7998 public boolean isDrawnLw() {
7999 final AppWindowToken atoken = mAppToken;
8000 return mSurface != null && !mDestroying
8001 && !mDrawPending && !mCommitDrawPending;
8002 }
8003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8005 boolean shownFrame, boolean onlyOpaque) {
8006 if (mSurface == null) {
8007 return false;
8008 }
8009 if (mAppToken != null && !mAppToken.appFullscreen) {
8010 return false;
8011 }
8012 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8013 return false;
8014 }
8015 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008016
8017 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8018 return frame.left <= mCompatibleScreenFrame.left &&
8019 frame.top <= mCompatibleScreenFrame.top &&
8020 frame.right >= mCompatibleScreenFrame.right &&
8021 frame.bottom >= mCompatibleScreenFrame.bottom;
8022 } else {
8023 return frame.left <= 0 && frame.top <= 0
8024 && frame.right >= screenWidth
8025 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 }
Romain Guy06882f82009-06-10 13:36:04 -07008028
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008029 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008030 * Return true if the window is opaque and fully drawn. This indicates
8031 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008032 */
8033 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008034 return (mAttrs.format == PixelFormat.OPAQUE
8035 || mAttrs.type == TYPE_WALLPAPER)
8036 && mSurface != null && mAnimation == null
8037 && (mAppToken == null || mAppToken.animation == null)
8038 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008039 }
8040
8041 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8042 return
8043 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008044 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8045 // only if it's visible
8046 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008047 // and only if the application fills the compatible screen
8048 mFrame.left <= mCompatibleScreenFrame.left &&
8049 mFrame.top <= mCompatibleScreenFrame.top &&
8050 mFrame.right >= mCompatibleScreenFrame.right &&
8051 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008052 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008053 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008054 }
8055
8056 boolean isFullscreen(int screenWidth, int screenHeight) {
8057 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008058 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 }
8060
8061 void removeLocked() {
8062 if (mAttachedWindow != null) {
8063 mAttachedWindow.mChildWindows.remove(this);
8064 }
8065 destroySurfaceLocked();
8066 mSession.windowRemovedLocked();
8067 try {
8068 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8069 } catch (RuntimeException e) {
8070 // Ignore if it has already been removed (usually because
8071 // we are doing this as part of processing a death note.)
8072 }
8073 }
8074
8075 private class DeathRecipient implements IBinder.DeathRecipient {
8076 public void binderDied() {
8077 try {
8078 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008079 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008080 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 if (win != null) {
8082 removeWindowLocked(mSession, win);
8083 }
8084 }
8085 } catch (IllegalArgumentException ex) {
8086 // This will happen if the window has already been
8087 // removed.
8088 }
8089 }
8090 }
8091
8092 /** Returns true if this window desires key events. */
8093 public final boolean canReceiveKeys() {
8094 return isVisibleOrAdding()
8095 && (mViewVisibility == View.VISIBLE)
8096 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8097 }
8098
8099 public boolean hasDrawnLw() {
8100 return mHasDrawn;
8101 }
8102
8103 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008104 return showLw(doAnimation, true);
8105 }
8106
8107 boolean showLw(boolean doAnimation, boolean requestAnim) {
8108 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8109 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008111 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008112 if (doAnimation) {
8113 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8114 + mPolicyVisibility + " mAnimation=" + mAnimation);
8115 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8116 doAnimation = false;
8117 } else if (mPolicyVisibility && mAnimation == null) {
8118 // Check for the case where we are currently visible and
8119 // not animating; we do not want to do animation at such a
8120 // point to become visible when we already are.
8121 doAnimation = false;
8122 }
8123 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008124 mPolicyVisibility = true;
8125 mPolicyVisibilityAfterAnim = true;
8126 if (doAnimation) {
8127 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8128 }
8129 if (requestAnim) {
8130 requestAnimationLocked(0);
8131 }
8132 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 }
8134
8135 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008136 return hideLw(doAnimation, true);
8137 }
8138
8139 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008140 if (doAnimation) {
8141 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8142 doAnimation = false;
8143 }
8144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8146 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008147 if (!current) {
8148 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008150 if (doAnimation) {
8151 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8152 if (mAnimation == null) {
8153 doAnimation = false;
8154 }
8155 }
8156 if (doAnimation) {
8157 mPolicyVisibilityAfterAnim = false;
8158 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008159 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008160 mPolicyVisibilityAfterAnim = false;
8161 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008162 // Window is no longer visible -- make sure if we were waiting
8163 // for it to be displayed before enabling the display, that
8164 // we allow the display to be enabled now.
8165 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008166 if (mCurrentFocus == this) {
8167 mFocusMayChange = true;
8168 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008169 }
8170 if (requestAnim) {
8171 requestAnimationLocked(0);
8172 }
8173 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 }
8175
8176 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008177 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8178 pw.print(" mClient="); pw.println(mClient.asBinder());
8179 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8180 if (mAttachedWindow != null || mLayoutAttached) {
8181 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8182 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8183 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008184 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8185 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8186 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008187 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8188 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008189 }
8190 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8191 pw.print(" mSubLayer="); pw.print(mSubLayer);
8192 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8193 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8194 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8195 pw.print("="); pw.print(mAnimLayer);
8196 pw.print(" mLastLayer="); pw.println(mLastLayer);
8197 if (mSurface != null) {
8198 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
8199 }
8200 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8201 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8202 if (mAppToken != null) {
8203 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8204 }
8205 if (mTargetAppToken != null) {
8206 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8207 }
8208 pw.print(prefix); pw.print("mViewVisibility=0x");
8209 pw.print(Integer.toHexString(mViewVisibility));
8210 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008211 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8212 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008213 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8214 pw.print(prefix); pw.print("mPolicyVisibility=");
8215 pw.print(mPolicyVisibility);
8216 pw.print(" mPolicyVisibilityAfterAnim=");
8217 pw.print(mPolicyVisibilityAfterAnim);
8218 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8219 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008220 if (!mRelayoutCalled) {
8221 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8222 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008223 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008224 pw.print(" h="); pw.print(mRequestedHeight);
8225 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008226 if (mXOffset != 0 || mYOffset != 0) {
8227 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8228 pw.print(" y="); pw.println(mYOffset);
8229 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008230 pw.print(prefix); pw.print("mGivenContentInsets=");
8231 mGivenContentInsets.printShortString(pw);
8232 pw.print(" mGivenVisibleInsets=");
8233 mGivenVisibleInsets.printShortString(pw);
8234 pw.println();
8235 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8236 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8237 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8238 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008239 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008240 pw.print(prefix); pw.print("mShownFrame=");
8241 mShownFrame.printShortString(pw);
8242 pw.print(" last="); mLastShownFrame.printShortString(pw);
8243 pw.println();
8244 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8245 pw.print(" last="); mLastFrame.printShortString(pw);
8246 pw.println();
8247 pw.print(prefix); pw.print("mContainingFrame=");
8248 mContainingFrame.printShortString(pw);
8249 pw.print(" mDisplayFrame=");
8250 mDisplayFrame.printShortString(pw);
8251 pw.println();
8252 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8253 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8254 pw.println();
8255 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8256 pw.print(" last="); mLastContentInsets.printShortString(pw);
8257 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8258 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8259 pw.println();
8260 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8261 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8262 pw.print(" mAlpha="); pw.print(mAlpha);
8263 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8264 }
8265 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8266 || mAnimation != null) {
8267 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8268 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8269 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8270 pw.print(" mAnimation="); pw.println(mAnimation);
8271 }
8272 if (mHasTransformation || mHasLocalTransformation) {
8273 pw.print(prefix); pw.print("XForm: has=");
8274 pw.print(mHasTransformation);
8275 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8276 pw.print(" "); mTransformation.printShortString(pw);
8277 pw.println();
8278 }
8279 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8280 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8281 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8282 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8283 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8284 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8285 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8286 pw.print(" mDestroying="); pw.print(mDestroying);
8287 pw.print(" mRemoved="); pw.println(mRemoved);
8288 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008289 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008290 pw.print(prefix); pw.print("mOrientationChanging=");
8291 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008292 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8293 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008294 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008295 if (mHScale != 1 || mVScale != 1) {
8296 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8297 pw.print(" mVScale="); pw.println(mVScale);
8298 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008299 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008300 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8301 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8302 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008303 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8304 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8305 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 }
8308
8309 @Override
8310 public String toString() {
8311 return "Window{"
8312 + Integer.toHexString(System.identityHashCode(this))
8313 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8314 }
8315 }
Romain Guy06882f82009-06-10 13:36:04 -07008316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 // -------------------------------------------------------------
8318 // Window Token State
8319 // -------------------------------------------------------------
8320
8321 class WindowToken {
8322 // The actual token.
8323 final IBinder token;
8324
8325 // The type of window this token is for, as per WindowManager.LayoutParams.
8326 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 // Set if this token was explicitly added by a client, so should
8329 // not be removed when all windows are removed.
8330 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008331
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008332 // For printing.
8333 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 // If this is an AppWindowToken, this is non-null.
8336 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 // All of the windows associated with this token.
8339 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8340
8341 // Is key dispatching paused for this token?
8342 boolean paused = false;
8343
8344 // Should this token's windows be hidden?
8345 boolean hidden;
8346
8347 // Temporary for finding which tokens no longer have visible windows.
8348 boolean hasVisible;
8349
Dianne Hackborna8f60182009-09-01 19:01:50 -07008350 // Set to true when this token is in a pending transaction where it
8351 // will be shown.
8352 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008353
Dianne Hackborna8f60182009-09-01 19:01:50 -07008354 // Set to true when this token is in a pending transaction where it
8355 // will be hidden.
8356 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008357
Dianne Hackborna8f60182009-09-01 19:01:50 -07008358 // Set to true when this token is in a pending transaction where its
8359 // windows will be put to the bottom of the list.
8360 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008361
Dianne Hackborna8f60182009-09-01 19:01:50 -07008362 // Set to true when this token is in a pending transaction where its
8363 // windows will be put to the top of the list.
8364 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 WindowToken(IBinder _token, int type, boolean _explicit) {
8367 token = _token;
8368 windowType = type;
8369 explicit = _explicit;
8370 }
8371
8372 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008373 pw.print(prefix); pw.print("token="); pw.println(token);
8374 pw.print(prefix); pw.print("windows="); pw.println(windows);
8375 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8376 pw.print(" hidden="); pw.print(hidden);
8377 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008378 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8379 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8380 pw.print(" waitingToHide="); pw.print(waitingToHide);
8381 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8382 pw.print(" sendingToTop="); pw.println(sendingToTop);
8383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 }
8385
8386 @Override
8387 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008388 if (stringName == null) {
8389 StringBuilder sb = new StringBuilder();
8390 sb.append("WindowToken{");
8391 sb.append(Integer.toHexString(System.identityHashCode(this)));
8392 sb.append(" token="); sb.append(token); sb.append('}');
8393 stringName = sb.toString();
8394 }
8395 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 }
8397 };
8398
8399 class AppWindowToken extends WindowToken {
8400 // Non-null only for application tokens.
8401 final IApplicationToken appToken;
8402
8403 // All of the windows and child windows that are included in this
8404 // application token. Note this list is NOT sorted!
8405 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8406
8407 int groupId = -1;
8408 boolean appFullscreen;
8409 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 // These are used for determining when all windows associated with
8412 // an activity have been drawn, so they can be made visible together
8413 // at the same time.
8414 int lastTransactionSequence = mTransactionSequence-1;
8415 int numInterestingWindows;
8416 int numDrawnWindows;
8417 boolean inPendingTransaction;
8418 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 // Is this token going to be hidden in a little while? If so, it
8421 // won't be taken into account for setting the screen orientation.
8422 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424 // Is this window's surface needed? This is almost like hidden, except
8425 // it will sometimes be true a little earlier: when the token has
8426 // been shown, but is still waiting for its app transition to execute
8427 // before making its windows shown.
8428 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 // Have we told the window clients to hide themselves?
8431 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 // Last visibility state we reported to the app token.
8434 boolean reportedVisible;
8435
8436 // Set to true when the token has been removed from the window mgr.
8437 boolean removed;
8438
8439 // Have we been asked to have this token keep the screen frozen?
8440 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 boolean animating;
8443 Animation animation;
8444 boolean hasTransformation;
8445 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 // Offset to the window of all layers in the token, for use by
8448 // AppWindowToken animations.
8449 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 // Information about an application starting window if displayed.
8452 StartingData startingData;
8453 WindowState startingWindow;
8454 View startingView;
8455 boolean startingDisplayed;
8456 boolean startingMoved;
8457 boolean firstWindowDrawn;
8458
8459 AppWindowToken(IApplicationToken _token) {
8460 super(_token.asBinder(),
8461 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8462 appWindowToken = this;
8463 appToken = _token;
8464 }
Romain Guy06882f82009-06-10 13:36:04 -07008465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008467 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 TAG, "Setting animation in " + this + ": " + anim);
8469 animation = anim;
8470 animating = false;
8471 anim.restrictDuration(MAX_ANIMATION_DURATION);
8472 anim.scaleCurrentDuration(mTransitionAnimationScale);
8473 int zorder = anim.getZAdjustment();
8474 int adj = 0;
8475 if (zorder == Animation.ZORDER_TOP) {
8476 adj = TYPE_LAYER_OFFSET;
8477 } else if (zorder == Animation.ZORDER_BOTTOM) {
8478 adj = -TYPE_LAYER_OFFSET;
8479 }
Romain Guy06882f82009-06-10 13:36:04 -07008480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 if (animLayerAdjustment != adj) {
8482 animLayerAdjustment = adj;
8483 updateLayers();
8484 }
8485 }
Romain Guy06882f82009-06-10 13:36:04 -07008486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 public void setDummyAnimation() {
8488 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008489 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 TAG, "Setting dummy animation in " + this);
8491 animation = sDummyAnimation;
8492 }
8493 }
8494
8495 public void clearAnimation() {
8496 if (animation != null) {
8497 animation = null;
8498 animating = true;
8499 }
8500 }
Romain Guy06882f82009-06-10 13:36:04 -07008501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 void updateLayers() {
8503 final int N = allAppWindows.size();
8504 final int adj = animLayerAdjustment;
8505 for (int i=0; i<N; i++) {
8506 WindowState w = allAppWindows.get(i);
8507 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008508 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 + w.mAnimLayer);
8510 if (w == mInputMethodTarget) {
8511 setInputMethodAnimLayerAdjustment(adj);
8512 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008513 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008514 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 }
8517 }
Romain Guy06882f82009-06-10 13:36:04 -07008518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 void sendAppVisibilityToClients() {
8520 final int N = allAppWindows.size();
8521 for (int i=0; i<N; i++) {
8522 WindowState win = allAppWindows.get(i);
8523 if (win == startingWindow && clientHidden) {
8524 // Don't hide the starting window.
8525 continue;
8526 }
8527 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008528 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008529 "Setting visibility of " + win + ": " + (!clientHidden));
8530 win.mClient.dispatchAppVisibility(!clientHidden);
8531 } catch (RemoteException e) {
8532 }
8533 }
8534 }
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 void showAllWindowsLocked() {
8537 final int NW = allAppWindows.size();
8538 for (int i=0; i<NW; i++) {
8539 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008540 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 "performing show on: " + w);
8542 w.performShowLocked();
8543 }
8544 }
Romain Guy06882f82009-06-10 13:36:04 -07008545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 // This must be called while inside a transaction.
8547 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008548 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 if (animation == sDummyAnimation) {
8552 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008553 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 // when it is really time to animate, this will be set to
8555 // a real animation and the next call will execute normally.
8556 return false;
8557 }
Romain Guy06882f82009-06-10 13:36:04 -07008558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8560 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008561 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 TAG, "Starting animation in " + this +
8563 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8564 + " scale=" + mTransitionAnimationScale
8565 + " allDrawn=" + allDrawn + " animating=" + animating);
8566 animation.initialize(dw, dh, dw, dh);
8567 animation.setStartTime(currentTime);
8568 animating = true;
8569 }
8570 transformation.clear();
8571 final boolean more = animation.getTransformation(
8572 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008573 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 TAG, "Stepped animation in " + this +
8575 ": more=" + more + ", xform=" + transformation);
8576 if (more) {
8577 // we're done!
8578 hasTransformation = true;
8579 return true;
8580 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008581 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 TAG, "Finished animation in " + this +
8583 " @ " + currentTime);
8584 animation = null;
8585 }
8586 } else if (animation != null) {
8587 // If the display is frozen, and there is a pending animation,
8588 // clear it and make sure we run the cleanup code.
8589 animating = true;
8590 animation = null;
8591 }
8592
8593 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008595 if (!animating) {
8596 return false;
8597 }
8598
8599 clearAnimation();
8600 animating = false;
8601 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8602 moveInputMethodWindowsIfNeededLocked(true);
8603 }
Romain Guy06882f82009-06-10 13:36:04 -07008604
Joe Onorato8a9b2202010-02-26 18:56:32 -08008605 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 TAG, "Animation done in " + this
8607 + ": reportedVisible=" + reportedVisible);
8608
8609 transformation.clear();
8610 if (animLayerAdjustment != 0) {
8611 animLayerAdjustment = 0;
8612 updateLayers();
8613 }
Romain Guy06882f82009-06-10 13:36:04 -07008614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 final int N = windows.size();
8616 for (int i=0; i<N; i++) {
8617 ((WindowState)windows.get(i)).finishExit();
8618 }
8619 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 return false;
8622 }
8623
8624 void updateReportedVisibilityLocked() {
8625 if (appToken == null) {
8626 return;
8627 }
Romain Guy06882f82009-06-10 13:36:04 -07008628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 int numInteresting = 0;
8630 int numVisible = 0;
8631 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008632
Joe Onorato8a9b2202010-02-26 18:56:32 -08008633 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 final int N = allAppWindows.size();
8635 for (int i=0; i<N; i++) {
8636 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008637 if (win == startingWindow || win.mAppFreezing
8638 || win.mViewVisibility != View.VISIBLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 continue;
8640 }
8641 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008642 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008643 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008644 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008645 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008646 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 + " pv=" + win.mPolicyVisibility
8648 + " dp=" + win.mDrawPending
8649 + " cdp=" + win.mCommitDrawPending
8650 + " ah=" + win.mAttachedHidden
8651 + " th="
8652 + (win.mAppToken != null
8653 ? win.mAppToken.hiddenRequested : false)
8654 + " a=" + win.mAnimating);
8655 }
8656 }
8657 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008658 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 if (!win.isAnimating()) {
8660 numVisible++;
8661 }
8662 nowGone = false;
8663 } else if (win.isAnimating()) {
8664 nowGone = false;
8665 }
8666 }
Romain Guy06882f82009-06-10 13:36:04 -07008667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008669 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 + numInteresting + " visible=" + numVisible);
8671 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008672 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 TAG, "Visibility changed in " + this
8674 + ": vis=" + nowVisible);
8675 reportedVisible = nowVisible;
8676 Message m = mH.obtainMessage(
8677 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8678 nowVisible ? 1 : 0,
8679 nowGone ? 1 : 0,
8680 this);
8681 mH.sendMessage(m);
8682 }
8683 }
Romain Guy06882f82009-06-10 13:36:04 -07008684
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008685 WindowState findMainWindow() {
8686 int j = windows.size();
8687 while (j > 0) {
8688 j--;
8689 WindowState win = windows.get(j);
8690 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8691 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8692 return win;
8693 }
8694 }
8695 return null;
8696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 void dump(PrintWriter pw, String prefix) {
8699 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008700 if (appToken != null) {
8701 pw.print(prefix); pw.println("app=true");
8702 }
8703 if (allAppWindows.size() > 0) {
8704 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8705 }
8706 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008707 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008708 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8709 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8710 pw.print(" clientHidden="); pw.print(clientHidden);
8711 pw.print(" willBeHidden="); pw.print(willBeHidden);
8712 pw.print(" reportedVisible="); pw.println(reportedVisible);
8713 if (paused || freezingScreen) {
8714 pw.print(prefix); pw.print("paused="); pw.print(paused);
8715 pw.print(" freezingScreen="); pw.println(freezingScreen);
8716 }
8717 if (numInterestingWindows != 0 || numDrawnWindows != 0
8718 || inPendingTransaction || allDrawn) {
8719 pw.print(prefix); pw.print("numInterestingWindows=");
8720 pw.print(numInterestingWindows);
8721 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8722 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8723 pw.print(" allDrawn="); pw.println(allDrawn);
8724 }
8725 if (animating || animation != null) {
8726 pw.print(prefix); pw.print("animating="); pw.print(animating);
8727 pw.print(" animation="); pw.println(animation);
8728 }
8729 if (animLayerAdjustment != 0) {
8730 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8731 }
8732 if (hasTransformation) {
8733 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8734 pw.print(" transformation="); transformation.printShortString(pw);
8735 pw.println();
8736 }
8737 if (startingData != null || removed || firstWindowDrawn) {
8738 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8739 pw.print(" removed="); pw.print(removed);
8740 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8741 }
8742 if (startingWindow != null || startingView != null
8743 || startingDisplayed || startingMoved) {
8744 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8745 pw.print(" startingView="); pw.print(startingView);
8746 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8747 pw.print(" startingMoved"); pw.println(startingMoved);
8748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 }
8750
8751 @Override
8752 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008753 if (stringName == null) {
8754 StringBuilder sb = new StringBuilder();
8755 sb.append("AppWindowToken{");
8756 sb.append(Integer.toHexString(System.identityHashCode(this)));
8757 sb.append(" token="); sb.append(token); sb.append('}');
8758 stringName = sb.toString();
8759 }
8760 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008761 }
8762 }
Romain Guy06882f82009-06-10 13:36:04 -07008763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 // -------------------------------------------------------------
8765 // DummyAnimation
8766 // -------------------------------------------------------------
8767
8768 // This is an animation that does nothing: it just immediately finishes
8769 // itself every time it is called. It is used as a stub animation in cases
8770 // where we want to synchronize multiple things that may be animating.
8771 static final class DummyAnimation extends Animation {
8772 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8773 return false;
8774 }
8775 }
8776 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 // -------------------------------------------------------------
8779 // Async Handler
8780 // -------------------------------------------------------------
8781
8782 static final class StartingData {
8783 final String pkg;
8784 final int theme;
8785 final CharSequence nonLocalizedLabel;
8786 final int labelRes;
8787 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8790 int _labelRes, int _icon) {
8791 pkg = _pkg;
8792 theme = _theme;
8793 nonLocalizedLabel = _nonLocalizedLabel;
8794 labelRes = _labelRes;
8795 icon = _icon;
8796 }
8797 }
8798
8799 private final class H extends Handler {
8800 public static final int REPORT_FOCUS_CHANGE = 2;
8801 public static final int REPORT_LOSING_FOCUS = 3;
8802 public static final int ANIMATE = 4;
8803 public static final int ADD_STARTING = 5;
8804 public static final int REMOVE_STARTING = 6;
8805 public static final int FINISHED_STARTING = 7;
8806 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8808 public static final int HOLD_SCREEN_CHANGED = 12;
8809 public static final int APP_TRANSITION_TIMEOUT = 13;
8810 public static final int PERSIST_ANIMATION_SCALE = 14;
8811 public static final int FORCE_GC = 15;
8812 public static final int ENABLE_SCREEN = 16;
8813 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008814 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008816 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 public H() {
8819 }
Romain Guy06882f82009-06-10 13:36:04 -07008820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 @Override
8822 public void handleMessage(Message msg) {
8823 switch (msg.what) {
8824 case REPORT_FOCUS_CHANGE: {
8825 WindowState lastFocus;
8826 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 synchronized(mWindowMap) {
8829 lastFocus = mLastFocus;
8830 newFocus = mCurrentFocus;
8831 if (lastFocus == newFocus) {
8832 // Focus is not changing, so nothing to do.
8833 return;
8834 }
8835 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008836 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008837 // + " to " + newFocus);
8838 if (newFocus != null && lastFocus != null
8839 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008840 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008841 mLosingFocus.add(lastFocus);
8842 lastFocus = null;
8843 }
8844 }
8845
8846 if (lastFocus != newFocus) {
8847 //System.out.println("Changing focus from " + lastFocus
8848 // + " to " + newFocus);
8849 if (newFocus != null) {
8850 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008851 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8853 } catch (RemoteException e) {
8854 // Ignore if process has died.
8855 }
8856 }
8857
8858 if (lastFocus != null) {
8859 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008860 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8862 } catch (RemoteException e) {
8863 // Ignore if process has died.
8864 }
8865 }
8866 }
8867 } break;
8868
8869 case REPORT_LOSING_FOCUS: {
8870 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 synchronized(mWindowMap) {
8873 losers = mLosingFocus;
8874 mLosingFocus = new ArrayList<WindowState>();
8875 }
8876
8877 final int N = losers.size();
8878 for (int i=0; i<N; i++) {
8879 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008880 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8882 } catch (RemoteException e) {
8883 // Ignore if process has died.
8884 }
8885 }
8886 } break;
8887
8888 case ANIMATE: {
8889 synchronized(mWindowMap) {
8890 mAnimationPending = false;
8891 performLayoutAndPlaceSurfacesLocked();
8892 }
8893 } break;
8894
8895 case ADD_STARTING: {
8896 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8897 final StartingData sd = wtoken.startingData;
8898
8899 if (sd == null) {
8900 // Animation has been canceled... do nothing.
8901 return;
8902 }
Romain Guy06882f82009-06-10 13:36:04 -07008903
Joe Onorato8a9b2202010-02-26 18:56:32 -08008904 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008907 View view = null;
8908 try {
8909 view = mPolicy.addStartingWindow(
8910 wtoken.token, sd.pkg,
8911 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8912 sd.icon);
8913 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008914 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 }
8916
8917 if (view != null) {
8918 boolean abort = false;
8919
8920 synchronized(mWindowMap) {
8921 if (wtoken.removed || wtoken.startingData == null) {
8922 // If the window was successfully added, then
8923 // we need to remove it.
8924 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008925 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 "Aborted starting " + wtoken
8927 + ": removed=" + wtoken.removed
8928 + " startingData=" + wtoken.startingData);
8929 wtoken.startingWindow = null;
8930 wtoken.startingData = null;
8931 abort = true;
8932 }
8933 } else {
8934 wtoken.startingView = view;
8935 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008936 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008937 "Added starting " + wtoken
8938 + ": startingWindow="
8939 + wtoken.startingWindow + " startingView="
8940 + wtoken.startingView);
8941 }
8942
8943 if (abort) {
8944 try {
8945 mPolicy.removeStartingWindow(wtoken.token, view);
8946 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008947 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 }
8949 }
8950 }
8951 } break;
8952
8953 case REMOVE_STARTING: {
8954 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8955 IBinder token = null;
8956 View view = null;
8957 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008958 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 + wtoken + ": startingWindow="
8960 + wtoken.startingWindow + " startingView="
8961 + wtoken.startingView);
8962 if (wtoken.startingWindow != null) {
8963 view = wtoken.startingView;
8964 token = wtoken.token;
8965 wtoken.startingData = null;
8966 wtoken.startingView = null;
8967 wtoken.startingWindow = null;
8968 }
8969 }
8970 if (view != null) {
8971 try {
8972 mPolicy.removeStartingWindow(token, view);
8973 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008974 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 }
8976 }
8977 } break;
8978
8979 case FINISHED_STARTING: {
8980 IBinder token = null;
8981 View view = null;
8982 while (true) {
8983 synchronized (mWindowMap) {
8984 final int N = mFinishedStarting.size();
8985 if (N <= 0) {
8986 break;
8987 }
8988 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
8989
Joe Onorato8a9b2202010-02-26 18:56:32 -08008990 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 "Finished starting " + wtoken
8992 + ": startingWindow=" + wtoken.startingWindow
8993 + " startingView=" + wtoken.startingView);
8994
8995 if (wtoken.startingWindow == null) {
8996 continue;
8997 }
8998
8999 view = wtoken.startingView;
9000 token = wtoken.token;
9001 wtoken.startingData = null;
9002 wtoken.startingView = null;
9003 wtoken.startingWindow = null;
9004 }
9005
9006 try {
9007 mPolicy.removeStartingWindow(token, view);
9008 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009009 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009010 }
9011 }
9012 } break;
9013
9014 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9015 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9016
9017 boolean nowVisible = msg.arg1 != 0;
9018 boolean nowGone = msg.arg2 != 0;
9019
9020 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009021 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009022 TAG, "Reporting visible in " + wtoken
9023 + " visible=" + nowVisible
9024 + " gone=" + nowGone);
9025 if (nowVisible) {
9026 wtoken.appToken.windowsVisible();
9027 } else {
9028 wtoken.appToken.windowsGone();
9029 }
9030 } catch (RemoteException ex) {
9031 }
9032 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 case WINDOW_FREEZE_TIMEOUT: {
9035 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009036 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 int i = mWindows.size();
9038 while (i > 0) {
9039 i--;
9040 WindowState w = (WindowState)mWindows.get(i);
9041 if (w.mOrientationChanging) {
9042 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009043 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 }
9045 }
9046 performLayoutAndPlaceSurfacesLocked();
9047 }
9048 break;
9049 }
Romain Guy06882f82009-06-10 13:36:04 -07009050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009051 case HOLD_SCREEN_CHANGED: {
9052 Session oldHold;
9053 Session newHold;
9054 synchronized (mWindowMap) {
9055 oldHold = mLastReportedHold;
9056 newHold = (Session)msg.obj;
9057 mLastReportedHold = newHold;
9058 }
Romain Guy06882f82009-06-10 13:36:04 -07009059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 if (oldHold != newHold) {
9061 try {
9062 if (oldHold != null) {
9063 mBatteryStats.noteStopWakelock(oldHold.mUid,
9064 "window",
9065 BatteryStats.WAKE_TYPE_WINDOW);
9066 }
9067 if (newHold != null) {
9068 mBatteryStats.noteStartWakelock(newHold.mUid,
9069 "window",
9070 BatteryStats.WAKE_TYPE_WINDOW);
9071 }
9072 } catch (RemoteException e) {
9073 }
9074 }
9075 break;
9076 }
Romain Guy06882f82009-06-10 13:36:04 -07009077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 case APP_TRANSITION_TIMEOUT: {
9079 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009080 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009081 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 "*** APP TRANSITION TIMEOUT");
9083 mAppTransitionReady = true;
9084 mAppTransitionTimeout = true;
9085 performLayoutAndPlaceSurfacesLocked();
9086 }
9087 }
9088 break;
9089 }
Romain Guy06882f82009-06-10 13:36:04 -07009090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 case PERSIST_ANIMATION_SCALE: {
9092 Settings.System.putFloat(mContext.getContentResolver(),
9093 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9094 Settings.System.putFloat(mContext.getContentResolver(),
9095 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9096 break;
9097 }
Romain Guy06882f82009-06-10 13:36:04 -07009098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 case FORCE_GC: {
9100 synchronized(mWindowMap) {
9101 if (mAnimationPending) {
9102 // If we are animating, don't do the gc now but
9103 // delay a bit so we don't interrupt the animation.
9104 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9105 2000);
9106 return;
9107 }
9108 // If we are currently rotating the display, it will
9109 // schedule a new message when done.
9110 if (mDisplayFrozen) {
9111 return;
9112 }
9113 mFreezeGcPending = 0;
9114 }
9115 Runtime.getRuntime().gc();
9116 break;
9117 }
Romain Guy06882f82009-06-10 13:36:04 -07009118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 case ENABLE_SCREEN: {
9120 performEnableScreen();
9121 break;
9122 }
Romain Guy06882f82009-06-10 13:36:04 -07009123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 case APP_FREEZE_TIMEOUT: {
9125 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009126 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127 int i = mAppTokens.size();
9128 while (i > 0) {
9129 i--;
9130 AppWindowToken tok = mAppTokens.get(i);
9131 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009132 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 unsetAppFreezingScreenLocked(tok, true, true);
9134 }
9135 }
9136 }
9137 break;
9138 }
Romain Guy06882f82009-06-10 13:36:04 -07009139
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009140 case SEND_NEW_CONFIGURATION: {
9141 removeMessages(SEND_NEW_CONFIGURATION);
9142 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009143 break;
9144 }
Romain Guy06882f82009-06-10 13:36:04 -07009145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 }
9147 }
9148 }
9149
9150 // -------------------------------------------------------------
9151 // IWindowManager API
9152 // -------------------------------------------------------------
9153
9154 public IWindowSession openSession(IInputMethodClient client,
9155 IInputContext inputContext) {
9156 if (client == null) throw new IllegalArgumentException("null client");
9157 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9158 return new Session(client, inputContext);
9159 }
9160
9161 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9162 synchronized (mWindowMap) {
9163 // The focus for the client is the window immediately below
9164 // where we would place the input method window.
9165 int idx = findDesiredInputMethodWindowIndexLocked(false);
9166 WindowState imFocus;
9167 if (idx > 0) {
9168 imFocus = (WindowState)mWindows.get(idx-1);
9169 if (imFocus != null) {
9170 if (imFocus.mSession.mClient != null &&
9171 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9172 return true;
9173 }
9174 }
9175 }
9176 }
9177 return false;
9178 }
Romain Guy06882f82009-06-10 13:36:04 -07009179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 // -------------------------------------------------------------
9181 // Internals
9182 // -------------------------------------------------------------
9183
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009184 final WindowState windowForClientLocked(Session session, IWindow client,
9185 boolean throwOnError) {
9186 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 }
Romain Guy06882f82009-06-10 13:36:04 -07009188
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009189 final WindowState windowForClientLocked(Session session, IBinder client,
9190 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009192 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009193 TAG, "Looking up client " + client + ": " + win);
9194 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009195 RuntimeException ex = new IllegalArgumentException(
9196 "Requested window " + client + " does not exist");
9197 if (throwOnError) {
9198 throw ex;
9199 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009200 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 return null;
9202 }
9203 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009204 RuntimeException ex = new IllegalArgumentException(
9205 "Requested window " + client + " is in session " +
9206 win.mSession + ", not " + session);
9207 if (throwOnError) {
9208 throw ex;
9209 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009210 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009211 return null;
9212 }
9213
9214 return win;
9215 }
9216
Dianne Hackborna8f60182009-09-01 19:01:50 -07009217 final void rebuildAppWindowListLocked() {
9218 int NW = mWindows.size();
9219 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009220 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009221 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009222
Dianne Hackborna8f60182009-09-01 19:01:50 -07009223 // First remove all existing app windows.
9224 i=0;
9225 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009226 WindowState w = (WindowState)mWindows.get(i);
9227 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009228 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009229 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009230 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009231 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009232 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009233 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009234 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9235 && lastWallpaper == i-1) {
9236 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009237 }
9238 i++;
9239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009240
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009241 // The wallpaper window(s) typically live at the bottom of the stack,
9242 // so skip them before adding app tokens.
9243 lastWallpaper++;
9244 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009245
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009246 // First add all of the exiting app tokens... these are no longer
9247 // in the main app list, but still have windows shown. We put them
9248 // in the back because now that the animation is over we no longer
9249 // will care about them.
9250 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009251 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009252 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009254
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009255 // And add in the still active app tokens in Z order.
9256 NT = mAppTokens.size();
9257 for (int j=0; j<NT; j++) {
9258 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009260
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009261 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009262 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009263 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009264 + " windows but added " + i);
9265 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 private final void assignLayersLocked() {
9269 int N = mWindows.size();
9270 int curBaseLayer = 0;
9271 int curLayer = 0;
9272 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009274 for (i=0; i<N; i++) {
9275 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009276 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9277 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 curLayer += WINDOW_LAYER_MULTIPLIER;
9279 w.mLayer = curLayer;
9280 } else {
9281 curBaseLayer = curLayer = w.mBaseLayer;
9282 w.mLayer = curLayer;
9283 }
9284 if (w.mTargetAppToken != null) {
9285 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9286 } else if (w.mAppToken != null) {
9287 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9288 } else {
9289 w.mAnimLayer = w.mLayer;
9290 }
9291 if (w.mIsImWindow) {
9292 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009293 } else if (w.mIsWallpaper) {
9294 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009296 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 + w.mAnimLayer);
9298 //System.out.println(
9299 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9300 }
9301 }
9302
9303 private boolean mInLayout = false;
9304 private final void performLayoutAndPlaceSurfacesLocked() {
9305 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009306 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 throw new RuntimeException("Recursive call!");
9308 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009309 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009310 return;
9311 }
9312
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009313 if (mWaitingForConfig) {
9314 // Our configuration has changed (most likely rotation), but we
9315 // don't yet have the complete configuration to report to
9316 // applications. Don't do any window layout until we have it.
9317 return;
9318 }
9319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 boolean recoveringMemory = false;
9321 if (mForceRemoves != null) {
9322 recoveringMemory = true;
9323 // Wait a little it for things to settle down, and off we go.
9324 for (int i=0; i<mForceRemoves.size(); i++) {
9325 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009326 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 removeWindowInnerLocked(ws.mSession, ws);
9328 }
9329 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009330 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 Object tmp = new Object();
9332 synchronized (tmp) {
9333 try {
9334 tmp.wait(250);
9335 } catch (InterruptedException e) {
9336 }
9337 }
9338 }
Romain Guy06882f82009-06-10 13:36:04 -07009339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 mInLayout = true;
9341 try {
9342 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 int i = mPendingRemove.size()-1;
9345 if (i >= 0) {
9346 while (i >= 0) {
9347 WindowState w = mPendingRemove.get(i);
9348 removeWindowInnerLocked(w.mSession, w);
9349 i--;
9350 }
9351 mPendingRemove.clear();
9352
9353 mInLayout = false;
9354 assignLayersLocked();
9355 mLayoutNeeded = true;
9356 performLayoutAndPlaceSurfacesLocked();
9357
9358 } else {
9359 mInLayout = false;
9360 if (mLayoutNeeded) {
9361 requestAnimationLocked(0);
9362 }
9363 }
9364 } catch (RuntimeException e) {
9365 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009366 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 }
9368 }
9369
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009370 private final int performLayoutLockedInner() {
9371 if (!mLayoutNeeded) {
9372 return 0;
9373 }
9374
9375 mLayoutNeeded = false;
9376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 final int dw = mDisplay.getWidth();
9378 final int dh = mDisplay.getHeight();
9379
9380 final int N = mWindows.size();
9381 int i;
9382
Joe Onorato8a9b2202010-02-26 18:56:32 -08009383 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009384 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9385
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009386 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009387
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009388 int seq = mLayoutSeq+1;
9389 if (seq < 0) seq = 0;
9390 mLayoutSeq = seq;
9391
9392 // First perform layout of any root windows (not attached
9393 // to another window).
9394 int topAttached = -1;
9395 for (i = N-1; i >= 0; i--) {
9396 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009398 // Don't do layout of a window if it is not visible, or
9399 // soon won't be visible, to avoid wasting time and funky
9400 // changes while a window is animating away.
9401 final AppWindowToken atoken = win.mAppToken;
9402 final boolean gone = win.mViewVisibility == View.GONE
9403 || !win.mRelayoutCalled
9404 || win.mRootToken.hidden
9405 || (atoken != null && atoken.hiddenRequested)
9406 || win.mAttachedHidden
9407 || win.mExiting || win.mDestroying;
9408
9409 if (!win.mLayoutAttached) {
9410 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9411 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9412 + " mLayoutAttached=" + win.mLayoutAttached);
9413 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9414 + win.mViewVisibility + " mRelayoutCalled="
9415 + win.mRelayoutCalled + " hidden="
9416 + win.mRootToken.hidden + " hiddenRequested="
9417 + (atoken != null && atoken.hiddenRequested)
9418 + " mAttachedHidden=" + win.mAttachedHidden);
9419 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009420
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009421 // If this view is GONE, then skip it -- keep the current
9422 // frame, and let the caller know so they can ignore it
9423 // if they want. (We do the normal layout for INVISIBLE
9424 // windows, since that means "perform layout as normal,
9425 // just don't display").
9426 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009427 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009428 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9429 win.mLayoutSeq = seq;
9430 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9431 + win.mFrame + " mContainingFrame="
9432 + win.mContainingFrame + " mDisplayFrame="
9433 + win.mDisplayFrame);
9434 } else {
9435 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009436 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009438 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009439
9440 // Now perform layout of attached windows, which usually
9441 // depend on the position of the window they are attached to.
9442 // XXX does not deal with windows that are attached to windows
9443 // that are themselves attached.
9444 for (i = topAttached; i >= 0; i--) {
9445 WindowState win = (WindowState) mWindows.get(i);
9446
9447 // If this view is GONE, then skip it -- keep the current
9448 // frame, and let the caller know so they can ignore it
9449 // if they want. (We do the normal layout for INVISIBLE
9450 // windows, since that means "perform layout as normal,
9451 // just don't display").
9452 if (win.mLayoutAttached) {
9453 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9454 + " mHaveFrame=" + win.mHaveFrame
9455 + " mViewVisibility=" + win.mViewVisibility
9456 + " mRelayoutCalled=" + win.mRelayoutCalled);
9457 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9458 || !win.mHaveFrame) {
9459 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9460 win.mLayoutSeq = seq;
9461 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9462 + win.mFrame + " mContainingFrame="
9463 + win.mContainingFrame + " mDisplayFrame="
9464 + win.mDisplayFrame);
9465 }
9466 }
9467 }
9468
9469 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 }
Romain Guy06882f82009-06-10 13:36:04 -07009471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 private final void performLayoutAndPlaceSurfacesLockedInner(
9473 boolean recoveringMemory) {
9474 final long currentTime = SystemClock.uptimeMillis();
9475 final int dw = mDisplay.getWidth();
9476 final int dh = mDisplay.getHeight();
9477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009478 int i;
9479
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009480 if (mFocusMayChange) {
9481 mFocusMayChange = false;
9482 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9483 }
9484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009485 if (mFxSession == null) {
9486 mFxSession = new SurfaceSession();
9487 }
Romain Guy06882f82009-06-10 13:36:04 -07009488
Joe Onorato8a9b2202010-02-26 18:56:32 -08009489 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490
9491 // Initialize state of exiting tokens.
9492 for (i=mExitingTokens.size()-1; i>=0; i--) {
9493 mExitingTokens.get(i).hasVisible = false;
9494 }
9495
9496 // Initialize state of exiting applications.
9497 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9498 mExitingAppTokens.get(i).hasVisible = false;
9499 }
9500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 boolean orientationChangeComplete = true;
9502 Session holdScreen = null;
9503 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009504 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009505 boolean focusDisplayed = false;
9506 boolean animating = false;
9507
9508 Surface.openTransaction();
9509 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009510 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009511 int repeats = 0;
9512 int changes = 0;
9513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009515 repeats++;
9516 if (repeats > 6) {
9517 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9518 mLayoutNeeded = false;
9519 break;
9520 }
9521
9522 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9523 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9524 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9525 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9526 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9527 assignLayersLocked();
9528 mLayoutNeeded = true;
9529 }
9530 }
9531 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9532 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9533 if (updateOrientationFromAppTokensLocked()) {
9534 mLayoutNeeded = true;
9535 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9536 }
9537 }
9538 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9539 mLayoutNeeded = true;
9540 }
9541 }
9542
9543 // FIRST LOOP: Perform a layout, if needed.
9544 if (repeats < 4) {
9545 changes = performLayoutLockedInner();
9546 if (changes != 0) {
9547 continue;
9548 }
9549 } else {
9550 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9551 changes = 0;
9552 }
9553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009554 final int transactionSequence = ++mTransactionSequence;
9555
9556 // Update animations of all applications, including those
9557 // associated with exiting/removed apps
9558 boolean tokensAnimating = false;
9559 final int NAT = mAppTokens.size();
9560 for (i=0; i<NAT; i++) {
9561 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9562 tokensAnimating = true;
9563 }
9564 }
9565 final int NEAT = mExitingAppTokens.size();
9566 for (i=0; i<NEAT; i++) {
9567 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9568 tokensAnimating = true;
9569 }
9570 }
9571
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009572 // SECOND LOOP: Execute animations and update visibility of windows.
9573
Joe Onorato8a9b2202010-02-26 18:56:32 -08009574 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009575 + transactionSequence + " tokensAnimating="
9576 + tokensAnimating);
9577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579
9580 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009581 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009582 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009583
9584 mPolicy.beginAnimationLw(dw, dh);
9585
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009586 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 for (i=N-1; i>=0; i--) {
9589 WindowState w = (WindowState)mWindows.get(i);
9590
9591 final WindowManager.LayoutParams attrs = w.mAttrs;
9592
9593 if (w.mSurface != null) {
9594 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009595 if (w.commitFinishDrawingLocked(currentTime)) {
9596 if ((w.mAttrs.flags
9597 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009598 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009599 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009600 wallpaperMayChange = true;
9601 }
9602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009603
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009604 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9606 animating = true;
9607 //w.dump(" ");
9608 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009609 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9610 wallpaperMayChange = true;
9611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009612
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009613 if (mPolicy.doesForceHide(w, attrs)) {
9614 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009615 if (DEBUG_VISIBILITY) Slog.v(TAG,
9616 "Animation done that could impact force hide: "
9617 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009618 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009619 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009620 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9621 forceHiding = true;
9622 }
9623 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9624 boolean changed;
9625 if (forceHiding) {
9626 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009627 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9628 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009629 } else {
9630 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009631 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9632 "Now policy shown: " + w);
9633 if (changed) {
9634 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009635 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009636 // Assume we will need to animate. If
9637 // we don't (because the wallpaper will
9638 // stay with the lock screen), then we will
9639 // clean up later.
9640 Animation a = mPolicy.createForceHideEnterAnimation();
9641 if (a != null) {
9642 w.setAnimation(a);
9643 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009644 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009645 if (mCurrentFocus == null ||
9646 mCurrentFocus.mLayer < w.mLayer) {
9647 // We are showing on to of the current
9648 // focus, so re-evaluate focus to make
9649 // sure it is correct.
9650 mFocusMayChange = true;
9651 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009652 }
9653 }
9654 if (changed && (attrs.flags
9655 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9656 wallpaperMayChange = true;
9657 }
9658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 mPolicy.animatingWindowLw(w, attrs);
9661 }
9662
9663 final AppWindowToken atoken = w.mAppToken;
9664 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9665 if (atoken.lastTransactionSequence != transactionSequence) {
9666 atoken.lastTransactionSequence = transactionSequence;
9667 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9668 atoken.startingDisplayed = false;
9669 }
9670 if ((w.isOnScreen() || w.mAttrs.type
9671 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9672 && !w.mExiting && !w.mDestroying) {
9673 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009674 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009675 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009677 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009678 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 + " pv=" + w.mPolicyVisibility
9680 + " dp=" + w.mDrawPending
9681 + " cdp=" + w.mCommitDrawPending
9682 + " ah=" + w.mAttachedHidden
9683 + " th=" + atoken.hiddenRequested
9684 + " a=" + w.mAnimating);
9685 }
9686 }
9687 if (w != atoken.startingWindow) {
9688 if (!atoken.freezingScreen || !w.mAppFreezing) {
9689 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009690 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009691 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009692 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009693 "tokenMayBeDrawn: " + atoken
9694 + " freezingScreen=" + atoken.freezingScreen
9695 + " mAppFreezing=" + w.mAppFreezing);
9696 tokenMayBeDrawn = true;
9697 }
9698 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009699 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 atoken.startingDisplayed = true;
9701 }
9702 }
9703 } else if (w.mReadyToShow) {
9704 w.performShowLocked();
9705 }
9706 }
9707
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009708 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709
9710 if (tokenMayBeDrawn) {
9711 // See if any windows have been drawn, so they (and others
9712 // associated with them) can now be shown.
9713 final int NT = mTokenList.size();
9714 for (i=0; i<NT; i++) {
9715 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9716 if (wtoken == null) {
9717 continue;
9718 }
9719 if (wtoken.freezingScreen) {
9720 int numInteresting = wtoken.numInterestingWindows;
9721 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009722 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 "allDrawn: " + wtoken
9724 + " interesting=" + numInteresting
9725 + " drawn=" + wtoken.numDrawnWindows);
9726 wtoken.showAllWindowsLocked();
9727 unsetAppFreezingScreenLocked(wtoken, false, true);
9728 orientationChangeComplete = true;
9729 }
9730 } else if (!wtoken.allDrawn) {
9731 int numInteresting = wtoken.numInterestingWindows;
9732 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009733 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009734 "allDrawn: " + wtoken
9735 + " interesting=" + numInteresting
9736 + " drawn=" + wtoken.numDrawnWindows);
9737 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009738 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739
9740 // We can now show all of the drawn windows!
9741 if (!mOpeningApps.contains(wtoken)) {
9742 wtoken.showAllWindowsLocked();
9743 }
9744 }
9745 }
9746 }
9747 }
9748
9749 // If we are ready to perform an app transition, check through
9750 // all of the app tokens to be shown and see if they are ready
9751 // to go.
9752 if (mAppTransitionReady) {
9753 int NN = mOpeningApps.size();
9754 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009755 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 "Checking " + NN + " opening apps (frozen="
9757 + mDisplayFrozen + " timeout="
9758 + mAppTransitionTimeout + ")...");
9759 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9760 // If the display isn't frozen, wait to do anything until
9761 // all of the apps are ready. Otherwise just go because
9762 // we'll unfreeze the display when everyone is ready.
9763 for (i=0; i<NN && goodToGo; i++) {
9764 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009765 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 "Check opening app" + wtoken + ": allDrawn="
9767 + wtoken.allDrawn + " startingDisplayed="
9768 + wtoken.startingDisplayed);
9769 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9770 && !wtoken.startingMoved) {
9771 goodToGo = false;
9772 }
9773 }
9774 }
9775 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009776 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 int transit = mNextAppTransition;
9778 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009779 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009781 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009782 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009783 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 mAppTransitionTimeout = false;
9785 mStartingIconInTransition = false;
9786 mSkipAppTransitionAnimation = false;
9787
9788 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9789
Dianne Hackborna8f60182009-09-01 19:01:50 -07009790 // If there are applications waiting to come to the
9791 // top of the stack, now is the time to move their windows.
9792 // (Note that we don't do apps going to the bottom
9793 // here -- we want to keep their windows in the old
9794 // Z-order until the animation completes.)
9795 if (mToTopApps.size() > 0) {
9796 NN = mAppTokens.size();
9797 for (i=0; i<NN; i++) {
9798 AppWindowToken wtoken = mAppTokens.get(i);
9799 if (wtoken.sendingToTop) {
9800 wtoken.sendingToTop = false;
9801 moveAppWindowsLocked(wtoken, NN, false);
9802 }
9803 }
9804 mToTopApps.clear();
9805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009806
Dianne Hackborn25994b42009-09-04 14:21:19 -07009807 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009808
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009809 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009810 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009811
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009812 // The top-most window will supply the layout params,
9813 // and we will determine it below.
9814 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009815 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009816 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009817
Joe Onorato8a9b2202010-02-26 18:56:32 -08009818 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009819 "New wallpaper target=" + mWallpaperTarget
9820 + ", lower target=" + mLowerWallpaperTarget
9821 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009822 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009823 // Do a first pass through the tokens for two
9824 // things:
9825 // (1) Determine if both the closing and opening
9826 // app token sets are wallpaper targets, in which
9827 // case special animations are needed
9828 // (since the wallpaper needs to stay static
9829 // behind them).
9830 // (2) Find the layout params of the top-most
9831 // application window in the tokens, which is
9832 // what will control the animation theme.
9833 final int NC = mClosingApps.size();
9834 NN = NC + mOpeningApps.size();
9835 for (i=0; i<NN; i++) {
9836 AppWindowToken wtoken;
9837 int mode;
9838 if (i < NC) {
9839 wtoken = mClosingApps.get(i);
9840 mode = 1;
9841 } else {
9842 wtoken = mOpeningApps.get(i-NC);
9843 mode = 2;
9844 }
9845 if (mLowerWallpaperTarget != null) {
9846 if (mLowerWallpaperTarget.mAppToken == wtoken
9847 || mUpperWallpaperTarget.mAppToken == wtoken) {
9848 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009849 }
9850 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009851 if (wtoken.appFullscreen) {
9852 WindowState ws = wtoken.findMainWindow();
9853 if (ws != null) {
9854 // If this is a compatibility mode
9855 // window, we will always use its anim.
9856 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9857 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009858 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009859 bestAnimLayer = Integer.MAX_VALUE;
9860 } else if (ws.mLayer > bestAnimLayer) {
9861 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009862 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009863 bestAnimLayer = ws.mLayer;
9864 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009865 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009866 }
9867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009868
Dianne Hackborn25994b42009-09-04 14:21:19 -07009869 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009870 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009871 "Wallpaper animation!");
9872 switch (transit) {
9873 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9874 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9875 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9876 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9877 break;
9878 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9879 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9880 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9881 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9882 break;
9883 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009884 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009885 "New transit: " + transit);
9886 } else if (oldWallpaper != null) {
9887 // We are transitioning from an activity with
9888 // a wallpaper to one without.
9889 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009890 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009891 "New transit away from wallpaper: " + transit);
9892 } else if (mWallpaperTarget != null) {
9893 // We are transitioning from an activity without
9894 // a wallpaper to now showing the wallpaper
9895 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009896 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009897 "New transit into wallpaper: " + transit);
9898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009899
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009900 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9901 mLastEnterAnimToken = animToken;
9902 mLastEnterAnimParams = animLp;
9903 } else if (mLastEnterAnimParams != null) {
9904 animLp = mLastEnterAnimParams;
9905 mLastEnterAnimToken = null;
9906 mLastEnterAnimParams = null;
9907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009908
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009909 // If all closing windows are obscured, then there is
9910 // no need to do an animation. This is the case, for
9911 // example, when this transition is being done behind
9912 // the lock screen.
9913 if (!mPolicy.allowAppAnimationsLw()) {
9914 animLp = null;
9915 }
9916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 NN = mOpeningApps.size();
9918 for (i=0; i<NN; i++) {
9919 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009920 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 "Now opening app" + wtoken);
9922 wtoken.reportedVisible = false;
9923 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009924 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009925 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009927 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009928 wtoken.showAllWindowsLocked();
9929 }
9930 NN = mClosingApps.size();
9931 for (i=0; i<NN; i++) {
9932 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009933 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 "Now closing app" + wtoken);
9935 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009936 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009937 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009939 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 // Force the allDrawn flag, because we want to start
9941 // this guy's animations regardless of whether it's
9942 // gotten drawn.
9943 wtoken.allDrawn = true;
9944 }
9945
Dianne Hackborn8b571a82009-09-25 16:09:43 -07009946 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 mOpeningApps.clear();
9949 mClosingApps.clear();
9950
9951 // This has changed the visibility of windows, so perform
9952 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009953 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -07009955 if (!moveInputMethodWindowsIfNeededLocked(true)) {
9956 assignLayersLocked();
9957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009959 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 }
9961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009962
Dianne Hackborna8f60182009-09-01 19:01:50 -07009963 if (!animating && mAppTransitionRunning) {
9964 // We have finished the animation of an app transition. To do
9965 // this, we have delayed a lot of operations like showing and
9966 // hiding apps, moving apps in Z-order, etc. The app token list
9967 // reflects the correct Z-order, but the window list may now
9968 // be out of sync with it. So here we will just rebuild the
9969 // entire app window list. Fun!
9970 mAppTransitionRunning = false;
9971 // Clear information about apps that were moving.
9972 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009973
Dianne Hackborna8f60182009-09-01 19:01:50 -07009974 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009975 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009976 moveInputMethodWindowsIfNeededLocked(false);
9977 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08009978 // Since the window list has been rebuilt, focus might
9979 // have to be recomputed since the actual order of windows
9980 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009981 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009983
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009984 int adjResult = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009985
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009986 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009987 // At this point, there was a window with a wallpaper that
9988 // was force hiding other windows behind it, but now it
9989 // is going away. This may be simple -- just animate
9990 // away the wallpaper and its window -- or it may be
9991 // hard -- the wallpaper now needs to be shown behind
9992 // something that was hidden.
9993 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009994 if (mLowerWallpaperTarget != null
9995 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009996 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009997 "wallpaperForceHiding changed with lower="
9998 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009999 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010000 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10001 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10002 if (mLowerWallpaperTarget.mAppToken.hidden) {
10003 // The lower target has become hidden before we
10004 // actually started the animation... let's completely
10005 // re-evaluate everything.
10006 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010007 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010008 }
10009 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010010 adjResult = adjustWallpaperWindowsLocked();
10011 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010012 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010013 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010014 + " NEW: " + mWallpaperTarget
10015 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010016 if (mLowerWallpaperTarget == null) {
10017 // Whoops, we don't need a special wallpaper animation.
10018 // Clear them out.
10019 forceHiding = false;
10020 for (i=N-1; i>=0; i--) {
10021 WindowState w = (WindowState)mWindows.get(i);
10022 if (w.mSurface != null) {
10023 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010024 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010025 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010026 forceHiding = true;
10027 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10028 if (!w.mAnimating) {
10029 // We set the animation above so it
10030 // is not yet running.
10031 w.clearAnimation();
10032 }
10033 }
10034 }
10035 }
10036 }
10037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010038
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010039 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010040 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010041 "Wallpaper may change! Adjusting");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010042 adjResult = adjustWallpaperWindowsLocked();
10043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010044
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010045 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010046 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010047 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010048 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010049 assignLayersLocked();
10050 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010051 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010052 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010053 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010055
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010056 if (mFocusMayChange) {
10057 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010058 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010059 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010060 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010061 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010062 }
10063
10064 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010065 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010067
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010068 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10069 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010070
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010071 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072
10073 // THIRD LOOP: Update the surfaces of all windows.
10074
10075 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10076
10077 boolean obscured = false;
10078 boolean blurring = false;
10079 boolean dimming = false;
10080 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010081 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010082 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010084 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010086 for (i=N-1; i>=0; i--) {
10087 WindowState w = (WindowState)mWindows.get(i);
10088
10089 boolean displayed = false;
10090 final WindowManager.LayoutParams attrs = w.mAttrs;
10091 final int attrFlags = attrs.flags;
10092
10093 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010094 // XXX NOTE: The logic here could be improved. We have
10095 // the decision about whether to resize a window separated
10096 // from whether to hide the surface. This can cause us to
10097 // resize a surface even if we are going to hide it. You
10098 // can see this by (1) holding device in landscape mode on
10099 // home screen; (2) tapping browser icon (device will rotate
10100 // to landscape; (3) tap home. The wallpaper will be resized
10101 // in step 2 but then immediately hidden, causing us to
10102 // have to resize and then redraw it again in step 3. It
10103 // would be nice to figure out how to avoid this, but it is
10104 // difficult because we do need to resize surfaces in some
10105 // cases while they are hidden such as when first showing a
10106 // window.
10107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010109 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110 TAG, "Placing surface #" + i + " " + w.mSurface
10111 + ": new=" + w.mShownFrame + ", old="
10112 + w.mLastShownFrame);
10113
10114 boolean resize;
10115 int width, height;
10116 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10117 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10118 w.mLastRequestedHeight != w.mRequestedHeight;
10119 // for a scaled surface, we just want to use
10120 // the requested size.
10121 width = w.mRequestedWidth;
10122 height = w.mRequestedHeight;
10123 w.mLastRequestedWidth = width;
10124 w.mLastRequestedHeight = height;
10125 w.mLastShownFrame.set(w.mShownFrame);
10126 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010127 if (SHOW_TRANSACTIONS) logSurface(w,
10128 "POS " + w.mShownFrame.left
10129 + ", " + w.mShownFrame.top, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10131 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010132 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 if (!recoveringMemory) {
10134 reclaimSomeSurfaceMemoryLocked(w, "position");
10135 }
10136 }
10137 } else {
10138 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10139 width = w.mShownFrame.width();
10140 height = w.mShownFrame.height();
10141 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 }
10143
10144 if (resize) {
10145 if (width < 1) width = 1;
10146 if (height < 1) height = 1;
10147 if (w.mSurface != null) {
10148 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010149 if (SHOW_TRANSACTIONS) logSurface(w,
10150 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010151 + w.mShownFrame.top + " SIZE "
10152 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010153 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010154 w.mSurfaceResized = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 w.mSurface.setSize(width, height);
10156 w.mSurface.setPosition(w.mShownFrame.left,
10157 w.mShownFrame.top);
10158 } catch (RuntimeException e) {
10159 // If something goes wrong with the surface (such
10160 // as running out of memory), don't take down the
10161 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010162 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 + "size=(" + width + "x" + height
10164 + "), pos=(" + w.mShownFrame.left
10165 + "," + w.mShownFrame.top + ")", e);
10166 if (!recoveringMemory) {
10167 reclaimSomeSurfaceMemoryLocked(w, "size");
10168 }
10169 }
10170 }
10171 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010172 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010173 w.mContentInsetsChanged =
10174 !w.mLastContentInsets.equals(w.mContentInsets);
10175 w.mVisibleInsetsChanged =
10176 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010177 boolean configChanged =
10178 w.mConfiguration != mCurConfiguration
10179 && (w.mConfiguration == null
10180 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010181 if (DEBUG_CONFIGURATION && configChanged) {
10182 Slog.v(TAG, "Win " + w + " config changed: "
10183 + mCurConfiguration);
10184 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010185 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010186 + ": configChanged=" + configChanged
10187 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010188 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010190 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010191 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010192 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193 w.mLastFrame.set(w.mFrame);
10194 w.mLastContentInsets.set(w.mContentInsets);
10195 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010196 // If the screen is currently frozen, then keep
10197 // it frozen until this window draws at its new
10198 // orientation.
10199 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010200 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010201 "Resizing while display frozen: " + w);
10202 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010203 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010204 mWindowsFreezingScreen = true;
10205 // XXX should probably keep timeout from
10206 // when we first froze the display.
10207 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10208 mH.sendMessageDelayed(mH.obtainMessage(
10209 H.WINDOW_FREEZE_TIMEOUT), 2000);
10210 }
10211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 // If the orientation is changing, then we need to
10213 // hold off on unfreezing the display until this
10214 // window has been redrawn; to do that, we need
10215 // to go through the process of getting informed
10216 // by the application when it has finished drawing.
10217 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010218 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 "Orientation start waiting for draw in "
10220 + w + ", surface " + w.mSurface);
10221 w.mDrawPending = true;
10222 w.mCommitDrawPending = false;
10223 w.mReadyToShow = false;
10224 if (w.mAppToken != null) {
10225 w.mAppToken.allDrawn = false;
10226 }
10227 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010228 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 "Resizing window " + w + " to " + w.mFrame);
10230 mResizingWindows.add(w);
10231 } else if (w.mOrientationChanging) {
10232 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010233 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 "Orientation not waiting for draw in "
10235 + w + ", surface " + w.mSurface);
10236 w.mOrientationChanging = false;
10237 }
10238 }
10239 }
10240
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010241 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242 if (!w.mLastHidden) {
10243 //dump();
10244 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010245 if (SHOW_TRANSACTIONS) logSurface(w,
10246 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 if (w.mSurface != null) {
10248 try {
10249 w.mSurface.hide();
10250 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010251 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252 }
10253 }
10254 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10255 }
10256 // If we are waiting for this window to handle an
10257 // orientation change, well, it is hidden, so
10258 // doesn't really matter. Note that this does
10259 // introduce a potential glitch if the window
10260 // becomes unhidden before it has drawn for the
10261 // new orientation.
10262 if (w.mOrientationChanging) {
10263 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010264 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 "Orientation change skips hidden " + w);
10266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 } else if (w.mLastLayer != w.mAnimLayer
10268 || w.mLastAlpha != w.mShownAlpha
10269 || w.mLastDsDx != w.mDsDx
10270 || w.mLastDtDx != w.mDtDx
10271 || w.mLastDsDy != w.mDsDy
10272 || w.mLastDtDy != w.mDtDy
10273 || w.mLastHScale != w.mHScale
10274 || w.mLastVScale != w.mVScale
10275 || w.mLastHidden) {
10276 displayed = true;
10277 w.mLastAlpha = w.mShownAlpha;
10278 w.mLastLayer = w.mAnimLayer;
10279 w.mLastDsDx = w.mDsDx;
10280 w.mLastDtDx = w.mDtDx;
10281 w.mLastDsDy = w.mDsDy;
10282 w.mLastDtDy = w.mDtDy;
10283 w.mLastHScale = w.mHScale;
10284 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010285 if (SHOW_TRANSACTIONS) logSurface(w,
10286 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010287 + " matrix=[" + (w.mDsDx*w.mHScale)
10288 + "," + (w.mDtDx*w.mVScale)
10289 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010290 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291 if (w.mSurface != null) {
10292 try {
10293 w.mSurface.setAlpha(w.mShownAlpha);
10294 w.mSurface.setLayer(w.mAnimLayer);
10295 w.mSurface.setMatrix(
10296 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10297 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10298 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010299 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010300 if (!recoveringMemory) {
10301 reclaimSomeSurfaceMemoryLocked(w, "update");
10302 }
10303 }
10304 }
10305
10306 if (w.mLastHidden && !w.mDrawPending
10307 && !w.mCommitDrawPending
10308 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010309 if (SHOW_TRANSACTIONS) logSurface(w,
10310 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010311 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010312 + " during relayout");
10313 if (showSurfaceRobustlyLocked(w)) {
10314 w.mHasDrawn = true;
10315 w.mLastHidden = false;
10316 } else {
10317 w.mOrientationChanging = false;
10318 }
10319 }
10320 if (w.mSurface != null) {
10321 w.mToken.hasVisible = true;
10322 }
10323 } else {
10324 displayed = true;
10325 }
10326
10327 if (displayed) {
10328 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010329 if (attrs.width == LayoutParams.MATCH_PARENT
10330 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 covered = true;
10332 }
10333 }
10334 if (w.mOrientationChanging) {
10335 if (w.mDrawPending || w.mCommitDrawPending) {
10336 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010337 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 "Orientation continue waiting for draw in " + w);
10339 } else {
10340 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010341 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 "Orientation change complete in " + w);
10343 }
10344 }
10345 w.mToken.hasVisible = true;
10346 }
10347 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010348 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 "Orientation change skips hidden " + w);
10350 w.mOrientationChanging = false;
10351 }
10352
10353 final boolean canBeSeen = w.isDisplayedLw();
10354
10355 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10356 focusDisplayed = true;
10357 }
10358
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010359 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010362 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 if (w.mSurface != null) {
10364 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10365 holdScreen = w.mSession;
10366 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010367 if (!syswin && w.mAttrs.screenBrightness >= 0
10368 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 screenBrightness = w.mAttrs.screenBrightness;
10370 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010371 if (!syswin && w.mAttrs.buttonBrightness >= 0
10372 && buttonBrightness < 0) {
10373 buttonBrightness = w.mAttrs.buttonBrightness;
10374 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010375 if (canBeSeen
10376 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10377 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10378 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010379 syswin = true;
10380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010382
Dianne Hackborn25994b42009-09-04 14:21:19 -070010383 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10384 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 // This window completely covers everything behind it,
10386 // so we want to leave all of them as unblurred (for
10387 // performance reasons).
10388 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010389 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010390 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010391 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010392 obscured = true;
10393 if (mBackgroundFillerSurface == null) {
10394 try {
10395 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010396 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010397 0, dw, dh,
10398 PixelFormat.OPAQUE,
10399 Surface.FX_SURFACE_NORMAL);
10400 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010401 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010402 }
10403 }
10404 try {
10405 mBackgroundFillerSurface.setPosition(0, 0);
10406 mBackgroundFillerSurface.setSize(dw, dh);
10407 // Using the same layer as Dim because they will never be shown at the
10408 // same time.
10409 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10410 mBackgroundFillerSurface.show();
10411 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010412 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010413 }
10414 backgroundFillerShown = true;
10415 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010416 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010418 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 + ": blurring=" + blurring
10420 + " obscured=" + obscured
10421 + " displayed=" + displayed);
10422 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10423 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010424 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010426 if (mDimAnimator == null) {
10427 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010429 mDimAnimator.show(dw, dh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010431 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 }
10433 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10434 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010435 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 blurring = true;
10437 mBlurShown = true;
10438 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010439 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010440 + mBlurSurface + ": CREATE");
10441 try {
Romain Guy06882f82009-06-10 13:36:04 -070010442 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010443 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 -1, 16, 16,
10445 PixelFormat.OPAQUE,
10446 Surface.FX_SURFACE_BLUR);
10447 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010448 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 }
10450 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010451 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 + mBlurSurface + ": SHOW pos=(0,0) (" +
10453 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
10454 if (mBlurSurface != null) {
10455 mBlurSurface.setPosition(0, 0);
10456 mBlurSurface.setSize(dw, dh);
10457 try {
10458 mBlurSurface.show();
10459 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010460 Slog.w(TAG, "Failure showing blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461 }
10462 }
10463 }
10464 mBlurSurface.setLayer(w.mAnimLayer-2);
10465 }
10466 }
10467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010468
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010469 if (obscuredChanged && mWallpaperTarget == w) {
10470 // This is the wallpaper target and its obscured state
10471 // changed... make sure the current wallaper's visibility
10472 // has been updated accordingly.
10473 updateWallpaperVisibilityLocked();
10474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010476
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010477 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10478 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010479 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010480 try {
10481 mBackgroundFillerSurface.hide();
10482 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010483 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010484 }
10485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010487 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010488 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10489 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 }
Romain Guy06882f82009-06-10 13:36:04 -070010491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010493 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 + ": HIDE");
10495 try {
10496 mBlurSurface.hide();
10497 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010498 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010499 }
10500 mBlurShown = false;
10501 }
10502
Joe Onorato8a9b2202010-02-26 18:56:32 -080010503 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010505 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 }
10507
10508 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010509
Joe Onorato8a9b2202010-02-26 18:56:32 -080010510 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 "With display frozen, orientationChangeComplete="
10512 + orientationChangeComplete);
10513 if (orientationChangeComplete) {
10514 if (mWindowsFreezingScreen) {
10515 mWindowsFreezingScreen = false;
10516 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10517 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010518 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 }
Romain Guy06882f82009-06-10 13:36:04 -070010520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 i = mResizingWindows.size();
10522 if (i > 0) {
10523 do {
10524 i--;
10525 WindowState win = mResizingWindows.get(i);
10526 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010527 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10528 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010529 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010530 boolean configChanged =
10531 win.mConfiguration != mCurConfiguration
10532 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010533 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10534 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10535 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010536 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010537 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010538 + " / " + mCurConfiguration + " / 0x"
10539 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010540 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010541 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 win.mClient.resized(win.mFrame.width(),
10543 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010544 win.mLastVisibleInsets, win.mDrawPending,
10545 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 win.mContentInsetsChanged = false;
10547 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010548 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010549 } catch (RemoteException e) {
10550 win.mOrientationChanging = false;
10551 }
10552 } while (i > 0);
10553 mResizingWindows.clear();
10554 }
Romain Guy06882f82009-06-10 13:36:04 -070010555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010557 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 i = mDestroySurface.size();
10559 if (i > 0) {
10560 do {
10561 i--;
10562 WindowState win = mDestroySurface.get(i);
10563 win.mDestroying = false;
10564 if (mInputMethodWindow == win) {
10565 mInputMethodWindow = null;
10566 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010567 if (win == mWallpaperTarget) {
10568 wallpaperDestroyed = true;
10569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 win.destroySurfaceLocked();
10571 } while (i > 0);
10572 mDestroySurface.clear();
10573 }
10574
10575 // Time to remove any exiting tokens?
10576 for (i=mExitingTokens.size()-1; i>=0; i--) {
10577 WindowToken token = mExitingTokens.get(i);
10578 if (!token.hasVisible) {
10579 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010580 if (token.windowType == TYPE_WALLPAPER) {
10581 mWallpaperTokens.remove(token);
10582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 }
10584 }
10585
10586 // Time to remove any exiting applications?
10587 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10588 AppWindowToken token = mExitingAppTokens.get(i);
10589 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010590 // Make sure there is no animation running on this token,
10591 // so any windows associated with it will be removed as
10592 // soon as their animations are complete
10593 token.animation = null;
10594 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 mAppTokens.remove(token);
10596 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010597 if (mLastEnterAnimToken == token) {
10598 mLastEnterAnimToken = null;
10599 mLastEnterAnimParams = null;
10600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 }
10602 }
10603
Dianne Hackborna8f60182009-09-01 19:01:50 -070010604 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010605
Dianne Hackborna8f60182009-09-01 19:01:50 -070010606 if (!animating && mAppTransitionRunning) {
10607 // We have finished the animation of an app transition. To do
10608 // this, we have delayed a lot of operations like showing and
10609 // hiding apps, moving apps in Z-order, etc. The app token list
10610 // reflects the correct Z-order, but the window list may now
10611 // be out of sync with it. So here we will just rebuild the
10612 // entire app window list. Fun!
10613 mAppTransitionRunning = false;
10614 needRelayout = true;
10615 rebuildAppWindowListLocked();
10616 // Clear information about apps that were moving.
10617 mToBottomApps.clear();
10618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 if (focusDisplayed) {
10621 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10622 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010623 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010624 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010625 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010626 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010627 requestAnimationLocked(0);
10628 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010629 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10630 }
10631 mQueue.setHoldScreenLocked(holdScreen != null);
10632 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10633 mPowerManager.setScreenBrightnessOverride(-1);
10634 } else {
10635 mPowerManager.setScreenBrightnessOverride((int)
10636 (screenBrightness * Power.BRIGHTNESS_ON));
10637 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010638 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10639 mPowerManager.setButtonBrightnessOverride(-1);
10640 } else {
10641 mPowerManager.setButtonBrightnessOverride((int)
10642 (buttonBrightness * Power.BRIGHTNESS_ON));
10643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 if (holdScreen != mHoldingScreenOn) {
10645 mHoldingScreenOn = holdScreen;
10646 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10647 mH.sendMessage(m);
10648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010649
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010650 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010651 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010652 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10653 LocalPowerManager.BUTTON_EVENT, true);
10654 mTurnOnScreen = false;
10655 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010656
10657 // Check to see if we are now in a state where the screen should
10658 // be enabled, because the window obscured flags have changed.
10659 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 }
10661
10662 void requestAnimationLocked(long delay) {
10663 if (!mAnimationPending) {
10664 mAnimationPending = true;
10665 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10666 }
10667 }
Romain Guy06882f82009-06-10 13:36:04 -070010668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 /**
10670 * Have the surface flinger show a surface, robustly dealing with
10671 * error conditions. In particular, if there is not enough memory
10672 * to show the surface, then we will try to get rid of other surfaces
10673 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 * @return Returns true if the surface was successfully shown.
10676 */
10677 boolean showSurfaceRobustlyLocked(WindowState win) {
10678 try {
10679 if (win.mSurface != null) {
10680 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010681 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010682 if (DEBUG_VISIBILITY) Slog.v(TAG,
10683 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010684 win.mTurnOnScreen = false;
10685 mTurnOnScreen = true;
10686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 }
10688 return true;
10689 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010690 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 }
Romain Guy06882f82009-06-10 13:36:04 -070010692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 return false;
10696 }
Romain Guy06882f82009-06-10 13:36:04 -070010697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10699 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010700
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010701 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 if (mForceRemoves == null) {
10705 mForceRemoves = new ArrayList<WindowState>();
10706 }
Romain Guy06882f82009-06-10 13:36:04 -070010707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 long callingIdentity = Binder.clearCallingIdentity();
10709 try {
10710 // There was some problem... first, do a sanity check of the
10711 // window list to make sure we haven't left any dangling surfaces
10712 // around.
10713 int N = mWindows.size();
10714 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010715 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 for (int i=0; i<N; i++) {
10717 WindowState ws = (WindowState)mWindows.get(i);
10718 if (ws.mSurface != null) {
10719 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010720 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 + ws + " surface=" + ws.mSurface
10722 + " token=" + win.mToken
10723 + " pid=" + ws.mSession.mPid
10724 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010725 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 ws.mSurface = null;
10727 mForceRemoves.add(ws);
10728 i--;
10729 N--;
10730 leakedSurface = true;
10731 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010732 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 + ws + " surface=" + ws.mSurface
10734 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010735 ws.mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 ws.mSurface = null;
10737 leakedSurface = true;
10738 }
10739 }
10740 }
Romain Guy06882f82009-06-10 13:36:04 -070010741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 boolean killedApps = false;
10743 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010744 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 SparseIntArray pidCandidates = new SparseIntArray();
10746 for (int i=0; i<N; i++) {
10747 WindowState ws = (WindowState)mWindows.get(i);
10748 if (ws.mSurface != null) {
10749 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10750 }
10751 }
10752 if (pidCandidates.size() > 0) {
10753 int[] pids = new int[pidCandidates.size()];
10754 for (int i=0; i<pids.length; i++) {
10755 pids[i] = pidCandidates.keyAt(i);
10756 }
10757 try {
10758 if (mActivityManager.killPidsForMemory(pids)) {
10759 killedApps = true;
10760 }
10761 } catch (RemoteException e) {
10762 }
10763 }
10764 }
Romain Guy06882f82009-06-10 13:36:04 -070010765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 if (leakedSurface || killedApps) {
10767 // We managed to reclaim some memory, so get rid of the trouble
10768 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010769 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010770 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010771 surface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 win.mSurface = null;
10773 }
Romain Guy06882f82009-06-10 13:36:04 -070010774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 try {
10776 win.mClient.dispatchGetNewSurface();
10777 } catch (RemoteException e) {
10778 }
10779 }
10780 } finally {
10781 Binder.restoreCallingIdentity(callingIdentity);
10782 }
10783 }
Romain Guy06882f82009-06-10 13:36:04 -070010784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 private boolean updateFocusedWindowLocked(int mode) {
10786 WindowState newFocus = computeFocusedWindowLocked();
10787 if (mCurrentFocus != newFocus) {
10788 // This check makes sure that we don't already have the focus
10789 // change message pending.
10790 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10791 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010792 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10794 final WindowState oldFocus = mCurrentFocus;
10795 mCurrentFocus = newFocus;
10796 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 final WindowState imWindow = mInputMethodWindow;
10799 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010800 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010802 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10803 mLayoutNeeded = true;
10804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010805 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10806 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010807 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10808 // Client will do the layout, but we need to assign layers
10809 // for handleNewWindowLocked() below.
10810 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 }
10812 }
Romain Guy06882f82009-06-10 13:36:04 -070010813
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010814 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10815 mKeyWaiter.handleNewWindowLocked(newFocus);
10816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 return true;
10818 }
10819 return false;
10820 }
10821
10822 private WindowState computeFocusedWindowLocked() {
10823 WindowState result = null;
10824 WindowState win;
10825
10826 int i = mWindows.size() - 1;
10827 int nextAppIndex = mAppTokens.size()-1;
10828 WindowToken nextApp = nextAppIndex >= 0
10829 ? mAppTokens.get(nextAppIndex) : null;
10830
10831 while (i >= 0) {
10832 win = (WindowState)mWindows.get(i);
10833
Joe Onorato8a9b2202010-02-26 18:56:32 -080010834 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 TAG, "Looking for focus: " + i
10836 + " = " + win
10837 + ", flags=" + win.mAttrs.flags
10838 + ", canReceive=" + win.canReceiveKeys());
10839
10840 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 // If this window's application has been removed, just skip it.
10843 if (thisApp != null && thisApp.removed) {
10844 i--;
10845 continue;
10846 }
Romain Guy06882f82009-06-10 13:36:04 -070010847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010848 // If there is a focused app, don't allow focus to go to any
10849 // windows below it. If this is an application window, step
10850 // through the app tokens until we find its app.
10851 if (thisApp != null && nextApp != null && thisApp != nextApp
10852 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10853 int origAppIndex = nextAppIndex;
10854 while (nextAppIndex > 0) {
10855 if (nextApp == mFocusedApp) {
10856 // Whoops, we are below the focused app... no focus
10857 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080010858 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 TAG, "Reached focused app: " + mFocusedApp);
10860 return null;
10861 }
10862 nextAppIndex--;
10863 nextApp = mAppTokens.get(nextAppIndex);
10864 if (nextApp == thisApp) {
10865 break;
10866 }
10867 }
10868 if (thisApp != nextApp) {
10869 // Uh oh, the app token doesn't exist! This shouldn't
10870 // happen, but if it does we can get totally hosed...
10871 // so restart at the original app.
10872 nextAppIndex = origAppIndex;
10873 nextApp = mAppTokens.get(nextAppIndex);
10874 }
10875 }
10876
10877 // Dispatch to this window if it is wants key events.
10878 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010879 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 TAG, "Found focus @ " + i + " = " + win);
10881 result = win;
10882 break;
10883 }
10884
10885 i--;
10886 }
10887
10888 return result;
10889 }
10890
10891 private void startFreezingDisplayLocked() {
10892 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010893 // Freezing the display also suspends key event delivery, to
10894 // keep events from going astray while the display is reconfigured.
10895 // If someone has changed orientation again while the screen is
10896 // still frozen, the events will continue to be blocked while the
10897 // successive orientation change is processed. To prevent spurious
10898 // ANRs, we reset the event dispatch timeout in this case.
10899 synchronized (mKeyWaiter) {
10900 mKeyWaiter.mWasFrozen = true;
10901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 return;
10903 }
Romain Guy06882f82009-06-10 13:36:04 -070010904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010908 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 if (mFreezeGcPending != 0) {
10910 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010911 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 mH.removeMessages(H.FORCE_GC);
10913 Runtime.getRuntime().gc();
10914 mFreezeGcPending = now;
10915 }
10916 } else {
10917 mFreezeGcPending = now;
10918 }
Romain Guy06882f82009-06-10 13:36:04 -070010919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070010921 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
10922 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010923 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 mAppTransitionReady = true;
10925 }
Romain Guy06882f82009-06-10 13:36:04 -070010926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927 if (PROFILE_ORIENTATION) {
10928 File file = new File("/data/system/frozen");
10929 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
10930 }
10931 Surface.freezeDisplay(0);
10932 }
Romain Guy06882f82009-06-10 13:36:04 -070010933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 private void stopFreezingDisplayLocked() {
10935 if (!mDisplayFrozen) {
10936 return;
10937 }
Romain Guy06882f82009-06-10 13:36:04 -070010938
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010939 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
10940 return;
10941 }
10942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 mDisplayFrozen = false;
10944 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
10945 if (PROFILE_ORIENTATION) {
10946 Debug.stopMethodTracing();
10947 }
10948 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070010949
Chris Tate2ad63a92009-03-25 17:36:48 -070010950 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
10951 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 synchronized (mKeyWaiter) {
10953 mKeyWaiter.mWasFrozen = true;
10954 mKeyWaiter.notifyAll();
10955 }
10956
10957 // A little kludge: a lot could have happened while the
10958 // display was frozen, so now that we are coming back we
10959 // do a gc so that any remote references the system
10960 // processes holds on others can be released if they are
10961 // no longer needed.
10962 mH.removeMessages(H.FORCE_GC);
10963 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
10964 2000);
Romain Guy06882f82009-06-10 13:36:04 -070010965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010966 mScreenFrozenLock.release();
10967 }
Romain Guy06882f82009-06-10 13:36:04 -070010968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 @Override
10970 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10971 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
10972 != PackageManager.PERMISSION_GRANTED) {
10973 pw.println("Permission Denial: can't dump WindowManager from from pid="
10974 + Binder.getCallingPid()
10975 + ", uid=" + Binder.getCallingUid());
10976 return;
10977 }
Romain Guy06882f82009-06-10 13:36:04 -070010978
Dianne Hackborna2e92262010-03-02 17:19:29 -080010979 pw.println("Input State:");
10980 mQueue.dump(pw, " ");
10981 pw.println(" ");
10982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010983 synchronized(mWindowMap) {
10984 pw.println("Current Window Manager state:");
10985 for (int i=mWindows.size()-1; i>=0; i--) {
10986 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010987 pw.print(" Window #"); pw.print(i); pw.print(' ');
10988 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 w.dump(pw, " ");
10990 }
10991 if (mInputMethodDialogs.size() > 0) {
10992 pw.println(" ");
10993 pw.println(" Input method dialogs:");
10994 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
10995 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010996 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 }
10998 }
10999 if (mPendingRemove.size() > 0) {
11000 pw.println(" ");
11001 pw.println(" Remove pending for:");
11002 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11003 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011004 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11005 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 w.dump(pw, " ");
11007 }
11008 }
11009 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11010 pw.println(" ");
11011 pw.println(" Windows force removing:");
11012 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11013 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011014 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11015 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 w.dump(pw, " ");
11017 }
11018 }
11019 if (mDestroySurface.size() > 0) {
11020 pw.println(" ");
11021 pw.println(" Windows waiting to destroy their surface:");
11022 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11023 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011024 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11025 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 w.dump(pw, " ");
11027 }
11028 }
11029 if (mLosingFocus.size() > 0) {
11030 pw.println(" ");
11031 pw.println(" Windows losing focus:");
11032 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11033 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011034 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11035 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011036 w.dump(pw, " ");
11037 }
11038 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011039 if (mResizingWindows.size() > 0) {
11040 pw.println(" ");
11041 pw.println(" Windows waiting to resize:");
11042 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11043 WindowState w = mResizingWindows.get(i);
11044 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11045 pw.print(w); pw.println(":");
11046 w.dump(pw, " ");
11047 }
11048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011049 if (mSessions.size() > 0) {
11050 pw.println(" ");
11051 pw.println(" All active sessions:");
11052 Iterator<Session> it = mSessions.iterator();
11053 while (it.hasNext()) {
11054 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011055 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011056 s.dump(pw, " ");
11057 }
11058 }
11059 if (mTokenMap.size() > 0) {
11060 pw.println(" ");
11061 pw.println(" All tokens:");
11062 Iterator<WindowToken> it = mTokenMap.values().iterator();
11063 while (it.hasNext()) {
11064 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011065 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 token.dump(pw, " ");
11067 }
11068 }
11069 if (mTokenList.size() > 0) {
11070 pw.println(" ");
11071 pw.println(" Window token list:");
11072 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011073 pw.print(" #"); pw.print(i); pw.print(": ");
11074 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011075 }
11076 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011077 if (mWallpaperTokens.size() > 0) {
11078 pw.println(" ");
11079 pw.println(" Wallpaper tokens:");
11080 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11081 WindowToken token = mWallpaperTokens.get(i);
11082 pw.print(" Wallpaper #"); pw.print(i);
11083 pw.print(' '); pw.print(token); pw.println(':');
11084 token.dump(pw, " ");
11085 }
11086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 if (mAppTokens.size() > 0) {
11088 pw.println(" ");
11089 pw.println(" Application tokens in Z order:");
11090 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011091 pw.print(" App #"); pw.print(i); pw.print(": ");
11092 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 }
11094 }
11095 if (mFinishedStarting.size() > 0) {
11096 pw.println(" ");
11097 pw.println(" Finishing start of application tokens:");
11098 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11099 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011100 pw.print(" Finished Starting #"); pw.print(i);
11101 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011102 token.dump(pw, " ");
11103 }
11104 }
11105 if (mExitingTokens.size() > 0) {
11106 pw.println(" ");
11107 pw.println(" Exiting tokens:");
11108 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11109 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011110 pw.print(" Exiting #"); pw.print(i);
11111 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 token.dump(pw, " ");
11113 }
11114 }
11115 if (mExitingAppTokens.size() > 0) {
11116 pw.println(" ");
11117 pw.println(" Exiting application tokens:");
11118 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11119 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011120 pw.print(" Exiting App #"); pw.print(i);
11121 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 token.dump(pw, " ");
11123 }
11124 }
11125 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011126 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11127 pw.print(" mLastFocus="); pw.println(mLastFocus);
11128 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11129 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11130 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011131 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011132 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11133 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11134 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11135 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011136 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11137 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11138 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011139 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11140 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11141 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11142 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011143 if (mDimAnimator != null) {
11144 mDimAnimator.printTo(pw);
11145 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011146 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011147 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011148 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011149 pw.print(mInputMethodAnimLayerAdjustment);
11150 pw.print(" mWallpaperAnimLayerAdjustment=");
11151 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011152 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11153 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011154 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11155 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011156 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11157 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011158 pw.print(" mRotation="); pw.print(mRotation);
11159 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11160 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11161 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11162 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11163 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11164 pw.print(" mNextAppTransition=0x");
11165 pw.print(Integer.toHexString(mNextAppTransition));
11166 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011167 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011168 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011169 if (mNextAppTransitionPackage != null) {
11170 pw.print(" mNextAppTransitionPackage=");
11171 pw.print(mNextAppTransitionPackage);
11172 pw.print(", mNextAppTransitionEnter=0x");
11173 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11174 pw.print(", mNextAppTransitionExit=0x");
11175 pw.print(Integer.toHexString(mNextAppTransitionExit));
11176 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011177 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11178 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011179 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11180 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11181 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11182 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011183 if (mOpeningApps.size() > 0) {
11184 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11185 }
11186 if (mClosingApps.size() > 0) {
11187 pw.print(" mClosingApps="); pw.println(mClosingApps);
11188 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011189 if (mToTopApps.size() > 0) {
11190 pw.print(" mToTopApps="); pw.println(mToTopApps);
11191 }
11192 if (mToBottomApps.size() > 0) {
11193 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11194 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011195 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11196 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011197 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011198 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11199 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11200 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11201 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11202 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11203 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011204 }
11205 }
11206
11207 public void monitor() {
11208 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011209 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 synchronized (mKeyWaiter) { }
11211 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011212
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011213 public void virtualKeyFeedback(KeyEvent event) {
11214 mPolicy.keyFeedbackFromInput(event);
11215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011216
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011217 /**
11218 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011219 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011220 */
11221 private static class DimAnimator {
11222 Surface mDimSurface;
11223 boolean mDimShown = false;
11224 float mDimCurrentAlpha;
11225 float mDimTargetAlpha;
11226 float mDimDeltaPerMs;
11227 long mLastDimAnimTime;
11228
11229 DimAnimator (SurfaceSession session) {
11230 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011231 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011232 + mDimSurface + ": CREATE");
11233 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011234 mDimSurface = new Surface(session, 0,
11235 "DimSurface",
11236 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011237 Surface.FX_SURFACE_DIM);
11238 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011239 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011240 }
11241 }
11242 }
11243
11244 /**
11245 * Show the dim surface.
11246 */
11247 void show(int dw, int dh) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011248 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011249 dw + "x" + dh + ")");
11250 mDimShown = true;
11251 try {
11252 mDimSurface.setPosition(0, 0);
11253 mDimSurface.setSize(dw, dh);
11254 mDimSurface.show();
11255 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011256 Slog.w(TAG, "Failure showing dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011257 }
11258 }
11259
11260 /**
11261 * Set's the dim surface's layer and update dim parameters that will be used in
11262 * {@link updateSurface} after all windows are examined.
11263 */
11264 void updateParameters(WindowState w, long currentTime) {
11265 mDimSurface.setLayer(w.mAnimLayer-1);
11266
11267 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011268 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011269 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011270 if (mDimTargetAlpha != target) {
11271 // If the desired dim level has changed, then
11272 // start an animation to it.
11273 mLastDimAnimTime = currentTime;
11274 long duration = (w.mAnimating && w.mAnimation != null)
11275 ? w.mAnimation.computeDurationHint()
11276 : DEFAULT_DIM_DURATION;
11277 if (target > mDimTargetAlpha) {
11278 // This is happening behind the activity UI,
11279 // so we can make it run a little longer to
11280 // give a stronger impression without disrupting
11281 // the user.
11282 duration *= DIM_DURATION_MULTIPLIER;
11283 }
11284 if (duration < 1) {
11285 // Don't divide by zero
11286 duration = 1;
11287 }
11288 mDimTargetAlpha = target;
11289 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11290 }
11291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011292
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011293 /**
11294 * Updating the surface's alpha. Returns true if the animation continues, or returns
11295 * false when the animation is finished and the dim surface is hidden.
11296 */
11297 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11298 if (!dimming) {
11299 if (mDimTargetAlpha != 0) {
11300 mLastDimAnimTime = currentTime;
11301 mDimTargetAlpha = 0;
11302 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11303 }
11304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011305
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011306 boolean animating = false;
11307 if (mLastDimAnimTime != 0) {
11308 mDimCurrentAlpha += mDimDeltaPerMs
11309 * (currentTime-mLastDimAnimTime);
11310 boolean more = true;
11311 if (displayFrozen) {
11312 // If the display is frozen, there is no reason to animate.
11313 more = false;
11314 } else if (mDimDeltaPerMs > 0) {
11315 if (mDimCurrentAlpha > mDimTargetAlpha) {
11316 more = false;
11317 }
11318 } else if (mDimDeltaPerMs < 0) {
11319 if (mDimCurrentAlpha < mDimTargetAlpha) {
11320 more = false;
11321 }
11322 } else {
11323 more = false;
11324 }
11325
11326 // Do we need to continue animating?
11327 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011328 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011329 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11330 mLastDimAnimTime = currentTime;
11331 mDimSurface.setAlpha(mDimCurrentAlpha);
11332 animating = true;
11333 } else {
11334 mDimCurrentAlpha = mDimTargetAlpha;
11335 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011336 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011337 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11338 mDimSurface.setAlpha(mDimCurrentAlpha);
11339 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011340 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011341 + ": HIDE");
11342 try {
11343 mDimSurface.hide();
11344 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011345 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011346 }
11347 mDimShown = false;
11348 }
11349 }
11350 }
11351 return animating;
11352 }
11353
11354 public void printTo(PrintWriter pw) {
11355 pw.print(" mDimShown="); pw.print(mDimShown);
11356 pw.print(" current="); pw.print(mDimCurrentAlpha);
11357 pw.print(" target="); pw.print(mDimTargetAlpha);
11358 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11359 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11360 }
11361 }
11362
11363 /**
11364 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11365 * This is used for opening/closing transition for apps in compatible mode.
11366 */
11367 private static class FadeInOutAnimation extends Animation {
11368 int mWidth;
11369 boolean mFadeIn;
11370
11371 public FadeInOutAnimation(boolean fadeIn) {
11372 setInterpolator(new AccelerateInterpolator());
11373 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11374 mFadeIn = fadeIn;
11375 }
11376
11377 @Override
11378 protected void applyTransformation(float interpolatedTime, Transformation t) {
11379 float x = interpolatedTime;
11380 if (!mFadeIn) {
11381 x = 1.0f - x; // reverse the interpolation for fade out
11382 }
11383 if (x < 0.5) {
11384 // move the window out of the screen.
11385 t.getMatrix().setTranslate(mWidth, 0);
11386 } else {
11387 t.getMatrix().setTranslate(0, 0);// show
11388 t.setAlpha((x - 0.5f) * 2);
11389 }
11390 }
11391
11392 @Override
11393 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11394 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11395 mWidth = width;
11396 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011397
11398 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011399 public int getZAdjustment() {
11400 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011401 }
11402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011403}