blob: f1c67d2e5114b16f735b7d55cceff1afc23d5a64 [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;
Dianne Hackbornce73c1e2010-04-12 23:11:38 -0700151 static final boolean DEBUG_FREEZE = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final boolean SHOW_TRANSACTIONS = false;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700153 static final boolean HIDE_STACK_CRAWLS = true;
Michael Chan53071d62009-05-13 17:29:48 -0700154 static final boolean MEASURE_LATENCY = false;
155 static private LatencyTimer lt;
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 static final boolean PROFILE_ORIENTATION = false;
158 static final boolean BLUR = true;
Dave Bortcfe65242009-04-09 14:51:04 -0700159 static final boolean localLOGV = DEBUG;
Romain Guy06882f82009-06-10 13:36:04 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /** How long to wait for subsequent key repeats, in milliseconds */
162 static final int KEY_REPEAT_DELAY = 50;
163
164 /** How much to multiply the policy's type layer, to reserve room
165 * for multiple windows of the same type and Z-ordering adjustment
166 * with TYPE_LAYER_OFFSET. */
167 static final int TYPE_LAYER_MULTIPLIER = 10000;
Romain Guy06882f82009-06-10 13:36:04 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** Offset from TYPE_LAYER_MULTIPLIER for moving a group of windows above
170 * or below others in the same layer. */
171 static final int TYPE_LAYER_OFFSET = 1000;
Romain Guy06882f82009-06-10 13:36:04 -0700172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 /** How much to increment the layer for each window, to reserve room
174 * for effect surfaces between them.
175 */
176 static final int WINDOW_LAYER_MULTIPLIER = 5;
Romain Guy06882f82009-06-10 13:36:04 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 /** The maximum length we will accept for a loaded animation duration:
179 * this is 10 seconds.
180 */
181 static final int MAX_ANIMATION_DURATION = 10*1000;
182
183 /** Amount of time (in milliseconds) to animate the dim surface from one
184 * value to another, when no window animation is driving it.
185 */
186 static final int DEFAULT_DIM_DURATION = 200;
187
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700188 /** Amount of time (in milliseconds) to animate the fade-in-out transition for
189 * compatible windows.
190 */
191 static final int DEFAULT_FADE_IN_OUT_DURATION = 400;
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /** Adjustment to time to perform a dim, to make it more dramatic.
194 */
195 static final int DIM_DURATION_MULTIPLIER = 6;
Romain Guy06882f82009-06-10 13:36:04 -0700196
Dianne Hackborncfaef692009-06-15 14:24:44 -0700197 static final int INJECT_FAILED = 0;
198 static final int INJECT_SUCCEEDED = 1;
199 static final int INJECT_NO_PERMISSION = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 static final int UPDATE_FOCUS_NORMAL = 0;
202 static final int UPDATE_FOCUS_WILL_ASSIGN_LAYERS = 1;
203 static final int UPDATE_FOCUS_PLACING_SURFACES = 2;
204 static final int UPDATE_FOCUS_WILL_PLACE_SURFACES = 3;
Romain Guy06882f82009-06-10 13:36:04 -0700205
Michael Chane96440f2009-05-06 10:27:36 -0700206 /** The minimum time between dispatching touch events. */
207 int mMinWaitTimeBetweenTouchEvents = 1000 / 35;
208
209 // Last touch event time
210 long mLastTouchEventTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700211
Michael Chane96440f2009-05-06 10:27:36 -0700212 // Last touch event type
213 int mLastTouchEventType = OTHER_EVENT;
Romain Guy06882f82009-06-10 13:36:04 -0700214
Michael Chane96440f2009-05-06 10:27:36 -0700215 // Time to wait before calling useractivity again. This saves CPU usage
216 // when we get a flood of touch events.
217 static final int MIN_TIME_BETWEEN_USERACTIVITIES = 1000;
218
219 // Last time we call user activity
220 long mLastUserActivityCallTime = 0;
221
Romain Guy06882f82009-06-10 13:36:04 -0700222 // Last time we updated battery stats
Michael Chane96440f2009-05-06 10:27:36 -0700223 long mLastBatteryStatsCallTime = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 private static final String SYSTEM_SECURE = "ro.secure";
Romain Guy06882f82009-06-10 13:36:04 -0700226 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
228 /**
229 * Condition waited on by {@link #reenableKeyguard} to know the call to
230 * the window policy has finished.
Mike Lockwood983ee092009-11-22 01:42:24 -0500231 * This is set to true only if mKeyguardTokenWatcher.acquired() has
232 * actually disabled the keyguard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 */
Mike Lockwood983ee092009-11-22 01:42:24 -0500234 private boolean mKeyguardDisabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Mike Lockwood983ee092009-11-22 01:42:24 -0500236 final TokenWatcher mKeyguardTokenWatcher = new TokenWatcher(
237 new Handler(), "WindowManagerService.mKeyguardTokenWatcher") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 public void acquired() {
239 mPolicy.enableKeyguard(false);
Mike Lockwood983ee092009-11-22 01:42:24 -0500240 mKeyguardDisabled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242 public void released() {
Dianne Hackborna33e3f72009-09-29 17:28:24 -0700243 mPolicy.enableKeyguard(true);
Mike Lockwood983ee092009-11-22 01:42:24 -0500244 synchronized (mKeyguardTokenWatcher) {
245 mKeyguardDisabled = false;
246 mKeyguardTokenWatcher.notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
248 }
249 };
250
251 final Context mContext;
252
253 final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final boolean mLimitedAlphaCompositing;
Romain Guy06882f82009-06-10 13:36:04 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final WindowManagerPolicy mPolicy = PolicyManager.makeNewWindowManager();
258
259 final IActivityManager mActivityManager;
Romain Guy06882f82009-06-10 13:36:04 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 final IBatteryStats mBatteryStats;
Romain Guy06882f82009-06-10 13:36:04 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * All currently active sessions with clients.
265 */
266 final HashSet<Session> mSessions = new HashSet<Session>();
Romain Guy06882f82009-06-10 13:36:04 -0700267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
269 * Mapping from an IWindow IBinder to the server's Window object.
270 * This is also used as the lock for all of our state.
271 */
272 final HashMap<IBinder, WindowState> mWindowMap = new HashMap<IBinder, WindowState>();
273
274 /**
275 * Mapping from a token IBinder to a WindowToken object.
276 */
277 final HashMap<IBinder, WindowToken> mTokenMap =
278 new HashMap<IBinder, WindowToken>();
279
280 /**
281 * The same tokens as mTokenMap, stored in a list for efficient iteration
282 * over them.
283 */
284 final ArrayList<WindowToken> mTokenList = new ArrayList<WindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * Window tokens that are in the process of exiting, but still
288 * on screen for animations.
289 */
290 final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
291
292 /**
293 * Z-ordered (bottom-most first) list of all application tokens, for
294 * controlling the ordering of windows in different applications. This
295 * contains WindowToken objects.
296 */
297 final ArrayList<AppWindowToken> mAppTokens = new ArrayList<AppWindowToken>();
298
299 /**
300 * Application tokens that are in the process of exiting, but still
301 * on screen for animations.
302 */
303 final ArrayList<AppWindowToken> mExitingAppTokens = new ArrayList<AppWindowToken>();
304
305 /**
306 * List of window tokens that have finished starting their application,
307 * and now need to have the policy remove their windows.
308 */
309 final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<AppWindowToken>();
310
311 /**
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700312 * This was the app token that was used to retrieve the last enter
313 * animation. It will be used for the next exit animation.
314 */
315 AppWindowToken mLastEnterAnimToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700317 /**
318 * These were the layout params used to retrieve the last enter animation.
319 * They will be used for the next exit animation.
320 */
321 LayoutParams mLastEnterAnimParams;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 * Z-ordered (bottom-most first) list of all Window objects.
325 */
326 final ArrayList mWindows = new ArrayList();
327
328 /**
329 * Windows that are being resized. Used so we can tell the client about
330 * the resize after closing the transaction in which we resized the
331 * underlying surface.
332 */
333 final ArrayList<WindowState> mResizingWindows = new ArrayList<WindowState>();
334
335 /**
336 * Windows whose animations have ended and now must be removed.
337 */
338 final ArrayList<WindowState> mPendingRemove = new ArrayList<WindowState>();
339
340 /**
341 * Windows whose surface should be destroyed.
342 */
343 final ArrayList<WindowState> mDestroySurface = new ArrayList<WindowState>();
344
345 /**
346 * Windows that have lost input focus and are waiting for the new
347 * focus window to be displayed before they are told about this.
348 */
349 ArrayList<WindowState> mLosingFocus = new ArrayList<WindowState>();
350
351 /**
352 * This is set when we have run out of memory, and will either be an empty
353 * list or contain windows that need to be force removed.
354 */
355 ArrayList<WindowState> mForceRemoves;
Romain Guy06882f82009-06-10 13:36:04 -0700356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 IInputMethodManager mInputMethodManager;
Romain Guy06882f82009-06-10 13:36:04 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 SurfaceSession mFxSession;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -0700360 private DimAnimator mDimAnimator = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 Surface mBlurSurface;
362 boolean mBlurShown;
Romain Guy06882f82009-06-10 13:36:04 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 int mTransactionSequence = 0;
Romain Guy06882f82009-06-10 13:36:04 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 final float[] mTmpFloats = new float[9];
367
368 boolean mSafeMode;
369 boolean mDisplayEnabled = false;
370 boolean mSystemBooted = false;
Christopher Tateb696aee2010-04-02 19:08:30 -0700371 int mInitialDisplayWidth = 0;
372 int mInitialDisplayHeight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 int mRotation = 0;
374 int mRequestedRotation = 0;
375 int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Dianne Hackborn321ae682009-03-27 16:16:03 -0700376 int mLastRotationFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 ArrayList<IRotationWatcher> mRotationWatchers
378 = new ArrayList<IRotationWatcher>();
Romain Guy06882f82009-06-10 13:36:04 -0700379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 boolean mLayoutNeeded = true;
381 boolean mAnimationPending = false;
382 boolean mDisplayFrozen = false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800383 boolean mWaitingForConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 boolean mWindowsFreezingScreen = false;
385 long mFreezeGcPending = 0;
386 int mAppsFreezingScreen = 0;
387
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800388 int mLayoutSeq = 0;
389
Dianne Hackbornb601ce12010-03-01 23:36:02 -0800390 // State while inside of layoutAndPlaceSurfacesLocked().
391 boolean mFocusMayChange;
392
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800393 Configuration mCurConfiguration = new Configuration();
394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 // This is held as long as we have the screen frozen, to give us time to
396 // perform a rotation animation when turning off shows the lock screen which
397 // changes the orientation.
398 PowerManager.WakeLock mScreenFrozenLock;
Romain Guy06882f82009-06-10 13:36:04 -0700399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 // State management of app transitions. When we are preparing for a
401 // transition, mNextAppTransition will be the kind of transition to
402 // perform or TRANSIT_NONE if we are not waiting. If we are waiting,
403 // mOpeningApps and mClosingApps are the lists of tokens that will be
404 // made visible or hidden at the next transition.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700405 int mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -0700406 String mNextAppTransitionPackage;
407 int mNextAppTransitionEnter;
408 int mNextAppTransitionExit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 boolean mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -0700410 boolean mAppTransitionRunning = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 boolean mAppTransitionTimeout = false;
412 boolean mStartingIconInTransition = false;
413 boolean mSkipAppTransitionAnimation = false;
414 final ArrayList<AppWindowToken> mOpeningApps = new ArrayList<AppWindowToken>();
415 final ArrayList<AppWindowToken> mClosingApps = new ArrayList<AppWindowToken>();
Dianne Hackborna8f60182009-09-01 19:01:50 -0700416 final ArrayList<AppWindowToken> mToTopApps = new ArrayList<AppWindowToken>();
417 final ArrayList<AppWindowToken> mToBottomApps = new ArrayList<AppWindowToken>();
Romain Guy06882f82009-06-10 13:36:04 -0700418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 //flag to detect fat touch events
420 boolean mFatTouch = false;
421 Display mDisplay;
Romain Guy06882f82009-06-10 13:36:04 -0700422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 H mH = new H();
424
425 WindowState mCurrentFocus = null;
426 WindowState mLastFocus = null;
Romain Guy06882f82009-06-10 13:36:04 -0700427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 // This just indicates the window the input method is on top of, not
429 // necessarily the window its input is going to.
430 WindowState mInputMethodTarget = null;
431 WindowState mUpcomingInputMethodTarget = null;
432 boolean mInputMethodTargetWaitingAnim;
433 int mInputMethodAnimLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 WindowState mInputMethodWindow = null;
436 final ArrayList<WindowState> mInputMethodDialogs = new ArrayList<WindowState>();
437
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700438 final ArrayList<WindowToken> mWallpaperTokens = new ArrayList<WindowToken>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700440 // If non-null, this is the currently visible window that is associated
441 // with the wallpaper.
442 WindowState mWallpaperTarget = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700443 // If non-null, we are in the middle of animating from one wallpaper target
444 // to another, and this is the lower one in Z-order.
445 WindowState mLowerWallpaperTarget = null;
446 // If non-null, we are in the middle of animating from one wallpaper target
447 // to another, and this is the higher one in Z-order.
448 WindowState mUpperWallpaperTarget = null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -0700449 int mWallpaperAnimLayerAdjustment;
Dianne Hackborn73e92b42009-10-15 14:29:19 -0700450 float mLastWallpaperX = -1;
451 float mLastWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -0800452 float mLastWallpaperXStep = -1;
453 float mLastWallpaperYStep = -1;
Dianne Hackborn6adba242009-11-10 11:10:09 -0800454 boolean mSendingPointersToWallpaper = false;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700455 // This is set when we are waiting for a wallpaper to tell us it is done
456 // changing its scroll position.
457 WindowState mWaitingOnWallpaper;
458 // The last time we had a timeout when waiting for a wallpaper.
459 long mLastWallpaperTimeoutTime;
460 // We give a wallpaper up to 150ms to finish scrolling.
461 static final long WALLPAPER_TIMEOUT = 150;
462 // Time we wait after a timeout before trying to wait again.
463 static final long WALLPAPER_TIMEOUT_RECOVERY = 10000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 AppWindowToken mFocusedApp = null;
466
467 PowerManagerService mPowerManager;
Romain Guy06882f82009-06-10 13:36:04 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 float mWindowAnimationScale = 1.0f;
470 float mTransitionAnimationScale = 1.0f;
Romain Guy06882f82009-06-10 13:36:04 -0700471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 final KeyWaiter mKeyWaiter = new KeyWaiter();
473 final KeyQ mQueue;
474 final InputDispatcherThread mInputThread;
475
476 // Who is holding the screen on.
477 Session mHoldingScreenOn;
Romain Guy06882f82009-06-10 13:36:04 -0700478
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700479 boolean mTurnOnScreen;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /**
482 * Whether the UI is currently running in touch mode (not showing
483 * navigational focus because the user is directly pressing the screen).
484 */
485 boolean mInTouchMode = false;
486
487 private ViewServer mViewServer;
488
489 final Rect mTempRect = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -0700490
Dianne Hackbornc485a602009-03-24 22:39:49 -0700491 final Configuration mTempConfiguration = new Configuration();
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700492 int mScreenLayout = Configuration.SCREENLAYOUT_SIZE_UNDEFINED;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700493
494 // The frame use to limit the size of the app running in compatibility mode.
495 Rect mCompatibleScreenFrame = new Rect();
496 // The surface used to fill the outer rim of the app running in compatibility mode.
497 Surface mBackgroundFillerSurface = null;
498 boolean mBackgroundFillerShown = false;
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public static WindowManagerService main(Context context,
501 PowerManagerService pm, boolean haveInputMethods) {
502 WMThread thr = new WMThread(context, pm, haveInputMethods);
503 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 synchronized (thr) {
506 while (thr.mService == null) {
507 try {
508 thr.wait();
509 } catch (InterruptedException e) {
510 }
511 }
512 }
Romain Guy06882f82009-06-10 13:36:04 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 return thr.mService;
515 }
Romain Guy06882f82009-06-10 13:36:04 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 static class WMThread extends Thread {
518 WindowManagerService mService;
Romain Guy06882f82009-06-10 13:36:04 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 private final Context mContext;
521 private final PowerManagerService mPM;
522 private final boolean mHaveInputMethods;
Romain Guy06882f82009-06-10 13:36:04 -0700523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public WMThread(Context context, PowerManagerService pm,
525 boolean haveInputMethods) {
526 super("WindowManager");
527 mContext = context;
528 mPM = pm;
529 mHaveInputMethods = haveInputMethods;
530 }
Romain Guy06882f82009-06-10 13:36:04 -0700531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public void run() {
533 Looper.prepare();
534 WindowManagerService s = new WindowManagerService(mContext, mPM,
535 mHaveInputMethods);
536 android.os.Process.setThreadPriority(
537 android.os.Process.THREAD_PRIORITY_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -0700538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 synchronized (this) {
540 mService = s;
541 notifyAll();
542 }
Romain Guy06882f82009-06-10 13:36:04 -0700543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 Looper.loop();
545 }
546 }
547
548 static class PolicyThread extends Thread {
549 private final WindowManagerPolicy mPolicy;
550 private final WindowManagerService mService;
551 private final Context mContext;
552 private final PowerManagerService mPM;
553 boolean mRunning = false;
Romain Guy06882f82009-06-10 13:36:04 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 public PolicyThread(WindowManagerPolicy policy,
556 WindowManagerService service, Context context,
557 PowerManagerService pm) {
558 super("WindowManagerPolicy");
559 mPolicy = policy;
560 mService = service;
561 mContext = context;
562 mPM = pm;
563 }
Romain Guy06882f82009-06-10 13:36:04 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public void run() {
566 Looper.prepare();
Dianne Hackbornac3587d2010-03-11 11:12:11 -0800567 WindowManagerPolicyThread.set(this, Looper.myLooper());
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 //Looper.myLooper().setMessageLogging(new LogPrinter(
Joe Onorato8a9b2202010-02-26 18:56:32 -0800570 // Log.VERBOSE, "WindowManagerPolicy", Log.LOG_ID_SYSTEM));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 android.os.Process.setThreadPriority(
572 android.os.Process.THREAD_PRIORITY_FOREGROUND);
573 mPolicy.init(mContext, mService, mPM);
Romain Guy06882f82009-06-10 13:36:04 -0700574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 synchronized (this) {
576 mRunning = true;
577 notifyAll();
578 }
Romain Guy06882f82009-06-10 13:36:04 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 Looper.loop();
581 }
582 }
583
584 private WindowManagerService(Context context, PowerManagerService pm,
585 boolean haveInputMethods) {
Michael Chan53071d62009-05-13 17:29:48 -0700586 if (MEASURE_LATENCY) {
587 lt = new LatencyTimer(100, 1000);
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 mContext = context;
591 mHaveInputMethods = haveInputMethods;
592 mLimitedAlphaCompositing = context.getResources().getBoolean(
593 com.android.internal.R.bool.config_sf_limitedAlpha);
Romain Guy06882f82009-06-10 13:36:04 -0700594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 mPowerManager = pm;
596 mPowerManager.setPolicy(mPolicy);
597 PowerManager pmc = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
598 mScreenFrozenLock = pmc.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
599 "SCREEN_FROZEN");
600 mScreenFrozenLock.setReferenceCounted(false);
601
602 mActivityManager = ActivityManagerNative.getDefault();
603 mBatteryStats = BatteryStatsService.getService();
604
605 // Get persisted window scale setting
606 mWindowAnimationScale = Settings.System.getFloat(context.getContentResolver(),
607 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
608 mTransitionAnimationScale = Settings.System.getFloat(context.getContentResolver(),
609 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
Romain Guy06882f82009-06-10 13:36:04 -0700610
Michael Chan9f028e62009-08-04 17:37:46 -0700611 int max_events_per_sec = 35;
612 try {
613 max_events_per_sec = Integer.parseInt(SystemProperties
614 .get("windowsmgr.max_events_per_sec"));
615 if (max_events_per_sec < 1) {
616 max_events_per_sec = 35;
617 }
618 } catch (NumberFormatException e) {
619 }
620 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mQueue = new KeyQ();
623
624 mInputThread = new InputDispatcherThread();
Romain Guy06882f82009-06-10 13:36:04 -0700625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
627 thr.start();
Romain Guy06882f82009-06-10 13:36:04 -0700628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 synchronized (thr) {
630 while (!thr.mRunning) {
631 try {
632 thr.wait();
633 } catch (InterruptedException e) {
634 }
635 }
636 }
Romain Guy06882f82009-06-10 13:36:04 -0700637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 mInputThread.start();
Romain Guy06882f82009-06-10 13:36:04 -0700639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 // Add ourself to the Watchdog monitors.
641 Watchdog.getInstance().addMonitor(this);
642 }
643
644 @Override
645 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
646 throws RemoteException {
647 try {
648 return super.onTransact(code, data, reply, flags);
649 } catch (RuntimeException e) {
650 // The window manager only throws security exceptions, so let's
651 // log all others.
652 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800653 Slog.e(TAG, "Window Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655 throw e;
656 }
657 }
658
659 private void placeWindowAfter(Object pos, WindowState window) {
660 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800661 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 TAG, "Adding window " + window + " at "
663 + (i+1) + " of " + mWindows.size() + " (after " + pos + ")");
664 mWindows.add(i+1, window);
665 }
666
667 private void placeWindowBefore(Object pos, WindowState window) {
668 final int i = mWindows.indexOf(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800669 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 TAG, "Adding window " + window + " at "
671 + i + " of " + mWindows.size() + " (before " + pos + ")");
672 mWindows.add(i, window);
673 }
674
675 //This method finds out the index of a window that has the same app token as
676 //win. used for z ordering the windows in mWindows
677 private int findIdxBasedOnAppTokens(WindowState win) {
678 //use a local variable to cache mWindows
679 ArrayList localmWindows = mWindows;
680 int jmax = localmWindows.size();
681 if(jmax == 0) {
682 return -1;
683 }
684 for(int j = (jmax-1); j >= 0; j--) {
685 WindowState wentry = (WindowState)localmWindows.get(j);
686 if(wentry.mAppToken == win.mAppToken) {
687 return j;
688 }
689 }
690 return -1;
691 }
Romain Guy06882f82009-06-10 13:36:04 -0700692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 private void addWindowToListInOrderLocked(WindowState win, boolean addToToken) {
694 final IWindow client = win.mClient;
695 final WindowToken token = win.mToken;
696 final ArrayList localmWindows = mWindows;
Romain Guy06882f82009-06-10 13:36:04 -0700697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 final int N = localmWindows.size();
699 final WindowState attached = win.mAttachedWindow;
700 int i;
701 if (attached == null) {
702 int tokenWindowsPos = token.windows.size();
703 if (token.appWindowToken != null) {
704 int index = tokenWindowsPos-1;
705 if (index >= 0) {
706 // If this application has existing windows, we
707 // simply place the new window on top of them... but
708 // keep the starting window on top.
709 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
710 // Base windows go behind everything else.
711 placeWindowBefore(token.windows.get(0), win);
712 tokenWindowsPos = 0;
713 } else {
714 AppWindowToken atoken = win.mAppToken;
715 if (atoken != null &&
716 token.windows.get(index) == atoken.startingWindow) {
717 placeWindowBefore(token.windows.get(index), win);
718 tokenWindowsPos--;
719 } else {
720 int newIdx = findIdxBasedOnAppTokens(win);
721 if(newIdx != -1) {
Romain Guy06882f82009-06-10 13:36:04 -0700722 //there is a window above this one associated with the same
723 //apptoken note that the window could be a floating window
724 //that was created later or a window at the top of the list of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 //windows associated with this token.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700727 TAG, "Adding window " + win + " at "
728 + (newIdx+1) + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 localmWindows.add(newIdx+1, win);
Romain Guy06882f82009-06-10 13:36:04 -0700730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732 }
733 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 TAG, "Figuring out where to add app window "
736 + client.asBinder() + " (token=" + token + ")");
737 // Figure out where the window should go, based on the
738 // order of applications.
739 final int NA = mAppTokens.size();
740 Object pos = null;
741 for (i=NA-1; i>=0; i--) {
742 AppWindowToken t = mAppTokens.get(i);
743 if (t == token) {
744 i--;
745 break;
746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Dianne Hackborna8f60182009-09-01 19:01:50 -0700748 // We haven't reached the token yet; if this token
749 // is not going to the bottom and has windows, we can
750 // use it as an anchor for when we do reach the token.
751 if (!t.sendingToBottom && t.windows.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 pos = t.windows.get(0);
753 }
754 }
755 // We now know the index into the apps. If we found
756 // an app window above, that gives us the position; else
757 // we need to look some more.
758 if (pos != null) {
759 // Move behind any windows attached to this one.
Romain Guy06882f82009-06-10 13:36:04 -0700760 WindowToken atoken =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 mTokenMap.get(((WindowState)pos).mClient.asBinder());
762 if (atoken != null) {
763 final int NC = atoken.windows.size();
764 if (NC > 0) {
765 WindowState bottom = atoken.windows.get(0);
766 if (bottom.mSubLayer < 0) {
767 pos = bottom;
768 }
769 }
770 }
771 placeWindowBefore(pos, win);
772 } else {
Dianne Hackborna8f60182009-09-01 19:01:50 -0700773 // Continue looking down until we find the first
774 // token that has windows.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 while (i >= 0) {
776 AppWindowToken t = mAppTokens.get(i);
777 final int NW = t.windows.size();
778 if (NW > 0) {
779 pos = t.windows.get(NW-1);
780 break;
781 }
782 i--;
783 }
784 if (pos != null) {
785 // Move in front of any windows attached to this
786 // one.
787 WindowToken atoken =
788 mTokenMap.get(((WindowState)pos).mClient.asBinder());
789 if (atoken != null) {
790 final int NC = atoken.windows.size();
791 if (NC > 0) {
792 WindowState top = atoken.windows.get(NC-1);
793 if (top.mSubLayer >= 0) {
794 pos = top;
795 }
796 }
797 }
798 placeWindowAfter(pos, win);
799 } else {
800 // Just search for the start of this layer.
801 final int myLayer = win.mBaseLayer;
802 for (i=0; i<N; i++) {
803 WindowState w = (WindowState)localmWindows.get(i);
804 if (w.mBaseLayer > myLayer) {
805 break;
806 }
807 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800808 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700809 TAG, "Adding window " + win + " at "
810 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 localmWindows.add(i, win);
812 }
813 }
814 }
815 } else {
816 // Figure out where window should go, based on layer.
817 final int myLayer = win.mBaseLayer;
818 for (i=N-1; i>=0; i--) {
819 if (((WindowState)localmWindows.get(i)).mBaseLayer <= myLayer) {
820 i++;
821 break;
822 }
823 }
824 if (i < 0) i = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800825 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700826 TAG, "Adding window " + win + " at "
827 + i + " of " + N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 localmWindows.add(i, win);
829 }
830 if (addToToken) {
831 token.windows.add(tokenWindowsPos, win);
832 }
833
834 } else {
835 // Figure out this window's ordering relative to the window
836 // it is attached to.
837 final int NA = token.windows.size();
838 final int sublayer = win.mSubLayer;
839 int largestSublayer = Integer.MIN_VALUE;
840 WindowState windowWithLargestSublayer = null;
841 for (i=0; i<NA; i++) {
842 WindowState w = token.windows.get(i);
843 final int wSublayer = w.mSubLayer;
844 if (wSublayer >= largestSublayer) {
845 largestSublayer = wSublayer;
846 windowWithLargestSublayer = w;
847 }
848 if (sublayer < 0) {
849 // For negative sublayers, we go below all windows
850 // in the same sublayer.
851 if (wSublayer >= sublayer) {
852 if (addToToken) {
853 token.windows.add(i, win);
854 }
855 placeWindowBefore(
856 wSublayer >= 0 ? attached : w, win);
857 break;
858 }
859 } else {
860 // For positive sublayers, we go above all windows
861 // in the same sublayer.
862 if (wSublayer > sublayer) {
863 if (addToToken) {
864 token.windows.add(i, win);
865 }
866 placeWindowBefore(w, win);
867 break;
868 }
869 }
870 }
871 if (i >= NA) {
872 if (addToToken) {
873 token.windows.add(win);
874 }
875 if (sublayer < 0) {
876 placeWindowBefore(attached, win);
877 } else {
878 placeWindowAfter(largestSublayer >= 0
879 ? windowWithLargestSublayer
880 : attached,
881 win);
882 }
883 }
884 }
Romain Guy06882f82009-06-10 13:36:04 -0700885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (win.mAppToken != null && addToToken) {
887 win.mAppToken.allAppWindows.add(win);
888 }
889 }
Romain Guy06882f82009-06-10 13:36:04 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 static boolean canBeImeTarget(WindowState w) {
892 final int fl = w.mAttrs.flags
893 & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
894 if (fl == 0 || fl == (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM)) {
895 return w.isVisibleOrAdding();
896 }
897 return false;
898 }
Romain Guy06882f82009-06-10 13:36:04 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 int findDesiredInputMethodWindowIndexLocked(boolean willMove) {
901 final ArrayList localmWindows = mWindows;
902 final int N = localmWindows.size();
903 WindowState w = null;
904 int i = N;
905 while (i > 0) {
906 i--;
907 w = (WindowState)localmWindows.get(i);
Romain Guy06882f82009-06-10 13:36:04 -0700908
Joe Onorato8a9b2202010-02-26 18:56:32 -0800909 //Slog.i(TAG, "Checking window @" + i + " " + w + " fl=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 // + Integer.toHexString(w.mAttrs.flags));
911 if (canBeImeTarget(w)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800912 //Slog.i(TAG, "Putting input method here!");
Romain Guy06882f82009-06-10 13:36:04 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 // Yet more tricksyness! If this window is a "starting"
915 // window, we do actually want to be on top of it, but
916 // it is not -really- where input will go. So if the caller
917 // is not actually looking to move the IME, look down below
918 // for a real window to target...
919 if (!willMove
920 && w.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
921 && i > 0) {
922 WindowState wb = (WindowState)localmWindows.get(i-1);
923 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
924 i--;
925 w = wb;
926 }
927 }
928 break;
929 }
930 }
Romain Guy06882f82009-06-10 13:36:04 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 mUpcomingInputMethodTarget = w;
Romain Guy06882f82009-06-10 13:36:04 -0700933
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Desired input method target="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 + w + " willMove=" + willMove);
Romain Guy06882f82009-06-10 13:36:04 -0700936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 if (willMove && w != null) {
938 final WindowState curTarget = mInputMethodTarget;
939 if (curTarget != null && curTarget.mAppToken != null) {
Romain Guy06882f82009-06-10 13:36:04 -0700940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 // Now some fun for dealing with window animations that
942 // modify the Z order. We need to look at all windows below
943 // the current target that are in this app, finding the highest
944 // visible one in layering.
945 AppWindowToken token = curTarget.mAppToken;
946 WindowState highestTarget = null;
947 int highestPos = 0;
948 if (token.animating || token.animation != null) {
949 int pos = 0;
950 pos = localmWindows.indexOf(curTarget);
951 while (pos >= 0) {
952 WindowState win = (WindowState)localmWindows.get(pos);
953 if (win.mAppToken != token) {
954 break;
955 }
956 if (!win.mRemoved) {
957 if (highestTarget == null || win.mAnimLayer >
958 highestTarget.mAnimLayer) {
959 highestTarget = win;
960 highestPos = pos;
961 }
962 }
963 pos--;
964 }
965 }
Romain Guy06882f82009-06-10 13:36:04 -0700966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 if (highestTarget != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800968 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "mNextAppTransition="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 + mNextAppTransition + " " + highestTarget
970 + " animating=" + highestTarget.isAnimating()
971 + " layer=" + highestTarget.mAnimLayer
972 + " new layer=" + w.mAnimLayer);
Romain Guy06882f82009-06-10 13:36:04 -0700973
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700974 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 // If we are currently setting up for an animation,
976 // hold everything until we can find out what will happen.
977 mInputMethodTargetWaitingAnim = true;
978 mInputMethodTarget = highestTarget;
979 return highestPos + 1;
980 } else if (highestTarget.isAnimating() &&
981 highestTarget.mAnimLayer > w.mAnimLayer) {
982 // If the window we are currently targeting is involved
983 // with an animation, and it is on top of the next target
984 // we will be over, then hold off on moving until
985 // that is done.
986 mInputMethodTarget = highestTarget;
987 return highestPos + 1;
988 }
989 }
990 }
991 }
Romain Guy06882f82009-06-10 13:36:04 -0700992
Joe Onorato8a9b2202010-02-26 18:56:32 -0800993 //Slog.i(TAG, "Placing input method @" + (i+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (w != null) {
995 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -0800996 if (DEBUG_INPUT_METHOD) {
997 RuntimeException e = null;
998 if (!HIDE_STACK_CRAWLS) {
999 e = new RuntimeException();
1000 e.fillInStackTrace();
1001 }
1002 Slog.w(TAG, "Moving IM target from "
1003 + mInputMethodTarget + " to " + w, e);
1004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 mInputMethodTarget = w;
1006 if (w.mAppToken != null) {
1007 setInputMethodAnimLayerAdjustment(w.mAppToken.animLayerAdjustment);
1008 } else {
1009 setInputMethodAnimLayerAdjustment(0);
1010 }
1011 }
1012 return i+1;
1013 }
1014 if (willMove) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08001015 if (DEBUG_INPUT_METHOD) {
1016 RuntimeException e = null;
1017 if (!HIDE_STACK_CRAWLS) {
1018 e = new RuntimeException();
1019 e.fillInStackTrace();
1020 }
1021 Slog.w(TAG, "Moving IM target from "
1022 + mInputMethodTarget + " to null", e);
1023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 mInputMethodTarget = null;
1025 setInputMethodAnimLayerAdjustment(0);
1026 }
1027 return -1;
1028 }
Romain Guy06882f82009-06-10 13:36:04 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 void addInputMethodWindowToListLocked(WindowState win) {
1031 int pos = findDesiredInputMethodWindowIndexLocked(true);
1032 if (pos >= 0) {
1033 win.mTargetAppToken = mInputMethodTarget.mAppToken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001034 if (DEBUG_WINDOW_MOVEMENT) Slog.v(
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001035 TAG, "Adding input method window " + win + " at " + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 mWindows.add(pos, win);
1037 moveInputMethodDialogsLocked(pos+1);
1038 return;
1039 }
1040 win.mTargetAppToken = null;
1041 addWindowToListInOrderLocked(win, true);
1042 moveInputMethodDialogsLocked(pos);
1043 }
Romain Guy06882f82009-06-10 13:36:04 -07001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 void setInputMethodAnimLayerAdjustment(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001046 if (DEBUG_LAYERS) Slog.v(TAG, "Setting im layer adj to " + adj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 mInputMethodAnimLayerAdjustment = adj;
1048 WindowState imw = mInputMethodWindow;
1049 if (imw != null) {
1050 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 + " anim layer: " + imw.mAnimLayer);
1053 int wi = imw.mChildWindows.size();
1054 while (wi > 0) {
1055 wi--;
1056 WindowState cw = (WindowState)imw.mChildWindows.get(wi);
1057 cw.mAnimLayer = cw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + cw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 + " anim layer: " + cw.mAnimLayer);
1060 }
1061 }
1062 int di = mInputMethodDialogs.size();
1063 while (di > 0) {
1064 di --;
1065 imw = mInputMethodDialogs.get(di);
1066 imw.mAnimLayer = imw.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001067 if (DEBUG_LAYERS) Slog.v(TAG, "IM win " + imw
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 + " anim layer: " + imw.mAnimLayer);
1069 }
1070 }
Romain Guy06882f82009-06-10 13:36:04 -07001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 private int tmpRemoveWindowLocked(int interestingPos, WindowState win) {
1073 int wpos = mWindows.indexOf(win);
1074 if (wpos >= 0) {
1075 if (wpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001076 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing at " + wpos + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 mWindows.remove(wpos);
1078 int NC = win.mChildWindows.size();
1079 while (NC > 0) {
1080 NC--;
1081 WindowState cw = (WindowState)win.mChildWindows.get(NC);
1082 int cpos = mWindows.indexOf(cw);
1083 if (cpos >= 0) {
1084 if (cpos < interestingPos) interestingPos--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001085 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Temp removing child at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001086 + cpos + ": " + cw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 mWindows.remove(cpos);
1088 }
1089 }
1090 }
1091 return interestingPos;
1092 }
Romain Guy06882f82009-06-10 13:36:04 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 private void reAddWindowToListInOrderLocked(WindowState win) {
1095 addWindowToListInOrderLocked(win, false);
1096 // This is a hack to get all of the child windows added as well
1097 // at the right position. Child windows should be rare and
1098 // this case should be rare, so it shouldn't be that big a deal.
1099 int wpos = mWindows.indexOf(win);
1100 if (wpos >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001101 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "ReAdd removing from " + wpos
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001102 + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 mWindows.remove(wpos);
1104 reAddWindowLocked(wpos, win);
1105 }
1106 }
Romain Guy06882f82009-06-10 13:36:04 -07001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 void logWindowList(String prefix) {
1109 int N = mWindows.size();
1110 while (N > 0) {
1111 N--;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001112 Slog.v(TAG, prefix + "#" + N + ": " + mWindows.get(N));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 }
Romain Guy06882f82009-06-10 13:36:04 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 void moveInputMethodDialogsLocked(int pos) {
1117 ArrayList<WindowState> dialogs = mInputMethodDialogs;
Romain Guy06882f82009-06-10 13:36:04 -07001118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 final int N = dialogs.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Removing " + N + " dialogs w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 for (int i=0; i<N; i++) {
1122 pos = tmpRemoveWindowLocked(pos, dialogs.get(i));
1123 }
1124 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.v(TAG, "Window list w/pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 logWindowList(" ");
1127 }
Romain Guy06882f82009-06-10 13:36:04 -07001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 if (pos >= 0) {
1130 final AppWindowToken targetAppToken = mInputMethodTarget.mAppToken;
1131 if (pos < mWindows.size()) {
1132 WindowState wp = (WindowState)mWindows.get(pos);
1133 if (wp == mInputMethodWindow) {
1134 pos++;
1135 }
1136 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001137 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Adding " + N + " dialogs at pos=" + pos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 for (int i=0; i<N; i++) {
1139 WindowState win = dialogs.get(i);
1140 win.mTargetAppToken = targetAppToken;
1141 pos = reAddWindowLocked(pos, win);
1142 }
1143 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 logWindowList(" ");
1146 }
1147 return;
1148 }
1149 for (int i=0; i<N; i++) {
1150 WindowState win = dialogs.get(i);
1151 win.mTargetAppToken = null;
1152 reAddWindowToListInOrderLocked(win);
1153 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001154 Slog.v(TAG, "No IM target, final list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 logWindowList(" ");
1156 }
1157 }
1158 }
Romain Guy06882f82009-06-10 13:36:04 -07001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 boolean moveInputMethodWindowsIfNeededLocked(boolean needAssignLayers) {
1161 final WindowState imWin = mInputMethodWindow;
1162 final int DN = mInputMethodDialogs.size();
1163 if (imWin == null && DN == 0) {
1164 return false;
1165 }
Romain Guy06882f82009-06-10 13:36:04 -07001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 int imPos = findDesiredInputMethodWindowIndexLocked(true);
1168 if (imPos >= 0) {
1169 // In this case, the input method windows are to be placed
1170 // immediately above the window they are targeting.
Romain Guy06882f82009-06-10 13:36:04 -07001171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 // First check to see if the input method windows are already
1173 // located here, and contiguous.
1174 final int N = mWindows.size();
1175 WindowState firstImWin = imPos < N
1176 ? (WindowState)mWindows.get(imPos) : null;
Romain Guy06882f82009-06-10 13:36:04 -07001177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // Figure out the actual input method window that should be
1179 // at the bottom of their stack.
1180 WindowState baseImWin = imWin != null
1181 ? imWin : mInputMethodDialogs.get(0);
1182 if (baseImWin.mChildWindows.size() > 0) {
1183 WindowState cw = (WindowState)baseImWin.mChildWindows.get(0);
1184 if (cw.mSubLayer < 0) baseImWin = cw;
1185 }
Romain Guy06882f82009-06-10 13:36:04 -07001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (firstImWin == baseImWin) {
1188 // The windows haven't moved... but are they still contiguous?
1189 // First find the top IM window.
1190 int pos = imPos+1;
1191 while (pos < N) {
1192 if (!((WindowState)mWindows.get(pos)).mIsImWindow) {
1193 break;
1194 }
1195 pos++;
1196 }
1197 pos++;
1198 // Now there should be no more input method windows above.
1199 while (pos < N) {
1200 if (((WindowState)mWindows.get(pos)).mIsImWindow) {
1201 break;
1202 }
1203 pos++;
1204 }
1205 if (pos >= N) {
1206 // All is good!
1207 return false;
1208 }
1209 }
Romain Guy06882f82009-06-10 13:36:04 -07001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 if (imWin != null) {
1212 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001213 Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 logWindowList(" ");
1215 }
1216 imPos = tmpRemoveWindowLocked(imPos, imWin);
1217 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001218 Slog.v(TAG, "List after moving with new pos " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 logWindowList(" ");
1220 }
1221 imWin.mTargetAppToken = mInputMethodTarget.mAppToken;
1222 reAddWindowLocked(imPos, imWin);
1223 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001224 Slog.v(TAG, "List after moving IM to " + imPos + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 logWindowList(" ");
1226 }
1227 if (DN > 0) moveInputMethodDialogsLocked(imPos+1);
1228 } else {
1229 moveInputMethodDialogsLocked(imPos);
1230 }
Romain Guy06882f82009-06-10 13:36:04 -07001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 } else {
1233 // In this case, the input method windows go in a fixed layer,
1234 // because they aren't currently associated with a focus window.
Romain Guy06882f82009-06-10 13:36:04 -07001235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 if (imWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 if (DEBUG_INPUT_METHOD) Slog.v(TAG, "Moving IM from " + imPos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 tmpRemoveWindowLocked(0, imWin);
1239 imWin.mTargetAppToken = null;
1240 reAddWindowToListInOrderLocked(imWin);
1241 if (DEBUG_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 Slog.v(TAG, "List with no IM target:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 logWindowList(" ");
1244 }
1245 if (DN > 0) moveInputMethodDialogsLocked(-1);;
1246 } else {
1247 moveInputMethodDialogsLocked(-1);;
1248 }
Romain Guy06882f82009-06-10 13:36:04 -07001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
Romain Guy06882f82009-06-10 13:36:04 -07001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 if (needAssignLayers) {
1253 assignLayersLocked();
1254 }
Romain Guy06882f82009-06-10 13:36:04 -07001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 return true;
1257 }
Romain Guy06882f82009-06-10 13:36:04 -07001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 void adjustInputMethodDialogsLocked() {
1260 moveInputMethodDialogsLocked(findDesiredInputMethodWindowIndexLocked(true));
1261 }
Romain Guy06882f82009-06-10 13:36:04 -07001262
Dianne Hackborn25994b42009-09-04 14:21:19 -07001263 final boolean isWallpaperVisible(WindowState wallpaperTarget) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001264 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper vis: target obscured="
Dianne Hackborn25994b42009-09-04 14:21:19 -07001265 + (wallpaperTarget != null ? Boolean.toString(wallpaperTarget.mObscured) : "??")
1266 + " anim=" + ((wallpaperTarget != null && wallpaperTarget.mAppToken != null)
1267 ? wallpaperTarget.mAppToken.animation : null)
1268 + " upper=" + mUpperWallpaperTarget
1269 + " lower=" + mLowerWallpaperTarget);
1270 return (wallpaperTarget != null
1271 && (!wallpaperTarget.mObscured || (wallpaperTarget.mAppToken != null
1272 && wallpaperTarget.mAppToken.animation != null)))
1273 || mUpperWallpaperTarget != null
1274 || mLowerWallpaperTarget != null;
1275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001277 static final int ADJUST_WALLPAPER_LAYERS_CHANGED = 1<<1;
1278 static final int ADJUST_WALLPAPER_VISIBILITY_CHANGED = 1<<2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001279
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001280 int adjustWallpaperWindowsLocked() {
1281 int changed = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001283 final int dw = mDisplay.getWidth();
1284 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001286 // First find top-most window that has asked to be on top of the
1287 // wallpaper; all wallpapers go behind it.
1288 final ArrayList localmWindows = mWindows;
1289 int N = localmWindows.size();
1290 WindowState w = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001291 WindowState foundW = null;
1292 int foundI = 0;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001293 WindowState topCurW = null;
1294 int topCurI = 0;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001295 int i = N;
1296 while (i > 0) {
1297 i--;
1298 w = (WindowState)localmWindows.get(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001299 if ((w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER)) {
1300 if (topCurW == null) {
1301 topCurW = w;
1302 topCurI = i;
1303 }
1304 continue;
1305 }
1306 topCurW = null;
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001307 if (w.mAppToken != null) {
1308 // If this window's app token is hidden and not animating,
1309 // it is of no interest to us.
1310 if (w.mAppToken.hidden && w.mAppToken.animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001312 "Skipping hidden or animating token: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001313 topCurW = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001314 continue;
1315 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001316 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001317 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w + ": readyfordisplay="
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001318 + w.isReadyForDisplay() + " drawpending=" + w.mDrawPending
1319 + " commitdrawpending=" + w.mCommitDrawPending);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001320 if ((w.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0 && w.isReadyForDisplay()
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07001321 && (mWallpaperTarget == w
1322 || (!w.mDrawPending && !w.mCommitDrawPending))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001323 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001324 "Found wallpaper activity: #" + i + "=" + w);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001325 foundW = w;
1326 foundI = i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001327 if (w == mWallpaperTarget && ((w.mAppToken != null
1328 && w.mAppToken.animation != null)
1329 || w.mAnimation != null)) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001330 // The current wallpaper target is animating, so we'll
1331 // look behind it for another possible target and figure
1332 // out what is going on below.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001333 if (DEBUG_WALLPAPER) Slog.v(TAG, "Win " + w
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001334 + ": token animating, looking behind.");
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001335 continue;
1336 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001337 break;
1338 }
1339 }
1340
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07001341 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001342 // If we are currently waiting for an app transition, and either
1343 // the current target or the next target are involved with it,
1344 // then hold off on doing anything with the wallpaper.
1345 // Note that we are checking here for just whether the target
1346 // is part of an app token... which is potentially overly aggressive
1347 // (the app token may not be involved in the transition), but good
1348 // enough (we'll just wait until whatever transition is pending
1349 // executes).
1350 if (mWallpaperTarget != null && mWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001351 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001352 "Wallpaper not changing: waiting for app anim in current target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001353 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001354 }
1355 if (foundW != null && foundW.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001356 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001357 "Wallpaper not changing: waiting for app anim in found target");
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001358 return 0;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001359 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001361
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001362 if (mWallpaperTarget != foundW) {
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001363 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 Slog.v(TAG, "New wallpaper target: " + foundW
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001365 + " oldTarget: " + mWallpaperTarget);
1366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001368 mLowerWallpaperTarget = null;
1369 mUpperWallpaperTarget = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001371 WindowState oldW = mWallpaperTarget;
1372 mWallpaperTarget = foundW;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001374 // Now what is happening... if the current and new targets are
1375 // animating, then we are in our super special mode!
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001376 if (foundW != null && oldW != null) {
1377 boolean oldAnim = oldW.mAnimation != null
1378 || (oldW.mAppToken != null && oldW.mAppToken.animation != null);
1379 boolean foundAnim = foundW.mAnimation != null
1380 || (foundW.mAppToken != null && foundW.mAppToken.animation != null);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001381 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 Slog.v(TAG, "New animation: " + foundAnim
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001383 + " old animation: " + oldAnim);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001384 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001385 if (foundAnim && oldAnim) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001386 int oldI = localmWindows.indexOf(oldW);
1387 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 Slog.v(TAG, "New i: " + foundI + " old i: " + oldI);
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001389 }
1390 if (oldI >= 0) {
1391 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001392 Slog.v(TAG, "Animating wallpapers: old#" + oldI
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001393 + "=" + oldW + "; new#" + foundI
1394 + "=" + foundW);
1395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001396
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001397 // Set the new target correctly.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001398 if (foundW.mAppToken != null && foundW.mAppToken.hiddenRequested) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001399 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001400 Slog.v(TAG, "Old wallpaper still the target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001401 }
1402 mWallpaperTarget = oldW;
1403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001405 // Now set the upper and lower wallpaper targets
1406 // correctly, and make sure that we are positioning
1407 // the wallpaper below the lower.
1408 if (foundI > oldI) {
1409 // The new target is on top of the old one.
1410 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001411 Slog.v(TAG, "Found target above old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001412 }
1413 mUpperWallpaperTarget = foundW;
1414 mLowerWallpaperTarget = oldW;
1415 foundW = oldW;
1416 foundI = oldI;
1417 } else {
1418 // The new target is below the old one.
1419 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001420 Slog.v(TAG, "Found target below old target.");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001421 }
1422 mUpperWallpaperTarget = oldW;
1423 mLowerWallpaperTarget = foundW;
1424 }
1425 }
1426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001429 } else if (mLowerWallpaperTarget != null) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001430 // Is it time to stop animating?
Dianne Hackborn6b1cb352009-09-28 18:27:26 -07001431 boolean lowerAnimating = mLowerWallpaperTarget.mAnimation != null
1432 || (mLowerWallpaperTarget.mAppToken != null
1433 && mLowerWallpaperTarget.mAppToken.animation != null);
1434 boolean upperAnimating = mUpperWallpaperTarget.mAnimation != null
1435 || (mUpperWallpaperTarget.mAppToken != null
1436 && mUpperWallpaperTarget.mAppToken.animation != null);
1437 if (!lowerAnimating || !upperAnimating) {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001438 if (DEBUG_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 Slog.v(TAG, "No longer animating wallpaper targets!");
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001440 }
1441 mLowerWallpaperTarget = null;
1442 mUpperWallpaperTarget = null;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001443 }
1444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001446 boolean visible = foundW != null;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001447 if (visible) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001448 // The window is visible to the compositor... but is it visible
1449 // to the user? That is what the wallpaper cares about.
Dianne Hackborn25994b42009-09-04 14:21:19 -07001450 visible = isWallpaperVisible(foundW);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001451 if (DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper visibility: " + visible);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001453 // If the wallpaper target is animating, we may need to copy
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001454 // its layer adjustment. Only do this if we are not transfering
1455 // between two wallpaper targets.
1456 mWallpaperAnimLayerAdjustment =
Dianne Hackborn3be63c02009-08-20 19:31:38 -07001457 (mLowerWallpaperTarget == null && foundW.mAppToken != null)
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001458 ? foundW.mAppToken.animLayerAdjustment : 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001460 final int maxLayer = mPolicy.getMaxWallpaperLayer()
1461 * TYPE_LAYER_MULTIPLIER
1462 + TYPE_LAYER_OFFSET;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001464 // Now w is the window we are supposed to be behind... but we
1465 // need to be sure to also be behind any of its attached windows,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001466 // AND any starting window associated with it, AND below the
1467 // maximum layer the policy allows for wallpapers.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001468 while (foundI > 0) {
1469 WindowState wb = (WindowState)localmWindows.get(foundI-1);
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07001470 if (wb.mBaseLayer < maxLayer &&
1471 wb.mAttachedWindow != foundW &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001472 (wb.mAttrs.type != TYPE_APPLICATION_STARTING ||
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001473 wb.mToken != foundW.mToken)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001474 // This window is not related to the previous one in any
1475 // interesting way, so stop here.
1476 break;
1477 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001478 foundW = wb;
1479 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001480 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07001481 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG_WALLPAPER) Slog.v(TAG, "No wallpaper target");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07001485 if (foundW == null && topCurW != null) {
1486 // There is no wallpaper target, so it goes at the bottom.
1487 // We will assume it is the same place as last time, if known.
1488 foundW = topCurW;
1489 foundI = topCurI+1;
1490 } else {
1491 // Okay i is the position immediately above the wallpaper. Look at
1492 // what is below it for later.
1493 foundW = foundI > 0 ? (WindowState)localmWindows.get(foundI-1) : null;
1494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Dianne Hackborn284ac932009-08-28 10:34:25 -07001496 if (visible) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001497 if (mWallpaperTarget.mWallpaperX >= 0) {
1498 mLastWallpaperX = mWallpaperTarget.mWallpaperX;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001499 mLastWallpaperXStep = mWallpaperTarget.mWallpaperXStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001500 }
1501 if (mWallpaperTarget.mWallpaperY >= 0) {
1502 mLastWallpaperY = mWallpaperTarget.mWallpaperY;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001503 mLastWallpaperYStep = mWallpaperTarget.mWallpaperYStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001504 }
Dianne Hackborn284ac932009-08-28 10:34:25 -07001505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001507 // Start stepping backwards from here, ensuring that our wallpaper windows
1508 // are correctly placed.
1509 int curTokenIndex = mWallpaperTokens.size();
1510 while (curTokenIndex > 0) {
1511 curTokenIndex--;
1512 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001513 if (token.hidden == visible) {
1514 changed |= ADJUST_WALLPAPER_VISIBILITY_CHANGED;
1515 token.hidden = !visible;
1516 // Need to do a layout to ensure the wallpaper now has the
1517 // correct size.
1518 mLayoutNeeded = true;
1519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001521 int curWallpaperIndex = token.windows.size();
1522 while (curWallpaperIndex > 0) {
1523 curWallpaperIndex--;
1524 WindowState wallpaper = token.windows.get(curWallpaperIndex);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001526 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001527 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001530 // First, make sure the client has the current visibility
1531 // state.
1532 if (wallpaper.mWallpaperVisible != visible) {
1533 wallpaper.mWallpaperVisible = visible;
1534 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001536 "Setting visibility of wallpaper " + wallpaper
1537 + ": " + visible);
1538 wallpaper.mClient.dispatchAppVisibility(visible);
1539 } catch (RemoteException e) {
1540 }
1541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001543 wallpaper.mAnimLayer = wallpaper.mLayer + mWallpaperAnimLayerAdjustment;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001545 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001547 // First, if this window is at the current index, then all
1548 // is well.
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001549 if (wallpaper == foundW) {
1550 foundI--;
1551 foundW = foundI > 0
1552 ? (WindowState)localmWindows.get(foundI-1) : null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001553 continue;
1554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001555
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001556 // The window didn't match... the current wallpaper window,
1557 // wherever it is, is in the wrong place, so make sure it is
1558 // not in the list.
1559 int oldIndex = localmWindows.indexOf(wallpaper);
1560 if (oldIndex >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Wallpaper removing at "
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001562 + oldIndex + ": " + wallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001563 localmWindows.remove(oldIndex);
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001564 if (oldIndex < foundI) {
1565 foundI--;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001566 }
1567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001569 // Now stick it in.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001570 if (DEBUG_WALLPAPER || DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001571 "Moving wallpaper " + wallpaper
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001572 + " from " + oldIndex + " to " + foundI);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001573
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07001574 localmWindows.add(foundI, wallpaper);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001575 changed |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001576 }
1577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001579 return changed;
1580 }
1581
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001582 void setWallpaperAnimLayerAdjustmentLocked(int adj) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001583 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001584 "Setting wallpaper layer adj to " + adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001585 mWallpaperAnimLayerAdjustment = adj;
1586 int curTokenIndex = mWallpaperTokens.size();
1587 while (curTokenIndex > 0) {
1588 curTokenIndex--;
1589 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1590 int curWallpaperIndex = token.windows.size();
1591 while (curWallpaperIndex > 0) {
1592 curWallpaperIndex--;
1593 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1594 wallpaper.mAnimLayer = wallpaper.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001595 if (DEBUG_LAYERS || DEBUG_WALLPAPER) Slog.v(TAG, "Wallpaper win "
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001596 + wallpaper + " anim layer: " + wallpaper.mAnimLayer);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07001597 }
1598 }
1599 }
1600
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001601 boolean updateWallpaperOffsetLocked(WindowState wallpaperWin, int dw, int dh,
1602 boolean sync) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001603 boolean changed = false;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001604 boolean rawChanged = false;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001605 float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001606 float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001607 int availw = wallpaperWin.mFrame.right-wallpaperWin.mFrame.left-dw;
1608 int offset = availw > 0 ? -(int)(availw*wpx+.5f) : 0;
1609 changed = wallpaperWin.mXOffset != offset;
1610 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001611 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001612 + wallpaperWin + " x: " + offset);
1613 wallpaperWin.mXOffset = offset;
1614 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001615 if (wallpaperWin.mWallpaperX != wpx || wallpaperWin.mWallpaperXStep != wpxs) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001616 wallpaperWin.mWallpaperX = wpx;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001617 wallpaperWin.mWallpaperXStep = wpxs;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001618 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001621 float wpy = mLastWallpaperY >= 0 ? mLastWallpaperY : 0.5f;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001622 float wpys = mLastWallpaperYStep >= 0 ? mLastWallpaperYStep : -1.0f;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001623 int availh = wallpaperWin.mFrame.bottom-wallpaperWin.mFrame.top-dh;
1624 offset = availh > 0 ? -(int)(availh*wpy+.5f) : 0;
1625 if (wallpaperWin.mYOffset != offset) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 if (DEBUG_WALLPAPER) Slog.v(TAG, "Update wallpaper "
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001627 + wallpaperWin + " y: " + offset);
1628 changed = true;
1629 wallpaperWin.mYOffset = offset;
1630 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001631 if (wallpaperWin.mWallpaperY != wpy || wallpaperWin.mWallpaperYStep != wpys) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001632 wallpaperWin.mWallpaperY = wpy;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001633 wallpaperWin.mWallpaperYStep = wpys;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001634 rawChanged = true;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001636
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07001637 if (rawChanged) {
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001638 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001639 if (DEBUG_WALLPAPER) Slog.v(TAG, "Report new wp offset "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001640 + wallpaperWin + " x=" + wallpaperWin.mWallpaperX
1641 + " y=" + wallpaperWin.mWallpaperY);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001642 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001643 mWaitingOnWallpaper = wallpaperWin;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001644 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001645 wallpaperWin.mClient.dispatchWallpaperOffsets(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001646 wallpaperWin.mWallpaperX, wallpaperWin.mWallpaperY,
1647 wallpaperWin.mWallpaperXStep, wallpaperWin.mWallpaperYStep, sync);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001648 if (sync) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001649 if (mWaitingOnWallpaper != null) {
1650 long start = SystemClock.uptimeMillis();
1651 if ((mLastWallpaperTimeoutTime+WALLPAPER_TIMEOUT_RECOVERY)
1652 < start) {
1653 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001654 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn75804932009-10-20 20:15:20 -07001655 "Waiting for offset complete...");
1656 mWindowMap.wait(WALLPAPER_TIMEOUT);
1657 } catch (InterruptedException e) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001658 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 if (DEBUG_WALLPAPER) Slog.v(TAG, "Offset complete!");
Dianne Hackborn75804932009-10-20 20:15:20 -07001660 if ((start+WALLPAPER_TIMEOUT)
1661 < SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001662 Slog.i(TAG, "Timeout waiting for wallpaper to offset: "
Dianne Hackborn75804932009-10-20 20:15:20 -07001663 + wallpaperWin);
1664 mLastWallpaperTimeoutTime = start;
1665 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001666 }
Dianne Hackborn75804932009-10-20 20:15:20 -07001667 mWaitingOnWallpaper = null;
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001668 }
1669 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001670 } catch (RemoteException e) {
1671 }
1672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001674 return changed;
1675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001676
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001677 void wallpaperOffsetsComplete(IBinder window) {
Dianne Hackborn75804932009-10-20 20:15:20 -07001678 synchronized (mWindowMap) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001679 if (mWaitingOnWallpaper != null &&
1680 mWaitingOnWallpaper.mClient.asBinder() == window) {
1681 mWaitingOnWallpaper = null;
Dianne Hackborn75804932009-10-20 20:15:20 -07001682 mWindowMap.notifyAll();
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001683 }
1684 }
1685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001686
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001687 boolean updateWallpaperOffsetLocked(WindowState changingTarget, boolean sync) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001688 final int dw = mDisplay.getWidth();
1689 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001691 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001693 WindowState target = mWallpaperTarget;
1694 if (target != null) {
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001695 if (target.mWallpaperX >= 0) {
1696 mLastWallpaperX = target.mWallpaperX;
1697 } else if (changingTarget.mWallpaperX >= 0) {
1698 mLastWallpaperX = changingTarget.mWallpaperX;
1699 }
1700 if (target.mWallpaperY >= 0) {
1701 mLastWallpaperY = target.mWallpaperY;
1702 } else if (changingTarget.mWallpaperY >= 0) {
1703 mLastWallpaperY = changingTarget.mWallpaperY;
1704 }
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
Dianne Hackborn73e92b42009-10-15 14:29:19 -07001707 int curTokenIndex = mWallpaperTokens.size();
1708 while (curTokenIndex > 0) {
1709 curTokenIndex--;
1710 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1711 int curWallpaperIndex = token.windows.size();
1712 while (curWallpaperIndex > 0) {
1713 curWallpaperIndex--;
1714 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1715 if (updateWallpaperOffsetLocked(wallpaper, dw, dh, sync)) {
1716 wallpaper.computeShownFrameLocked();
1717 changed = true;
1718 // We only want to be synchronous with one wallpaper.
1719 sync = false;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001720 }
1721 }
1722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001724 return changed;
1725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001726
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001727 void updateWallpaperVisibilityLocked() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07001728 final boolean visible = isWallpaperVisible(mWallpaperTarget);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001729 final int dw = mDisplay.getWidth();
1730 final int dh = mDisplay.getHeight();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001731
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001732 int curTokenIndex = mWallpaperTokens.size();
1733 while (curTokenIndex > 0) {
1734 curTokenIndex--;
1735 WindowToken token = mWallpaperTokens.get(curTokenIndex);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001736 if (token.hidden == visible) {
1737 token.hidden = !visible;
1738 // Need to do a layout to ensure the wallpaper now has the
1739 // correct size.
1740 mLayoutNeeded = true;
1741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001743 int curWallpaperIndex = token.windows.size();
1744 while (curWallpaperIndex > 0) {
1745 curWallpaperIndex--;
1746 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1747 if (visible) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001748 updateWallpaperOffsetLocked(wallpaper, dw, dh, false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001751 if (wallpaper.mWallpaperVisible != visible) {
1752 wallpaper.mWallpaperVisible = visible;
1753 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001754 if (DEBUG_VISIBILITY || DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07001755 "Updating visibility of wallpaper " + wallpaper
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07001756 + ": " + visible);
1757 wallpaper.mClient.dispatchAppVisibility(visible);
1758 } catch (RemoteException e) {
1759 }
1760 }
1761 }
1762 }
1763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001764
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001765 void sendPointerToWallpaperLocked(WindowState srcWin,
1766 MotionEvent pointer, long eventTime) {
1767 int curTokenIndex = mWallpaperTokens.size();
1768 while (curTokenIndex > 0) {
1769 curTokenIndex--;
1770 WindowToken token = mWallpaperTokens.get(curTokenIndex);
1771 int curWallpaperIndex = token.windows.size();
1772 while (curWallpaperIndex > 0) {
1773 curWallpaperIndex--;
1774 WindowState wallpaper = token.windows.get(curWallpaperIndex);
1775 if ((wallpaper.mAttrs.flags &
1776 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
1777 continue;
1778 }
1779 try {
1780 MotionEvent ev = MotionEvent.obtainNoHistory(pointer);
Dianne Hackborn6adba242009-11-10 11:10:09 -08001781 if (srcWin != null) {
1782 ev.offsetLocation(srcWin.mFrame.left-wallpaper.mFrame.left,
1783 srcWin.mFrame.top-wallpaper.mFrame.top);
1784 } else {
1785 ev.offsetLocation(-wallpaper.mFrame.left, -wallpaper.mFrame.top);
1786 }
1787 switch (pointer.getAction()) {
1788 case MotionEvent.ACTION_DOWN:
1789 mSendingPointersToWallpaper = true;
1790 break;
1791 case MotionEvent.ACTION_UP:
1792 mSendingPointersToWallpaper = false;
1793 break;
1794 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001795 wallpaper.mClient.dispatchPointer(ev, eventTime, false);
1796 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001797 Slog.w(TAG, "Failure sending pointer to wallpaper", e);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001798 }
1799 }
1800 }
1801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001803 void dispatchPointerElsewhereLocked(WindowState srcWin, WindowState relWin,
1804 MotionEvent pointer, long eventTime, boolean skipped) {
1805 if (relWin != null) {
1806 mPolicy.dispatchedPointerEventLw(pointer, relWin.mFrame.left, relWin.mFrame.top);
1807 } else {
1808 mPolicy.dispatchedPointerEventLw(pointer, 0, 0);
1809 }
1810
1811 // If we sent an initial down to the wallpaper, then continue
1812 // sending events until the final up.
1813 if (mSendingPointersToWallpaper) {
1814 if (skipped) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001815 Slog.i(TAG, "Sending skipped pointer to wallpaper!");
Dianne Hackborn90d2db32010-02-11 22:19:06 -08001816 }
1817 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1818
1819 // If we are on top of the wallpaper, then the wallpaper also
1820 // gets to see this movement.
1821 } else if (srcWin != null
1822 && pointer.getAction() == MotionEvent.ACTION_DOWN
1823 && mWallpaperTarget == srcWin
1824 && srcWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
1825 sendPointerToWallpaperLocked(relWin, pointer, eventTime);
1826 }
1827 }
1828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 public int addWindow(Session session, IWindow client,
1830 WindowManager.LayoutParams attrs, int viewVisibility,
1831 Rect outContentInsets) {
1832 int res = mPolicy.checkAddPermission(attrs);
1833 if (res != WindowManagerImpl.ADD_OKAY) {
1834 return res;
1835 }
Romain Guy06882f82009-06-10 13:36:04 -07001836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 boolean reportNewConfig = false;
1838 WindowState attachedWindow = null;
1839 WindowState win = null;
Romain Guy06882f82009-06-10 13:36:04 -07001840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 synchronized(mWindowMap) {
1842 // Instantiating a Display requires talking with the simulator,
1843 // so don't do it until we know the system is mostly up and
1844 // running.
1845 if (mDisplay == null) {
1846 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1847 mDisplay = wm.getDefaultDisplay();
Christopher Tateb696aee2010-04-02 19:08:30 -07001848 mInitialDisplayWidth = mDisplay.getWidth();
1849 mInitialDisplayHeight = mDisplay.getHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 mQueue.setDisplay(mDisplay);
1851 reportNewConfig = true;
1852 }
Romain Guy06882f82009-06-10 13:36:04 -07001853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (mWindowMap.containsKey(client.asBinder())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001855 Slog.w(TAG, "Window " + client + " is already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 return WindowManagerImpl.ADD_DUPLICATE_ADD;
1857 }
1858
1859 if (attrs.type >= FIRST_SUB_WINDOW && attrs.type <= LAST_SUB_WINDOW) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001860 attachedWindow = windowForClientLocked(null, attrs.token, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 if (attachedWindow == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001862 Slog.w(TAG, "Attempted to add window with token that is not a window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 + attrs.token + ". Aborting.");
1864 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1865 }
1866 if (attachedWindow.mAttrs.type >= FIRST_SUB_WINDOW
1867 && attachedWindow.mAttrs.type <= LAST_SUB_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 Slog.w(TAG, "Attempted to add window with token that is a sub-window: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 + attrs.token + ". Aborting.");
1870 return WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN;
1871 }
1872 }
1873
1874 boolean addToken = false;
1875 WindowToken token = mTokenMap.get(attrs.token);
1876 if (token == null) {
1877 if (attrs.type >= FIRST_APPLICATION_WINDOW
1878 && attrs.type <= LAST_APPLICATION_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 Slog.w(TAG, "Attempted to add application 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 }
1883 if (attrs.type == TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001884 Slog.w(TAG, "Attempted to add input method window with unknown token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 + attrs.token + ". Aborting.");
1886 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1887 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001888 if (attrs.type == TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001889 Slog.w(TAG, "Attempted to add wallpaper window with unknown token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001890 + attrs.token + ". Aborting.");
1891 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 token = new WindowToken(attrs.token, -1, false);
1894 addToken = true;
1895 } else if (attrs.type >= FIRST_APPLICATION_WINDOW
1896 && attrs.type <= LAST_APPLICATION_WINDOW) {
1897 AppWindowToken atoken = token.appWindowToken;
1898 if (atoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001899 Slog.w(TAG, "Attempted to add window with non-application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 + token + ". Aborting.");
1901 return WindowManagerImpl.ADD_NOT_APP_TOKEN;
1902 } else if (atoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001903 Slog.w(TAG, "Attempted to add window with exiting application token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 + token + ". Aborting.");
1905 return WindowManagerImpl.ADD_APP_EXITING;
1906 }
1907 if (attrs.type == TYPE_APPLICATION_STARTING && atoken.firstWindowDrawn) {
1908 // No need for this guy!
Joe Onorato8a9b2202010-02-26 18:56:32 -08001909 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 TAG, "**** NO NEED TO START: " + attrs.getTitle());
1911 return WindowManagerImpl.ADD_STARTING_NOT_NEEDED;
1912 }
1913 } else if (attrs.type == TYPE_INPUT_METHOD) {
1914 if (token.windowType != TYPE_INPUT_METHOD) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001915 Slog.w(TAG, "Attempted to add input method window with bad token "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 + attrs.token + ". Aborting.");
1917 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1918 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001919 } else if (attrs.type == TYPE_WALLPAPER) {
1920 if (token.windowType != TYPE_WALLPAPER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001921 Slog.w(TAG, "Attempted to add wallpaper window with bad token "
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001922 + attrs.token + ". Aborting.");
1923 return WindowManagerImpl.ADD_BAD_APP_TOKEN;
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926
1927 win = new WindowState(session, client, token,
1928 attachedWindow, attrs, viewVisibility);
1929 if (win.mDeathRecipient == null) {
1930 // Client has apparently died, so there is no reason to
1931 // continue.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 Slog.w(TAG, "Adding window client " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 + " that is dead, aborting.");
1934 return WindowManagerImpl.ADD_APP_EXITING;
1935 }
1936
1937 mPolicy.adjustWindowParamsLw(win.mAttrs);
Romain Guy06882f82009-06-10 13:36:04 -07001938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 res = mPolicy.prepareAddWindowLw(win, attrs);
1940 if (res != WindowManagerImpl.ADD_OKAY) {
1941 return res;
1942 }
1943
1944 // From now on, no exceptions or errors allowed!
1945
1946 res = WindowManagerImpl.ADD_OKAY;
Romain Guy06882f82009-06-10 13:36:04 -07001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07001949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 if (addToken) {
1951 mTokenMap.put(attrs.token, token);
1952 mTokenList.add(token);
1953 }
1954 win.attach();
1955 mWindowMap.put(client.asBinder(), win);
1956
1957 if (attrs.type == TYPE_APPLICATION_STARTING &&
1958 token.appWindowToken != null) {
1959 token.appWindowToken.startingWindow = win;
1960 }
1961
1962 boolean imMayMove = true;
Romain Guy06882f82009-06-10 13:36:04 -07001963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 if (attrs.type == TYPE_INPUT_METHOD) {
1965 mInputMethodWindow = win;
1966 addInputMethodWindowToListLocked(win);
1967 imMayMove = false;
1968 } else if (attrs.type == TYPE_INPUT_METHOD_DIALOG) {
1969 mInputMethodDialogs.add(win);
1970 addWindowToListInOrderLocked(win, true);
1971 adjustInputMethodDialogsLocked();
1972 imMayMove = false;
1973 } else {
1974 addWindowToListInOrderLocked(win, true);
Dianne Hackborn19382ac2009-09-11 21:13:37 -07001975 if (attrs.type == TYPE_WALLPAPER) {
1976 mLastWallpaperTimeoutTime = 0;
1977 adjustWallpaperWindowsLocked();
1978 } else if ((attrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001979 adjustWallpaperWindowsLocked();
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
Romain Guy06882f82009-06-10 13:36:04 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 win.mEnterAnimationPending = true;
Romain Guy06882f82009-06-10 13:36:04 -07001984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 mPolicy.getContentInsetHintLw(attrs, outContentInsets);
Romain Guy06882f82009-06-10 13:36:04 -07001986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if (mInTouchMode) {
1988 res |= WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE;
1989 }
1990 if (win == null || win.mAppToken == null || !win.mAppToken.clientHidden) {
1991 res |= WindowManagerImpl.ADD_FLAG_APP_VISIBLE;
1992 }
Romain Guy06882f82009-06-10 13:36:04 -07001993
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001994 boolean focusChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 if (win.canReceiveKeys()) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08001996 if ((focusChanged=updateFocusedWindowLocked(UPDATE_FOCUS_WILL_ASSIGN_LAYERS))
1997 == true) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 imMayMove = false;
1999 }
2000 }
Romain Guy06882f82009-06-10 13:36:04 -07002001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 if (imMayMove) {
Romain Guy06882f82009-06-10 13:36:04 -07002003 moveInputMethodWindowsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Romain Guy06882f82009-06-10 13:36:04 -07002005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 assignLayersLocked();
2007 // Don't do layout here, the window must call
2008 // relayout to be displayed, so we'll do it there.
Romain Guy06882f82009-06-10 13:36:04 -07002009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 //dump();
2011
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002012 if (focusChanged) {
2013 if (mCurrentFocus != null) {
2014 mKeyWaiter.handleNewWindowLocked(mCurrentFocus);
2015 }
2016 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002017 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 TAG, "New client " + client.asBinder()
2019 + ": window=" + win);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002020
2021 if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked()) {
2022 reportNewConfig = true;
2023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025
2026 // sendNewConfiguration() checks caller permissions so we must call it with
2027 // privilege. updateOrientationFromAppTokens() clears and resets the caller
2028 // identity anyway, so it's safe to just clear & restore around this whole
2029 // block.
2030 final long origId = Binder.clearCallingIdentity();
2031 if (reportNewConfig) {
2032 sendNewConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
2034 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 return res;
2037 }
Romain Guy06882f82009-06-10 13:36:04 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 public void removeWindow(Session session, IWindow client) {
2040 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002041 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 if (win == null) {
2043 return;
2044 }
2045 removeWindowLocked(session, win);
2046 }
2047 }
Romain Guy06882f82009-06-10 13:36:04 -07002048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 public void removeWindowLocked(Session session, WindowState win) {
2050
Joe Onorato8a9b2202010-02-26 18:56:32 -08002051 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 TAG, "Remove " + win + " client="
2053 + Integer.toHexString(System.identityHashCode(
2054 win.mClient.asBinder()))
2055 + ", surface=" + win.mSurface);
2056
2057 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002058
Joe Onorato8a9b2202010-02-26 18:56:32 -08002059 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 TAG, "Remove " + win + ": mSurface=" + win.mSurface
2061 + " mExiting=" + win.mExiting
2062 + " isAnimating=" + win.isAnimating()
2063 + " app-animation="
2064 + (win.mAppToken != null ? win.mAppToken.animation : null)
2065 + " inPendingTransaction="
2066 + (win.mAppToken != null ? win.mAppToken.inPendingTransaction : false)
2067 + " mDisplayFrozen=" + mDisplayFrozen);
2068 // Visibility of the removed window. Will be used later to update orientation later on.
2069 boolean wasVisible = false;
2070 // First, see if we need to run an animation. If we do, we have
2071 // to hold off on removing the window until the animation is done.
2072 // If the display is frozen, just remove immediately, since the
2073 // animation wouldn't be seen.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002074 if (win.mSurface != null && !mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 // If we are not currently running the exit animation, we
2076 // need to see about starting one.
2077 if (wasVisible=win.isWinVisibleLw()) {
Romain Guy06882f82009-06-10 13:36:04 -07002078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2080 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2081 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2082 }
2083 // Try starting an animation.
2084 if (applyAnimationLocked(win, transit, false)) {
2085 win.mExiting = true;
2086 }
2087 }
2088 if (win.mExiting || win.isAnimating()) {
2089 // The exit animation is running... wait for it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002090 //Slog.i(TAG, "*** Running exit animation...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 win.mExiting = true;
2092 win.mRemoveOnExit = true;
2093 mLayoutNeeded = true;
2094 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
2095 performLayoutAndPlaceSurfacesLocked();
2096 if (win.mAppToken != null) {
2097 win.mAppToken.updateReportedVisibilityLocked();
2098 }
2099 //dump();
2100 Binder.restoreCallingIdentity(origId);
2101 return;
2102 }
2103 }
2104
2105 removeWindowInnerLocked(session, win);
2106 // Removing a visible window will effect the computed orientation
2107 // So just update orientation if needed.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002108 if (wasVisible && computeForcedAppOrientationLocked()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002109 != mForcedAppOrientation
2110 && updateOrientationFromAppTokensLocked()) {
2111 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2114 Binder.restoreCallingIdentity(origId);
2115 }
Romain Guy06882f82009-06-10 13:36:04 -07002116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 private void removeWindowInnerLocked(Session session, WindowState win) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002118 mKeyWaiter.finishedKey(session, win.mClient, true,
2119 KeyWaiter.RETURN_NOTHING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 mKeyWaiter.releasePendingPointerLocked(win.mSession);
2121 mKeyWaiter.releasePendingTrackballLocked(win.mSession);
Romain Guy06882f82009-06-10 13:36:04 -07002122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 win.mRemoved = true;
Romain Guy06882f82009-06-10 13:36:04 -07002124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 if (mInputMethodTarget == win) {
2126 moveInputMethodWindowsIfNeededLocked(false);
2127 }
Romain Guy06882f82009-06-10 13:36:04 -07002128
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002129 if (false) {
2130 RuntimeException e = new RuntimeException("here");
2131 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002132 Slog.w(TAG, "Removing window " + win, e);
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07002133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 mPolicy.removeWindowLw(win);
2136 win.removeLocked();
2137
2138 mWindowMap.remove(win.mClient.asBinder());
2139 mWindows.remove(win);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002140 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Final remove of window: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141
2142 if (mInputMethodWindow == win) {
2143 mInputMethodWindow = null;
2144 } else if (win.mAttrs.type == TYPE_INPUT_METHOD_DIALOG) {
2145 mInputMethodDialogs.remove(win);
2146 }
Romain Guy06882f82009-06-10 13:36:04 -07002147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 final WindowToken token = win.mToken;
2149 final AppWindowToken atoken = win.mAppToken;
2150 token.windows.remove(win);
2151 if (atoken != null) {
2152 atoken.allAppWindows.remove(win);
2153 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002154 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 TAG, "**** Removing window " + win + ": count="
2156 + token.windows.size());
2157 if (token.windows.size() == 0) {
2158 if (!token.explicit) {
2159 mTokenMap.remove(token.token);
2160 mTokenList.remove(token);
2161 } else if (atoken != null) {
2162 atoken.firstWindowDrawn = false;
2163 }
2164 }
2165
2166 if (atoken != null) {
2167 if (atoken.startingWindow == win) {
2168 atoken.startingWindow = null;
2169 } else if (atoken.allAppWindows.size() == 0 && atoken.startingData != null) {
2170 // If this is the last window and we had requested a starting
2171 // transition window, well there is no point now.
2172 atoken.startingData = null;
2173 } else if (atoken.allAppWindows.size() == 1 && atoken.startingView != null) {
2174 // If this is the last window except for a starting transition
2175 // window, we need to get rid of the starting transition.
2176 if (DEBUG_STARTING_WINDOW) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002177 Slog.v(TAG, "Schedule remove starting " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 + ": no more real windows");
2179 }
2180 Message m = mH.obtainMessage(H.REMOVE_STARTING, atoken);
2181 mH.sendMessage(m);
2182 }
2183 }
Romain Guy06882f82009-06-10 13:36:04 -07002184
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002185 if (win.mAttrs.type == TYPE_WALLPAPER) {
2186 mLastWallpaperTimeoutTime = 0;
2187 adjustWallpaperWindowsLocked();
2188 } else if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07002189 adjustWallpaperWindowsLocked();
2190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 if (!mInLayout) {
2193 assignLayersLocked();
2194 mLayoutNeeded = true;
2195 performLayoutAndPlaceSurfacesLocked();
2196 if (win.mAppToken != null) {
2197 win.mAppToken.updateReportedVisibilityLocked();
2198 }
2199 }
2200 }
2201
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002202 private static void logSurface(WindowState w, String msg, RuntimeException where) {
2203 String str = " SURFACE " + Integer.toHexString(w.hashCode())
2204 + ": " + msg + " / " + w.mAttrs.getTitle();
2205 if (where != null) {
2206 Slog.i(TAG, str, where);
2207 } else {
2208 Slog.i(TAG, str);
2209 }
2210 }
2211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 private void setTransparentRegionWindow(Session session, IWindow client, Region region) {
2213 long origId = Binder.clearCallingIdentity();
2214 try {
2215 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002216 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 if ((w != null) && (w.mSurface != null)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002218 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 Surface.openTransaction();
2220 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002221 if (SHOW_TRANSACTIONS) logSurface(w,
2222 "transparentRegionHint=" + region, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 w.mSurface.setTransparentRegionHint(region);
2224 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002225 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 Surface.closeTransaction();
2227 }
2228 }
2229 }
2230 } finally {
2231 Binder.restoreCallingIdentity(origId);
2232 }
2233 }
2234
2235 void setInsetsWindow(Session session, IWindow client,
Romain Guy06882f82009-06-10 13:36:04 -07002236 int touchableInsets, Rect contentInsets,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 Rect visibleInsets) {
2238 long origId = Binder.clearCallingIdentity();
2239 try {
2240 synchronized (mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002241 WindowState w = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 if (w != null) {
2243 w.mGivenInsetsPending = false;
2244 w.mGivenContentInsets.set(contentInsets);
2245 w.mGivenVisibleInsets.set(visibleInsets);
2246 w.mTouchableInsets = touchableInsets;
2247 mLayoutNeeded = true;
2248 performLayoutAndPlaceSurfacesLocked();
2249 }
2250 }
2251 } finally {
2252 Binder.restoreCallingIdentity(origId);
2253 }
2254 }
Romain Guy06882f82009-06-10 13:36:04 -07002255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 public void getWindowDisplayFrame(Session session, IWindow client,
2257 Rect outDisplayFrame) {
2258 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002259 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (win == null) {
2261 outDisplayFrame.setEmpty();
2262 return;
2263 }
2264 outDisplayFrame.set(win.mDisplayFrame);
2265 }
2266 }
2267
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002268 public void setWindowWallpaperPositionLocked(WindowState window, float x, float y,
2269 float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002270 if (window.mWallpaperX != x || window.mWallpaperY != y) {
2271 window.mWallpaperX = x;
2272 window.mWallpaperY = y;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08002273 window.mWallpaperXStep = xStep;
2274 window.mWallpaperYStep = yStep;
Dianne Hackborn73e92b42009-10-15 14:29:19 -07002275 if (updateWallpaperOffsetLocked(window, true)) {
2276 performLayoutAndPlaceSurfacesLocked();
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07002277 }
2278 }
2279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002280
Dianne Hackborn75804932009-10-20 20:15:20 -07002281 void wallpaperCommandComplete(IBinder window, Bundle result) {
2282 synchronized (mWindowMap) {
2283 if (mWaitingOnWallpaper != null &&
2284 mWaitingOnWallpaper.mClient.asBinder() == window) {
2285 mWaitingOnWallpaper = null;
2286 mWindowMap.notifyAll();
2287 }
2288 }
2289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
Dianne Hackborn75804932009-10-20 20:15:20 -07002291 public Bundle sendWindowWallpaperCommandLocked(WindowState window,
2292 String action, int x, int y, int z, Bundle extras, boolean sync) {
2293 if (window == mWallpaperTarget || window == mLowerWallpaperTarget
2294 || window == mUpperWallpaperTarget) {
2295 boolean doWait = sync;
2296 int curTokenIndex = mWallpaperTokens.size();
2297 while (curTokenIndex > 0) {
2298 curTokenIndex--;
2299 WindowToken token = mWallpaperTokens.get(curTokenIndex);
2300 int curWallpaperIndex = token.windows.size();
2301 while (curWallpaperIndex > 0) {
2302 curWallpaperIndex--;
2303 WindowState wallpaper = token.windows.get(curWallpaperIndex);
2304 try {
2305 wallpaper.mClient.dispatchWallpaperCommand(action,
2306 x, y, z, extras, sync);
2307 // We only want to be synchronous with one wallpaper.
2308 sync = false;
2309 } catch (RemoteException e) {
2310 }
2311 }
2312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002313
Dianne Hackborn75804932009-10-20 20:15:20 -07002314 if (doWait) {
2315 // XXX Need to wait for result.
2316 }
2317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002318
Dianne Hackborn75804932009-10-20 20:15:20 -07002319 return null;
2320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 public int relayoutWindow(Session session, IWindow client,
2323 WindowManager.LayoutParams attrs, int requestedWidth,
2324 int requestedHeight, int viewVisibility, boolean insetsPending,
2325 Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002326 Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 boolean displayed = false;
2328 boolean inTouchMode;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002329 boolean configChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002333 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 if (win == null) {
2335 return 0;
2336 }
2337 win.mRequestedWidth = requestedWidth;
2338 win.mRequestedHeight = requestedHeight;
2339
2340 if (attrs != null) {
2341 mPolicy.adjustWindowParamsLw(attrs);
2342 }
Romain Guy06882f82009-06-10 13:36:04 -07002343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 int attrChanges = 0;
2345 int flagChanges = 0;
2346 if (attrs != null) {
2347 flagChanges = win.mAttrs.flags ^= attrs.flags;
2348 attrChanges = win.mAttrs.copyFrom(attrs);
2349 }
2350
Joe Onorato8a9b2202010-02-26 18:56:32 -08002351 if (DEBUG_LAYOUT) Slog.v(TAG, "Relayout " + win + ": " + win.mAttrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352
2353 if ((attrChanges & WindowManager.LayoutParams.ALPHA_CHANGED) != 0) {
2354 win.mAlpha = attrs.alpha;
2355 }
2356
2357 final boolean scaledWindow =
2358 ((win.mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0);
2359
2360 if (scaledWindow) {
2361 // requested{Width|Height} Surface's physical size
2362 // attrs.{width|height} Size on screen
2363 win.mHScale = (attrs.width != requestedWidth) ?
2364 (attrs.width / (float)requestedWidth) : 1.0f;
2365 win.mVScale = (attrs.height != requestedHeight) ?
2366 (attrs.height / (float)requestedHeight) : 1.0f;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08002367 } else {
2368 win.mHScale = win.mVScale = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370
2371 boolean imMayMove = (flagChanges&(
2372 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
2373 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 boolean focusMayChange = win.mViewVisibility != viewVisibility
2376 || ((flagChanges&WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) != 0)
2377 || (!win.mRelayoutCalled);
Romain Guy06882f82009-06-10 13:36:04 -07002378
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002379 boolean wallpaperMayMove = win.mViewVisibility != viewVisibility
2380 && (win.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 win.mRelayoutCalled = true;
2383 final int oldVisibility = win.mViewVisibility;
2384 win.mViewVisibility = viewVisibility;
2385 if (viewVisibility == View.VISIBLE &&
2386 (win.mAppToken == null || !win.mAppToken.clientHidden)) {
2387 displayed = !win.isVisibleLw();
2388 if (win.mExiting) {
2389 win.mExiting = false;
2390 win.mAnimation = null;
2391 }
2392 if (win.mDestroying) {
2393 win.mDestroying = false;
2394 mDestroySurface.remove(win);
2395 }
2396 if (oldVisibility == View.GONE) {
2397 win.mEnterAnimationPending = true;
2398 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002399 if (displayed) {
2400 if (win.mSurface != null && !win.mDrawPending
2401 && !win.mCommitDrawPending && !mDisplayFrozen
2402 && mPolicy.isScreenOn()) {
2403 applyEnterAnimationLocked(win);
2404 }
2405 if ((win.mAttrs.flags
2406 & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
2407 if (DEBUG_VISIBILITY) Slog.v(TAG,
2408 "Relayout window turning screen on: " + win);
2409 win.mTurnOnScreen = true;
2410 }
2411 int diff = 0;
2412 if (win.mConfiguration != mCurConfiguration
2413 && (win.mConfiguration == null
2414 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0)) {
2415 win.mConfiguration = mCurConfiguration;
2416 if (DEBUG_CONFIGURATION) {
2417 Slog.i(TAG, "Window " + win + " visible with new config: "
2418 + win.mConfiguration + " / 0x"
2419 + Integer.toHexString(diff));
2420 }
2421 outConfig.setTo(mCurConfiguration);
2422 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07002423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
2425 // To change the format, we need to re-build the surface.
2426 win.destroySurfaceLocked();
2427 displayed = true;
2428 }
2429 try {
2430 Surface surface = win.createSurfaceLocked();
2431 if (surface != null) {
2432 outSurface.copyFrom(surface);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002433 win.mReportDestroySurface = false;
2434 win.mSurfacePendingDestroy = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002435 if (SHOW_TRANSACTIONS) Slog.i(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002436 " OUT SURFACE " + outSurface + ": copied");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 } else {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002438 // For some reason there isn't a surface. Clear the
2439 // caller's object so they see the same state.
2440 outSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
2442 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002443 Slog.w(TAG, "Exception thrown when creating surface for client "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002444 + client + " (" + win.mAttrs.getTitle() + ")",
2445 e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 Binder.restoreCallingIdentity(origId);
2447 return 0;
2448 }
2449 if (displayed) {
2450 focusMayChange = true;
2451 }
2452 if (win.mAttrs.type == TYPE_INPUT_METHOD
2453 && mInputMethodWindow == null) {
2454 mInputMethodWindow = win;
2455 imMayMove = true;
2456 }
Dianne Hackborn558947c2009-12-18 16:02:50 -08002457 if (win.mAttrs.type == TYPE_BASE_APPLICATION
2458 && win.mAppToken != null
2459 && win.mAppToken.startingWindow != null) {
2460 // Special handling of starting window over the base
2461 // window of the app: propagate lock screen flags to it,
2462 // to provide the correct semantics while starting.
2463 final int mask =
2464 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Mike Lockwoodef731622010-01-27 17:51:34 -05002465 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
2466 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
Dianne Hackborn558947c2009-12-18 16:02:50 -08002467 WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
2468 sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
2469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 } else {
2471 win.mEnterAnimationPending = false;
2472 if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002473 if (DEBUG_VISIBILITY) Slog.i(TAG, "Relayout invis " + win
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002474 + ": mExiting=" + win.mExiting
2475 + " mSurfacePendingDestroy=" + win.mSurfacePendingDestroy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 // If we are not currently running the exit animation, we
2477 // need to see about starting one.
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002478 if (!win.mExiting || win.mSurfacePendingDestroy) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 // Try starting an animation; if there isn't one, we
2480 // can destroy the surface right away.
2481 int transit = WindowManagerPolicy.TRANSIT_EXIT;
2482 if (win.getAttrs().type == TYPE_APPLICATION_STARTING) {
2483 transit = WindowManagerPolicy.TRANSIT_PREVIEW_DONE;
2484 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002485 if (!win.mSurfacePendingDestroy && win.isWinVisibleLw() &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 applyAnimationLocked(win, transit, false)) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002487 focusMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 win.mExiting = true;
2489 mKeyWaiter.finishedKey(session, client, true,
2490 KeyWaiter.RETURN_NOTHING);
2491 } else if (win.isAnimating()) {
2492 // Currently in a hide animation... turn this into
2493 // an exit.
2494 win.mExiting = true;
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07002495 } else if (win == mWallpaperTarget) {
2496 // If the wallpaper is currently behind this
2497 // window, we need to change both of them inside
2498 // of a transaction to avoid artifacts.
2499 win.mExiting = true;
2500 win.mAnimating = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 } else {
2502 if (mInputMethodWindow == win) {
2503 mInputMethodWindow = null;
2504 }
2505 win.destroySurfaceLocked();
2506 }
2507 }
2508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002509
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002510 if (win.mSurface == null || (win.getAttrs().flags
2511 & WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING) == 0
2512 || win.mSurfacePendingDestroy) {
2513 // We are being called from a local process, which
2514 // means outSurface holds its current surface. Ensure the
2515 // surface object is cleared, but we don't want it actually
2516 // destroyed at this point.
2517 win.mSurfacePendingDestroy = false;
2518 outSurface.release();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002519 if (DEBUG_VISIBILITY) Slog.i(TAG, "Releasing surface in: " + win);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002520 } else if (win.mSurface != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002521 if (DEBUG_VISIBILITY) Slog.i(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002522 "Keeping surface, will report destroy: " + win);
2523 win.mReportDestroySurface = true;
2524 outSurface.copyFrom(win.mSurface);
2525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
2527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 if (focusMayChange) {
2529 //System.out.println("Focus may change: " + win.mAttrs.getTitle());
2530 if (updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 imMayMove = false;
2532 }
2533 //System.out.println("Relayout " + win + ": focus=" + mCurrentFocus);
2534 }
Romain Guy06882f82009-06-10 13:36:04 -07002535
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08002536 // updateFocusedWindowLocked() already assigned layers so we only need to
2537 // reassign them at this point if the IM window state gets shuffled
2538 boolean assignLayers = false;
Romain Guy06882f82009-06-10 13:36:04 -07002539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 if (imMayMove) {
Dianne Hackborn8abd5f02009-11-20 18:09:03 -08002541 if (moveInputMethodWindowsIfNeededLocked(false) || displayed) {
2542 // Little hack here -- we -should- be able to rely on the
2543 // function to return true if the IME has moved and needs
2544 // its layer recomputed. However, if the IME was hidden
2545 // and isn't actually moved in the list, its layer may be
2546 // out of data so we make sure to recompute it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 assignLayers = true;
2548 }
2549 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002550 if (wallpaperMayMove) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002551 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002552 assignLayers = true;
2553 }
2554 }
Romain Guy06882f82009-06-10 13:36:04 -07002555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 mLayoutNeeded = true;
2557 win.mGivenInsetsPending = insetsPending;
2558 if (assignLayers) {
2559 assignLayersLocked();
2560 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002561 configChanged = updateOrientationFromAppTokensLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 performLayoutAndPlaceSurfacesLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -07002563 if (displayed && win.mIsWallpaper) {
2564 updateWallpaperOffsetLocked(win, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07002565 mDisplay.getHeight(), false);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 if (win.mAppToken != null) {
2568 win.mAppToken.updateReportedVisibilityLocked();
2569 }
2570 outFrame.set(win.mFrame);
2571 outContentInsets.set(win.mContentInsets);
2572 outVisibleInsets.set(win.mVisibleInsets);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002573 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 TAG, "Relayout given client " + client.asBinder()
Romain Guy06882f82009-06-10 13:36:04 -07002575 + ", requestedWidth=" + requestedWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 + ", requestedHeight=" + requestedHeight
2577 + ", viewVisibility=" + viewVisibility
2578 + "\nRelayout returning frame=" + outFrame
2579 + ", surface=" + outSurface);
2580
Joe Onorato8a9b2202010-02-26 18:56:32 -08002581 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 TAG, "Relayout of " + win + ": focusMayChange=" + focusMayChange);
2583
2584 inTouchMode = mInTouchMode;
2585 }
2586
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002587 if (configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 sendNewConfiguration();
2589 }
Romain Guy06882f82009-06-10 13:36:04 -07002590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 Binder.restoreCallingIdentity(origId);
Romain Guy06882f82009-06-10 13:36:04 -07002592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 return (inTouchMode ? WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE : 0)
2594 | (displayed ? WindowManagerImpl.RELAYOUT_FIRST_TIME : 0);
2595 }
2596
2597 public void finishDrawingWindow(Session session, IWindow client) {
2598 final long origId = Binder.clearCallingIdentity();
2599 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002600 WindowState win = windowForClientLocked(session, client, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 if (win != null && win.finishDrawingLocked()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07002602 if ((win.mAttrs.flags&FLAG_SHOW_WALLPAPER) != 0) {
2603 adjustWallpaperWindowsLocked();
2604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 mLayoutNeeded = true;
2606 performLayoutAndPlaceSurfacesLocked();
2607 }
2608 }
2609 Binder.restoreCallingIdentity(origId);
2610 }
2611
2612 private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002613 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 + (lp != null ? lp.packageName : null)
2615 + " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
2616 if (lp != null && lp.windowAnimations != 0) {
2617 // If this is a system resource, don't try to load it from the
2618 // application resources. It is nice to avoid loading application
2619 // resources if we can.
2620 String packageName = lp.packageName != null ? lp.packageName : "android";
2621 int resId = lp.windowAnimations;
2622 if ((resId&0xFF000000) == 0x01000000) {
2623 packageName = "android";
2624 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002625 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 + packageName);
2627 return AttributeCache.instance().get(packageName, resId,
2628 com.android.internal.R.styleable.WindowAnimation);
2629 }
2630 return null;
2631 }
Romain Guy06882f82009-06-10 13:36:04 -07002632
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002633 private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002634 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: params package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002635 + packageName + " resId=0x" + Integer.toHexString(resId));
2636 if (packageName != null) {
2637 if ((resId&0xFF000000) == 0x01000000) {
2638 packageName = "android";
2639 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002640 if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002641 + packageName);
2642 return AttributeCache.instance().get(packageName, resId,
2643 com.android.internal.R.styleable.WindowAnimation);
2644 }
2645 return null;
2646 }
2647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 private void applyEnterAnimationLocked(WindowState win) {
2649 int transit = WindowManagerPolicy.TRANSIT_SHOW;
2650 if (win.mEnterAnimationPending) {
2651 win.mEnterAnimationPending = false;
2652 transit = WindowManagerPolicy.TRANSIT_ENTER;
2653 }
2654
2655 applyAnimationLocked(win, transit, true);
2656 }
2657
2658 private boolean applyAnimationLocked(WindowState win,
2659 int transit, boolean isEntrance) {
2660 if (win.mLocalAnimating && win.mAnimationIsEntrance == isEntrance) {
2661 // If we are trying to apply an animation, but already running
2662 // an animation of the same type, then just leave that one alone.
2663 return true;
2664 }
Romain Guy06882f82009-06-10 13:36:04 -07002665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 // Only apply an animation if the display isn't frozen. If it is
2667 // frozen, there is no reason to animate and it can cause strange
2668 // artifacts when we unfreeze the display if some different animation
2669 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002670 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 int anim = mPolicy.selectAnimationLw(win, transit);
2672 int attr = -1;
2673 Animation a = null;
2674 if (anim != 0) {
2675 a = AnimationUtils.loadAnimation(mContext, anim);
2676 } else {
2677 switch (transit) {
2678 case WindowManagerPolicy.TRANSIT_ENTER:
2679 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
2680 break;
2681 case WindowManagerPolicy.TRANSIT_EXIT:
2682 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
2683 break;
2684 case WindowManagerPolicy.TRANSIT_SHOW:
2685 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
2686 break;
2687 case WindowManagerPolicy.TRANSIT_HIDE:
2688 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
2689 break;
2690 }
2691 if (attr >= 0) {
2692 a = loadAnimation(win.mAttrs, attr);
2693 }
2694 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002695 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: win=" + win
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
2697 + " mAnimation=" + win.mAnimation
2698 + " isEntrance=" + isEntrance);
2699 if (a != null) {
2700 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002701 RuntimeException e = null;
2702 if (!HIDE_STACK_CRAWLS) {
2703 e = new RuntimeException();
2704 e.fillInStackTrace();
2705 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002706 Slog.v(TAG, "Loaded animation " + a + " for " + win, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
2708 win.setAnimation(a);
2709 win.mAnimationIsEntrance = isEntrance;
2710 }
2711 } else {
2712 win.clearAnimation();
2713 }
2714
2715 return win.mAnimation != null;
2716 }
2717
2718 private Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
2719 int anim = 0;
2720 Context context = mContext;
2721 if (animAttr >= 0) {
2722 AttributeCache.Entry ent = getCachedAnimations(lp);
2723 if (ent != null) {
2724 context = ent.context;
2725 anim = ent.array.getResourceId(animAttr, 0);
2726 }
2727 }
2728 if (anim != 0) {
2729 return AnimationUtils.loadAnimation(context, anim);
2730 }
2731 return null;
2732 }
Romain Guy06882f82009-06-10 13:36:04 -07002733
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002734 private Animation loadAnimation(String packageName, int resId) {
2735 int anim = 0;
2736 Context context = mContext;
2737 if (resId >= 0) {
2738 AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
2739 if (ent != null) {
2740 context = ent.context;
2741 anim = resId;
2742 }
2743 }
2744 if (anim != 0) {
2745 return AnimationUtils.loadAnimation(context, anim);
2746 }
2747 return null;
2748 }
2749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 private boolean applyAnimationLocked(AppWindowToken wtoken,
2751 WindowManager.LayoutParams lp, int transit, boolean enter) {
2752 // Only apply an animation if the display isn't frozen. If it is
2753 // frozen, there is no reason to animate and it can cause strange
2754 // artifacts when we unfreeze the display if some different animation
2755 // is running.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08002756 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002757 Animation a;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07002758 if (lp != null && (lp.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002759 a = new FadeInOutAnimation(enter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002760 if (DEBUG_ANIM) Slog.v(TAG,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002761 "applying FadeInOutAnimation for a window in compatibility mode");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002762 } else if (mNextAppTransitionPackage != null) {
2763 a = loadAnimation(mNextAppTransitionPackage, enter ?
2764 mNextAppTransitionEnter : mNextAppTransitionExit);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002765 } else {
2766 int animAttr = 0;
2767 switch (transit) {
2768 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
2769 animAttr = enter
2770 ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation
2771 : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation;
2772 break;
2773 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
2774 animAttr = enter
2775 ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation
2776 : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation;
2777 break;
2778 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
2779 animAttr = enter
2780 ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation
2781 : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation;
2782 break;
2783 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
2784 animAttr = enter
2785 ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation
2786 : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation;
2787 break;
2788 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
2789 animAttr = enter
2790 ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation
2791 : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation;
2792 break;
2793 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
2794 animAttr = enter
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07002795 ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002796 : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation;
2797 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002798 case WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002799 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002800 ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation
2801 : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002802 break;
Dianne Hackborn25994b42009-09-04 14:21:19 -07002803 case WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE:
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002804 animAttr = enter
Dianne Hackborn25994b42009-09-04 14:21:19 -07002805 ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation
2806 : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation;
2807 break;
2808 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN:
2809 animAttr = enter
2810 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation
2811 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation;
2812 break;
2813 case WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE:
2814 animAttr = enter
2815 ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation
2816 : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07002817 break;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002818 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002819 a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002820 if (DEBUG_ANIM) Slog.v(TAG, "applyAnimation: wtoken=" + wtoken
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07002821 + " anim=" + a
2822 + " animAttr=0x" + Integer.toHexString(animAttr)
2823 + " transit=" + transit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 if (a != null) {
2826 if (DEBUG_ANIM) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08002827 RuntimeException e = null;
2828 if (!HIDE_STACK_CRAWLS) {
2829 e = new RuntimeException();
2830 e.fillInStackTrace();
2831 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002832 Slog.v(TAG, "Loaded animation " + a + " for " + wtoken, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
2834 wtoken.setAnimation(a);
2835 }
2836 } else {
2837 wtoken.clearAnimation();
2838 }
2839
2840 return wtoken.animation != null;
2841 }
2842
2843 // -------------------------------------------------------------
2844 // Application Window Tokens
2845 // -------------------------------------------------------------
2846
2847 public void validateAppTokens(List tokens) {
2848 int v = tokens.size()-1;
2849 int m = mAppTokens.size()-1;
2850 while (v >= 0 && m >= 0) {
2851 AppWindowToken wtoken = mAppTokens.get(m);
2852 if (wtoken.removed) {
2853 m--;
2854 continue;
2855 }
2856 if (tokens.get(v) != wtoken.token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002857 Slog.w(TAG, "Tokens out of sync: external is " + tokens.get(v)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 + " @ " + v + ", internal is " + wtoken.token + " @ " + m);
2859 }
2860 v--;
2861 m--;
2862 }
2863 while (v >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002864 Slog.w(TAG, "External token not found: " + tokens.get(v) + " @ " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 v--;
2866 }
2867 while (m >= 0) {
2868 AppWindowToken wtoken = mAppTokens.get(m);
2869 if (!wtoken.removed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002870 Slog.w(TAG, "Invalid internal token: " + wtoken.token + " @ " + m);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 }
2872 m--;
2873 }
2874 }
2875
2876 boolean checkCallingPermission(String permission, String func) {
2877 // Quick check: if the calling permission is me, it's all okay.
2878 if (Binder.getCallingPid() == Process.myPid()) {
2879 return true;
2880 }
Romain Guy06882f82009-06-10 13:36:04 -07002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 if (mContext.checkCallingPermission(permission)
2883 == PackageManager.PERMISSION_GRANTED) {
2884 return true;
2885 }
2886 String msg = "Permission Denial: " + func + " from pid="
2887 + Binder.getCallingPid()
2888 + ", uid=" + Binder.getCallingUid()
2889 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002890 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 return false;
2892 }
Romain Guy06882f82009-06-10 13:36:04 -07002893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 AppWindowToken findAppWindowToken(IBinder token) {
2895 WindowToken wtoken = mTokenMap.get(token);
2896 if (wtoken == null) {
2897 return null;
2898 }
2899 return wtoken.appWindowToken;
2900 }
Romain Guy06882f82009-06-10 13:36:04 -07002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 public void addWindowToken(IBinder token, int type) {
2903 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2904 "addWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002905 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 }
Romain Guy06882f82009-06-10 13:36:04 -07002907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 synchronized(mWindowMap) {
2909 WindowToken wtoken = mTokenMap.get(token);
2910 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002911 Slog.w(TAG, "Attempted to add existing input method token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 return;
2913 }
2914 wtoken = new WindowToken(token, type, true);
2915 mTokenMap.put(token, wtoken);
2916 mTokenList.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002917 if (type == TYPE_WALLPAPER) {
2918 mWallpaperTokens.add(wtoken);
2919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
2921 }
Romain Guy06882f82009-06-10 13:36:04 -07002922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 public void removeWindowToken(IBinder token) {
2924 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2925 "removeWindowToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002926 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 }
2928
2929 final long origId = Binder.clearCallingIdentity();
2930 synchronized(mWindowMap) {
2931 WindowToken wtoken = mTokenMap.remove(token);
2932 mTokenList.remove(wtoken);
2933 if (wtoken != null) {
2934 boolean delayed = false;
2935 if (!wtoken.hidden) {
2936 wtoken.hidden = true;
Romain Guy06882f82009-06-10 13:36:04 -07002937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 final int N = wtoken.windows.size();
2939 boolean changed = false;
Romain Guy06882f82009-06-10 13:36:04 -07002940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 for (int i=0; i<N; i++) {
2942 WindowState win = wtoken.windows.get(i);
2943
2944 if (win.isAnimating()) {
2945 delayed = true;
2946 }
Romain Guy06882f82009-06-10 13:36:04 -07002947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 if (win.isVisibleNow()) {
2949 applyAnimationLocked(win,
2950 WindowManagerPolicy.TRANSIT_EXIT, false);
2951 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
2952 KeyWaiter.RETURN_NOTHING);
2953 changed = true;
2954 }
2955 }
2956
2957 if (changed) {
2958 mLayoutNeeded = true;
2959 performLayoutAndPlaceSurfacesLocked();
2960 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
2961 }
Romain Guy06882f82009-06-10 13:36:04 -07002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 if (delayed) {
2964 mExitingTokens.add(wtoken);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002965 } else if (wtoken.windowType == TYPE_WALLPAPER) {
2966 mWallpaperTokens.remove(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
2968 }
Romain Guy06882f82009-06-10 13:36:04 -07002969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002971 Slog.w(TAG, "Attempted to remove non-existing token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 }
2973 }
2974 Binder.restoreCallingIdentity(origId);
2975 }
2976
2977 public void addAppToken(int addPos, IApplicationToken token,
2978 int groupId, int requestedOrientation, boolean fullscreen) {
2979 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
2980 "addAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07002981 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
Romain Guy06882f82009-06-10 13:36:04 -07002983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 synchronized(mWindowMap) {
2985 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
2986 if (wtoken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002987 Slog.w(TAG, "Attempted to add existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 return;
2989 }
2990 wtoken = new AppWindowToken(token);
2991 wtoken.groupId = groupId;
2992 wtoken.appFullscreen = fullscreen;
2993 wtoken.requestedOrientation = requestedOrientation;
2994 mAppTokens.add(addPos, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002995 if (localLOGV) Slog.v(TAG, "Adding new app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 mTokenMap.put(token.asBinder(), wtoken);
2997 mTokenList.add(wtoken);
Romain Guy06882f82009-06-10 13:36:04 -07002998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 // Application tokens start out hidden.
3000 wtoken.hidden = true;
3001 wtoken.hiddenRequested = true;
Romain Guy06882f82009-06-10 13:36:04 -07003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 //dump();
3004 }
3005 }
Romain Guy06882f82009-06-10 13:36:04 -07003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 public void setAppGroupId(IBinder token, int groupId) {
3008 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3009 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003010 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 }
3012
3013 synchronized(mWindowMap) {
3014 AppWindowToken wtoken = findAppWindowToken(token);
3015 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003016 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 return;
3018 }
3019 wtoken.groupId = groupId;
3020 }
3021 }
Romain Guy06882f82009-06-10 13:36:04 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 public int getOrientationFromWindowsLocked() {
3024 int pos = mWindows.size() - 1;
3025 while (pos >= 0) {
3026 WindowState wtoken = (WindowState) mWindows.get(pos);
3027 pos--;
3028 if (wtoken.mAppToken != null) {
3029 // We hit an application window. so the orientation will be determined by the
3030 // app window. No point in continuing further.
3031 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3032 }
Christopher Tateb696aee2010-04-02 19:08:30 -07003033 if (!wtoken.isVisibleLw() || !wtoken.mPolicyVisibilityAfterAnim) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 continue;
3035 }
3036 int req = wtoken.mAttrs.screenOrientation;
3037 if((req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) ||
3038 (req == ActivityInfo.SCREEN_ORIENTATION_BEHIND)){
3039 continue;
3040 } else {
3041 return req;
3042 }
3043 }
3044 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3045 }
Romain Guy06882f82009-06-10 13:36:04 -07003046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 public int getOrientationFromAppTokensLocked() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003048 int pos = mAppTokens.size() - 1;
3049 int curGroup = 0;
3050 int lastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3051 boolean findingBehind = false;
3052 boolean haveGroup = false;
3053 boolean lastFullscreen = false;
3054 while (pos >= 0) {
3055 AppWindowToken wtoken = mAppTokens.get(pos);
3056 pos--;
3057 // if we're about to tear down this window and not seek for
3058 // the behind activity, don't use it for orientation
3059 if (!findingBehind
3060 && (!wtoken.hidden && wtoken.hiddenRequested)) {
3061 continue;
3062 }
3063
3064 if (!haveGroup) {
3065 // We ignore any hidden applications on the top.
3066 if (wtoken.hiddenRequested || wtoken.willBeHidden) {
The Android Open Source Project10592532009-03-18 17:39:46 -07003067 continue;
3068 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003069 haveGroup = true;
3070 curGroup = wtoken.groupId;
3071 lastOrientation = wtoken.requestedOrientation;
3072 } else if (curGroup != wtoken.groupId) {
3073 // If we have hit a new application group, and the bottom
3074 // of the previous group didn't explicitly say to use
3075 // the orientation behind it, and the last app was
3076 // full screen, then we'll stick with the
3077 // user's orientation.
3078 if (lastOrientation != ActivityInfo.SCREEN_ORIENTATION_BEHIND
3079 && lastFullscreen) {
3080 return lastOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003083 int or = wtoken.requestedOrientation;
3084 // If this application is fullscreen, and didn't explicitly say
3085 // to use the orientation behind it, then just take whatever
3086 // orientation it has and ignores whatever is under it.
3087 lastFullscreen = wtoken.appFullscreen;
3088 if (lastFullscreen
3089 && or != ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
3090 return or;
3091 }
3092 // If this application has requested an explicit orientation,
3093 // then use it.
3094 if (or == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ||
3095 or == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
3096 or == ActivityInfo.SCREEN_ORIENTATION_SENSOR ||
3097 or == ActivityInfo.SCREEN_ORIENTATION_NOSENSOR ||
3098 or == ActivityInfo.SCREEN_ORIENTATION_USER) {
3099 return or;
3100 }
3101 findingBehind |= (or == ActivityInfo.SCREEN_ORIENTATION_BEHIND);
3102 }
3103 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 }
Romain Guy06882f82009-06-10 13:36:04 -07003105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 public Configuration updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003107 Configuration currentConfig, IBinder freezeThisOneIfNeeded) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003108 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3109 "updateOrientationFromAppTokens()")) {
3110 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3111 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003112
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003113 Configuration config = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 long ident = Binder.clearCallingIdentity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003115
3116 synchronized(mWindowMap) {
3117 if (updateOrientationFromAppTokensLocked()) {
3118 if (freezeThisOneIfNeeded != null) {
3119 AppWindowToken wtoken = findAppWindowToken(
3120 freezeThisOneIfNeeded);
3121 if (wtoken != null) {
3122 startAppFreezingScreenLocked(wtoken,
3123 ActivityInfo.CONFIG_ORIENTATION);
3124 }
3125 }
3126 config = computeNewConfigurationLocked();
3127
3128 } else if (currentConfig != null) {
3129 // No obvious action we need to take, but if our current
3130 // state mismatches the activity maanager's, update it
3131 mTempConfiguration.setToDefaults();
3132 if (computeNewConfigurationLocked(mTempConfiguration)) {
3133 if (currentConfig.diff(mTempConfiguration) != 0) {
3134 mWaitingForConfig = true;
3135 mLayoutNeeded = true;
3136 startFreezingDisplayLocked();
3137 config = new Configuration(mTempConfiguration);
3138 }
3139 }
3140 }
3141 }
3142
Dianne Hackborncfaef692009-06-15 14:24:44 -07003143 Binder.restoreCallingIdentity(ident);
3144 return config;
3145 }
3146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 /*
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003148 * Determine the new desired orientation of the display, returning
3149 * a non-null new Configuration if it has changed from the current
3150 * orientation. IF TRUE IS RETURNED SOMEONE MUST CALL
3151 * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
3152 * SCREEN. This will typically be done for you if you call
3153 * sendNewConfiguration().
3154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 * The orientation is computed from non-application windows first. If none of
3156 * the non-application windows specify orientation, the orientation is computed from
Romain Guy06882f82009-06-10 13:36:04 -07003157 * application tokens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 * @see android.view.IWindowManager#updateOrientationFromAppTokens(
3159 * android.os.IBinder)
3160 */
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003161 boolean updateOrientationFromAppTokensLocked() {
Christopher Tateb696aee2010-04-02 19:08:30 -07003162 if (mDisplayFrozen) {
3163 // If the display is frozen, some activities may be in the middle
3164 // of restarting, and thus have removed their old window. If the
3165 // window has the flag to hide the lock screen, then the lock screen
3166 // can re-appear and inflict its own orientation on us. Keep the
3167 // orientation stable until this all settles down.
3168 return false;
3169 }
3170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 boolean changed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 long ident = Binder.clearCallingIdentity();
3173 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003174 int req = computeForcedAppOrientationLocked();
Romain Guy06882f82009-06-10 13:36:04 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 if (req != mForcedAppOrientation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 mForcedAppOrientation = req;
3178 //send a message to Policy indicating orientation change to take
3179 //action like disabling/enabling sensors etc.,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003180 mPolicy.setCurrentOrientationLw(req);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003181 if (setRotationUncheckedLocked(WindowManagerPolicy.USE_LAST_ROTATION,
3182 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)) {
3183 changed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
3185 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003186
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003187 return changed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 } finally {
3189 Binder.restoreCallingIdentity(ident);
3190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 }
Romain Guy06882f82009-06-10 13:36:04 -07003192
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003193 int computeForcedAppOrientationLocked() {
3194 int req = getOrientationFromWindowsLocked();
3195 if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
3196 req = getOrientationFromAppTokensLocked();
3197 }
3198 return req;
3199 }
Romain Guy06882f82009-06-10 13:36:04 -07003200
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003201 public void setNewConfiguration(Configuration config) {
3202 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3203 "setNewConfiguration()")) {
3204 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
3205 }
3206
3207 synchronized(mWindowMap) {
3208 mCurConfiguration = new Configuration(config);
3209 mWaitingForConfig = false;
3210 performLayoutAndPlaceSurfacesLocked();
3211 }
3212 }
3213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 public void setAppOrientation(IApplicationToken token, int requestedOrientation) {
3215 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3216 "setAppOrientation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003217 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 }
Romain Guy06882f82009-06-10 13:36:04 -07003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 synchronized(mWindowMap) {
3221 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3222 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003223 Slog.w(TAG, "Attempted to set orientation of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 return;
3225 }
Romain Guy06882f82009-06-10 13:36:04 -07003226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 wtoken.requestedOrientation = requestedOrientation;
3228 }
3229 }
Romain Guy06882f82009-06-10 13:36:04 -07003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 public int getAppOrientation(IApplicationToken token) {
3232 synchronized(mWindowMap) {
3233 AppWindowToken wtoken = findAppWindowToken(token.asBinder());
3234 if (wtoken == null) {
3235 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3236 }
Romain Guy06882f82009-06-10 13:36:04 -07003237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 return wtoken.requestedOrientation;
3239 }
3240 }
Romain Guy06882f82009-06-10 13:36:04 -07003241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 public void setFocusedApp(IBinder token, boolean moveFocusNow) {
3243 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3244 "setFocusedApp()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003245 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 }
3247
3248 synchronized(mWindowMap) {
3249 boolean changed = false;
3250 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003251 if (DEBUG_FOCUS) Slog.v(TAG, "Clearing focused app, was " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 changed = mFocusedApp != null;
3253 mFocusedApp = null;
3254 mKeyWaiter.tickle();
3255 } else {
3256 AppWindowToken newFocus = findAppWindowToken(token);
3257 if (newFocus == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003258 Slog.w(TAG, "Attempted to set focus to non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 return;
3260 }
3261 changed = mFocusedApp != newFocus;
3262 mFocusedApp = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003263 if (DEBUG_FOCUS) Slog.v(TAG, "Set focused app to: " + mFocusedApp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 mKeyWaiter.tickle();
3265 }
3266
3267 if (moveFocusNow && changed) {
3268 final long origId = Binder.clearCallingIdentity();
3269 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3270 Binder.restoreCallingIdentity(origId);
3271 }
3272 }
3273 }
3274
3275 public void prepareAppTransition(int transit) {
3276 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3277 "prepareAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003278 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 }
Romain Guy06882f82009-06-10 13:36:04 -07003280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003282 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 TAG, "Prepare app transition: transit=" + transit
3284 + " mNextAppTransition=" + mNextAppTransition);
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003285 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003286 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET
3287 || mNextAppTransition == WindowManagerPolicy.TRANSIT_NONE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 mNextAppTransition = transit;
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07003289 } else if (transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
3290 && mNextAppTransition == WindowManagerPolicy.TRANSIT_TASK_CLOSE) {
3291 // Opening a new task always supersedes a close for the anim.
3292 mNextAppTransition = transit;
3293 } else if (transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
3294 && mNextAppTransition == WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE) {
3295 // Opening a new activity always supersedes a close for the anim.
3296 mNextAppTransition = transit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 }
3298 mAppTransitionReady = false;
3299 mAppTransitionTimeout = false;
3300 mStartingIconInTransition = false;
3301 mSkipAppTransitionAnimation = false;
3302 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
3303 mH.sendMessageDelayed(mH.obtainMessage(H.APP_TRANSITION_TIMEOUT),
3304 5000);
3305 }
3306 }
3307 }
3308
3309 public int getPendingAppTransition() {
3310 return mNextAppTransition;
3311 }
Romain Guy06882f82009-06-10 13:36:04 -07003312
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003313 public void overridePendingAppTransition(String packageName,
3314 int enterAnim, int exitAnim) {
Dianne Hackborn8b571a82009-09-25 16:09:43 -07003315 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003316 mNextAppTransitionPackage = packageName;
3317 mNextAppTransitionEnter = enterAnim;
3318 mNextAppTransitionExit = exitAnim;
3319 }
3320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 public void executeAppTransition() {
3323 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3324 "executeAppTransition()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003325 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 }
Romain Guy06882f82009-06-10 13:36:04 -07003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 synchronized(mWindowMap) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003329 if (DEBUG_APP_TRANSITIONS) {
3330 RuntimeException e = new RuntimeException("here");
3331 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003332 Slog.w(TAG, "Execute app transition: mNextAppTransition="
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003333 + mNextAppTransition, e);
3334 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003335 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 mAppTransitionReady = true;
3337 final long origId = Binder.clearCallingIdentity();
3338 performLayoutAndPlaceSurfacesLocked();
3339 Binder.restoreCallingIdentity(origId);
3340 }
3341 }
3342 }
3343
3344 public void setAppStartingWindow(IBinder token, String pkg,
3345 int theme, CharSequence nonLocalizedLabel, int labelRes, int icon,
3346 IBinder transferFrom, boolean createIfNeeded) {
3347 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3348 "setAppStartingIcon()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003349 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351
3352 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003353 if (DEBUG_STARTING_WINDOW) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 TAG, "setAppStartingIcon: token=" + token + " pkg=" + pkg
3355 + " transferFrom=" + transferFrom);
Romain Guy06882f82009-06-10 13:36:04 -07003356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 AppWindowToken wtoken = findAppWindowToken(token);
3358 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003359 Slog.w(TAG, "Attempted to set icon of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 return;
3361 }
3362
3363 // If the display is frozen, we won't do anything until the
3364 // actual window is displayed so there is no reason to put in
3365 // the starting window.
Dianne Hackbornde2606d2009-12-18 16:53:55 -08003366 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 return;
3368 }
Romain Guy06882f82009-06-10 13:36:04 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 if (wtoken.startingData != null) {
3371 return;
3372 }
Romain Guy06882f82009-06-10 13:36:04 -07003373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 if (transferFrom != null) {
3375 AppWindowToken ttoken = findAppWindowToken(transferFrom);
3376 if (ttoken != null) {
3377 WindowState startingWindow = ttoken.startingWindow;
3378 if (startingWindow != null) {
3379 if (mStartingIconInTransition) {
3380 // In this case, the starting icon has already
3381 // been displayed, so start letting windows get
3382 // shown immediately without any more transitions.
3383 mSkipAppTransitionAnimation = true;
3384 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003385 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 "Moving existing starting from " + ttoken
3387 + " to " + wtoken);
3388 final long origId = Binder.clearCallingIdentity();
Romain Guy06882f82009-06-10 13:36:04 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 // Transfer the starting window over to the new
3391 // token.
3392 wtoken.startingData = ttoken.startingData;
3393 wtoken.startingView = ttoken.startingView;
3394 wtoken.startingWindow = startingWindow;
3395 ttoken.startingData = null;
3396 ttoken.startingView = null;
3397 ttoken.startingWindow = null;
3398 ttoken.startingMoved = true;
3399 startingWindow.mToken = wtoken;
Dianne Hackbornef49c572009-03-24 19:27:32 -07003400 startingWindow.mRootToken = wtoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 startingWindow.mAppToken = wtoken;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003402 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003403 "Removing starting window: " + startingWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 mWindows.remove(startingWindow);
3405 ttoken.windows.remove(startingWindow);
3406 ttoken.allAppWindows.remove(startingWindow);
3407 addWindowToListInOrderLocked(startingWindow, true);
Romain Guy06882f82009-06-10 13:36:04 -07003408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 // Propagate other interesting state between the
3410 // tokens. If the old token is displayed, we should
3411 // immediately force the new one to be displayed. If
3412 // it is animating, we need to move that animation to
3413 // the new one.
3414 if (ttoken.allDrawn) {
3415 wtoken.allDrawn = true;
3416 }
3417 if (ttoken.firstWindowDrawn) {
3418 wtoken.firstWindowDrawn = true;
3419 }
3420 if (!ttoken.hidden) {
3421 wtoken.hidden = false;
3422 wtoken.hiddenRequested = false;
3423 wtoken.willBeHidden = false;
3424 }
3425 if (wtoken.clientHidden != ttoken.clientHidden) {
3426 wtoken.clientHidden = ttoken.clientHidden;
3427 wtoken.sendAppVisibilityToClients();
3428 }
3429 if (ttoken.animation != null) {
3430 wtoken.animation = ttoken.animation;
3431 wtoken.animating = ttoken.animating;
3432 wtoken.animLayerAdjustment = ttoken.animLayerAdjustment;
3433 ttoken.animation = null;
3434 ttoken.animLayerAdjustment = 0;
3435 wtoken.updateLayers();
3436 ttoken.updateLayers();
3437 }
Romain Guy06882f82009-06-10 13:36:04 -07003438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 mLayoutNeeded = true;
3441 performLayoutAndPlaceSurfacesLocked();
3442 Binder.restoreCallingIdentity(origId);
3443 return;
3444 } else if (ttoken.startingData != null) {
3445 // The previous app was getting ready to show a
3446 // starting window, but hasn't yet done so. Steal it!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003447 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 "Moving pending starting from " + ttoken
3449 + " to " + wtoken);
3450 wtoken.startingData = ttoken.startingData;
3451 ttoken.startingData = null;
3452 ttoken.startingMoved = true;
3453 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3454 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3455 // want to process the message ASAP, before any other queued
3456 // messages.
3457 mH.sendMessageAtFrontOfQueue(m);
3458 return;
3459 }
3460 }
3461 }
3462
3463 // There is no existing starting window, and the caller doesn't
3464 // want us to create one, so that's it!
3465 if (!createIfNeeded) {
3466 return;
3467 }
Romain Guy06882f82009-06-10 13:36:04 -07003468
Dianne Hackborn284ac932009-08-28 10:34:25 -07003469 // If this is a translucent or wallpaper window, then don't
3470 // show a starting window -- the current effect (a full-screen
3471 // opaque starting window that fades away to the real contents
3472 // when it is ready) does not work for this.
3473 if (theme != 0) {
3474 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
3475 com.android.internal.R.styleable.Window);
3476 if (ent.array.getBoolean(
3477 com.android.internal.R.styleable.Window_windowIsTranslucent, false)) {
3478 return;
3479 }
3480 if (ent.array.getBoolean(
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07003481 com.android.internal.R.styleable.Window_windowIsFloating, false)) {
3482 return;
3483 }
3484 if (ent.array.getBoolean(
Dianne Hackborn284ac932009-08-28 10:34:25 -07003485 com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
3486 return;
3487 }
3488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 mStartingIconInTransition = true;
3491 wtoken.startingData = new StartingData(
3492 pkg, theme, nonLocalizedLabel,
3493 labelRes, icon);
3494 Message m = mH.obtainMessage(H.ADD_STARTING, wtoken);
3495 // Note: we really want to do sendMessageAtFrontOfQueue() because we
3496 // want to process the message ASAP, before any other queued
3497 // messages.
3498 mH.sendMessageAtFrontOfQueue(m);
3499 }
3500 }
3501
3502 public void setAppWillBeHidden(IBinder token) {
3503 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3504 "setAppWillBeHidden()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003505 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507
3508 AppWindowToken wtoken;
3509
3510 synchronized(mWindowMap) {
3511 wtoken = findAppWindowToken(token);
3512 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003513 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 -08003514 return;
3515 }
3516 wtoken.willBeHidden = true;
3517 }
3518 }
Romain Guy06882f82009-06-10 13:36:04 -07003519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 boolean setTokenVisibilityLocked(AppWindowToken wtoken, WindowManager.LayoutParams lp,
3521 boolean visible, int transit, boolean performLayout) {
3522 boolean delayed = false;
3523
3524 if (wtoken.clientHidden == visible) {
3525 wtoken.clientHidden = !visible;
3526 wtoken.sendAppVisibilityToClients();
3527 }
Romain Guy06882f82009-06-10 13:36:04 -07003528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 wtoken.willBeHidden = false;
3530 if (wtoken.hidden == visible) {
3531 final int N = wtoken.allAppWindows.size();
3532 boolean changed = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003533 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 TAG, "Changing app " + wtoken + " hidden=" + wtoken.hidden
3535 + " performLayout=" + performLayout);
Romain Guy06882f82009-06-10 13:36:04 -07003536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 boolean runningAppAnimation = false;
Romain Guy06882f82009-06-10 13:36:04 -07003538
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003539 if (transit != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 if (wtoken.animation == sDummyAnimation) {
3541 wtoken.animation = null;
3542 }
3543 applyAnimationLocked(wtoken, lp, transit, visible);
3544 changed = true;
3545 if (wtoken.animation != null) {
3546 delayed = runningAppAnimation = true;
3547 }
3548 }
Romain Guy06882f82009-06-10 13:36:04 -07003549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 for (int i=0; i<N; i++) {
3551 WindowState win = wtoken.allAppWindows.get(i);
3552 if (win == wtoken.startingWindow) {
3553 continue;
3554 }
3555
3556 if (win.isAnimating()) {
3557 delayed = true;
3558 }
Romain Guy06882f82009-06-10 13:36:04 -07003559
Joe Onorato8a9b2202010-02-26 18:56:32 -08003560 //Slog.i(TAG, "Window " + win + ": vis=" + win.isVisible());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 //win.dump(" ");
3562 if (visible) {
3563 if (!win.isVisibleNow()) {
3564 if (!runningAppAnimation) {
3565 applyAnimationLocked(win,
3566 WindowManagerPolicy.TRANSIT_ENTER, true);
3567 }
3568 changed = true;
3569 }
3570 } else if (win.isVisibleNow()) {
3571 if (!runningAppAnimation) {
3572 applyAnimationLocked(win,
3573 WindowManagerPolicy.TRANSIT_EXIT, false);
3574 }
3575 mKeyWaiter.finishedKey(win.mSession, win.mClient, true,
3576 KeyWaiter.RETURN_NOTHING);
3577 changed = true;
3578 }
3579 }
3580
3581 wtoken.hidden = wtoken.hiddenRequested = !visible;
3582 if (!visible) {
3583 unsetAppFreezingScreenLocked(wtoken, true, true);
3584 } else {
3585 // If we are being set visible, and the starting window is
3586 // not yet displayed, then make sure it doesn't get displayed.
3587 WindowState swin = wtoken.startingWindow;
3588 if (swin != null && (swin.mDrawPending
3589 || swin.mCommitDrawPending)) {
3590 swin.mPolicyVisibility = false;
3591 swin.mPolicyVisibilityAfterAnim = false;
3592 }
3593 }
Romain Guy06882f82009-06-10 13:36:04 -07003594
Joe Onorato8a9b2202010-02-26 18:56:32 -08003595 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "setTokenVisibilityLocked: " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 + ": hidden=" + wtoken.hidden + " hiddenRequested="
3597 + wtoken.hiddenRequested);
Romain Guy06882f82009-06-10 13:36:04 -07003598
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003599 if (changed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 mLayoutNeeded = true;
Dianne Hackborn9b52a212009-12-11 14:51:35 -08003601 if (performLayout) {
3602 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
3603 performLayoutAndPlaceSurfacesLocked();
3604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 }
3606 }
3607
3608 if (wtoken.animation != null) {
3609 delayed = true;
3610 }
Romain Guy06882f82009-06-10 13:36:04 -07003611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 return delayed;
3613 }
3614
3615 public void setAppVisibility(IBinder token, boolean visible) {
3616 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3617 "setAppVisibility()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003618 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 }
3620
3621 AppWindowToken wtoken;
3622
3623 synchronized(mWindowMap) {
3624 wtoken = findAppWindowToken(token);
3625 if (wtoken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003626 Slog.w(TAG, "Attempted to set visibility of non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 return;
3628 }
3629
3630 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003631 RuntimeException e = null;
3632 if (!HIDE_STACK_CRAWLS) {
3633 e = new RuntimeException();
3634 e.fillInStackTrace();
3635 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003636 Slog.v(TAG, "setAppVisibility(" + token + ", " + visible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 + "): mNextAppTransition=" + mNextAppTransition
3638 + " hidden=" + wtoken.hidden
3639 + " hiddenRequested=" + wtoken.hiddenRequested, e);
3640 }
Romain Guy06882f82009-06-10 13:36:04 -07003641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 // If we are preparing an app transition, then delay changing
3643 // the visibility of this token until we execute that transition.
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003644 if (!mDisplayFrozen && mPolicy.isScreenOn()
3645 && mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 // Already in requested state, don't do anything more.
3647 if (wtoken.hiddenRequested != visible) {
3648 return;
3649 }
3650 wtoken.hiddenRequested = !visible;
Romain Guy06882f82009-06-10 13:36:04 -07003651
Joe Onorato8a9b2202010-02-26 18:56:32 -08003652 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 TAG, "Setting dummy animation on: " + wtoken);
3654 wtoken.setDummyAnimation();
3655 mOpeningApps.remove(wtoken);
3656 mClosingApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003657 wtoken.waitingToShow = wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 wtoken.inPendingTransaction = true;
3659 if (visible) {
3660 mOpeningApps.add(wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 wtoken.startingDisplayed = false;
3662 wtoken.startingMoved = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003663
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003664 // If the token is currently hidden (should be the
3665 // common case), then we need to set up to wait for
3666 // its windows to be ready.
3667 if (wtoken.hidden) {
3668 wtoken.allDrawn = false;
3669 wtoken.waitingToShow = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003670
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003671 if (wtoken.clientHidden) {
3672 // In the case where we are making an app visible
3673 // but holding off for a transition, we still need
3674 // to tell the client to make its windows visible so
3675 // they get drawn. Otherwise, we will wait on
3676 // performing the transition until all windows have
3677 // been drawn, they never will be, and we are sad.
3678 wtoken.clientHidden = false;
3679 wtoken.sendAppVisibilityToClients();
3680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 }
3682 } else {
3683 mClosingApps.add(wtoken);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003684
Dianne Hackborn195f6a02009-11-24 11:26:00 -08003685 // If the token is currently visible (should be the
3686 // common case), then set up to wait for it to be hidden.
3687 if (!wtoken.hidden) {
3688 wtoken.waitingToHide = true;
3689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
3691 return;
3692 }
Romain Guy06882f82009-06-10 13:36:04 -07003693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003695 setTokenVisibilityLocked(wtoken, null, visible, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 wtoken.updateReportedVisibilityLocked();
3697 Binder.restoreCallingIdentity(origId);
3698 }
3699 }
3700
3701 void unsetAppFreezingScreenLocked(AppWindowToken wtoken,
3702 boolean unfreezeSurfaceNow, boolean force) {
3703 if (wtoken.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003704 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + wtoken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 + " force=" + force);
3706 final int N = wtoken.allAppWindows.size();
3707 boolean unfrozeWindows = false;
3708 for (int i=0; i<N; i++) {
3709 WindowState w = wtoken.allAppWindows.get(i);
3710 if (w.mAppFreezing) {
3711 w.mAppFreezing = false;
3712 if (w.mSurface != null && !w.mOrientationChanging) {
3713 w.mOrientationChanging = true;
3714 }
3715 unfrozeWindows = true;
3716 }
3717 }
3718 if (force || unfrozeWindows) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003719 if (DEBUG_ORIENTATION) Slog.v(TAG, "No longer freezing: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 wtoken.freezingScreen = false;
3721 mAppsFreezingScreen--;
3722 }
3723 if (unfreezeSurfaceNow) {
3724 if (unfrozeWindows) {
3725 mLayoutNeeded = true;
3726 performLayoutAndPlaceSurfacesLocked();
3727 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003728 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 }
3730 }
3731 }
Romain Guy06882f82009-06-10 13:36:04 -07003732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 public void startAppFreezingScreenLocked(AppWindowToken wtoken,
3734 int configChanges) {
3735 if (DEBUG_ORIENTATION) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08003736 RuntimeException e = null;
3737 if (!HIDE_STACK_CRAWLS) {
3738 e = new RuntimeException();
3739 e.fillInStackTrace();
3740 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003741 Slog.i(TAG, "Set freezing of " + wtoken.appToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 + ": hidden=" + wtoken.hidden + " freezing="
3743 + wtoken.freezingScreen, e);
3744 }
3745 if (!wtoken.hiddenRequested) {
3746 if (!wtoken.freezingScreen) {
3747 wtoken.freezingScreen = true;
3748 mAppsFreezingScreen++;
3749 if (mAppsFreezingScreen == 1) {
3750 startFreezingDisplayLocked();
3751 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
3752 mH.sendMessageDelayed(mH.obtainMessage(H.APP_FREEZE_TIMEOUT),
3753 5000);
3754 }
3755 }
3756 final int N = wtoken.allAppWindows.size();
3757 for (int i=0; i<N; i++) {
3758 WindowState w = wtoken.allAppWindows.get(i);
3759 w.mAppFreezing = true;
3760 }
3761 }
3762 }
Romain Guy06882f82009-06-10 13:36:04 -07003763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 public void startAppFreezingScreen(IBinder token, int configChanges) {
3765 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3766 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003767 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
3769
3770 synchronized(mWindowMap) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08003771 if (configChanges == 0 && !mDisplayFrozen && mPolicy.isScreenOn()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003772 if (DEBUG_ORIENTATION) Slog.v(TAG, "Skipping set freeze of " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 return;
3774 }
Romain Guy06882f82009-06-10 13:36:04 -07003775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 AppWindowToken wtoken = findAppWindowToken(token);
3777 if (wtoken == null || wtoken.appToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003778 Slog.w(TAG, "Attempted to freeze screen with non-existing app token: " + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 return;
3780 }
3781 final long origId = Binder.clearCallingIdentity();
3782 startAppFreezingScreenLocked(wtoken, configChanges);
3783 Binder.restoreCallingIdentity(origId);
3784 }
3785 }
Romain Guy06882f82009-06-10 13:36:04 -07003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 public void stopAppFreezingScreen(IBinder token, boolean force) {
3788 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3789 "setAppFreezingScreen()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003790 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792
3793 synchronized(mWindowMap) {
3794 AppWindowToken wtoken = findAppWindowToken(token);
3795 if (wtoken == null || wtoken.appToken == null) {
3796 return;
3797 }
3798 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08003799 if (DEBUG_ORIENTATION) Slog.v(TAG, "Clear freezing of " + token
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 + ": hidden=" + wtoken.hidden + " freezing=" + wtoken.freezingScreen);
3801 unsetAppFreezingScreenLocked(wtoken, true, force);
3802 Binder.restoreCallingIdentity(origId);
3803 }
3804 }
Romain Guy06882f82009-06-10 13:36:04 -07003805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 public void removeAppToken(IBinder token) {
3807 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
3808 "removeAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07003809 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 }
3811
3812 AppWindowToken wtoken = null;
3813 AppWindowToken startingToken = null;
3814 boolean delayed = false;
3815
3816 final long origId = Binder.clearCallingIdentity();
3817 synchronized(mWindowMap) {
3818 WindowToken basewtoken = mTokenMap.remove(token);
3819 mTokenList.remove(basewtoken);
3820 if (basewtoken != null && (wtoken=basewtoken.appWindowToken) != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003821 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "Removing app token: " + wtoken);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003822 delayed = setTokenVisibilityLocked(wtoken, null, false, WindowManagerPolicy.TRANSIT_UNSET, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 wtoken.inPendingTransaction = false;
3824 mOpeningApps.remove(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003825 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 if (mClosingApps.contains(wtoken)) {
3827 delayed = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003828 } else if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 mClosingApps.add(wtoken);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003830 wtoken.waitingToHide = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 delayed = true;
3832 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003833 if (DEBUG_APP_TRANSITIONS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 TAG, "Removing app " + wtoken + " delayed=" + delayed
3835 + " animation=" + wtoken.animation
3836 + " animating=" + wtoken.animating);
3837 if (delayed) {
3838 // set the token aside because it has an active animation to be finished
3839 mExitingAppTokens.add(wtoken);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003840 } else {
3841 // Make sure there is no animation running on this token,
3842 // so any windows associated with it will be removed as
3843 // soon as their animations are complete
3844 wtoken.animation = null;
3845 wtoken.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 }
3847 mAppTokens.remove(wtoken);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07003848 if (mLastEnterAnimToken == wtoken) {
3849 mLastEnterAnimToken = null;
3850 mLastEnterAnimParams = null;
3851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 wtoken.removed = true;
3853 if (wtoken.startingData != null) {
3854 startingToken = wtoken;
3855 }
3856 unsetAppFreezingScreenLocked(wtoken, true, true);
3857 if (mFocusedApp == wtoken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003858 if (DEBUG_FOCUS) Slog.v(TAG, "Removing focused app token:" + wtoken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 mFocusedApp = null;
3860 updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL);
3861 mKeyWaiter.tickle();
3862 }
3863 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003864 Slog.w(TAG, "Attempted to remove non-existing app token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 }
Romain Guy06882f82009-06-10 13:36:04 -07003866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 if (!delayed && wtoken != null) {
3868 wtoken.updateReportedVisibilityLocked();
3869 }
3870 }
3871 Binder.restoreCallingIdentity(origId);
3872
3873 if (startingToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003874 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Schedule remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 + startingToken + ": app token removed");
3876 Message m = mH.obtainMessage(H.REMOVE_STARTING, startingToken);
3877 mH.sendMessage(m);
3878 }
3879 }
3880
3881 private boolean tmpRemoveAppWindowsLocked(WindowToken token) {
3882 final int NW = token.windows.size();
3883 for (int i=0; i<NW; i++) {
3884 WindowState win = token.windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003885 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Tmp removing app window " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 mWindows.remove(win);
3887 int j = win.mChildWindows.size();
3888 while (j > 0) {
3889 j--;
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003890 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003891 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003892 "Tmp removing child window " + cwin);
3893 mWindows.remove(cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895 }
3896 return NW > 0;
3897 }
3898
3899 void dumpAppTokensLocked() {
3900 for (int i=mAppTokens.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003901 Slog.v(TAG, " #" + i + ": " + mAppTokens.get(i).token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903 }
Romain Guy06882f82009-06-10 13:36:04 -07003904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 void dumpWindowsLocked() {
3906 for (int i=mWindows.size()-1; i>=0; i--) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003907 Slog.v(TAG, " #" + i + ": " + mWindows.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 }
3909 }
Romain Guy06882f82009-06-10 13:36:04 -07003910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 private int findWindowOffsetLocked(int tokenPos) {
3912 final int NW = mWindows.size();
3913
3914 if (tokenPos >= mAppTokens.size()) {
3915 int i = NW;
3916 while (i > 0) {
3917 i--;
3918 WindowState win = (WindowState)mWindows.get(i);
3919 if (win.getAppToken() != null) {
3920 return i+1;
3921 }
3922 }
3923 }
3924
3925 while (tokenPos > 0) {
3926 // Find the first app token below the new position that has
3927 // a window displayed.
3928 final AppWindowToken wtoken = mAppTokens.get(tokenPos-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003929 if (DEBUG_REORDER) Slog.v(TAG, "Looking for lower windows @ "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 + tokenPos + " -- " + wtoken.token);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003931 if (wtoken.sendingToBottom) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003932 if (DEBUG_REORDER) Slog.v(TAG,
Dianne Hackborna8f60182009-09-01 19:01:50 -07003933 "Skipping token -- currently sending to bottom");
3934 tokenPos--;
3935 continue;
3936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 int i = wtoken.windows.size();
3938 while (i > 0) {
3939 i--;
3940 WindowState win = wtoken.windows.get(i);
3941 int j = win.mChildWindows.size();
3942 while (j > 0) {
3943 j--;
3944 WindowState cwin = (WindowState)win.mChildWindows.get(j);
Dianne Hackborna8f60182009-09-01 19:01:50 -07003945 if (cwin.mSubLayer >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 for (int pos=NW-1; pos>=0; pos--) {
3947 if (mWindows.get(pos) == cwin) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003948 if (DEBUG_REORDER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 "Found child win @" + (pos+1));
3950 return pos+1;
3951 }
3952 }
3953 }
3954 }
3955 for (int pos=NW-1; pos>=0; pos--) {
3956 if (mWindows.get(pos) == win) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003957 if (DEBUG_REORDER) Slog.v(TAG, "Found win @" + (pos+1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 return pos+1;
3959 }
3960 }
3961 }
3962 tokenPos--;
3963 }
3964
3965 return 0;
3966 }
3967
3968 private final int reAddWindowLocked(int index, WindowState win) {
3969 final int NCW = win.mChildWindows.size();
3970 boolean added = false;
3971 for (int j=0; j<NCW; j++) {
3972 WindowState cwin = (WindowState)win.mChildWindows.get(j);
3973 if (!added && cwin.mSubLayer >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003974 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding child window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003975 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 mWindows.add(index, win);
3977 index++;
3978 added = true;
3979 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003980 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003981 + index + ": " + cwin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 mWindows.add(index, cwin);
3983 index++;
3984 }
3985 if (!added) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003986 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG, "Re-adding window at "
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07003987 + index + ": " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 mWindows.add(index, win);
3989 index++;
3990 }
3991 return index;
3992 }
Romain Guy06882f82009-06-10 13:36:04 -07003993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 private final int reAddAppWindowsLocked(int index, WindowToken token) {
3995 final int NW = token.windows.size();
3996 for (int i=0; i<NW; i++) {
3997 index = reAddWindowLocked(index, token.windows.get(i));
3998 }
3999 return index;
4000 }
4001
4002 public void moveAppToken(int index, IBinder token) {
4003 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4004 "moveAppToken()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004005 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 }
4007
4008 synchronized(mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004009 if (DEBUG_REORDER) Slog.v(TAG, "Initial app tokens:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 if (DEBUG_REORDER) dumpAppTokensLocked();
4011 final AppWindowToken wtoken = findAppWindowToken(token);
4012 if (wtoken == null || !mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004013 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 + token + " (" + wtoken + ")");
4015 return;
4016 }
4017 mAppTokens.add(index, wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004018 if (DEBUG_REORDER) Slog.v(TAG, "Moved " + token + " to " + index + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 if (DEBUG_REORDER) dumpAppTokensLocked();
Romain Guy06882f82009-06-10 13:36:04 -07004020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 final long origId = Binder.clearCallingIdentity();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004022 if (DEBUG_REORDER) Slog.v(TAG, "Removing windows in " + token + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 if (DEBUG_REORDER) dumpWindowsLocked();
4024 if (tmpRemoveAppWindowsLocked(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004025 if (DEBUG_REORDER) Slog.v(TAG, "Adding windows back in:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 if (DEBUG_REORDER) dumpWindowsLocked();
4027 reAddAppWindowsLocked(findWindowOffsetLocked(index), wtoken);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004028 if (DEBUG_REORDER) Slog.v(TAG, "Final window list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 if (DEBUG_REORDER) dumpWindowsLocked();
4030 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 mLayoutNeeded = true;
4032 performLayoutAndPlaceSurfacesLocked();
4033 }
4034 Binder.restoreCallingIdentity(origId);
4035 }
4036 }
4037
4038 private void removeAppTokensLocked(List<IBinder> tokens) {
4039 // XXX This should be done more efficiently!
4040 // (take advantage of the fact that both lists should be
4041 // ordered in the same way.)
4042 int N = tokens.size();
4043 for (int i=0; i<N; i++) {
4044 IBinder token = tokens.get(i);
4045 final AppWindowToken wtoken = findAppWindowToken(token);
4046 if (!mAppTokens.remove(wtoken)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004047 Slog.w(TAG, "Attempting to reorder token that doesn't exist: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 + token + " (" + wtoken + ")");
4049 i--;
4050 N--;
4051 }
4052 }
4053 }
4054
Dianne Hackborna8f60182009-09-01 19:01:50 -07004055 private void moveAppWindowsLocked(AppWindowToken wtoken, int tokenPos,
4056 boolean updateFocusAndLayout) {
4057 // First remove all of the windows from the list.
4058 tmpRemoveAppWindowsLocked(wtoken);
4059
4060 // Where to start adding?
4061 int pos = findWindowOffsetLocked(tokenPos);
4062
4063 // And now add them back at the correct place.
4064 pos = reAddAppWindowsLocked(pos, wtoken);
4065
4066 if (updateFocusAndLayout) {
4067 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4068 assignLayersLocked();
4069 }
4070 mLayoutNeeded = true;
4071 performLayoutAndPlaceSurfacesLocked();
4072 }
4073 }
4074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 private void moveAppWindowsLocked(List<IBinder> tokens, int tokenPos) {
4076 // First remove all of the windows from the list.
4077 final int N = tokens.size();
4078 int i;
4079 for (i=0; i<N; i++) {
4080 WindowToken token = mTokenMap.get(tokens.get(i));
4081 if (token != null) {
4082 tmpRemoveAppWindowsLocked(token);
4083 }
4084 }
4085
4086 // Where to start adding?
4087 int pos = findWindowOffsetLocked(tokenPos);
4088
4089 // And now add them back at the correct place.
4090 for (i=0; i<N; i++) {
4091 WindowToken token = mTokenMap.get(tokens.get(i));
4092 if (token != null) {
4093 pos = reAddAppWindowsLocked(pos, token);
4094 }
4095 }
4096
Dianne Hackborna8f60182009-09-01 19:01:50 -07004097 if (!updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
4098 assignLayersLocked();
4099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 mLayoutNeeded = true;
4101 performLayoutAndPlaceSurfacesLocked();
4102
4103 //dump();
4104 }
4105
4106 public void moveAppTokensToTop(List<IBinder> tokens) {
4107 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4108 "moveAppTokensToTop()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004109 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 }
4111
4112 final long origId = Binder.clearCallingIdentity();
4113 synchronized(mWindowMap) {
4114 removeAppTokensLocked(tokens);
4115 final int N = tokens.size();
4116 for (int i=0; i<N; i++) {
4117 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4118 if (wt != null) {
4119 mAppTokens.add(wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004120 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004121 mToTopApps.remove(wt);
4122 mToBottomApps.remove(wt);
4123 mToTopApps.add(wt);
4124 wt.sendingToBottom = false;
4125 wt.sendingToTop = true;
4126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 }
4128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004129
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004130 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004131 moveAppWindowsLocked(tokens, mAppTokens.size());
4132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
4134 Binder.restoreCallingIdentity(origId);
4135 }
4136
4137 public void moveAppTokensToBottom(List<IBinder> tokens) {
4138 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
4139 "moveAppTokensToBottom()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004140 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 }
4142
4143 final long origId = Binder.clearCallingIdentity();
4144 synchronized(mWindowMap) {
4145 removeAppTokensLocked(tokens);
4146 final int N = tokens.size();
4147 int pos = 0;
4148 for (int i=0; i<N; i++) {
4149 AppWindowToken wt = findAppWindowToken(tokens.get(i));
4150 if (wt != null) {
4151 mAppTokens.add(pos, wt);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004152 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004153 mToTopApps.remove(wt);
4154 mToBottomApps.remove(wt);
4155 mToBottomApps.add(i, wt);
4156 wt.sendingToTop = false;
4157 wt.sendingToBottom = true;
4158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 pos++;
4160 }
4161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004162
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07004163 if (mNextAppTransition == WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07004164 moveAppWindowsLocked(tokens, 0);
4165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 }
4167 Binder.restoreCallingIdentity(origId);
4168 }
4169
4170 // -------------------------------------------------------------
4171 // Misc IWindowSession methods
4172 // -------------------------------------------------------------
Romain Guy06882f82009-06-10 13:36:04 -07004173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 public void disableKeyguard(IBinder token, String tag) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004175 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 != PackageManager.PERMISSION_GRANTED) {
4177 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4178 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004179 synchronized (mKeyguardTokenWatcher) {
4180 mKeyguardTokenWatcher.acquire(token, tag);
Mike Lockwooddd884682009-10-11 16:57:08 -04004181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
4183
4184 public void reenableKeyguard(IBinder token) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004185 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 != PackageManager.PERMISSION_GRANTED) {
4187 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4188 }
Mike Lockwood983ee092009-11-22 01:42:24 -05004189 synchronized (mKeyguardTokenWatcher) {
4190 mKeyguardTokenWatcher.release(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191
Mike Lockwood983ee092009-11-22 01:42:24 -05004192 if (!mKeyguardTokenWatcher.isAcquired()) {
4193 // If we are the last one to reenable the keyguard wait until
4194 // we have actaully finished reenabling until returning.
4195 // It is possible that reenableKeyguard() can be called before
4196 // the previous disableKeyguard() is handled, in which case
4197 // neither mKeyguardTokenWatcher.acquired() or released() would
4198 // be called. In that case mKeyguardDisabled will be false here
4199 // and we have nothing to wait for.
4200 while (mKeyguardDisabled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 try {
Mike Lockwood983ee092009-11-22 01:42:24 -05004202 mKeyguardTokenWatcher.wait();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 } catch (InterruptedException e) {
4204 Thread.currentThread().interrupt();
4205 }
4206 }
4207 }
4208 }
4209 }
4210
4211 /**
4212 * @see android.app.KeyguardManager#exitKeyguardSecurely
4213 */
4214 public void exitKeyguardSecurely(final IOnKeyguardExitResult callback) {
Mike Lockwood733fdf32009-09-28 19:08:53 -04004215 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 != PackageManager.PERMISSION_GRANTED) {
4217 throw new SecurityException("Requires DISABLE_KEYGUARD permission");
4218 }
4219 mPolicy.exitKeyguardSecurely(new WindowManagerPolicy.OnKeyguardExitResult() {
4220 public void onKeyguardExitResult(boolean success) {
4221 try {
4222 callback.onKeyguardExitResult(success);
4223 } catch (RemoteException e) {
4224 // Client has died, we don't care.
4225 }
4226 }
4227 });
4228 }
4229
4230 public boolean inKeyguardRestrictedInputMode() {
4231 return mPolicy.inKeyguardRestrictedKeyInputMode();
4232 }
Romain Guy06882f82009-06-10 13:36:04 -07004233
Dianne Hackbornffa42482009-09-23 22:20:11 -07004234 public void closeSystemDialogs(String reason) {
4235 synchronized(mWindowMap) {
4236 for (int i=mWindows.size()-1; i>=0; i--) {
4237 WindowState w = (WindowState)mWindows.get(i);
4238 if (w.mSurface != null) {
4239 try {
4240 w.mClient.closeSystemDialogs(reason);
4241 } catch (RemoteException e) {
4242 }
4243 }
4244 }
4245 }
4246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 static float fixScale(float scale) {
4249 if (scale < 0) scale = 0;
4250 else if (scale > 20) scale = 20;
4251 return Math.abs(scale);
4252 }
Romain Guy06882f82009-06-10 13:36:04 -07004253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 public void setAnimationScale(int which, float scale) {
4255 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4256 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004257 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
4259
4260 if (scale < 0) scale = 0;
4261 else if (scale > 20) scale = 20;
4262 scale = Math.abs(scale);
4263 switch (which) {
4264 case 0: mWindowAnimationScale = fixScale(scale); break;
4265 case 1: mTransitionAnimationScale = fixScale(scale); break;
4266 }
Romain Guy06882f82009-06-10 13:36:04 -07004267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 // Persist setting
4269 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4270 }
Romain Guy06882f82009-06-10 13:36:04 -07004271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 public void setAnimationScales(float[] scales) {
4273 if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
4274 "setAnimationScale()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004275 throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
4277
4278 if (scales != null) {
4279 if (scales.length >= 1) {
4280 mWindowAnimationScale = fixScale(scales[0]);
4281 }
4282 if (scales.length >= 2) {
4283 mTransitionAnimationScale = fixScale(scales[1]);
4284 }
4285 }
Romain Guy06882f82009-06-10 13:36:04 -07004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 // Persist setting
4288 mH.obtainMessage(H.PERSIST_ANIMATION_SCALE).sendToTarget();
4289 }
Romain Guy06882f82009-06-10 13:36:04 -07004290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 public float getAnimationScale(int which) {
4292 switch (which) {
4293 case 0: return mWindowAnimationScale;
4294 case 1: return mTransitionAnimationScale;
4295 }
4296 return 0;
4297 }
Romain Guy06882f82009-06-10 13:36:04 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 public float[] getAnimationScales() {
4300 return new float[] { mWindowAnimationScale, mTransitionAnimationScale };
4301 }
Romain Guy06882f82009-06-10 13:36:04 -07004302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 public int getSwitchState(int sw) {
4304 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4305 "getSwitchState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004306 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 }
4308 return KeyInputQueue.getSwitchState(sw);
4309 }
Romain Guy06882f82009-06-10 13:36:04 -07004310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 public int getSwitchStateForDevice(int devid, int sw) {
4312 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4313 "getSwitchStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004314 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
4316 return KeyInputQueue.getSwitchState(devid, sw);
4317 }
Romain Guy06882f82009-06-10 13:36:04 -07004318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 public int getScancodeState(int sw) {
4320 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4321 "getScancodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004322 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004324 return mQueue.getScancodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 }
Romain Guy06882f82009-06-10 13:36:04 -07004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 public int getScancodeStateForDevice(int devid, int sw) {
4328 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4329 "getScancodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004330 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004332 return mQueue.getScancodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 }
Romain Guy06882f82009-06-10 13:36:04 -07004334
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004335 public int getTrackballScancodeState(int sw) {
4336 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4337 "getTrackballScancodeState()")) {
4338 throw new SecurityException("Requires READ_INPUT_STATE permission");
4339 }
4340 return mQueue.getTrackballScancodeState(sw);
4341 }
4342
4343 public int getDPadScancodeState(int sw) {
4344 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4345 "getDPadScancodeState()")) {
4346 throw new SecurityException("Requires READ_INPUT_STATE permission");
4347 }
4348 return mQueue.getDPadScancodeState(sw);
4349 }
4350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 public int getKeycodeState(int sw) {
4352 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4353 "getKeycodeState()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004354 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004356 return mQueue.getKeycodeState(sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
Romain Guy06882f82009-06-10 13:36:04 -07004358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 public int getKeycodeStateForDevice(int devid, int sw) {
4360 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4361 "getKeycodeStateForDevice()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004362 throw new SecurityException("Requires READ_INPUT_STATE permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 }
Dianne Hackborn6af0d502009-09-28 13:25:46 -07004364 return mQueue.getKeycodeState(devid, sw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 }
Romain Guy06882f82009-06-10 13:36:04 -07004366
Dianne Hackborn1d62ea92009-11-17 12:49:50 -08004367 public int getTrackballKeycodeState(int sw) {
4368 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4369 "getTrackballKeycodeState()")) {
4370 throw new SecurityException("Requires READ_INPUT_STATE permission");
4371 }
4372 return mQueue.getTrackballKeycodeState(sw);
4373 }
4374
4375 public int getDPadKeycodeState(int sw) {
4376 if (!checkCallingPermission(android.Manifest.permission.READ_INPUT_STATE,
4377 "getDPadKeycodeState()")) {
4378 throw new SecurityException("Requires READ_INPUT_STATE permission");
4379 }
4380 return mQueue.getDPadKeycodeState(sw);
4381 }
4382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 public boolean hasKeys(int[] keycodes, boolean[] keyExists) {
4384 return KeyInputQueue.hasKeys(keycodes, keyExists);
4385 }
Romain Guy06882f82009-06-10 13:36:04 -07004386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 public void enableScreenAfterBoot() {
4388 synchronized(mWindowMap) {
4389 if (mSystemBooted) {
4390 return;
4391 }
4392 mSystemBooted = true;
4393 }
Romain Guy06882f82009-06-10 13:36:04 -07004394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 performEnableScreen();
4396 }
Romain Guy06882f82009-06-10 13:36:04 -07004397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 public void enableScreenIfNeededLocked() {
4399 if (mDisplayEnabled) {
4400 return;
4401 }
4402 if (!mSystemBooted) {
4403 return;
4404 }
4405 mH.sendMessage(mH.obtainMessage(H.ENABLE_SCREEN));
4406 }
Romain Guy06882f82009-06-10 13:36:04 -07004407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 public void performEnableScreen() {
4409 synchronized(mWindowMap) {
4410 if (mDisplayEnabled) {
4411 return;
4412 }
4413 if (!mSystemBooted) {
4414 return;
4415 }
Romain Guy06882f82009-06-10 13:36:04 -07004416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 // Don't enable the screen until all existing windows
4418 // have been drawn.
4419 final int N = mWindows.size();
4420 for (int i=0; i<N; i++) {
4421 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn5943c202010-04-12 21:36:49 -07004422 if (w.isVisibleLw() && !w.mObscured
4423 && (w.mOrientationChanging || !w.isDrawnLw())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 return;
4425 }
4426 }
Romain Guy06882f82009-06-10 13:36:04 -07004427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 mDisplayEnabled = true;
4429 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004430 Slog.i(TAG, "ENABLING SCREEN!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 StringWriter sw = new StringWriter();
4432 PrintWriter pw = new PrintWriter(sw);
4433 this.dump(null, pw, null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004434 Slog.i(TAG, sw.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 }
4436 try {
4437 IBinder surfaceFlinger = ServiceManager.getService("SurfaceFlinger");
4438 if (surfaceFlinger != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004439 //Slog.i(TAG, "******* TELLING SURFACE FLINGER WE ARE BOOTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 Parcel data = Parcel.obtain();
4441 data.writeInterfaceToken("android.ui.ISurfaceComposer");
4442 surfaceFlinger.transact(IBinder.FIRST_CALL_TRANSACTION,
4443 data, null, 0);
4444 data.recycle();
4445 }
4446 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004447 Slog.e(TAG, "Boot completed: SurfaceFlinger is dead!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 }
4449 }
Romain Guy06882f82009-06-10 13:36:04 -07004450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 mPolicy.enableScreenAfterBoot();
Romain Guy06882f82009-06-10 13:36:04 -07004452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 // Make sure the last requested orientation has been applied.
Dianne Hackborn321ae682009-03-27 16:16:03 -07004454 setRotationUnchecked(WindowManagerPolicy.USE_LAST_ROTATION, false,
4455 mLastRotationFlags | Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 }
Romain Guy06882f82009-06-10 13:36:04 -07004457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 public void setInTouchMode(boolean mode) {
4459 synchronized(mWindowMap) {
4460 mInTouchMode = mode;
4461 }
4462 }
4463
Romain Guy06882f82009-06-10 13:36:04 -07004464 public void setRotation(int rotation,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004465 boolean alwaysSendConfiguration, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 if (!checkCallingPermission(android.Manifest.permission.SET_ORIENTATION,
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004467 "setRotation()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07004468 throw new SecurityException("Requires SET_ORIENTATION permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 }
4470
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004471 setRotationUnchecked(rotation, alwaysSendConfiguration, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 }
Romain Guy06882f82009-06-10 13:36:04 -07004473
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004474 public void setRotationUnchecked(int rotation,
4475 boolean alwaysSendConfiguration, int animFlags) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004476 if(DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 "alwaysSendConfiguration set to "+alwaysSendConfiguration);
Romain Guy06882f82009-06-10 13:36:04 -07004478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 long origId = Binder.clearCallingIdentity();
4480 boolean changed;
4481 synchronized(mWindowMap) {
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004482 changed = setRotationUncheckedLocked(rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 }
Romain Guy06882f82009-06-10 13:36:04 -07004484
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004485 if (changed || alwaysSendConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 sendNewConfiguration();
4487 }
Romain Guy06882f82009-06-10 13:36:04 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 Binder.restoreCallingIdentity(origId);
4490 }
Romain Guy06882f82009-06-10 13:36:04 -07004491
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004492 /**
4493 * Apply a new rotation to the screen, respecting the requests of
4494 * applications. Use WindowManagerPolicy.USE_LAST_ROTATION to simply
4495 * re-evaluate the desired rotation.
4496 *
4497 * Returns null if the rotation has been changed. In this case YOU
4498 * MUST CALL setNewConfiguration() TO UNFREEZE THE SCREEN.
4499 */
Dianne Hackborn1e880db2009-03-27 16:04:08 -07004500 public boolean setRotationUncheckedLocked(int rotation, int animFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 boolean changed;
4502 if (rotation == WindowManagerPolicy.USE_LAST_ROTATION) {
4503 rotation = mRequestedRotation;
4504 } else {
4505 mRequestedRotation = rotation;
Dianne Hackborn321ae682009-03-27 16:16:03 -07004506 mLastRotationFlags = animFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004508 if (DEBUG_ORIENTATION) Slog.v(TAG, "Overwriting rotation value from " + rotation);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004509 rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 mRotation, mDisplayEnabled);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004511 if (DEBUG_ORIENTATION) Slog.v(TAG, "new rotation is set to " + rotation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 changed = mDisplayEnabled && mRotation != rotation;
Romain Guy06882f82009-06-10 13:36:04 -07004513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 if (changed) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004515 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 "Rotation changed to " + rotation
4517 + " from " + mRotation
4518 + " (forceApp=" + mForcedAppOrientation
4519 + ", req=" + mRequestedRotation + ")");
4520 mRotation = rotation;
4521 mWindowsFreezingScreen = true;
4522 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
4523 mH.sendMessageDelayed(mH.obtainMessage(H.WINDOW_FREEZE_TIMEOUT),
4524 2000);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004525 mWaitingForConfig = true;
4526 mLayoutNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 startFreezingDisplayLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004528 Slog.i(TAG, "Setting rotation to " + rotation + ", animFlags=" + animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 mQueue.setOrientation(rotation);
4530 if (mDisplayEnabled) {
Dianne Hackborn321ae682009-03-27 16:16:03 -07004531 Surface.setOrientation(0, rotation, animFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
4533 for (int i=mWindows.size()-1; i>=0; i--) {
4534 WindowState w = (WindowState)mWindows.get(i);
4535 if (w.mSurface != null) {
4536 w.mOrientationChanging = true;
4537 }
4538 }
4539 for (int i=mRotationWatchers.size()-1; i>=0; i--) {
4540 try {
4541 mRotationWatchers.get(i).onRotationChanged(rotation);
4542 } catch (RemoteException e) {
4543 }
4544 }
4545 } //end if changed
Romain Guy06882f82009-06-10 13:36:04 -07004546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 return changed;
4548 }
Romain Guy06882f82009-06-10 13:36:04 -07004549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 public int getRotation() {
4551 return mRotation;
4552 }
4553
4554 public int watchRotation(IRotationWatcher watcher) {
4555 final IBinder watcherBinder = watcher.asBinder();
4556 IBinder.DeathRecipient dr = new IBinder.DeathRecipient() {
4557 public void binderDied() {
4558 synchronized (mWindowMap) {
4559 for (int i=0; i<mRotationWatchers.size(); i++) {
4560 if (watcherBinder == mRotationWatchers.get(i).asBinder()) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004561 IRotationWatcher removed = mRotationWatchers.remove(i);
4562 if (removed != null) {
4563 removed.asBinder().unlinkToDeath(this, 0);
4564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 i--;
4566 }
4567 }
4568 }
4569 }
4570 };
Romain Guy06882f82009-06-10 13:36:04 -07004571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 synchronized (mWindowMap) {
4573 try {
4574 watcher.asBinder().linkToDeath(dr, 0);
4575 mRotationWatchers.add(watcher);
4576 } catch (RemoteException e) {
4577 // Client died, no cleanup needed.
4578 }
Romain Guy06882f82009-06-10 13:36:04 -07004579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 return mRotation;
4581 }
4582 }
4583
4584 /**
4585 * Starts the view server on the specified port.
4586 *
4587 * @param port The port to listener to.
4588 *
4589 * @return True if the server was successfully started, false otherwise.
4590 *
4591 * @see com.android.server.ViewServer
4592 * @see com.android.server.ViewServer#VIEW_SERVER_DEFAULT_PORT
4593 */
4594 public boolean startViewServer(int port) {
Romain Guy06882f82009-06-10 13:36:04 -07004595 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 return false;
4597 }
4598
4599 if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
4600 return false;
4601 }
4602
4603 if (port < 1024) {
4604 return false;
4605 }
4606
4607 if (mViewServer != null) {
4608 if (!mViewServer.isRunning()) {
4609 try {
4610 return mViewServer.start();
4611 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004612 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 }
4614 }
4615 return false;
4616 }
4617
4618 try {
4619 mViewServer = new ViewServer(this, port);
4620 return mViewServer.start();
4621 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004622 Slog.w(TAG, "View server did not start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 }
4624 return false;
4625 }
4626
Romain Guy06882f82009-06-10 13:36:04 -07004627 private boolean isSystemSecure() {
4628 return "1".equals(SystemProperties.get(SYSTEM_SECURE, "1")) &&
4629 "0".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
4630 }
4631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 /**
4633 * Stops the view server if it exists.
4634 *
4635 * @return True if the server stopped, false if it wasn't started or
4636 * couldn't be stopped.
4637 *
4638 * @see com.android.server.ViewServer
4639 */
4640 public boolean stopViewServer() {
Romain Guy06882f82009-06-10 13:36:04 -07004641 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 return false;
4643 }
4644
4645 if (!checkCallingPermission(Manifest.permission.DUMP, "stopViewServer")) {
4646 return false;
4647 }
4648
4649 if (mViewServer != null) {
4650 return mViewServer.stop();
4651 }
4652 return false;
4653 }
4654
4655 /**
4656 * Indicates whether the view server is running.
4657 *
4658 * @return True if the server is running, false otherwise.
4659 *
4660 * @see com.android.server.ViewServer
4661 */
4662 public boolean isViewServerRunning() {
Romain Guy06882f82009-06-10 13:36:04 -07004663 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 return false;
4665 }
4666
4667 if (!checkCallingPermission(Manifest.permission.DUMP, "isViewServerRunning")) {
4668 return false;
4669 }
4670
4671 return mViewServer != null && mViewServer.isRunning();
4672 }
4673
4674 /**
4675 * Lists all availble windows in the system. The listing is written in the
4676 * specified Socket's output stream with the following syntax:
4677 * windowHashCodeInHexadecimal windowName
4678 * Each line of the ouput represents a different window.
4679 *
4680 * @param client The remote client to send the listing to.
4681 * @return False if an error occured, true otherwise.
4682 */
4683 boolean viewServerListWindows(Socket client) {
Romain Guy06882f82009-06-10 13:36:04 -07004684 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 return false;
4686 }
4687
4688 boolean result = true;
4689
4690 Object[] windows;
4691 synchronized (mWindowMap) {
4692 windows = new Object[mWindows.size()];
4693 //noinspection unchecked
4694 windows = mWindows.toArray(windows);
4695 }
4696
4697 BufferedWriter out = null;
4698
4699 // Any uncaught exception will crash the system process
4700 try {
4701 OutputStream clientStream = client.getOutputStream();
4702 out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
4703
4704 final int count = windows.length;
4705 for (int i = 0; i < count; i++) {
4706 final WindowState w = (WindowState) windows[i];
4707 out.write(Integer.toHexString(System.identityHashCode(w)));
4708 out.write(' ');
4709 out.append(w.mAttrs.getTitle());
4710 out.write('\n');
4711 }
4712
4713 out.write("DONE.\n");
4714 out.flush();
4715 } catch (Exception e) {
4716 result = false;
4717 } finally {
4718 if (out != null) {
4719 try {
4720 out.close();
4721 } catch (IOException e) {
4722 result = false;
4723 }
4724 }
4725 }
4726
4727 return result;
4728 }
4729
4730 /**
4731 * Sends a command to a target window. The result of the command, if any, will be
4732 * written in the output stream of the specified socket.
4733 *
4734 * The parameters must follow this syntax:
4735 * windowHashcode extra
4736 *
4737 * Where XX is the length in characeters of the windowTitle.
4738 *
4739 * The first parameter is the target window. The window with the specified hashcode
4740 * will be the target. If no target can be found, nothing happens. The extra parameters
4741 * will be delivered to the target window and as parameters to the command itself.
4742 *
4743 * @param client The remote client to sent the result, if any, to.
4744 * @param command The command to execute.
4745 * @param parameters The command parameters.
4746 *
4747 * @return True if the command was successfully delivered, false otherwise. This does
4748 * not indicate whether the command itself was successful.
4749 */
4750 boolean viewServerWindowCommand(Socket client, String command, String parameters) {
Romain Guy06882f82009-06-10 13:36:04 -07004751 if (isSystemSecure()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 return false;
4753 }
4754
4755 boolean success = true;
4756 Parcel data = null;
4757 Parcel reply = null;
4758
4759 // Any uncaught exception will crash the system process
4760 try {
4761 // Find the hashcode of the window
4762 int index = parameters.indexOf(' ');
4763 if (index == -1) {
4764 index = parameters.length();
4765 }
4766 final String code = parameters.substring(0, index);
Romain Guy236092a2009-12-14 15:31:48 -08004767 int hashCode = (int) Long.parseLong(code, 16);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768
4769 // Extract the command's parameter after the window description
4770 if (index < parameters.length()) {
4771 parameters = parameters.substring(index + 1);
4772 } else {
4773 parameters = "";
4774 }
4775
4776 final WindowManagerService.WindowState window = findWindow(hashCode);
4777 if (window == null) {
4778 return false;
4779 }
4780
4781 data = Parcel.obtain();
4782 data.writeInterfaceToken("android.view.IWindow");
4783 data.writeString(command);
4784 data.writeString(parameters);
4785 data.writeInt(1);
4786 ParcelFileDescriptor.fromSocket(client).writeToParcel(data, 0);
4787
4788 reply = Parcel.obtain();
4789
4790 final IBinder binder = window.mClient.asBinder();
4791 // TODO: GET THE TRANSACTION CODE IN A SAFER MANNER
4792 binder.transact(IBinder.FIRST_CALL_TRANSACTION, data, reply, 0);
4793
4794 reply.readException();
4795
4796 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004797 Slog.w(TAG, "Could not send command " + command + " with parameters " + parameters, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004798 success = false;
4799 } finally {
4800 if (data != null) {
4801 data.recycle();
4802 }
4803 if (reply != null) {
4804 reply.recycle();
4805 }
4806 }
4807
4808 return success;
4809 }
4810
4811 private WindowState findWindow(int hashCode) {
4812 if (hashCode == -1) {
4813 return getFocusedWindow();
4814 }
4815
4816 synchronized (mWindowMap) {
4817 final ArrayList windows = mWindows;
4818 final int count = windows.size();
4819
4820 for (int i = 0; i < count; i++) {
4821 WindowState w = (WindowState) windows.get(i);
4822 if (System.identityHashCode(w) == hashCode) {
4823 return w;
4824 }
4825 }
4826 }
4827
4828 return null;
4829 }
4830
4831 /*
4832 * Instruct the Activity Manager to fetch the current configuration and broadcast
4833 * that to config-changed listeners if appropriate.
4834 */
4835 void sendNewConfiguration() {
4836 try {
4837 mActivityManager.updateConfiguration(null);
4838 } catch (RemoteException e) {
4839 }
4840 }
Romain Guy06882f82009-06-10 13:36:04 -07004841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 public Configuration computeNewConfiguration() {
4843 synchronized (mWindowMap) {
Dianne Hackbornc485a602009-03-24 22:39:49 -07004844 return computeNewConfigurationLocked();
4845 }
4846 }
Romain Guy06882f82009-06-10 13:36:04 -07004847
Dianne Hackbornc485a602009-03-24 22:39:49 -07004848 Configuration computeNewConfigurationLocked() {
4849 Configuration config = new Configuration();
4850 if (!computeNewConfigurationLocked(config)) {
4851 return null;
4852 }
Dianne Hackbornc485a602009-03-24 22:39:49 -07004853 return config;
4854 }
Romain Guy06882f82009-06-10 13:36:04 -07004855
Dianne Hackbornc485a602009-03-24 22:39:49 -07004856 boolean computeNewConfigurationLocked(Configuration config) {
4857 if (mDisplay == null) {
4858 return false;
4859 }
4860 mQueue.getInputConfiguration(config);
Christopher Tateb696aee2010-04-02 19:08:30 -07004861
4862 // Use the effective "visual" dimensions based on current rotation
4863 final boolean rotated = (mRotation == Surface.ROTATION_90
4864 || mRotation == Surface.ROTATION_270);
4865 final int dw = rotated ? mInitialDisplayHeight : mInitialDisplayWidth;
4866 final int dh = rotated ? mInitialDisplayWidth : mInitialDisplayHeight;
4867
Dianne Hackbornc485a602009-03-24 22:39:49 -07004868 int orientation = Configuration.ORIENTATION_SQUARE;
4869 if (dw < dh) {
4870 orientation = Configuration.ORIENTATION_PORTRAIT;
4871 } else if (dw > dh) {
4872 orientation = Configuration.ORIENTATION_LANDSCAPE;
4873 }
4874 config.orientation = orientation;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004875
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004876 DisplayMetrics dm = new DisplayMetrics();
4877 mDisplay.getMetrics(dm);
4878 CompatibilityInfo.updateCompatibleScreenFrame(dm, orientation, mCompatibleScreenFrame);
4879
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004880 if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07004881 // Note we only do this once because at this point we don't
4882 // expect the screen to change in this way at runtime, and want
4883 // to avoid all of this computation for every config change.
Dianne Hackborn723738c2009-06-25 19:48:04 -07004884 int longSize = dw;
4885 int shortSize = dh;
4886 if (longSize < shortSize) {
4887 int tmp = longSize;
4888 longSize = shortSize;
4889 shortSize = tmp;
4890 }
4891 longSize = (int)(longSize/dm.density);
4892 shortSize = (int)(shortSize/dm.density);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07004893
Dianne Hackborn723738c2009-06-25 19:48:04 -07004894 // These semi-magic numbers define our compatibility modes for
4895 // applications with different screens. Don't change unless you
4896 // make sure to test lots and lots of apps!
4897 if (longSize < 470) {
4898 // This is shorter than an HVGA normal density screen (which
4899 // is 480 pixels on its long side).
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004900 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL
4901 | Configuration.SCREENLAYOUT_LONG_NO;
Dianne Hackborn723738c2009-06-25 19:48:04 -07004902 } else {
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07004903 // What size is this screen screen?
4904 if (longSize >= 800 && shortSize >= 600) {
4905 // SVGA or larger screens at medium density are the point
4906 // at which we consider it to be an extra large screen.
4907 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE;
4908 } else if (longSize >= 640 && shortSize >= 480) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004909 // VGA or larger screens at medium density are the point
4910 // at which we consider it to be a large screen.
4911 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
4912 } else {
4913 mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004914
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004915 // If this screen is wider than normal HVGA, or taller
4916 // than FWVGA, then for old apps we want to run in size
4917 // compatibility mode.
4918 if (shortSize > 321 || longSize > 570) {
4919 mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;
4920 }
4921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004922
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004923 // Is this a long screen?
4924 if (((longSize*3)/5) >= (shortSize-1)) {
4925 // Anything wider than WVGA (5:3) is considering to be long.
4926 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
4927 } else {
4928 mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
4929 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07004930 }
4931 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07004932 config.screenLayout = mScreenLayout;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004933
Dianne Hackbornc485a602009-03-24 22:39:49 -07004934 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
4935 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
4936 mPolicy.adjustConfigurationLw(config);
4937 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938 }
Romain Guy06882f82009-06-10 13:36:04 -07004939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 // -------------------------------------------------------------
4941 // Input Events and Focus Management
4942 // -------------------------------------------------------------
4943
4944 private final void wakeupIfNeeded(WindowState targetWin, int eventType) {
Michael Chane96440f2009-05-06 10:27:36 -07004945 long curTime = SystemClock.uptimeMillis();
4946
Michael Chane10de972009-05-18 11:24:50 -07004947 if (eventType == TOUCH_EVENT || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT) {
Michael Chane96440f2009-05-06 10:27:36 -07004948 if (mLastTouchEventType == eventType &&
4949 (curTime - mLastUserActivityCallTime) < MIN_TIME_BETWEEN_USERACTIVITIES) {
4950 return;
4951 }
4952 mLastUserActivityCallTime = curTime;
4953 mLastTouchEventType = eventType;
4954 }
4955
4956 if (targetWin == null
4957 || targetWin.mAttrs.type != WindowManager.LayoutParams.TYPE_KEYGUARD) {
4958 mPowerManager.userActivity(curTime, false, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
4960 }
4961
4962 // tells if it's a cheek event or not -- this function is stateful
4963 private static final int EVENT_NONE = 0;
4964 private static final int EVENT_UNKNOWN = 0;
4965 private static final int EVENT_CHEEK = 0;
4966 private static final int EVENT_IGNORE_DURATION = 300; // ms
4967 private static final float CHEEK_THRESHOLD = 0.6f;
4968 private int mEventState = EVENT_NONE;
4969 private float mEventSize;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 private int eventType(MotionEvent ev) {
4972 float size = ev.getSize();
4973 switch (ev.getAction()) {
4974 case MotionEvent.ACTION_DOWN:
4975 mEventSize = size;
4976 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_EVENT;
4977 case MotionEvent.ACTION_UP:
4978 if (size > mEventSize) mEventSize = size;
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004979 return (mEventSize > CHEEK_THRESHOLD) ? CHEEK_EVENT : TOUCH_UP_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 case MotionEvent.ACTION_MOVE:
4981 final int N = ev.getHistorySize();
4982 if (size > mEventSize) mEventSize = size;
4983 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4984 for (int i=0; i<N; i++) {
4985 size = ev.getHistoricalSize(i);
4986 if (size > mEventSize) mEventSize = size;
4987 if (mEventSize > CHEEK_THRESHOLD) return CHEEK_EVENT;
4988 }
4989 if (ev.getEventTime() < ev.getDownTime() + EVENT_IGNORE_DURATION) {
4990 return TOUCH_EVENT;
4991 } else {
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07004992 return LONG_TOUCH_EVENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 }
4994 default:
4995 // not good
4996 return OTHER_EVENT;
4997 }
4998 }
4999
5000 /**
5001 * @return Returns true if event was dispatched, false if it was dropped for any reason
5002 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005003 private int dispatchPointer(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005004 if (DEBUG_INPUT || WindowManagerPolicy.WATCH_POINTER) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 "dispatchPointer " + ev);
5006
Michael Chan53071d62009-05-13 17:29:48 -07005007 if (MEASURE_LATENCY) {
5008 lt.sample("3 Wait for last dispatch ", System.nanoTime() - qev.whenNano);
5009 }
5010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 Object targetObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005012 ev, true, false, pid, uid);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005013
Michael Chan53071d62009-05-13 17:29:48 -07005014 if (MEASURE_LATENCY) {
5015 lt.sample("3 Last dispatch finished ", System.nanoTime() - qev.whenNano);
5016 }
5017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 int action = ev.getAction();
Romain Guy06882f82009-06-10 13:36:04 -07005019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 if (action == MotionEvent.ACTION_UP) {
5021 // let go of our target
5022 mKeyWaiter.mMotionTarget = null;
5023 mPowerManager.logPointerUpEvent();
5024 } else if (action == MotionEvent.ACTION_DOWN) {
5025 mPowerManager.logPointerDownEvent();
5026 }
5027
5028 if (targetObj == null) {
5029 // In this case we are either dropping the event, or have received
5030 // a move or up without a down. It is common to receive move
5031 // events in such a way, since this means the user is moving the
5032 // pointer without actually pressing down. All other cases should
5033 // be atypical, so let's log them.
Michael Chane96440f2009-05-06 10:27:36 -07005034 if (action != MotionEvent.ACTION_MOVE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005035 Slog.w(TAG, "No window to dispatch pointer action " + ev.getAction());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 }
Dianne Hackborn6adba242009-11-10 11:10:09 -08005037 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005038 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 if (qev != null) {
5041 mQueue.recycleEvent(qev);
5042 }
5043 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005044 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 }
5046 if (targetObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005047 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005048 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), true);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 if (qev != null) {
5051 mQueue.recycleEvent(qev);
5052 }
5053 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005054 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 }
Romain Guy06882f82009-06-10 13:36:04 -07005056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 WindowState target = (WindowState)targetObj;
Romain Guy06882f82009-06-10 13:36:04 -07005058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 final long eventTime = ev.getEventTime();
Michael Chan53071d62009-05-13 17:29:48 -07005060 final long eventTimeNano = ev.getEventTimeNano();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005061
Joe Onorato8a9b2202010-02-26 18:56:32 -08005062 //Slog.i(TAG, "Sending " + ev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063
5064 if (uid != 0 && uid != target.mSession.mUid) {
5065 if (mContext.checkPermission(
5066 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5067 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005068 Slog.w(TAG, "Permission denied: injecting pointer event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 + pid + " uid " + uid + " to window " + target
5070 + " owned by uid " + target.mSession.mUid);
5071 if (qev != null) {
5072 mQueue.recycleEvent(qev);
5073 }
5074 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005075 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 }
5077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005078
Michael Chan53071d62009-05-13 17:29:48 -07005079 if (MEASURE_LATENCY) {
5080 lt.sample("4 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5081 }
5082
Romain Guy06882f82009-06-10 13:36:04 -07005083 if ((target.mAttrs.flags &
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES) != 0) {
5085 //target wants to ignore fat touch events
5086 boolean cheekPress = mPolicy.isCheekPressedAgainstScreen(ev);
5087 //explicit flag to return without processing event further
5088 boolean returnFlag = false;
5089 if((action == MotionEvent.ACTION_DOWN)) {
5090 mFatTouch = false;
5091 if(cheekPress) {
5092 mFatTouch = true;
5093 returnFlag = true;
5094 }
5095 } else {
5096 if(action == MotionEvent.ACTION_UP) {
5097 if(mFatTouch) {
5098 //earlier even was invalid doesnt matter if current up is cheekpress or not
5099 mFatTouch = false;
5100 returnFlag = true;
5101 } else if(cheekPress) {
5102 //cancel the earlier event
5103 ev.setAction(MotionEvent.ACTION_CANCEL);
5104 action = MotionEvent.ACTION_CANCEL;
5105 }
5106 } else if(action == MotionEvent.ACTION_MOVE) {
5107 if(mFatTouch) {
5108 //two cases here
5109 //an invalid down followed by 0 or moves(valid or invalid)
Romain Guy06882f82009-06-10 13:36:04 -07005110 //a valid down, invalid move, more moves. want to ignore till up
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 returnFlag = true;
5112 } else if(cheekPress) {
5113 //valid down followed by invalid moves
5114 //an invalid move have to cancel earlier action
5115 ev.setAction(MotionEvent.ACTION_CANCEL);
5116 action = MotionEvent.ACTION_CANCEL;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005117 if (DEBUG_INPUT) Slog.v(TAG, "Sending cancel for invalid ACTION_MOVE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 //note that the subsequent invalid moves will not get here
5119 mFatTouch = true;
5120 }
5121 }
5122 } //else if action
5123 if(returnFlag) {
5124 //recycle que, ev
5125 if (qev != null) {
5126 mQueue.recycleEvent(qev);
5127 }
5128 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005129 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131 } //end if target
Michael Chane96440f2009-05-06 10:27:36 -07005132
Michael Chan9f028e62009-08-04 17:37:46 -07005133 // Enable this for testing the "right" value
5134 if (false && action == MotionEvent.ACTION_DOWN) {
Michael Chane96440f2009-05-06 10:27:36 -07005135 int max_events_per_sec = 35;
5136 try {
5137 max_events_per_sec = Integer.parseInt(SystemProperties
5138 .get("windowsmgr.max_events_per_sec"));
5139 if (max_events_per_sec < 1) {
5140 max_events_per_sec = 35;
5141 }
5142 } catch (NumberFormatException e) {
5143 }
5144 mMinWaitTimeBetweenTouchEvents = 1000 / max_events_per_sec;
5145 }
5146
5147 /*
5148 * Throttle events to minimize CPU usage when there's a flood of events
5149 * e.g. constant contact with the screen
5150 */
5151 if (action == MotionEvent.ACTION_MOVE) {
5152 long nextEventTime = mLastTouchEventTime + mMinWaitTimeBetweenTouchEvents;
5153 long now = SystemClock.uptimeMillis();
5154 if (now < nextEventTime) {
5155 try {
5156 Thread.sleep(nextEventTime - now);
5157 } catch (InterruptedException e) {
5158 }
5159 mLastTouchEventTime = nextEventTime;
5160 } else {
5161 mLastTouchEventTime = now;
5162 }
5163 }
5164
Michael Chan53071d62009-05-13 17:29:48 -07005165 if (MEASURE_LATENCY) {
5166 lt.sample("5 in dispatchPointer ", System.nanoTime() - eventTimeNano);
5167 }
5168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 synchronized(mWindowMap) {
Dianne Hackborn6adba242009-11-10 11:10:09 -08005170 if (!target.isVisibleLw()) {
5171 // During this motion dispatch, the target window has become
5172 // invisible.
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005173 dispatchPointerElsewhereLocked(null, null, ev, ev.getEventTime(), false);
Dianne Hackborn6adba242009-11-10 11:10:09 -08005174 if (qev != null) {
5175 mQueue.recycleEvent(qev);
5176 }
5177 ev.recycle();
5178 return INJECT_SUCCEEDED;
5179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 if (qev != null && action == MotionEvent.ACTION_MOVE) {
5182 mKeyWaiter.bindTargetWindowLocked(target,
5183 KeyWaiter.RETURN_PENDING_POINTER, qev);
5184 ev = null;
5185 } else {
5186 if (action == MotionEvent.ACTION_DOWN) {
5187 WindowState out = mKeyWaiter.mOutsideTouchTargets;
5188 if (out != null) {
5189 MotionEvent oev = MotionEvent.obtain(ev);
5190 oev.setAction(MotionEvent.ACTION_OUTSIDE);
5191 do {
5192 final Rect frame = out.mFrame;
5193 oev.offsetLocation(-(float)frame.left, -(float)frame.top);
5194 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005195 out.mClient.dispatchPointer(oev, eventTime, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005197 Slog.i(TAG, "WINDOW DIED during outside motion dispatch: " + out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 }
5199 oev.offsetLocation((float)frame.left, (float)frame.top);
5200 out = out.mNextOutsideTouch;
5201 } while (out != null);
5202 mKeyWaiter.mOutsideTouchTargets = null;
5203 }
5204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005205
Dianne Hackborn90d2db32010-02-11 22:19:06 -08005206 dispatchPointerElsewhereLocked(target, null, ev, ev.getEventTime(), false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005207
Dianne Hackborn6adba242009-11-10 11:10:09 -08005208 final Rect frame = target.mFrame;
5209 ev.offsetLocation(-(float)frame.left, -(float)frame.top);
5210 mKeyWaiter.bindTargetWindowLocked(target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
5212 }
Romain Guy06882f82009-06-10 13:36:04 -07005213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 // finally offset the event to the target's coordinate system and
5215 // dispatch the event.
5216 try {
5217 if (DEBUG_INPUT || DEBUG_FOCUS || WindowManagerPolicy.WATCH_POINTER) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005218 Slog.v(TAG, "Delivering pointer " + qev + " to " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005220
Michael Chan53071d62009-05-13 17:29:48 -07005221 if (MEASURE_LATENCY) {
5222 lt.sample("6 before svr->client ipc ", System.nanoTime() - eventTimeNano);
5223 }
5224
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005225 target.mClient.dispatchPointer(ev, eventTime, true);
Michael Chan53071d62009-05-13 17:29:48 -07005226
5227 if (MEASURE_LATENCY) {
5228 lt.sample("7 after svr->client ipc ", System.nanoTime() - eventTimeNano);
5229 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005230 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005232 Slog.i(TAG, "WINDOW DIED during motion dispatch: " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 mKeyWaiter.mMotionTarget = null;
5234 try {
5235 removeWindow(target.mSession, target.mClient);
5236 } catch (java.util.NoSuchElementException ex) {
5237 // This will happen if the window has already been
5238 // removed.
5239 }
5240 }
Dianne Hackborncfaef692009-06-15 14:24:44 -07005241 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
Romain Guy06882f82009-06-10 13:36:04 -07005243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 /**
5245 * @return Returns true if event was dispatched, false if it was dropped for any reason
5246 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005247 private int dispatchTrackball(QueuedEvent qev, MotionEvent ev, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005248 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 TAG, "dispatchTrackball [" + ev.getAction() +"] <" + ev.getX() + ", " + ev.getY() + ">");
Romain Guy06882f82009-06-10 13:36:04 -07005250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 Object focusObj = mKeyWaiter.waitForNextEventTarget(null, qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005252 ev, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005254 Slog.w(TAG, "No focus window, dropping trackball: " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 if (qev != null) {
5256 mQueue.recycleEvent(qev);
5257 }
5258 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005259 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 }
5261 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
5262 if (qev != null) {
5263 mQueue.recycleEvent(qev);
5264 }
5265 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005266 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 }
Romain Guy06882f82009-06-10 13:36:04 -07005268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 if (uid != 0 && uid != focus.mSession.mUid) {
5272 if (mContext.checkPermission(
5273 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5274 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005275 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 + pid + " uid " + uid + " to window " + focus
5277 + " owned by uid " + focus.mSession.mUid);
5278 if (qev != null) {
5279 mQueue.recycleEvent(qev);
5280 }
5281 ev.recycle();
Dianne Hackborncfaef692009-06-15 14:24:44 -07005282 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 }
5284 }
Romain Guy06882f82009-06-10 13:36:04 -07005285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 final long eventTime = ev.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 synchronized(mWindowMap) {
5289 if (qev != null && ev.getAction() == MotionEvent.ACTION_MOVE) {
5290 mKeyWaiter.bindTargetWindowLocked(focus,
5291 KeyWaiter.RETURN_PENDING_TRACKBALL, qev);
5292 // We don't deliver movement events to the client, we hold
5293 // them and wait for them to call back.
5294 ev = null;
5295 } else {
5296 mKeyWaiter.bindTargetWindowLocked(focus);
5297 }
5298 }
Romain Guy06882f82009-06-10 13:36:04 -07005299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 try {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07005301 focus.mClient.dispatchTrackball(ev, eventTime, true);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005302 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005304 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 try {
5306 removeWindow(focus.mSession, focus.mClient);
5307 } catch (java.util.NoSuchElementException ex) {
5308 // This will happen if the window has already been
5309 // removed.
5310 }
5311 }
Romain Guy06882f82009-06-10 13:36:04 -07005312
Dianne Hackborncfaef692009-06-15 14:24:44 -07005313 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
Romain Guy06882f82009-06-10 13:36:04 -07005315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 /**
5317 * @return Returns true if event was dispatched, false if it was dropped for any reason
5318 */
Dianne Hackborncfaef692009-06-15 14:24:44 -07005319 private int dispatchKey(KeyEvent event, int pid, int uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005320 if (DEBUG_INPUT) Slog.v(TAG, "Dispatch key: " + event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321
5322 Object focusObj = mKeyWaiter.waitForNextEventTarget(event, null,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005323 null, false, false, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 if (focusObj == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005325 Slog.w(TAG, "No focus window, dropping: " + event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005326 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005327 }
5328 if (focusObj == mKeyWaiter.CONSUMED_EVENT_TOKEN) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005329 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
Romain Guy06882f82009-06-10 13:36:04 -07005331
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005332 // Okay we have finished waiting for the last event to be processed.
5333 // First off, if this is a repeat event, check to see if there is
5334 // a corresponding up event in the queue. If there is, we will
5335 // just drop the repeat, because it makes no sense to repeat after
5336 // the user has released a key. (This is especially important for
5337 // long presses.)
5338 if (event.getRepeatCount() > 0 && mQueue.hasKeyUpEvent(event)) {
5339 return INJECT_SUCCEEDED;
5340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 WindowState focus = (WindowState)focusObj;
Romain Guy06882f82009-06-10 13:36:04 -07005343
Joe Onorato8a9b2202010-02-26 18:56:32 -08005344 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 TAG, "Dispatching to " + focus + ": " + event);
5346
5347 if (uid != 0 && uid != focus.mSession.mUid) {
5348 if (mContext.checkPermission(
5349 android.Manifest.permission.INJECT_EVENTS, pid, uid)
5350 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005351 Slog.w(TAG, "Permission denied: injecting key event from pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 + pid + " uid " + uid + " to window " + focus
5353 + " owned by uid " + focus.mSession.mUid);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005354 return INJECT_NO_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 }
5356 }
Romain Guy06882f82009-06-10 13:36:04 -07005357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 synchronized(mWindowMap) {
5359 mKeyWaiter.bindTargetWindowLocked(focus);
5360 }
5361
5362 // NOSHIP extra state logging
5363 mKeyWaiter.recordDispatchState(event, focus);
5364 // END NOSHIP
Romain Guy06882f82009-06-10 13:36:04 -07005365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 try {
5367 if (DEBUG_INPUT || DEBUG_FOCUS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005368 Slog.v(TAG, "Delivering key " + event.getKeyCode()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 + " to " + focus);
5370 }
5371 focus.mClient.dispatchKey(event);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005372 return INJECT_SUCCEEDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 } catch (android.os.RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005374 Slog.i(TAG, "WINDOW DIED during key dispatch: " + focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 try {
5376 removeWindow(focus.mSession, focus.mClient);
5377 } catch (java.util.NoSuchElementException ex) {
5378 // This will happen if the window has already been
5379 // removed.
5380 }
5381 }
Romain Guy06882f82009-06-10 13:36:04 -07005382
Dianne Hackborncfaef692009-06-15 14:24:44 -07005383 return INJECT_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 }
Romain Guy06882f82009-06-10 13:36:04 -07005385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 public void pauseKeyDispatching(IBinder _token) {
5387 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5388 "pauseKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005389 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 }
5391
5392 synchronized (mWindowMap) {
5393 WindowToken token = mTokenMap.get(_token);
5394 if (token != null) {
5395 mKeyWaiter.pauseDispatchingLocked(token);
5396 }
5397 }
5398 }
5399
5400 public void resumeKeyDispatching(IBinder _token) {
5401 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5402 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005403 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 }
5405
5406 synchronized (mWindowMap) {
5407 WindowToken token = mTokenMap.get(_token);
5408 if (token != null) {
5409 mKeyWaiter.resumeDispatchingLocked(token);
5410 }
5411 }
5412 }
5413
5414 public void setEventDispatching(boolean enabled) {
5415 if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
5416 "resumeKeyDispatching()")) {
Dianne Hackborncfaef692009-06-15 14:24:44 -07005417 throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418 }
5419
5420 synchronized (mWindowMap) {
5421 mKeyWaiter.setEventDispatchingLocked(enabled);
5422 }
5423 }
Romain Guy06882f82009-06-10 13:36:04 -07005424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 /**
5426 * Injects a keystroke event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005427 *
5428 * @param ev A motion event describing the keystroke action. (Be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 * {@link SystemClock#uptimeMillis()} as the timebase.)
5430 * @param sync If true, wait for the event to be completed before returning to the caller.
5431 * @return Returns true if event was dispatched, false if it was dropped for any reason
5432 */
5433 public boolean injectKeyEvent(KeyEvent ev, boolean sync) {
5434 long downTime = ev.getDownTime();
5435 long eventTime = ev.getEventTime();
5436
5437 int action = ev.getAction();
5438 int code = ev.getKeyCode();
5439 int repeatCount = ev.getRepeatCount();
5440 int metaState = ev.getMetaState();
5441 int deviceId = ev.getDeviceId();
5442 int scancode = ev.getScanCode();
5443
5444 if (eventTime == 0) eventTime = SystemClock.uptimeMillis();
5445 if (downTime == 0) downTime = eventTime;
5446
5447 KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
The Android Open Source Project10592532009-03-18 17:39:46 -07005448 deviceId, scancode, KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005450 final int pid = Binder.getCallingPid();
5451 final int uid = Binder.getCallingUid();
5452 final long ident = Binder.clearCallingIdentity();
5453 final int result = dispatchKey(newEvent, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005455 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005457 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005458 switch (result) {
5459 case INJECT_NO_PERMISSION:
5460 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305461 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005462 case INJECT_SUCCEEDED:
5463 return true;
5464 }
5465 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467
5468 /**
5469 * Inject a pointer (touch) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005470 *
5471 * @param ev A motion event describing the pointer (touch) action. (As noted in
5472 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 * {@link SystemClock#uptimeMillis()} as the timebase.)
5474 * @param sync If true, wait for the event to be completed before returning to the caller.
5475 * @return Returns true if event was dispatched, false if it was dropped for any reason
5476 */
5477 public boolean injectPointerEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005478 final int pid = Binder.getCallingPid();
5479 final int uid = Binder.getCallingUid();
5480 final long ident = Binder.clearCallingIdentity();
5481 final int result = dispatchPointer(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005483 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005485 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005486 switch (result) {
5487 case INJECT_NO_PERMISSION:
5488 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305489 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005490 case INJECT_SUCCEEDED:
5491 return true;
5492 }
5493 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 }
Romain Guy06882f82009-06-10 13:36:04 -07005495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 /**
5497 * Inject a trackball (navigation device) event into the UI.
Romain Guy06882f82009-06-10 13:36:04 -07005498 *
5499 * @param ev A motion event describing the trackball action. (As noted in
5500 * {@link MotionEvent#obtain(long, long, int, float, float, int)}, be sure to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 * {@link SystemClock#uptimeMillis()} as the timebase.)
5502 * @param sync If true, wait for the event to be completed before returning to the caller.
5503 * @return Returns true if event was dispatched, false if it was dropped for any reason
5504 */
5505 public boolean injectTrackballEvent(MotionEvent ev, boolean sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005506 final int pid = Binder.getCallingPid();
5507 final int uid = Binder.getCallingUid();
5508 final long ident = Binder.clearCallingIdentity();
5509 final int result = dispatchTrackball(null, ev, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 if (sync) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005511 mKeyWaiter.waitForNextEventTarget(null, null, null, false, true, pid, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 }
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005513 Binder.restoreCallingIdentity(ident);
Dianne Hackborncfaef692009-06-15 14:24:44 -07005514 switch (result) {
5515 case INJECT_NO_PERMISSION:
5516 throw new SecurityException(
Chander S Pechetty27f3de62010-02-10 22:14:00 +05305517 "Injecting to another application requires INJECT_EVENTS permission");
Dianne Hackborncfaef692009-06-15 14:24:44 -07005518 case INJECT_SUCCEEDED:
5519 return true;
5520 }
5521 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 }
Romain Guy06882f82009-06-10 13:36:04 -07005523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 private WindowState getFocusedWindow() {
5525 synchronized (mWindowMap) {
5526 return getFocusedWindowLocked();
5527 }
5528 }
5529
5530 private WindowState getFocusedWindowLocked() {
5531 return mCurrentFocus;
5532 }
Romain Guy06882f82009-06-10 13:36:04 -07005533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 /**
5535 * This class holds the state for dispatching key events. This state
5536 * is protected by the KeyWaiter instance, NOT by the window lock. You
5537 * can be holding the main window lock while acquire the KeyWaiter lock,
5538 * but not the other way around.
5539 */
5540 final class KeyWaiter {
5541 // NOSHIP debugging
5542 public class DispatchState {
5543 private KeyEvent event;
5544 private WindowState focus;
5545 private long time;
5546 private WindowState lastWin;
5547 private IBinder lastBinder;
5548 private boolean finished;
5549 private boolean gotFirstWindow;
5550 private boolean eventDispatching;
5551 private long timeToSwitch;
5552 private boolean wasFrozen;
5553 private boolean focusPaused;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005554 private WindowState curFocus;
Romain Guy06882f82009-06-10 13:36:04 -07005555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 DispatchState(KeyEvent theEvent, WindowState theFocus) {
5557 focus = theFocus;
5558 event = theEvent;
5559 time = System.currentTimeMillis();
5560 // snapshot KeyWaiter state
5561 lastWin = mLastWin;
5562 lastBinder = mLastBinder;
5563 finished = mFinished;
5564 gotFirstWindow = mGotFirstWindow;
5565 eventDispatching = mEventDispatching;
5566 timeToSwitch = mTimeToSwitch;
5567 wasFrozen = mWasFrozen;
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005568 curFocus = mCurrentFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 // cache the paused state at ctor time as well
5570 if (theFocus == null || theFocus.mToken == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 focusPaused = false;
5572 } else {
5573 focusPaused = theFocus.mToken.paused;
5574 }
5575 }
Romain Guy06882f82009-06-10 13:36:04 -07005576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005577 public String toString() {
5578 return "{{" + event + " to " + focus + " @ " + time
5579 + " lw=" + lastWin + " lb=" + lastBinder
5580 + " fin=" + finished + " gfw=" + gotFirstWindow
5581 + " ed=" + eventDispatching + " tts=" + timeToSwitch
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005582 + " wf=" + wasFrozen + " fp=" + focusPaused
Christopher Tate46d45252010-02-09 15:48:57 -08005583 + " mcf=" + curFocus + "}}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 }
5585 };
5586 private DispatchState mDispatchState = null;
5587 public void recordDispatchState(KeyEvent theEvent, WindowState theFocus) {
5588 mDispatchState = new DispatchState(theEvent, theFocus);
5589 }
5590 // END NOSHIP
5591
5592 public static final int RETURN_NOTHING = 0;
5593 public static final int RETURN_PENDING_POINTER = 1;
5594 public static final int RETURN_PENDING_TRACKBALL = 2;
Romain Guy06882f82009-06-10 13:36:04 -07005595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 final Object SKIP_TARGET_TOKEN = new Object();
5597 final Object CONSUMED_EVENT_TOKEN = new Object();
Romain Guy06882f82009-06-10 13:36:04 -07005598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 private WindowState mLastWin = null;
5600 private IBinder mLastBinder = null;
5601 private boolean mFinished = true;
5602 private boolean mGotFirstWindow = false;
5603 private boolean mEventDispatching = true;
5604 private long mTimeToSwitch = 0;
5605 /* package */ boolean mWasFrozen = false;
Romain Guy06882f82009-06-10 13:36:04 -07005606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 // Target of Motion events
5608 WindowState mMotionTarget;
Romain Guy06882f82009-06-10 13:36:04 -07005609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005610 // Windows above the target who would like to receive an "outside"
5611 // touch event for any down events outside of them.
5612 WindowState mOutsideTouchTargets;
5613
5614 /**
5615 * Wait for the last event dispatch to complete, then find the next
5616 * target that should receive the given event and wait for that one
5617 * to be ready to receive it.
5618 */
5619 Object waitForNextEventTarget(KeyEvent nextKey, QueuedEvent qev,
5620 MotionEvent nextMotion, boolean isPointerEvent,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005621 boolean failIfTimeout, int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 long startTime = SystemClock.uptimeMillis();
5623 long keyDispatchingTimeout = 5 * 1000;
5624 long waitedFor = 0;
5625
5626 while (true) {
5627 // Figure out which window we care about. It is either the
5628 // last window we are waiting to have process the event or,
5629 // if none, then the next window we think the event should go
5630 // to. Note: we retrieve mLastWin outside of the lock, so
5631 // it may change before we lock. Thus we must check it again.
5632 WindowState targetWin = mLastWin;
5633 boolean targetIsNew = targetWin == null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005634 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 TAG, "waitForLastKey: mFinished=" + mFinished +
5636 ", mLastWin=" + mLastWin);
5637 if (targetIsNew) {
5638 Object target = findTargetWindow(nextKey, qev, nextMotion,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005639 isPointerEvent, callingPid, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 if (target == SKIP_TARGET_TOKEN) {
5641 // The user has pressed a special key, and we are
5642 // dropping all pending events before it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005643 if (DEBUG_INPUT) Slog.v(TAG, "Skipping: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 + " " + nextMotion);
5645 return null;
5646 }
5647 if (target == CONSUMED_EVENT_TOKEN) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005648 if (DEBUG_INPUT) Slog.v(TAG, "Consumed: " + nextKey
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 + " " + nextMotion);
5650 return target;
5651 }
5652 targetWin = (WindowState)target;
5653 }
Romain Guy06882f82009-06-10 13:36:04 -07005654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 AppWindowToken targetApp = null;
Romain Guy06882f82009-06-10 13:36:04 -07005656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 // Now: is it okay to send the next event to this window?
5658 synchronized (this) {
5659 // First: did we come here based on the last window not
5660 // being null, but it changed by the time we got here?
5661 // If so, try again.
5662 if (!targetIsNew && mLastWin == null) {
5663 continue;
5664 }
Romain Guy06882f82009-06-10 13:36:04 -07005665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 // We never dispatch events if not finished with the
5667 // last one, or the display is frozen.
5668 if (mFinished && !mDisplayFrozen) {
5669 // If event dispatching is disabled, then we
5670 // just consume the events.
5671 if (!mEventDispatching) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005672 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 "Skipping event; dispatching disabled: "
5674 + nextKey + " " + nextMotion);
5675 return null;
5676 }
5677 if (targetWin != null) {
5678 // If this is a new target, and that target is not
5679 // paused or unresponsive, then all looks good to
5680 // handle the event.
5681 if (targetIsNew && !targetWin.mToken.paused) {
5682 return targetWin;
5683 }
Romain Guy06882f82009-06-10 13:36:04 -07005684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 // If we didn't find a target window, and there is no
5686 // focused app window, then just eat the events.
5687 } else if (mFocusedApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005688 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 "Skipping event; no focused app: "
5690 + nextKey + " " + nextMotion);
5691 return null;
5692 }
5693 }
Romain Guy06882f82009-06-10 13:36:04 -07005694
Joe Onorato8a9b2202010-02-26 18:56:32 -08005695 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 TAG, "Waiting for last key in " + mLastBinder
5697 + " target=" + targetWin
5698 + " mFinished=" + mFinished
5699 + " mDisplayFrozen=" + mDisplayFrozen
5700 + " targetIsNew=" + targetIsNew
5701 + " paused="
5702 + (targetWin != null ? targetWin.mToken.paused : false)
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005703 + " mFocusedApp=" + mFocusedApp
5704 + " mCurrentFocus=" + mCurrentFocus);
Romain Guy06882f82009-06-10 13:36:04 -07005705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 targetApp = targetWin != null
5707 ? targetWin.mAppToken : mFocusedApp;
Romain Guy06882f82009-06-10 13:36:04 -07005708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 long curTimeout = keyDispatchingTimeout;
5710 if (mTimeToSwitch != 0) {
5711 long now = SystemClock.uptimeMillis();
5712 if (mTimeToSwitch <= now) {
5713 // If an app switch key has been pressed, and we have
5714 // waited too long for the current app to finish
5715 // processing keys, then wait no more!
Christopher Tate136b1f92010-02-11 17:51:24 -08005716 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 continue;
5718 }
5719 long switchTimeout = mTimeToSwitch - now;
5720 if (curTimeout > switchTimeout) {
5721 curTimeout = switchTimeout;
5722 }
5723 }
Romain Guy06882f82009-06-10 13:36:04 -07005724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 try {
5726 // after that continue
5727 // processing keys, so we don't get stuck.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005728 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 TAG, "Waiting for key dispatch: " + curTimeout);
5730 wait(curTimeout);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005731 if (DEBUG_INPUT) Slog.v(TAG, "Finished waiting @"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 + SystemClock.uptimeMillis() + " startTime="
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08005733 + startTime + " switchTime=" + mTimeToSwitch
5734 + " target=" + targetWin + " mLW=" + mLastWin
5735 + " mLB=" + mLastBinder + " fin=" + mFinished
5736 + " mCurrentFocus=" + mCurrentFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005737 } catch (InterruptedException e) {
5738 }
5739 }
5740
5741 // If we were frozen during configuration change, restart the
5742 // timeout checks from now; otherwise look at whether we timed
5743 // out before awakening.
5744 if (mWasFrozen) {
5745 waitedFor = 0;
5746 mWasFrozen = false;
5747 } else {
5748 waitedFor = SystemClock.uptimeMillis() - startTime;
5749 }
5750
5751 if (waitedFor >= keyDispatchingTimeout && mTimeToSwitch == 0) {
5752 IApplicationToken at = null;
5753 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005754 Slog.w(TAG, "Key dispatching timed out sending to " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 (targetWin != null ? targetWin.mAttrs.getTitle()
Ken Shirriff8200b202010-02-04 13:34:37 -08005756 : "<null>: no window ready for key dispatch"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 // NOSHIP debugging
Joe Onorato8a9b2202010-02-26 18:56:32 -08005758 Slog.w(TAG, "Previous dispatch state: " + mDispatchState);
5759 Slog.w(TAG, "Current dispatch state: " +
Ken Shirriff8200b202010-02-04 13:34:37 -08005760 new DispatchState(nextKey, targetWin));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 // END NOSHIP
5762 //dump();
5763 if (targetWin != null) {
5764 at = targetWin.getAppToken();
5765 } else if (targetApp != null) {
5766 at = targetApp.appToken;
5767 }
5768 }
5769
5770 boolean abort = true;
5771 if (at != null) {
5772 try {
5773 long timeout = at.getKeyDispatchingTimeout();
5774 if (timeout > waitedFor) {
5775 // we did not wait the proper amount of time for this application.
5776 // set the timeout to be the real timeout and wait again.
5777 keyDispatchingTimeout = timeout - waitedFor;
5778 continue;
5779 } else {
5780 abort = at.keyDispatchingTimedOut();
5781 }
5782 } catch (RemoteException ex) {
5783 }
5784 }
5785
5786 synchronized (this) {
5787 if (abort && (mLastWin == targetWin || targetWin == null)) {
5788 mFinished = true;
Romain Guy06882f82009-06-10 13:36:04 -07005789 if (mLastWin != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005790 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 "Window " + mLastWin +
5792 " timed out on key input");
5793 if (mLastWin.mToken.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005794 Slog.w(TAG, "Un-pausing dispatching to this window");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 mLastWin.mToken.paused = false;
5796 }
5797 }
5798 if (mMotionTarget == targetWin) {
5799 mMotionTarget = null;
5800 }
5801 mLastWin = null;
5802 mLastBinder = null;
5803 if (failIfTimeout || targetWin == null) {
5804 return null;
5805 }
5806 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005807 Slog.w(TAG, "Continuing to wait for key to be dispatched");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 startTime = SystemClock.uptimeMillis();
5809 }
5810 }
5811 }
5812 }
5813 }
Romain Guy06882f82009-06-10 13:36:04 -07005814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 Object findTargetWindow(KeyEvent nextKey, QueuedEvent qev,
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005816 MotionEvent nextMotion, boolean isPointerEvent,
5817 int callingPid, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 mOutsideTouchTargets = null;
Romain Guy06882f82009-06-10 13:36:04 -07005819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 if (nextKey != null) {
5821 // Find the target window for a normal key event.
5822 final int keycode = nextKey.getKeyCode();
5823 final int repeatCount = nextKey.getRepeatCount();
5824 final boolean down = nextKey.getAction() != KeyEvent.ACTION_UP;
5825 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(keycode);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 if (!dispatch) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005828 if (callingUid == 0 ||
5829 mContext.checkPermission(
5830 android.Manifest.permission.INJECT_EVENTS,
5831 callingPid, callingUid)
5832 == PackageManager.PERMISSION_GRANTED) {
5833 mPolicy.interceptKeyTi(null, keycode,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005834 nextKey.getMetaState(), down, repeatCount,
5835 nextKey.getFlags());
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005836 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005837 Slog.w(TAG, "Event timeout during app switch: dropping "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 + nextKey);
5839 return SKIP_TARGET_TOKEN;
5840 }
Romain Guy06882f82009-06-10 13:36:04 -07005841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 // System.out.println("##### [" + SystemClock.uptimeMillis() + "] WindowManagerService.dispatchKey(" + keycode + ", " + down + ", " + repeatCount + ")");
Romain Guy06882f82009-06-10 13:36:04 -07005843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 WindowState focus = null;
5845 synchronized(mWindowMap) {
5846 focus = getFocusedWindowLocked();
5847 }
Romain Guy06882f82009-06-10 13:36:04 -07005848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
Romain Guy06882f82009-06-10 13:36:04 -07005850
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005851 if (callingUid == 0 ||
5852 (focus != null && callingUid == focus.mSession.mUid) ||
5853 mContext.checkPermission(
5854 android.Manifest.permission.INJECT_EVENTS,
5855 callingPid, callingUid)
5856 == PackageManager.PERMISSION_GRANTED) {
5857 if (mPolicy.interceptKeyTi(focus,
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005858 keycode, nextKey.getMetaState(), down, repeatCount,
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07005859 nextKey.getFlags())) {
Dianne Hackborn2bd33d72009-06-26 18:59:01 -07005860 return CONSUMED_EVENT_TOKEN;
5861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 }
Romain Guy06882f82009-06-10 13:36:04 -07005863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 return focus;
Romain Guy06882f82009-06-10 13:36:04 -07005865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 } else if (!isPointerEvent) {
5867 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(-1);
5868 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005869 Slog.w(TAG, "Event timeout during app switch: dropping trackball "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 + nextMotion);
5871 return SKIP_TARGET_TOKEN;
5872 }
Romain Guy06882f82009-06-10 13:36:04 -07005873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 WindowState focus = null;
5875 synchronized(mWindowMap) {
5876 focus = getFocusedWindowLocked();
5877 }
Romain Guy06882f82009-06-10 13:36:04 -07005878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 wakeupIfNeeded(focus, LocalPowerManager.BUTTON_EVENT);
5880 return focus;
5881 }
Romain Guy06882f82009-06-10 13:36:04 -07005882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 if (nextMotion == null) {
5884 return SKIP_TARGET_TOKEN;
5885 }
Romain Guy06882f82009-06-10 13:36:04 -07005886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 boolean dispatch = mKeyWaiter.checkShouldDispatchKey(
5888 KeyEvent.KEYCODE_UNKNOWN);
5889 if (!dispatch) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005890 Slog.w(TAG, "Event timeout during app switch: dropping pointer "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 + nextMotion);
5892 return SKIP_TARGET_TOKEN;
5893 }
Romain Guy06882f82009-06-10 13:36:04 -07005894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 // Find the target window for a pointer event.
5896 int action = nextMotion.getAction();
5897 final float xf = nextMotion.getX();
5898 final float yf = nextMotion.getY();
5899 final long eventTime = nextMotion.getEventTime();
Romain Guy06882f82009-06-10 13:36:04 -07005900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 final boolean screenWasOff = qev != null
5902 && (qev.flags&WindowManagerPolicy.FLAG_BRIGHT_HERE) != 0;
Romain Guy06882f82009-06-10 13:36:04 -07005903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 WindowState target = null;
Romain Guy06882f82009-06-10 13:36:04 -07005905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 synchronized(mWindowMap) {
5907 synchronized (this) {
5908 if (action == MotionEvent.ACTION_DOWN) {
5909 if (mMotionTarget != null) {
5910 // this is weird, we got a pen down, but we thought it was
5911 // already down!
5912 // XXX: We should probably send an ACTION_UP to the current
5913 // target.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005914 Slog.w(TAG, "Pointer down received while already down in: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 + mMotionTarget);
5916 mMotionTarget = null;
5917 }
Romain Guy06882f82009-06-10 13:36:04 -07005918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 // ACTION_DOWN is special, because we need to lock next events to
5920 // the window we'll land onto.
5921 final int x = (int)xf;
5922 final int y = (int)yf;
Romain Guy06882f82009-06-10 13:36:04 -07005923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 final ArrayList windows = mWindows;
5925 final int N = windows.size();
5926 WindowState topErrWindow = null;
5927 final Rect tmpRect = mTempRect;
5928 for (int i=N-1; i>=0; i--) {
5929 WindowState child = (WindowState)windows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005930 //Slog.i(TAG, "Checking dispatch to: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 final int flags = child.mAttrs.flags;
5932 if ((flags & WindowManager.LayoutParams.FLAG_SYSTEM_ERROR) != 0) {
5933 if (topErrWindow == null) {
5934 topErrWindow = child;
5935 }
5936 }
5937 if (!child.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005938 //Slog.i(TAG, "Not visible!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 continue;
5940 }
5941 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005942 //Slog.i(TAG, "Not touchable!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 if ((flags & WindowManager.LayoutParams
5944 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5945 child.mNextOutsideTouch = mOutsideTouchTargets;
5946 mOutsideTouchTargets = child;
5947 }
5948 continue;
5949 }
5950 tmpRect.set(child.mFrame);
5951 if (child.mTouchableInsets == ViewTreeObserver
5952 .InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT) {
5953 // The touch is inside of the window if it is
5954 // inside the frame, AND the content part of that
5955 // frame that was given by the application.
5956 tmpRect.left += child.mGivenContentInsets.left;
5957 tmpRect.top += child.mGivenContentInsets.top;
5958 tmpRect.right -= child.mGivenContentInsets.right;
5959 tmpRect.bottom -= child.mGivenContentInsets.bottom;
5960 } else if (child.mTouchableInsets == ViewTreeObserver
5961 .InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE) {
5962 // The touch is inside of the window if it is
5963 // inside the frame, AND the visible part of that
5964 // frame that was given by the application.
5965 tmpRect.left += child.mGivenVisibleInsets.left;
5966 tmpRect.top += child.mGivenVisibleInsets.top;
5967 tmpRect.right -= child.mGivenVisibleInsets.right;
5968 tmpRect.bottom -= child.mGivenVisibleInsets.bottom;
5969 }
5970 final int touchFlags = flags &
5971 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5972 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
5973 if (tmpRect.contains(x, y) || touchFlags == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005974 //Slog.i(TAG, "Using this target!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 if (!screenWasOff || (flags &
5976 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING) != 0) {
5977 mMotionTarget = child;
5978 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005979 //Slog.i(TAG, "Waking, skip!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 mMotionTarget = null;
5981 }
5982 break;
5983 }
Romain Guy06882f82009-06-10 13:36:04 -07005984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 if ((flags & WindowManager.LayoutParams
5986 .FLAG_WATCH_OUTSIDE_TOUCH) != 0) {
5987 child.mNextOutsideTouch = mOutsideTouchTargets;
5988 mOutsideTouchTargets = child;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005989 //Slog.i(TAG, "Adding to outside target list: " + child);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 }
5991 }
5992
5993 // if there's an error window but it's not accepting
5994 // focus (typically because it is not yet visible) just
5995 // wait for it -- any other focused window may in fact
5996 // be in ANR state.
5997 if (topErrWindow != null && mMotionTarget != topErrWindow) {
5998 mMotionTarget = null;
5999 }
6000 }
Romain Guy06882f82009-06-10 13:36:04 -07006001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 target = mMotionTarget;
6003 }
6004 }
Romain Guy06882f82009-06-10 13:36:04 -07006005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 wakeupIfNeeded(target, eventType(nextMotion));
Romain Guy06882f82009-06-10 13:36:04 -07006007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 // Pointer events are a little different -- if there isn't a
6009 // target found for any event, then just drop it.
6010 return target != null ? target : SKIP_TARGET_TOKEN;
6011 }
Romain Guy06882f82009-06-10 13:36:04 -07006012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006013 boolean checkShouldDispatchKey(int keycode) {
6014 synchronized (this) {
6015 if (mPolicy.isAppSwitchKeyTqTiLwLi(keycode)) {
6016 mTimeToSwitch = 0;
6017 return true;
6018 }
6019 if (mTimeToSwitch != 0
6020 && mTimeToSwitch < SystemClock.uptimeMillis()) {
6021 return false;
6022 }
6023 return true;
6024 }
6025 }
Romain Guy06882f82009-06-10 13:36:04 -07006026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 void bindTargetWindowLocked(WindowState win,
6028 int pendingWhat, QueuedEvent pendingMotion) {
6029 synchronized (this) {
6030 bindTargetWindowLockedLocked(win, pendingWhat, pendingMotion);
6031 }
6032 }
Romain Guy06882f82009-06-10 13:36:04 -07006033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 void bindTargetWindowLocked(WindowState win) {
6035 synchronized (this) {
6036 bindTargetWindowLockedLocked(win, RETURN_NOTHING, null);
6037 }
6038 }
6039
6040 void bindTargetWindowLockedLocked(WindowState win,
6041 int pendingWhat, QueuedEvent pendingMotion) {
6042 mLastWin = win;
6043 mLastBinder = win.mClient.asBinder();
6044 mFinished = false;
6045 if (pendingMotion != null) {
6046 final Session s = win.mSession;
6047 if (pendingWhat == RETURN_PENDING_POINTER) {
6048 releasePendingPointerLocked(s);
6049 s.mPendingPointerMove = pendingMotion;
6050 s.mPendingPointerWindow = win;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006051 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 "bindTargetToWindow " + s.mPendingPointerMove);
6053 } else if (pendingWhat == RETURN_PENDING_TRACKBALL) {
6054 releasePendingTrackballLocked(s);
6055 s.mPendingTrackballMove = pendingMotion;
6056 s.mPendingTrackballWindow = win;
6057 }
6058 }
6059 }
Romain Guy06882f82009-06-10 13:36:04 -07006060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 void releasePendingPointerLocked(Session s) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006062 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 "releasePendingPointer " + s.mPendingPointerMove);
6064 if (s.mPendingPointerMove != null) {
6065 mQueue.recycleEvent(s.mPendingPointerMove);
6066 s.mPendingPointerMove = null;
6067 }
6068 }
Romain Guy06882f82009-06-10 13:36:04 -07006069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 void releasePendingTrackballLocked(Session s) {
6071 if (s.mPendingTrackballMove != null) {
6072 mQueue.recycleEvent(s.mPendingTrackballMove);
6073 s.mPendingTrackballMove = null;
6074 }
6075 }
Romain Guy06882f82009-06-10 13:36:04 -07006076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 MotionEvent finishedKey(Session session, IWindow client, boolean force,
6078 int returnWhat) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006079 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 TAG, "finishedKey: client=" + client + ", force=" + force);
6081
6082 if (client == null) {
6083 return null;
6084 }
6085
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006086 MotionEvent res = null;
6087 QueuedEvent qev = null;
6088 WindowState win = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006091 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 TAG, "finishedKey: client=" + client.asBinder()
6093 + ", force=" + force + ", last=" + mLastBinder
6094 + " (token=" + (mLastWin != null ? mLastWin.mToken : null) + ")");
6095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 if (returnWhat == RETURN_PENDING_POINTER) {
6097 qev = session.mPendingPointerMove;
6098 win = session.mPendingPointerWindow;
6099 session.mPendingPointerMove = null;
6100 session.mPendingPointerWindow = null;
6101 } else if (returnWhat == RETURN_PENDING_TRACKBALL) {
6102 qev = session.mPendingTrackballMove;
6103 win = session.mPendingTrackballWindow;
6104 session.mPendingTrackballMove = null;
6105 session.mPendingTrackballWindow = null;
6106 }
Romain Guy06882f82009-06-10 13:36:04 -07006107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 if (mLastBinder == client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006109 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 TAG, "finishedKey: last paused="
6111 + ((mLastWin != null) ? mLastWin.mToken.paused : "null"));
6112 if (mLastWin != null && (!mLastWin.mToken.paused || force
6113 || !mEventDispatching)) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006114 doFinishedKeyLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 } else {
6116 // Make sure to wake up anyone currently waiting to
6117 // dispatch a key, so they can re-evaluate their
6118 // current situation.
6119 mFinished = true;
6120 notifyAll();
6121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122 }
Romain Guy06882f82009-06-10 13:36:04 -07006123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 if (qev != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006125 res = (MotionEvent)qev.event;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006126 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 "Returning pending motion: " + res);
6128 mQueue.recycleEvent(qev);
6129 if (win != null && returnWhat == RETURN_PENDING_POINTER) {
6130 res.offsetLocation(-win.mFrame.left, -win.mFrame.top);
6131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 }
Christopher Tate2624fbc2009-12-11 12:11:31 -08006133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006134
Christopher Tate2624fbc2009-12-11 12:11:31 -08006135 if (res != null && returnWhat == RETURN_PENDING_POINTER) {
6136 synchronized (mWindowMap) {
Dianne Hackborn90d2db32010-02-11 22:19:06 -08006137 dispatchPointerElsewhereLocked(win, win, res, res.getEventTime(), false);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006138 }
6139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006140
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07006141 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 }
6143
6144 void tickle() {
6145 synchronized (this) {
6146 notifyAll();
6147 }
6148 }
Romain Guy06882f82009-06-10 13:36:04 -07006149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 void handleNewWindowLocked(WindowState newWindow) {
6151 if (!newWindow.canReceiveKeys()) {
6152 return;
6153 }
6154 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006155 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 TAG, "New key dispatch window: win="
6157 + newWindow.mClient.asBinder()
6158 + ", last=" + mLastBinder
6159 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6160 + "), finished=" + mFinished + ", paused="
6161 + newWindow.mToken.paused);
6162
6163 // Displaying a window implicitly causes dispatching to
6164 // be unpaused. (This is to protect against bugs if someone
6165 // pauses dispatching but forgets to resume.)
6166 newWindow.mToken.paused = false;
6167
6168 mGotFirstWindow = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169
6170 if ((newWindow.mAttrs.flags & FLAG_SYSTEM_ERROR) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006171 if (DEBUG_INPUT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 "New SYSTEM_ERROR window; resetting state");
6173 mLastWin = null;
6174 mLastBinder = null;
6175 mMotionTarget = null;
6176 mFinished = true;
6177 } else if (mLastWin != null) {
6178 // If the new window is above the window we are
6179 // waiting on, then stop waiting and let key dispatching
6180 // start on the new guy.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006181 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006182 TAG, "Last win layer=" + mLastWin.mLayer
6183 + ", new win layer=" + newWindow.mLayer);
6184 if (newWindow.mLayer >= mLastWin.mLayer) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006185 // The new window is above the old; finish pending input to the last
6186 // window and start directing it to the new one.
6187 mLastWin.mToken.paused = false;
Christopher Tate136b1f92010-02-11 17:51:24 -08006188 doFinishedKeyLocked(false); // does a notifyAll()
6189 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190 }
6191 }
6192
The Android Open Source Projectc474dec2009-03-04 09:49:09 -08006193 // Now that we've put a new window state in place, make the event waiter
6194 // take notice and retarget its attentions.
6195 notifyAll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 }
6197 }
6198
6199 void pauseDispatchingLocked(WindowToken token) {
6200 synchronized (this)
6201 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006202 if (DEBUG_INPUT) Slog.v(TAG, "Pausing WindowToken " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 token.paused = true;
6204
6205 /*
6206 if (mLastWin != null && !mFinished && mLastWin.mBaseLayer <= layer) {
6207 mPaused = true;
6208 } else {
6209 if (mLastWin == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006210 Slog.i(TAG, "Key dispatching not paused: no last window.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 } else if (mFinished) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006212 Slog.i(TAG, "Key dispatching not paused: finished last key.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006214 Slog.i(TAG, "Key dispatching not paused: window in higher layer.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 }
6216 }
6217 */
6218 }
6219 }
6220
6221 void resumeDispatchingLocked(WindowToken token) {
6222 synchronized (this) {
6223 if (token.paused) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006224 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 TAG, "Resuming WindowToken " + token
6226 + ", last=" + mLastBinder
6227 + " (token=" + (mLastWin != null ? mLastWin.mToken : null)
6228 + "), finished=" + mFinished + ", paused="
6229 + token.paused);
6230 token.paused = false;
6231 if (mLastWin != null && mLastWin.mToken == token && mFinished) {
Christopher Tate136b1f92010-02-11 17:51:24 -08006232 doFinishedKeyLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 } else {
6234 notifyAll();
6235 }
6236 }
6237 }
6238 }
6239
6240 void setEventDispatchingLocked(boolean enabled) {
6241 synchronized (this) {
6242 mEventDispatching = enabled;
6243 notifyAll();
6244 }
6245 }
Romain Guy06882f82009-06-10 13:36:04 -07006246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 void appSwitchComing() {
6248 synchronized (this) {
6249 // Don't wait for more than .5 seconds for app to finish
6250 // processing the pending events.
6251 long now = SystemClock.uptimeMillis() + 500;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006252 if (DEBUG_INPUT) Slog.v(TAG, "appSwitchComing: " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 if (mTimeToSwitch == 0 || now < mTimeToSwitch) {
6254 mTimeToSwitch = now;
6255 }
6256 notifyAll();
6257 }
6258 }
Romain Guy06882f82009-06-10 13:36:04 -07006259
Christopher Tate136b1f92010-02-11 17:51:24 -08006260 private final void doFinishedKeyLocked(boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006261 if (mLastWin != null) {
6262 releasePendingPointerLocked(mLastWin.mSession);
6263 releasePendingTrackballLocked(mLastWin.mSession);
6264 }
Romain Guy06882f82009-06-10 13:36:04 -07006265
Christopher Tate136b1f92010-02-11 17:51:24 -08006266 if (force || mLastWin == null || !mLastWin.mToken.paused
6267 || !mLastWin.isVisibleLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 // If the current window has been paused, we aren't -really-
6269 // finished... so let the waiters still wait.
6270 mLastWin = null;
6271 mLastBinder = null;
6272 }
6273 mFinished = true;
6274 notifyAll();
6275 }
6276 }
6277
6278 private class KeyQ extends KeyInputQueue
6279 implements KeyInputQueue.FilterCallback {
6280 PowerManager.WakeLock mHoldingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07006281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 KeyQ() {
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07006283 super(mContext, WindowManagerService.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
6285 mHoldingScreen = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
6286 "KEEP_SCREEN_ON_FLAG");
6287 mHoldingScreen.setReferenceCounted(false);
6288 }
6289
6290 @Override
6291 boolean preprocessEvent(InputDevice device, RawInputEvent event) {
6292 if (mPolicy.preprocessInputEventTq(event)) {
6293 return true;
6294 }
Romain Guy06882f82009-06-10 13:36:04 -07006295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 switch (event.type) {
6297 case RawInputEvent.EV_KEY: {
6298 // XXX begin hack
6299 if (DEBUG) {
6300 if (event.keycode == KeyEvent.KEYCODE_G) {
6301 if (event.value != 0) {
6302 // G down
6303 mPolicy.screenTurnedOff(WindowManagerPolicy.OFF_BECAUSE_OF_USER);
6304 }
6305 return false;
6306 }
6307 if (event.keycode == KeyEvent.KEYCODE_D) {
6308 if (event.value != 0) {
6309 //dump();
6310 }
6311 return false;
6312 }
6313 }
6314 // XXX end hack
Romain Guy06882f82009-06-10 13:36:04 -07006315
Charles Mendis322591c2009-10-29 11:06:59 -07006316 boolean screenIsOff = !mPowerManager.isScreenOn();
6317 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 int actions = mPolicy.interceptKeyTq(event, !screenIsOff);
Romain Guy06882f82009-06-10 13:36:04 -07006319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 if ((actions & WindowManagerPolicy.ACTION_GO_TO_SLEEP) != 0) {
6321 mPowerManager.goToSleep(event.when);
6322 }
6323
6324 if (screenIsOff) {
6325 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6326 }
6327 if (screenIsDim) {
6328 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6329 }
6330 if ((actions & WindowManagerPolicy.ACTION_POKE_USER_ACTIVITY) != 0) {
6331 mPowerManager.userActivity(event.when, false,
Michael Chane96440f2009-05-06 10:27:36 -07006332 LocalPowerManager.BUTTON_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 }
Romain Guy06882f82009-06-10 13:36:04 -07006334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006335 if ((actions & WindowManagerPolicy.ACTION_PASS_TO_USER) != 0) {
6336 if (event.value != 0 && mPolicy.isAppSwitchKeyTqTiLwLi(event.keycode)) {
6337 filterQueue(this);
6338 mKeyWaiter.appSwitchComing();
6339 }
6340 return true;
6341 } else {
6342 return false;
6343 }
6344 }
Romain Guy06882f82009-06-10 13:36:04 -07006345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 case RawInputEvent.EV_REL: {
Charles Mendis322591c2009-10-29 11:06:59 -07006347 boolean screenIsOff = !mPowerManager.isScreenOn();
6348 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 if (screenIsOff) {
6350 if (!mPolicy.isWakeRelMovementTq(event.deviceId,
6351 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006352 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 return false;
6354 }
6355 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6356 }
6357 if (screenIsDim) {
6358 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6359 }
6360 return true;
6361 }
Romain Guy06882f82009-06-10 13:36:04 -07006362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 case RawInputEvent.EV_ABS: {
Charles Mendis322591c2009-10-29 11:06:59 -07006364 boolean screenIsOff = !mPowerManager.isScreenOn();
6365 boolean screenIsDim = !mPowerManager.isScreenBright();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 if (screenIsOff) {
6367 if (!mPolicy.isWakeAbsMovementTq(event.deviceId,
6368 device.classes, event)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006369 //Slog.i(TAG, "dropping because screenIsOff and !isWakeKey");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 return false;
6371 }
6372 event.flags |= WindowManagerPolicy.FLAG_WOKE_HERE;
6373 }
6374 if (screenIsDim) {
6375 event.flags |= WindowManagerPolicy.FLAG_BRIGHT_HERE;
6376 }
6377 return true;
6378 }
Romain Guy06882f82009-06-10 13:36:04 -07006379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 default:
6381 return true;
6382 }
6383 }
6384
6385 public int filterEvent(QueuedEvent ev) {
6386 switch (ev.classType) {
6387 case RawInputEvent.CLASS_KEYBOARD:
6388 KeyEvent ke = (KeyEvent)ev.event;
6389 if (mPolicy.isMovementKeyTi(ke.getKeyCode())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006390 Slog.w(TAG, "Dropping movement key during app switch: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 + ke.getKeyCode() + ", action=" + ke.getAction());
6392 return FILTER_REMOVE;
6393 }
6394 return FILTER_ABORT;
6395 default:
6396 return FILTER_KEEP;
6397 }
6398 }
Romain Guy06882f82009-06-10 13:36:04 -07006399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006400 /**
6401 * Must be called with the main window manager lock held.
6402 */
6403 void setHoldScreenLocked(boolean holding) {
6404 boolean state = mHoldingScreen.isHeld();
6405 if (holding != state) {
6406 if (holding) {
6407 mHoldingScreen.acquire();
6408 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006409 mPolicy.screenOnStoppedLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 mHoldingScreen.release();
6411 }
6412 }
6413 }
Michael Chan53071d62009-05-13 17:29:48 -07006414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415
6416 public boolean detectSafeMode() {
6417 mSafeMode = mPolicy.detectSafeMode();
6418 return mSafeMode;
6419 }
Romain Guy06882f82009-06-10 13:36:04 -07006420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 public void systemReady() {
6422 mPolicy.systemReady();
6423 }
Romain Guy06882f82009-06-10 13:36:04 -07006424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 private final class InputDispatcherThread extends Thread {
6426 // Time to wait when there is nothing to do: 9999 seconds.
6427 static final int LONG_WAIT=9999*1000;
6428
6429 public InputDispatcherThread() {
6430 super("InputDispatcher");
6431 }
Romain Guy06882f82009-06-10 13:36:04 -07006432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 @Override
6434 public void run() {
6435 while (true) {
6436 try {
6437 process();
6438 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006439 Slog.e(TAG, "Exception in input dispatcher", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 }
6441 }
6442 }
Romain Guy06882f82009-06-10 13:36:04 -07006443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 private void process() {
6445 android.os.Process.setThreadPriority(
6446 android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
Romain Guy06882f82009-06-10 13:36:04 -07006447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 // The last key event we saw
6449 KeyEvent lastKey = null;
6450
6451 // Last keydown time for auto-repeating keys
6452 long lastKeyTime = SystemClock.uptimeMillis();
6453 long nextKeyTime = lastKeyTime+LONG_WAIT;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006454 long downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455
Romain Guy06882f82009-06-10 13:36:04 -07006456 // How many successive repeats we generated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 int keyRepeatCount = 0;
6458
6459 // Need to report that configuration has changed?
6460 boolean configChanged = false;
Romain Guy06882f82009-06-10 13:36:04 -07006461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 while (true) {
6463 long curTime = SystemClock.uptimeMillis();
6464
Joe Onorato8a9b2202010-02-26 18:56:32 -08006465 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 TAG, "Waiting for next key: now=" + curTime
6467 + ", repeat @ " + nextKeyTime);
6468
6469 // Retrieve next event, waiting only as long as the next
6470 // repeat timeout. If the configuration has changed, then
6471 // don't wait at all -- we'll report the change as soon as
6472 // we have processed all events.
6473 QueuedEvent ev = mQueue.getEvent(
6474 (int)((!configChanged && curTime < nextKeyTime)
6475 ? (nextKeyTime-curTime) : 0));
6476
Joe Onorato8a9b2202010-02-26 18:56:32 -08006477 if (DEBUG_INPUT && ev != null) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 TAG, "Event: type=" + ev.classType + " data=" + ev.event);
6479
Michael Chan53071d62009-05-13 17:29:48 -07006480 if (MEASURE_LATENCY) {
6481 lt.sample("2 got event ", System.nanoTime() - ev.whenNano);
6482 }
6483
Mike Lockwood3d0ea722009-10-21 22:58:29 -04006484 if (lastKey != null && !mPolicy.allowKeyRepeat()) {
6485 // cancel key repeat at the request of the policy.
6486 lastKey = null;
6487 downTime = 0;
6488 lastKeyTime = curTime;
6489 nextKeyTime = curTime + LONG_WAIT;
6490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 try {
6492 if (ev != null) {
Michael Chan53071d62009-05-13 17:29:48 -07006493 curTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 int eventType;
6495 if (ev.classType == RawInputEvent.CLASS_TOUCHSCREEN) {
6496 eventType = eventType((MotionEvent)ev.event);
6497 } else if (ev.classType == RawInputEvent.CLASS_KEYBOARD ||
6498 ev.classType == RawInputEvent.CLASS_TRACKBALL) {
6499 eventType = LocalPowerManager.BUTTON_EVENT;
6500 } else {
6501 eventType = LocalPowerManager.OTHER_EVENT;
6502 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006503 try {
Michael Chan53071d62009-05-13 17:29:48 -07006504 if ((curTime - mLastBatteryStatsCallTime)
Michael Chane96440f2009-05-06 10:27:36 -07006505 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
Michael Chan53071d62009-05-13 17:29:48 -07006506 mLastBatteryStatsCallTime = curTime;
Michael Chane96440f2009-05-06 10:27:36 -07006507 mBatteryStats.noteInputEvent();
6508 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006509 } catch (RemoteException e) {
6510 // Ignore
6511 }
Michael Chane10de972009-05-18 11:24:50 -07006512
Mike Lockwood5db42402009-11-30 14:51:51 -05006513 if (ev.classType == RawInputEvent.CLASS_CONFIGURATION_CHANGED) {
6514 // do not wake screen in this case
6515 } else if (eventType != TOUCH_EVENT
Michael Chane10de972009-05-18 11:24:50 -07006516 && eventType != LONG_TOUCH_EVENT
6517 && eventType != CHEEK_EVENT) {
6518 mPowerManager.userActivity(curTime, false,
6519 eventType, false);
6520 } else if (mLastTouchEventType != eventType
6521 || (curTime - mLastUserActivityCallTime)
6522 >= MIN_TIME_BETWEEN_USERACTIVITIES) {
6523 mLastUserActivityCallTime = curTime;
6524 mLastTouchEventType = eventType;
6525 mPowerManager.userActivity(curTime, false,
6526 eventType, false);
6527 }
6528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006529 switch (ev.classType) {
6530 case RawInputEvent.CLASS_KEYBOARD:
6531 KeyEvent ke = (KeyEvent)ev.event;
6532 if (ke.isDown()) {
6533 lastKey = ke;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006534 downTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 keyRepeatCount = 0;
6536 lastKeyTime = curTime;
6537 nextKeyTime = lastKeyTime
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006538 + ViewConfiguration.getLongPressTimeout();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006539 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 TAG, "Received key down: first repeat @ "
6541 + nextKeyTime);
6542 } else {
6543 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006544 downTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 // Arbitrary long timeout.
6546 lastKeyTime = curTime;
6547 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006548 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 TAG, "Received key up: ignore repeat @ "
6550 + nextKeyTime);
6551 }
6552 dispatchKey((KeyEvent)ev.event, 0, 0);
6553 mQueue.recycleEvent(ev);
6554 break;
6555 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006556 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6558 break;
6559 case RawInputEvent.CLASS_TRACKBALL:
6560 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6561 break;
6562 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6563 configChanged = true;
6564 break;
6565 default:
6566 mQueue.recycleEvent(ev);
6567 break;
6568 }
Romain Guy06882f82009-06-10 13:36:04 -07006569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 } else if (configChanged) {
6571 configChanged = false;
6572 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 } else if (lastKey != null) {
6575 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 // Timeout occurred while key was down. If it is at or
6578 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006579 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 TAG, "Key timeout: repeat=" + nextKeyTime
6581 + ", now=" + curTime);
6582 if (curTime < nextKeyTime) {
6583 continue;
6584 }
Romain Guy06882f82009-06-10 13:36:04 -07006585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 lastKeyTime = nextKeyTime;
6587 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6588 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006589 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 TAG, "Key repeat: count=" + keyRepeatCount
6591 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006592 KeyEvent newEvent;
6593 if (downTime != 0 && (downTime
6594 + ViewConfiguration.getLongPressTimeout())
6595 <= curTime) {
6596 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6597 curTime, keyRepeatCount,
6598 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6599 downTime = 0;
6600 } else {
6601 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6602 curTime, keyRepeatCount);
6603 }
6604 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 } else {
6607 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 lastKeyTime = curTime;
6610 nextKeyTime = curTime + LONG_WAIT;
6611 }
Romain Guy06882f82009-06-10 13:36:04 -07006612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006614 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 "Input thread received uncaught exception: " + e, e);
6616 }
6617 }
6618 }
6619 }
6620
6621 // -------------------------------------------------------------
6622 // Client Session State
6623 // -------------------------------------------------------------
6624
6625 private final class Session extends IWindowSession.Stub
6626 implements IBinder.DeathRecipient {
6627 final IInputMethodClient mClient;
6628 final IInputContext mInputContext;
6629 final int mUid;
6630 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006631 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 SurfaceSession mSurfaceSession;
6633 int mNumWindow = 0;
6634 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 /**
6637 * Current pointer move event being dispatched to client window... must
6638 * hold key lock to access.
6639 */
6640 QueuedEvent mPendingPointerMove;
6641 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 /**
6644 * Current trackball move event being dispatched to client window... must
6645 * hold key lock to access.
6646 */
6647 QueuedEvent mPendingTrackballMove;
6648 WindowState mPendingTrackballWindow;
6649
6650 public Session(IInputMethodClient client, IInputContext inputContext) {
6651 mClient = client;
6652 mInputContext = inputContext;
6653 mUid = Binder.getCallingUid();
6654 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006655 StringBuilder sb = new StringBuilder();
6656 sb.append("Session{");
6657 sb.append(Integer.toHexString(System.identityHashCode(this)));
6658 sb.append(" uid ");
6659 sb.append(mUid);
6660 sb.append("}");
6661 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 synchronized (mWindowMap) {
6664 if (mInputMethodManager == null && mHaveInputMethods) {
6665 IBinder b = ServiceManager.getService(
6666 Context.INPUT_METHOD_SERVICE);
6667 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6668 }
6669 }
6670 long ident = Binder.clearCallingIdentity();
6671 try {
6672 // Note: it is safe to call in to the input method manager
6673 // here because we are not holding our lock.
6674 if (mInputMethodManager != null) {
6675 mInputMethodManager.addClient(client, inputContext,
6676 mUid, mPid);
6677 } else {
6678 client.setUsingInputMethod(false);
6679 }
6680 client.asBinder().linkToDeath(this, 0);
6681 } catch (RemoteException e) {
6682 // The caller has died, so we can just forget about this.
6683 try {
6684 if (mInputMethodManager != null) {
6685 mInputMethodManager.removeClient(client);
6686 }
6687 } catch (RemoteException ee) {
6688 }
6689 } finally {
6690 Binder.restoreCallingIdentity(ident);
6691 }
6692 }
Romain Guy06882f82009-06-10 13:36:04 -07006693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 @Override
6695 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6696 throws RemoteException {
6697 try {
6698 return super.onTransact(code, data, reply, flags);
6699 } catch (RuntimeException e) {
6700 // Log all 'real' exceptions thrown to the caller
6701 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006702 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 }
6704 throw e;
6705 }
6706 }
6707
6708 public void binderDied() {
6709 // Note: it is safe to call in to the input method manager
6710 // here because we are not holding our lock.
6711 try {
6712 if (mInputMethodManager != null) {
6713 mInputMethodManager.removeClient(mClient);
6714 }
6715 } catch (RemoteException e) {
6716 }
6717 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006718 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 mClientDead = true;
6720 killSessionLocked();
6721 }
6722 }
6723
6724 public int add(IWindow window, WindowManager.LayoutParams attrs,
6725 int viewVisibility, Rect outContentInsets) {
6726 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6727 }
Romain Guy06882f82009-06-10 13:36:04 -07006728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 public void remove(IWindow window) {
6730 removeWindow(this, window);
6731 }
Romain Guy06882f82009-06-10 13:36:04 -07006732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6734 int requestedWidth, int requestedHeight, int viewFlags,
6735 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006736 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 return relayoutWindow(this, window, attrs,
6738 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006739 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 }
Romain Guy06882f82009-06-10 13:36:04 -07006741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 public void setTransparentRegion(IWindow window, Region region) {
6743 setTransparentRegionWindow(this, window, region);
6744 }
Romain Guy06882f82009-06-10 13:36:04 -07006745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 public void setInsets(IWindow window, int touchableInsets,
6747 Rect contentInsets, Rect visibleInsets) {
6748 setInsetsWindow(this, window, touchableInsets, contentInsets,
6749 visibleInsets);
6750 }
Romain Guy06882f82009-06-10 13:36:04 -07006751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006752 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6753 getWindowDisplayFrame(this, window, outDisplayFrame);
6754 }
Romain Guy06882f82009-06-10 13:36:04 -07006755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006757 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 TAG, "IWindow finishDrawing called for " + window);
6759 finishDrawingWindow(this, window);
6760 }
6761
6762 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006763 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 TAG, "IWindow finishKey called for " + window);
6765 mKeyWaiter.finishedKey(this, window, false,
6766 KeyWaiter.RETURN_NOTHING);
6767 }
6768
6769 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006770 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 TAG, "IWindow getPendingMotionEvent called for " + window);
6772 return mKeyWaiter.finishedKey(this, window, false,
6773 KeyWaiter.RETURN_PENDING_POINTER);
6774 }
Romain Guy06882f82009-06-10 13:36:04 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006777 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 TAG, "IWindow getPendingMotionEvent called for " + window);
6779 return mKeyWaiter.finishedKey(this, window, false,
6780 KeyWaiter.RETURN_PENDING_TRACKBALL);
6781 }
6782
6783 public void setInTouchMode(boolean mode) {
6784 synchronized(mWindowMap) {
6785 mInTouchMode = mode;
6786 }
6787 }
6788
6789 public boolean getInTouchMode() {
6790 synchronized(mWindowMap) {
6791 return mInTouchMode;
6792 }
6793 }
6794
6795 public boolean performHapticFeedback(IWindow window, int effectId,
6796 boolean always) {
6797 synchronized(mWindowMap) {
6798 long ident = Binder.clearCallingIdentity();
6799 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006800 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006801 windowForClientLocked(this, window, true),
6802 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 } finally {
6804 Binder.restoreCallingIdentity(ident);
6805 }
6806 }
6807 }
Romain Guy06882f82009-06-10 13:36:04 -07006808
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006809 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006810 synchronized(mWindowMap) {
6811 long ident = Binder.clearCallingIdentity();
6812 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006813 setWindowWallpaperPositionLocked(
6814 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006815 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006816 } finally {
6817 Binder.restoreCallingIdentity(ident);
6818 }
6819 }
6820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006821
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006822 public void wallpaperOffsetsComplete(IBinder window) {
6823 WindowManagerService.this.wallpaperOffsetsComplete(window);
6824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006825
Dianne Hackborn75804932009-10-20 20:15:20 -07006826 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6827 int z, Bundle extras, boolean sync) {
6828 synchronized(mWindowMap) {
6829 long ident = Binder.clearCallingIdentity();
6830 try {
6831 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006832 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006833 action, x, y, z, extras, sync);
6834 } finally {
6835 Binder.restoreCallingIdentity(ident);
6836 }
6837 }
6838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006839
Dianne Hackborn75804932009-10-20 20:15:20 -07006840 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6841 WindowManagerService.this.wallpaperCommandComplete(window, result);
6842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 void windowAddedLocked() {
6845 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006846 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 TAG, "First window added to " + this + ", creating SurfaceSession");
6848 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006849 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006850 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 mSessions.add(this);
6852 }
6853 mNumWindow++;
6854 }
6855
6856 void windowRemovedLocked() {
6857 mNumWindow--;
6858 killSessionLocked();
6859 }
Romain Guy06882f82009-06-10 13:36:04 -07006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 void killSessionLocked() {
6862 if (mNumWindow <= 0 && mClientDead) {
6863 mSessions.remove(this);
6864 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006865 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 TAG, "Last window removed from " + this
6867 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006868 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006869 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 try {
6871 mSurfaceSession.kill();
6872 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006873 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 + mSurfaceSession + " in session " + this
6875 + ": " + e.toString());
6876 }
6877 mSurfaceSession = null;
6878 }
6879 }
6880 }
Romain Guy06882f82009-06-10 13:36:04 -07006881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006883 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6884 pw.print(" mClientDead="); pw.print(mClientDead);
6885 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6886 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6887 pw.print(prefix);
6888 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6889 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6890 }
6891 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6892 pw.print(prefix);
6893 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6894 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 }
6897
6898 @Override
6899 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006900 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902 }
6903
6904 // -------------------------------------------------------------
6905 // Client Window State
6906 // -------------------------------------------------------------
6907
6908 private final class WindowState implements WindowManagerPolicy.WindowState {
6909 final Session mSession;
6910 final IWindow mClient;
6911 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006912 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 AppWindowToken mAppToken;
6914 AppWindowToken mTargetAppToken;
6915 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6916 final DeathRecipient mDeathRecipient;
6917 final WindowState mAttachedWindow;
6918 final ArrayList mChildWindows = new ArrayList();
6919 final int mBaseLayer;
6920 final int mSubLayer;
6921 final boolean mLayoutAttached;
6922 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006923 final boolean mIsWallpaper;
6924 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 int mViewVisibility;
6926 boolean mPolicyVisibility = true;
6927 boolean mPolicyVisibilityAfterAnim = true;
6928 boolean mAppFreezing;
6929 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006930 boolean mReportDestroySurface;
6931 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006932 boolean mAttachedHidden; // is our parent window hidden?
6933 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006934 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 int mRequestedWidth;
6936 int mRequestedHeight;
6937 int mLastRequestedWidth;
6938 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 int mLayer;
6940 int mAnimLayer;
6941 int mLastLayer;
6942 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006943 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006944 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945
6946 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006947
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006948 int mLayoutSeq = -1;
6949
6950 Configuration mConfiguration = null;
6951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 // Actual frame shown on-screen (may be modified by animation)
6953 final Rect mShownFrame = new Rect();
6954 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08006957 * Set when we have changed the size of the surface, to know that
6958 * we must tell them application to resize (and thus redraw itself).
6959 */
6960 boolean mSurfaceResized;
6961
6962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 * Insets that determine the actually visible area
6964 */
6965 final Rect mVisibleInsets = new Rect();
6966 final Rect mLastVisibleInsets = new Rect();
6967 boolean mVisibleInsetsChanged;
6968
6969 /**
6970 * Insets that are covered by system windows
6971 */
6972 final Rect mContentInsets = new Rect();
6973 final Rect mLastContentInsets = new Rect();
6974 boolean mContentInsetsChanged;
6975
6976 /**
6977 * Set to true if we are waiting for this window to receive its
6978 * given internal insets before laying out other windows based on it.
6979 */
6980 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 /**
6983 * These are the content insets that were given during layout for
6984 * this window, to be applied to windows behind it.
6985 */
6986 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 /**
6989 * These are the visible insets that were given during layout for
6990 * this window, to be applied to windows behind it.
6991 */
6992 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 /**
6995 * Flag indicating whether the touchable region should be adjusted by
6996 * the visible insets; if false the area outside the visible insets is
6997 * NOT touchable, so we must use those to adjust the frame during hit
6998 * tests.
6999 */
7000 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 // Current transformation being applied.
7003 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7004 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7005 float mHScale=1, mVScale=1;
7006 float mLastHScale=1, mLastVScale=1;
7007 final Matrix mTmpMatrix = new Matrix();
7008
7009 // "Real" frame that the application sees.
7010 final Rect mFrame = new Rect();
7011 final Rect mLastFrame = new Rect();
7012
7013 final Rect mContainingFrame = new Rect();
7014 final Rect mDisplayFrame = new Rect();
7015 final Rect mContentFrame = new Rect();
7016 final Rect mVisibleFrame = new Rect();
7017
7018 float mShownAlpha = 1;
7019 float mAlpha = 1;
7020 float mLastAlpha = 1;
7021
7022 // Set to true if, when the window gets displayed, it should perform
7023 // an enter animation.
7024 boolean mEnterAnimationPending;
7025
7026 // Currently running animation.
7027 boolean mAnimating;
7028 boolean mLocalAnimating;
7029 Animation mAnimation;
7030 boolean mAnimationIsEntrance;
7031 boolean mHasTransformation;
7032 boolean mHasLocalTransformation;
7033 final Transformation mTransformation = new Transformation();
7034
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007035 // If a window showing a wallpaper: the requested offset for the
7036 // wallpaper; if a wallpaper window: the currently applied offset.
7037 float mWallpaperX = -1;
7038 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007039
7040 // If a window showing a wallpaper: what fraction of the offset
7041 // range corresponds to a full virtual screen.
7042 float mWallpaperXStep = -1;
7043 float mWallpaperYStep = -1;
7044
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007045 // Wallpaper windows: pixels offset based on above variables.
7046 int mXOffset;
7047 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 // This is set after IWindowSession.relayout() has been called at
7050 // least once for the window. It allows us to detect the situation
7051 // where we don't yet have a surface, but should have one soon, so
7052 // we can give the window focus before waiting for the relayout.
7053 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007055 // This is set after the Surface has been created but before the
7056 // window has been drawn. During this time the surface is hidden.
7057 boolean mDrawPending;
7058
7059 // This is set after the window has finished drawing for the first
7060 // time but before its surface is shown. The surface will be
7061 // displayed when the next layout is run.
7062 boolean mCommitDrawPending;
7063
7064 // This is set during the time after the window's drawing has been
7065 // committed, and before its surface is actually shown. It is used
7066 // to delay showing the surface until all windows in a token are ready
7067 // to be shown.
7068 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 // Set when the window has been shown in the screen the first time.
7071 boolean mHasDrawn;
7072
7073 // Currently running an exit animation?
7074 boolean mExiting;
7075
7076 // Currently on the mDestroySurface list?
7077 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 // Completely remove from window manager after exit animation?
7080 boolean mRemoveOnExit;
7081
7082 // Set when the orientation is changing and this window has not yet
7083 // been updated for the new orientation.
7084 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 // Is this window now (or just being) removed?
7087 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007088
Dianne Hackborn16064f92010-03-25 00:47:24 -07007089 // For debugging, this is the last information given to the surface flinger.
7090 boolean mSurfaceShown;
7091 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7092 int mSurfaceLayer;
7093 float mSurfaceAlpha;
7094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007095 WindowState(Session s, IWindow c, WindowToken token,
7096 WindowState attachedWindow, WindowManager.LayoutParams a,
7097 int viewVisibility) {
7098 mSession = s;
7099 mClient = c;
7100 mToken = token;
7101 mAttrs.copyFrom(a);
7102 mViewVisibility = viewVisibility;
7103 DeathRecipient deathRecipient = new DeathRecipient();
7104 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007105 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 TAG, "Window " + this + " client=" + c.asBinder()
7107 + " token=" + token + " (" + mAttrs.token + ")");
7108 try {
7109 c.asBinder().linkToDeath(deathRecipient, 0);
7110 } catch (RemoteException e) {
7111 mDeathRecipient = null;
7112 mAttachedWindow = null;
7113 mLayoutAttached = false;
7114 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007115 mIsWallpaper = false;
7116 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 mBaseLayer = 0;
7118 mSubLayer = 0;
7119 return;
7120 }
7121 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7124 mAttrs.type <= LAST_SUB_WINDOW)) {
7125 // The multiplier here is to reserve space for multiple
7126 // windows in the same type layer.
7127 mBaseLayer = mPolicy.windowTypeToLayerLw(
7128 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7129 + TYPE_LAYER_OFFSET;
7130 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7131 mAttachedWindow = attachedWindow;
7132 mAttachedWindow.mChildWindows.add(this);
7133 mLayoutAttached = mAttrs.type !=
7134 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7135 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7136 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007137 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7138 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 } else {
7140 // The multiplier here is to reserve space for multiple
7141 // windows in the same type layer.
7142 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7143 * TYPE_LAYER_MULTIPLIER
7144 + TYPE_LAYER_OFFSET;
7145 mSubLayer = 0;
7146 mAttachedWindow = null;
7147 mLayoutAttached = false;
7148 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7149 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007150 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7151 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 }
7153
7154 WindowState appWin = this;
7155 while (appWin.mAttachedWindow != null) {
7156 appWin = mAttachedWindow;
7157 }
7158 WindowToken appToken = appWin.mToken;
7159 while (appToken.appWindowToken == null) {
7160 WindowToken parent = mTokenMap.get(appToken.token);
7161 if (parent == null || appToken == parent) {
7162 break;
7163 }
7164 appToken = parent;
7165 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007166 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 mAppToken = appToken.appWindowToken;
7168
7169 mSurface = null;
7170 mRequestedWidth = 0;
7171 mRequestedHeight = 0;
7172 mLastRequestedWidth = 0;
7173 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007174 mXOffset = 0;
7175 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 mLayer = 0;
7177 mAnimLayer = 0;
7178 mLastLayer = 0;
7179 }
7180
7181 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007182 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 TAG, "Attaching " + this + " token=" + mToken
7184 + ", list=" + mToken.windows);
7185 mSession.windowAddedLocked();
7186 }
7187
7188 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7189 mHaveFrame = true;
7190
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007191 final Rect container = mContainingFrame;
7192 container.set(pf);
7193
7194 final Rect display = mDisplayFrame;
7195 display.set(df);
7196
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007197 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007198 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007199 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7200 display.intersect(mCompatibleScreenFrame);
7201 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007202 }
7203
7204 final int pw = container.right - container.left;
7205 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206
7207 int w,h;
7208 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7209 w = mAttrs.width < 0 ? pw : mAttrs.width;
7210 h = mAttrs.height< 0 ? ph : mAttrs.height;
7211 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007212 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7213 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 }
Romain Guy06882f82009-06-10 13:36:04 -07007215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 final Rect content = mContentFrame;
7217 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 final Rect visible = mVisibleFrame;
7220 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007223 final int fw = frame.width();
7224 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007226 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7227 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7228
7229 Gravity.apply(mAttrs.gravity, w, h, container,
7230 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7231 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7232
7233 //System.out.println("Out: " + mFrame);
7234
7235 // Now make sure the window fits in the overall display.
7236 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007238 // Make sure the content and visible frames are inside of the
7239 // final window frame.
7240 if (content.left < frame.left) content.left = frame.left;
7241 if (content.top < frame.top) content.top = frame.top;
7242 if (content.right > frame.right) content.right = frame.right;
7243 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7244 if (visible.left < frame.left) visible.left = frame.left;
7245 if (visible.top < frame.top) visible.top = frame.top;
7246 if (visible.right > frame.right) visible.right = frame.right;
7247 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 final Rect contentInsets = mContentInsets;
7250 contentInsets.left = content.left-frame.left;
7251 contentInsets.top = content.top-frame.top;
7252 contentInsets.right = frame.right-content.right;
7253 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 final Rect visibleInsets = mVisibleInsets;
7256 visibleInsets.left = visible.left-frame.left;
7257 visibleInsets.top = visible.top-frame.top;
7258 visibleInsets.right = frame.right-visible.right;
7259 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007260
Dianne Hackborn284ac932009-08-28 10:34:25 -07007261 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7262 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007263 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 if (localLOGV) {
7267 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7268 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007269 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 + mRequestedWidth + ", mRequestedheight="
7271 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7272 + "): frame=" + mFrame.toShortString()
7273 + " ci=" + contentInsets.toShortString()
7274 + " vi=" + visibleInsets.toShortString());
7275 //}
7276 }
7277 }
Romain Guy06882f82009-06-10 13:36:04 -07007278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 public Rect getFrameLw() {
7280 return mFrame;
7281 }
7282
7283 public Rect getShownFrameLw() {
7284 return mShownFrame;
7285 }
7286
7287 public Rect getDisplayFrameLw() {
7288 return mDisplayFrame;
7289 }
7290
7291 public Rect getContentFrameLw() {
7292 return mContentFrame;
7293 }
7294
7295 public Rect getVisibleFrameLw() {
7296 return mVisibleFrame;
7297 }
7298
7299 public boolean getGivenInsetsPendingLw() {
7300 return mGivenInsetsPending;
7301 }
7302
7303 public Rect getGivenContentInsetsLw() {
7304 return mGivenContentInsets;
7305 }
Romain Guy06882f82009-06-10 13:36:04 -07007306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 public Rect getGivenVisibleInsetsLw() {
7308 return mGivenVisibleInsets;
7309 }
Romain Guy06882f82009-06-10 13:36:04 -07007310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 public WindowManager.LayoutParams getAttrs() {
7312 return mAttrs;
7313 }
7314
7315 public int getSurfaceLayer() {
7316 return mLayer;
7317 }
Romain Guy06882f82009-06-10 13:36:04 -07007318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 public IApplicationToken getAppToken() {
7320 return mAppToken != null ? mAppToken.appToken : null;
7321 }
7322
7323 public boolean hasAppShownWindows() {
7324 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7325 }
7326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007328 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 TAG, "Setting animation in " + this + ": " + anim);
7330 mAnimating = false;
7331 mLocalAnimating = false;
7332 mAnimation = anim;
7333 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7334 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7335 }
7336
7337 public void clearAnimation() {
7338 if (mAnimation != null) {
7339 mAnimating = true;
7340 mLocalAnimating = false;
7341 mAnimation = null;
7342 }
7343 }
Romain Guy06882f82009-06-10 13:36:04 -07007344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007345 Surface createSurfaceLocked() {
7346 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007347 mReportDestroySurface = false;
7348 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 mDrawPending = true;
7350 mCommitDrawPending = false;
7351 mReadyToShow = false;
7352 if (mAppToken != null) {
7353 mAppToken.allDrawn = false;
7354 }
7355
7356 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007357 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 flags |= Surface.PUSH_BUFFERS;
7359 }
7360
7361 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7362 flags |= Surface.SECURE;
7363 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007364 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007365 TAG, "Creating surface in session "
7366 + mSession.mSurfaceSession + " window " + this
7367 + " w=" + mFrame.width()
7368 + " h=" + mFrame.height() + " format="
7369 + mAttrs.format + " flags=" + flags);
7370
7371 int w = mFrame.width();
7372 int h = mFrame.height();
7373 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7374 // for a scaled surface, we always want the requested
7375 // size.
7376 w = mRequestedWidth;
7377 h = mRequestedHeight;
7378 }
7379
Romain Guy9825ec62009-10-01 00:58:09 -07007380 // Something is wrong and SurfaceFlinger will not like this,
7381 // try to revert to sane values
7382 if (w <= 0) w = 1;
7383 if (h <= 0) h = 1;
7384
Dianne Hackborn16064f92010-03-25 00:47:24 -07007385 mSurfaceShown = false;
7386 mSurfaceLayer = 0;
7387 mSurfaceAlpha = 1;
7388 mSurfaceX = 0;
7389 mSurfaceY = 0;
7390 mSurfaceW = w;
7391 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 try {
7393 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007394 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007395 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007397 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007398 + mSurface + " IN SESSION "
7399 + mSession.mSurfaceSession
7400 + ": pid=" + mSession.mPid + " format="
7401 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007402 + Integer.toHexString(flags)
7403 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007404 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007405 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 reclaimSomeSurfaceMemoryLocked(this, "create");
7407 return null;
7408 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007409 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 return null;
7411 }
Romain Guy06882f82009-06-10 13:36:04 -07007412
Joe Onorato8a9b2202010-02-26 18:56:32 -08007413 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 TAG, "Got surface: " + mSurface
7415 + ", set left=" + mFrame.left + " top=" + mFrame.top
7416 + ", animLayer=" + mAnimLayer);
7417 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007418 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007419 if (SHOW_TRANSACTIONS) logSurface(this,
7420 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7421 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7422 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 }
7424 Surface.openTransaction();
7425 try {
7426 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007427 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007428 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007429 mSurface.setPosition(mSurfaceX, mSurfaceY);
7430 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007432 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 mSurface.hide();
7434 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007435 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 mSurface.setFlags(Surface.SURFACE_DITHER,
7437 Surface.SURFACE_DITHER);
7438 }
7439 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007440 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7442 }
7443 mLastHidden = true;
7444 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007445 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 Surface.closeTransaction();
7447 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007448 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 TAG, "Created surface " + this);
7450 }
7451 return mSurface;
7452 }
Romain Guy06882f82009-06-10 13:36:04 -07007453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 void destroySurfaceLocked() {
7455 // Window is no longer on-screen, so can no longer receive
7456 // key events... if we were waiting for it to finish
7457 // handling a key event, the wait is over!
7458 mKeyWaiter.finishedKey(mSession, mClient, true,
7459 KeyWaiter.RETURN_NOTHING);
7460 mKeyWaiter.releasePendingPointerLocked(mSession);
7461 mKeyWaiter.releasePendingTrackballLocked(mSession);
7462
7463 if (mAppToken != null && this == mAppToken.startingWindow) {
7464 mAppToken.startingDisplayed = false;
7465 }
Romain Guy06882f82009-06-10 13:36:04 -07007466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007468 mDrawPending = false;
7469 mCommitDrawPending = false;
7470 mReadyToShow = false;
7471
7472 int i = mChildWindows.size();
7473 while (i > 0) {
7474 i--;
7475 WindowState c = (WindowState)mChildWindows.get(i);
7476 c.mAttachedHidden = true;
7477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007478
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007479 if (mReportDestroySurface) {
7480 mReportDestroySurface = false;
7481 mSurfacePendingDestroy = true;
7482 try {
7483 mClient.dispatchGetNewSurface();
7484 // We'll really destroy on the next time around.
7485 return;
7486 } catch (RemoteException e) {
7487 }
7488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007491 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007492 RuntimeException e = null;
7493 if (!HIDE_STACK_CRAWLS) {
7494 e = new RuntimeException();
7495 e.fillInStackTrace();
7496 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007497 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007498 + mSurface + ", session " + mSession, e);
7499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007501 RuntimeException e = null;
7502 if (!HIDE_STACK_CRAWLS) {
7503 e = new RuntimeException();
7504 e.fillInStackTrace();
7505 }
7506 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007508 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007510 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 + " surface " + mSurface + " session " + mSession
7512 + ": " + e.toString());
7513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007514
Dianne Hackborn16064f92010-03-25 00:47:24 -07007515 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 }
7518 }
7519
7520 boolean finishDrawingLocked() {
7521 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007522 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 TAG, "finishDrawingLocked: " + mSurface);
7524 mCommitDrawPending = true;
7525 mDrawPending = false;
7526 return true;
7527 }
7528 return false;
7529 }
7530
7531 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007532 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007533 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007535 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 }
7537 mCommitDrawPending = false;
7538 mReadyToShow = true;
7539 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7540 final AppWindowToken atoken = mAppToken;
7541 if (atoken == null || atoken.allDrawn || starting) {
7542 performShowLocked();
7543 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007544 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 }
7546
7547 // This must be called while inside a transaction.
7548 boolean performShowLocked() {
7549 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007550 RuntimeException e = null;
7551 if (!HIDE_STACK_CRAWLS) {
7552 e = new RuntimeException();
7553 e.fillInStackTrace();
7554 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007555 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7557 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7558 }
7559 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007560 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7561 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007562 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 + " during animation: policyVis=" + mPolicyVisibility
7564 + " attHidden=" + mAttachedHidden
7565 + " tok.hiddenRequested="
7566 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007567 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 + (mAppToken != null ? mAppToken.hidden : false)
7569 + " animating=" + mAnimating
7570 + " tok animating="
7571 + (mAppToken != null ? mAppToken.animating : false));
7572 if (!showSurfaceRobustlyLocked(this)) {
7573 return false;
7574 }
7575 mLastAlpha = -1;
7576 mHasDrawn = true;
7577 mLastHidden = false;
7578 mReadyToShow = false;
7579 enableScreenIfNeededLocked();
7580
7581 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 int i = mChildWindows.size();
7584 while (i > 0) {
7585 i--;
7586 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007587 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007589 if (c.mSurface != null) {
7590 c.performShowLocked();
7591 // It hadn't been shown, which means layout not
7592 // performed on it, so now we want to make sure to
7593 // do a layout. If called from within the transaction
7594 // loop, this will cause it to restart with a new
7595 // layout.
7596 mLayoutNeeded = true;
7597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 }
7599 }
Romain Guy06882f82009-06-10 13:36:04 -07007600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 if (mAttrs.type != TYPE_APPLICATION_STARTING
7602 && mAppToken != null) {
7603 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007604
Dianne Hackborn248b1882009-09-16 16:46:44 -07007605 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007606 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007607 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007609 // If this initial window is animating, stop it -- we
7610 // will do an animation to reveal it from behind the
7611 // starting window, so there is no need for it to also
7612 // be doing its own stuff.
7613 if (mAnimation != null) {
7614 mAnimation = null;
7615 // Make sure we clean up the animation.
7616 mAnimating = true;
7617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 mFinishedStarting.add(mAppToken);
7619 mH.sendEmptyMessage(H.FINISHED_STARTING);
7620 }
7621 mAppToken.updateReportedVisibilityLocked();
7622 }
7623 }
7624 return true;
7625 }
Romain Guy06882f82009-06-10 13:36:04 -07007626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 // This must be called while inside a transaction. Returns true if
7628 // there is more animation to run.
7629 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007630 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7634 mHasTransformation = true;
7635 mHasLocalTransformation = true;
7636 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007637 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 TAG, "Starting animation in " + this +
7639 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7640 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7641 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7642 mAnimation.setStartTime(currentTime);
7643 mLocalAnimating = true;
7644 mAnimating = true;
7645 }
7646 mTransformation.clear();
7647 final boolean more = mAnimation.getTransformation(
7648 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007649 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 TAG, "Stepped animation in " + this +
7651 ": more=" + more + ", xform=" + mTransformation);
7652 if (more) {
7653 // we're not done!
7654 return true;
7655 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007656 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 TAG, "Finished animation in " + this +
7658 " @ " + currentTime);
7659 mAnimation = null;
7660 //WindowManagerService.this.dump();
7661 }
7662 mHasLocalTransformation = false;
7663 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007664 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 // When our app token is animating, we kind-of pretend like
7666 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7667 // part of this check means that we will only do this if
7668 // our window is not currently exiting, or it is not
7669 // locally animating itself. The idea being that one that
7670 // is exiting and doing a local animation should be removed
7671 // once that animation is done.
7672 mAnimating = true;
7673 mHasTransformation = true;
7674 mTransformation.clear();
7675 return false;
7676 } else if (mHasTransformation) {
7677 // Little trick to get through the path below to act like
7678 // we have finished an animation.
7679 mAnimating = true;
7680 } else if (isAnimating()) {
7681 mAnimating = true;
7682 }
7683 } else if (mAnimation != null) {
7684 // If the display is frozen, and there is a pending animation,
7685 // clear it and make sure we run the cleanup code.
7686 mAnimating = true;
7687 mLocalAnimating = true;
7688 mAnimation = null;
7689 }
Romain Guy06882f82009-06-10 13:36:04 -07007690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 if (!mAnimating && !mLocalAnimating) {
7692 return false;
7693 }
7694
Joe Onorato8a9b2202010-02-26 18:56:32 -08007695 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 TAG, "Animation done in " + this + ": exiting=" + mExiting
7697 + ", reportedVisible="
7698 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 mAnimating = false;
7701 mLocalAnimating = false;
7702 mAnimation = null;
7703 mAnimLayer = mLayer;
7704 if (mIsImWindow) {
7705 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007706 } else if (mIsWallpaper) {
7707 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007709 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 + " anim layer: " + mAnimLayer);
7711 mHasTransformation = false;
7712 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007713 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7714 if (DEBUG_VISIBILITY) {
7715 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7716 + mPolicyVisibilityAfterAnim);
7717 }
7718 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7719 if (!mPolicyVisibility) {
7720 if (mCurrentFocus == this) {
7721 mFocusMayChange = true;
7722 }
7723 // Window is no longer visible -- make sure if we were waiting
7724 // for it to be displayed before enabling the display, that
7725 // we allow the display to be enabled now.
7726 enableScreenIfNeededLocked();
7727 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 mTransformation.clear();
7730 if (mHasDrawn
7731 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7732 && mAppToken != null
7733 && mAppToken.firstWindowDrawn
7734 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007735 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 + mToken + ": first real window done animating");
7737 mFinishedStarting.add(mAppToken);
7738 mH.sendEmptyMessage(H.FINISHED_STARTING);
7739 }
Romain Guy06882f82009-06-10 13:36:04 -07007740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 finishExit();
7742
7743 if (mAppToken != null) {
7744 mAppToken.updateReportedVisibilityLocked();
7745 }
7746
7747 return false;
7748 }
7749
7750 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007751 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007752 TAG, "finishExit in " + this
7753 + ": exiting=" + mExiting
7754 + " remove=" + mRemoveOnExit
7755 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 final int N = mChildWindows.size();
7758 for (int i=0; i<N; i++) {
7759 ((WindowState)mChildWindows.get(i)).finishExit();
7760 }
Romain Guy06882f82009-06-10 13:36:04 -07007761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 if (!mExiting) {
7763 return;
7764 }
Romain Guy06882f82009-06-10 13:36:04 -07007765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766 if (isWindowAnimating()) {
7767 return;
7768 }
7769
Joe Onorato8a9b2202010-02-26 18:56:32 -08007770 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771 TAG, "Exit animation finished in " + this
7772 + ": remove=" + mRemoveOnExit);
7773 if (mSurface != null) {
7774 mDestroySurface.add(this);
7775 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007776 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007777 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 try {
7779 mSurface.hide();
7780 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007781 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 }
7783 mLastHidden = true;
7784 mKeyWaiter.releasePendingPointerLocked(mSession);
7785 }
7786 mExiting = false;
7787 if (mRemoveOnExit) {
7788 mPendingRemove.add(this);
7789 mRemoveOnExit = false;
7790 }
7791 }
Romain Guy06882f82009-06-10 13:36:04 -07007792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7794 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7795 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7796 if (dtdx < -.000001f || dtdx > .000001f) return false;
7797 if (dsdy < -.000001f || dsdy > .000001f) return false;
7798 return true;
7799 }
Romain Guy06882f82009-06-10 13:36:04 -07007800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 void computeShownFrameLocked() {
7802 final boolean selfTransformation = mHasLocalTransformation;
7803 Transformation attachedTransformation =
7804 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7805 ? mAttachedWindow.mTransformation : null;
7806 Transformation appTransformation =
7807 (mAppToken != null && mAppToken.hasTransformation)
7808 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007809
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007810 // Wallpapers are animated based on the "real" window they
7811 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007812 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007813 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007814 if (mWallpaperTarget.mHasLocalTransformation &&
7815 mWallpaperTarget.mAnimation != null &&
7816 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007817 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007818 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007819 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007820 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007821 }
7822 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007823 mWallpaperTarget.mAppToken.hasTransformation &&
7824 mWallpaperTarget.mAppToken.animation != null &&
7825 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007826 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007827 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007828 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007829 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007830 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 if (selfTransformation || attachedTransformation != null
7834 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007835 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007836 final Rect frame = mFrame;
7837 final float tmpFloats[] = mTmpFloats;
7838 final Matrix tmpMatrix = mTmpMatrix;
7839
7840 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007841 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007843 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007845 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007847 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007850 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 }
7852
7853 // "convert" it into SurfaceFlinger's format
7854 // (a 2x2 matrix + an offset)
7855 // Here we must not transform the position of the surface
7856 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007857 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007859 tmpMatrix.getValues(tmpFloats);
7860 mDsDx = tmpFloats[Matrix.MSCALE_X];
7861 mDtDx = tmpFloats[Matrix.MSKEW_X];
7862 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7863 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007864 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7865 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 int w = frame.width();
7867 int h = frame.height();
7868 mShownFrame.set(x, y, x+w, y+h);
7869
7870 // Now set the alpha... but because our current hardware
7871 // can't do alpha transformation on a non-opaque surface,
7872 // turn it off if we are running an animation that is also
7873 // transforming since it is more important to have that
7874 // animation be smooth.
7875 mShownAlpha = mAlpha;
7876 if (!mLimitedAlphaCompositing
7877 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7878 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7879 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007880 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 if (selfTransformation) {
7882 mShownAlpha *= mTransformation.getAlpha();
7883 }
7884 if (attachedTransformation != null) {
7885 mShownAlpha *= attachedTransformation.getAlpha();
7886 }
7887 if (appTransformation != null) {
7888 mShownAlpha *= appTransformation.getAlpha();
7889 }
7890 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007891 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 }
Romain Guy06882f82009-06-10 13:36:04 -07007893
Joe Onorato8a9b2202010-02-26 18:56:32 -08007894 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 TAG, "Continuing animation in " + this +
7896 ": " + mShownFrame +
7897 ", alpha=" + mTransformation.getAlpha());
7898 return;
7899 }
Romain Guy06882f82009-06-10 13:36:04 -07007900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007902 if (mXOffset != 0 || mYOffset != 0) {
7903 mShownFrame.offset(mXOffset, mYOffset);
7904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 mShownAlpha = mAlpha;
7906 mDsDx = 1;
7907 mDtDx = 0;
7908 mDsDy = 0;
7909 mDtDy = 1;
7910 }
Romain Guy06882f82009-06-10 13:36:04 -07007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 /**
7913 * Is this window visible? It is not visible if there is no
7914 * surface, or we are in the process of running an exit animation
7915 * that will remove the surface, or its app token has been hidden.
7916 */
7917 public boolean isVisibleLw() {
7918 final AppWindowToken atoken = mAppToken;
7919 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7920 && (atoken == null || !atoken.hiddenRequested)
7921 && !mExiting && !mDestroying;
7922 }
7923
7924 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007925 * Like {@link #isVisibleLw}, but also counts a window that is currently
7926 * "hidden" behind the keyguard as visible. This allows us to apply
7927 * things like window flags that impact the keyguard.
7928 * XXX I am starting to think we need to have ANOTHER visibility flag
7929 * for this "hidden behind keyguard" state rather than overloading
7930 * mPolicyVisibility. Ungh.
7931 */
7932 public boolean isVisibleOrBehindKeyguardLw() {
7933 final AppWindowToken atoken = mAppToken;
7934 return mSurface != null && !mAttachedHidden
7935 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07007936 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007937 && !mExiting && !mDestroying;
7938 }
7939
7940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 * Is this window visible, ignoring its app token? It is not visible
7942 * if there is no surface, or we are in the process of running an exit animation
7943 * that will remove the surface.
7944 */
7945 public boolean isWinVisibleLw() {
7946 final AppWindowToken atoken = mAppToken;
7947 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7948 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7949 && !mExiting && !mDestroying;
7950 }
7951
7952 /**
7953 * The same as isVisible(), but follows the current hidden state of
7954 * the associated app token, not the pending requested hidden state.
7955 */
7956 boolean isVisibleNow() {
7957 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007958 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 }
7960
7961 /**
7962 * Same as isVisible(), but we also count it as visible between the
7963 * call to IWindowSession.add() and the first relayout().
7964 */
7965 boolean isVisibleOrAdding() {
7966 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007967 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7969 && mPolicyVisibility && !mAttachedHidden
7970 && (atoken == null || !atoken.hiddenRequested)
7971 && !mExiting && !mDestroying;
7972 }
7973
7974 /**
7975 * Is this window currently on-screen? It is on-screen either if it
7976 * is visible or it is currently running an animation before no longer
7977 * being visible.
7978 */
7979 boolean isOnScreen() {
7980 final AppWindowToken atoken = mAppToken;
7981 if (atoken != null) {
7982 return mSurface != null && mPolicyVisibility && !mDestroying
7983 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007984 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 } else {
7986 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07007987 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 }
7989 }
Romain Guy06882f82009-06-10 13:36:04 -07007990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 /**
7992 * Like isOnScreen(), but we don't return true if the window is part
7993 * of a transition that has not yet been started.
7994 */
7995 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007996 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007997 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07007998 return false;
7999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008001 final boolean animating = atoken != null
8002 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008004 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8005 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008006 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 }
8008
8009 /** Is the window or its container currently animating? */
8010 boolean isAnimating() {
8011 final WindowState attached = mAttachedWindow;
8012 final AppWindowToken atoken = mAppToken;
8013 return mAnimation != null
8014 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008015 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 (atoken.animation != null
8017 || atoken.inPendingTransaction));
8018 }
8019
8020 /** Is this window currently animating? */
8021 boolean isWindowAnimating() {
8022 return mAnimation != null;
8023 }
8024
8025 /**
8026 * Like isOnScreen, but returns false if the surface hasn't yet
8027 * been drawn.
8028 */
8029 public boolean isDisplayedLw() {
8030 final AppWindowToken atoken = mAppToken;
8031 return mSurface != null && mPolicyVisibility && !mDestroying
8032 && !mDrawPending && !mCommitDrawPending
8033 && ((!mAttachedHidden &&
8034 (atoken == null || !atoken.hiddenRequested))
8035 || mAnimating);
8036 }
8037
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008038 /**
8039 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008040 * complete UI in to. Note that this returns true if the orientation
8041 * is changing even if the window hasn't redrawn because we don't want
8042 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008043 */
8044 public boolean isDrawnLw() {
8045 final AppWindowToken atoken = mAppToken;
8046 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008047 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008048 }
8049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8051 boolean shownFrame, boolean onlyOpaque) {
8052 if (mSurface == null) {
8053 return false;
8054 }
8055 if (mAppToken != null && !mAppToken.appFullscreen) {
8056 return false;
8057 }
8058 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8059 return false;
8060 }
8061 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008062
8063 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8064 return frame.left <= mCompatibleScreenFrame.left &&
8065 frame.top <= mCompatibleScreenFrame.top &&
8066 frame.right >= mCompatibleScreenFrame.right &&
8067 frame.bottom >= mCompatibleScreenFrame.bottom;
8068 } else {
8069 return frame.left <= 0 && frame.top <= 0
8070 && frame.right >= screenWidth
8071 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
Romain Guy06882f82009-06-10 13:36:04 -07008074
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008075 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008076 * Return true if the window is opaque and fully drawn. This indicates
8077 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008078 */
8079 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008080 return (mAttrs.format == PixelFormat.OPAQUE
8081 || mAttrs.type == TYPE_WALLPAPER)
8082 && mSurface != null && mAnimation == null
8083 && (mAppToken == null || mAppToken.animation == null)
8084 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008085 }
8086
8087 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8088 return
8089 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008090 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8091 // only if it's visible
8092 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008093 // and only if the application fills the compatible screen
8094 mFrame.left <= mCompatibleScreenFrame.left &&
8095 mFrame.top <= mCompatibleScreenFrame.top &&
8096 mFrame.right >= mCompatibleScreenFrame.right &&
8097 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008098 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008099 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008100 }
8101
8102 boolean isFullscreen(int screenWidth, int screenHeight) {
8103 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008104 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 }
8106
8107 void removeLocked() {
8108 if (mAttachedWindow != null) {
8109 mAttachedWindow.mChildWindows.remove(this);
8110 }
8111 destroySurfaceLocked();
8112 mSession.windowRemovedLocked();
8113 try {
8114 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8115 } catch (RuntimeException e) {
8116 // Ignore if it has already been removed (usually because
8117 // we are doing this as part of processing a death note.)
8118 }
8119 }
8120
8121 private class DeathRecipient implements IBinder.DeathRecipient {
8122 public void binderDied() {
8123 try {
8124 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008125 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008126 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 if (win != null) {
8128 removeWindowLocked(mSession, win);
8129 }
8130 }
8131 } catch (IllegalArgumentException ex) {
8132 // This will happen if the window has already been
8133 // removed.
8134 }
8135 }
8136 }
8137
8138 /** Returns true if this window desires key events. */
8139 public final boolean canReceiveKeys() {
8140 return isVisibleOrAdding()
8141 && (mViewVisibility == View.VISIBLE)
8142 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8143 }
8144
8145 public boolean hasDrawnLw() {
8146 return mHasDrawn;
8147 }
8148
8149 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008150 return showLw(doAnimation, true);
8151 }
8152
8153 boolean showLw(boolean doAnimation, boolean requestAnim) {
8154 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8155 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008157 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008158 if (doAnimation) {
8159 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8160 + mPolicyVisibility + " mAnimation=" + mAnimation);
8161 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8162 doAnimation = false;
8163 } else if (mPolicyVisibility && mAnimation == null) {
8164 // Check for the case where we are currently visible and
8165 // not animating; we do not want to do animation at such a
8166 // point to become visible when we already are.
8167 doAnimation = false;
8168 }
8169 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008170 mPolicyVisibility = true;
8171 mPolicyVisibilityAfterAnim = true;
8172 if (doAnimation) {
8173 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8174 }
8175 if (requestAnim) {
8176 requestAnimationLocked(0);
8177 }
8178 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008182 return hideLw(doAnimation, true);
8183 }
8184
8185 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008186 if (doAnimation) {
8187 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8188 doAnimation = false;
8189 }
8190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8192 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008193 if (!current) {
8194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008196 if (doAnimation) {
8197 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8198 if (mAnimation == null) {
8199 doAnimation = false;
8200 }
8201 }
8202 if (doAnimation) {
8203 mPolicyVisibilityAfterAnim = false;
8204 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008205 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008206 mPolicyVisibilityAfterAnim = false;
8207 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008208 // Window is no longer visible -- make sure if we were waiting
8209 // for it to be displayed before enabling the display, that
8210 // we allow the display to be enabled now.
8211 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008212 if (mCurrentFocus == this) {
8213 mFocusMayChange = true;
8214 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008215 }
8216 if (requestAnim) {
8217 requestAnimationLocked(0);
8218 }
8219 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 }
8221
8222 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008223 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8224 pw.print(" mClient="); pw.println(mClient.asBinder());
8225 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8226 if (mAttachedWindow != null || mLayoutAttached) {
8227 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8228 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8229 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008230 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8231 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8232 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008233 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8234 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008235 }
8236 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8237 pw.print(" mSubLayer="); pw.print(mSubLayer);
8238 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8239 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8240 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8241 pw.print("="); pw.print(mAnimLayer);
8242 pw.print(" mLastLayer="); pw.println(mLastLayer);
8243 if (mSurface != null) {
8244 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008245 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8246 pw.print(" layer="); pw.print(mSurfaceLayer);
8247 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8248 pw.print(" rect=("); pw.print(mSurfaceX);
8249 pw.print(","); pw.print(mSurfaceY);
8250 pw.print(") "); pw.print(mSurfaceW);
8251 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008252 }
8253 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8254 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8255 if (mAppToken != null) {
8256 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8257 }
8258 if (mTargetAppToken != null) {
8259 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8260 }
8261 pw.print(prefix); pw.print("mViewVisibility=0x");
8262 pw.print(Integer.toHexString(mViewVisibility));
8263 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008264 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8265 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008266 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8267 pw.print(prefix); pw.print("mPolicyVisibility=");
8268 pw.print(mPolicyVisibility);
8269 pw.print(" mPolicyVisibilityAfterAnim=");
8270 pw.print(mPolicyVisibilityAfterAnim);
8271 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8272 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008273 if (!mRelayoutCalled) {
8274 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8275 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008276 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008277 pw.print(" h="); pw.print(mRequestedHeight);
8278 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008279 if (mXOffset != 0 || mYOffset != 0) {
8280 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8281 pw.print(" y="); pw.println(mYOffset);
8282 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008283 pw.print(prefix); pw.print("mGivenContentInsets=");
8284 mGivenContentInsets.printShortString(pw);
8285 pw.print(" mGivenVisibleInsets=");
8286 mGivenVisibleInsets.printShortString(pw);
8287 pw.println();
8288 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8289 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8290 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8291 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008292 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008293 pw.print(prefix); pw.print("mShownFrame=");
8294 mShownFrame.printShortString(pw);
8295 pw.print(" last="); mLastShownFrame.printShortString(pw);
8296 pw.println();
8297 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8298 pw.print(" last="); mLastFrame.printShortString(pw);
8299 pw.println();
8300 pw.print(prefix); pw.print("mContainingFrame=");
8301 mContainingFrame.printShortString(pw);
8302 pw.print(" mDisplayFrame=");
8303 mDisplayFrame.printShortString(pw);
8304 pw.println();
8305 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8306 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8307 pw.println();
8308 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8309 pw.print(" last="); mLastContentInsets.printShortString(pw);
8310 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8311 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8312 pw.println();
8313 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8314 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8315 pw.print(" mAlpha="); pw.print(mAlpha);
8316 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8317 }
8318 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8319 || mAnimation != null) {
8320 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8321 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8322 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8323 pw.print(" mAnimation="); pw.println(mAnimation);
8324 }
8325 if (mHasTransformation || mHasLocalTransformation) {
8326 pw.print(prefix); pw.print("XForm: has=");
8327 pw.print(mHasTransformation);
8328 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8329 pw.print(" "); mTransformation.printShortString(pw);
8330 pw.println();
8331 }
8332 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8333 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8334 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8335 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8336 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8337 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8338 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8339 pw.print(" mDestroying="); pw.print(mDestroying);
8340 pw.print(" mRemoved="); pw.println(mRemoved);
8341 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008342 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008343 pw.print(prefix); pw.print("mOrientationChanging=");
8344 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008345 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8346 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008347 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008348 if (mHScale != 1 || mVScale != 1) {
8349 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8350 pw.print(" mVScale="); pw.println(mVScale);
8351 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008352 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008353 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8354 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8355 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008356 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8357 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8358 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 }
8361
8362 @Override
8363 public String toString() {
8364 return "Window{"
8365 + Integer.toHexString(System.identityHashCode(this))
8366 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8367 }
8368 }
Romain Guy06882f82009-06-10 13:36:04 -07008369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 // -------------------------------------------------------------
8371 // Window Token State
8372 // -------------------------------------------------------------
8373
8374 class WindowToken {
8375 // The actual token.
8376 final IBinder token;
8377
8378 // The type of window this token is for, as per WindowManager.LayoutParams.
8379 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 // Set if this token was explicitly added by a client, so should
8382 // not be removed when all windows are removed.
8383 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008384
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008385 // For printing.
8386 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 // If this is an AppWindowToken, this is non-null.
8389 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 // All of the windows associated with this token.
8392 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8393
8394 // Is key dispatching paused for this token?
8395 boolean paused = false;
8396
8397 // Should this token's windows be hidden?
8398 boolean hidden;
8399
8400 // Temporary for finding which tokens no longer have visible windows.
8401 boolean hasVisible;
8402
Dianne Hackborna8f60182009-09-01 19:01:50 -07008403 // Set to true when this token is in a pending transaction where it
8404 // will be shown.
8405 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008406
Dianne Hackborna8f60182009-09-01 19:01:50 -07008407 // Set to true when this token is in a pending transaction where it
8408 // will be hidden.
8409 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008410
Dianne Hackborna8f60182009-09-01 19:01:50 -07008411 // Set to true when this token is in a pending transaction where its
8412 // windows will be put to the bottom of the list.
8413 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008414
Dianne Hackborna8f60182009-09-01 19:01:50 -07008415 // Set to true when this token is in a pending transaction where its
8416 // windows will be put to the top of the list.
8417 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 WindowToken(IBinder _token, int type, boolean _explicit) {
8420 token = _token;
8421 windowType = type;
8422 explicit = _explicit;
8423 }
8424
8425 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008426 pw.print(prefix); pw.print("token="); pw.println(token);
8427 pw.print(prefix); pw.print("windows="); pw.println(windows);
8428 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8429 pw.print(" hidden="); pw.print(hidden);
8430 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008431 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8432 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8433 pw.print(" waitingToHide="); pw.print(waitingToHide);
8434 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8435 pw.print(" sendingToTop="); pw.println(sendingToTop);
8436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 }
8438
8439 @Override
8440 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008441 if (stringName == null) {
8442 StringBuilder sb = new StringBuilder();
8443 sb.append("WindowToken{");
8444 sb.append(Integer.toHexString(System.identityHashCode(this)));
8445 sb.append(" token="); sb.append(token); sb.append('}');
8446 stringName = sb.toString();
8447 }
8448 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449 }
8450 };
8451
8452 class AppWindowToken extends WindowToken {
8453 // Non-null only for application tokens.
8454 final IApplicationToken appToken;
8455
8456 // All of the windows and child windows that are included in this
8457 // application token. Note this list is NOT sorted!
8458 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8459
8460 int groupId = -1;
8461 boolean appFullscreen;
8462 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 // These are used for determining when all windows associated with
8465 // an activity have been drawn, so they can be made visible together
8466 // at the same time.
8467 int lastTransactionSequence = mTransactionSequence-1;
8468 int numInterestingWindows;
8469 int numDrawnWindows;
8470 boolean inPendingTransaction;
8471 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 // Is this token going to be hidden in a little while? If so, it
8474 // won't be taken into account for setting the screen orientation.
8475 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 // Is this window's surface needed? This is almost like hidden, except
8478 // it will sometimes be true a little earlier: when the token has
8479 // been shown, but is still waiting for its app transition to execute
8480 // before making its windows shown.
8481 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 // Have we told the window clients to hide themselves?
8484 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 // Last visibility state we reported to the app token.
8487 boolean reportedVisible;
8488
8489 // Set to true when the token has been removed from the window mgr.
8490 boolean removed;
8491
8492 // Have we been asked to have this token keep the screen frozen?
8493 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008495 boolean animating;
8496 Animation animation;
8497 boolean hasTransformation;
8498 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 // Offset to the window of all layers in the token, for use by
8501 // AppWindowToken animations.
8502 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 // Information about an application starting window if displayed.
8505 StartingData startingData;
8506 WindowState startingWindow;
8507 View startingView;
8508 boolean startingDisplayed;
8509 boolean startingMoved;
8510 boolean firstWindowDrawn;
8511
8512 AppWindowToken(IApplicationToken _token) {
8513 super(_token.asBinder(),
8514 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8515 appWindowToken = this;
8516 appToken = _token;
8517 }
Romain Guy06882f82009-06-10 13:36:04 -07008518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008520 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 TAG, "Setting animation in " + this + ": " + anim);
8522 animation = anim;
8523 animating = false;
8524 anim.restrictDuration(MAX_ANIMATION_DURATION);
8525 anim.scaleCurrentDuration(mTransitionAnimationScale);
8526 int zorder = anim.getZAdjustment();
8527 int adj = 0;
8528 if (zorder == Animation.ZORDER_TOP) {
8529 adj = TYPE_LAYER_OFFSET;
8530 } else if (zorder == Animation.ZORDER_BOTTOM) {
8531 adj = -TYPE_LAYER_OFFSET;
8532 }
Romain Guy06882f82009-06-10 13:36:04 -07008533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 if (animLayerAdjustment != adj) {
8535 animLayerAdjustment = adj;
8536 updateLayers();
8537 }
8538 }
Romain Guy06882f82009-06-10 13:36:04 -07008539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 public void setDummyAnimation() {
8541 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008542 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 TAG, "Setting dummy animation in " + this);
8544 animation = sDummyAnimation;
8545 }
8546 }
8547
8548 public void clearAnimation() {
8549 if (animation != null) {
8550 animation = null;
8551 animating = true;
8552 }
8553 }
Romain Guy06882f82009-06-10 13:36:04 -07008554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 void updateLayers() {
8556 final int N = allAppWindows.size();
8557 final int adj = animLayerAdjustment;
8558 for (int i=0; i<N; i++) {
8559 WindowState w = allAppWindows.get(i);
8560 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008561 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 + w.mAnimLayer);
8563 if (w == mInputMethodTarget) {
8564 setInputMethodAnimLayerAdjustment(adj);
8565 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008566 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008567 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 }
8570 }
Romain Guy06882f82009-06-10 13:36:04 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 void sendAppVisibilityToClients() {
8573 final int N = allAppWindows.size();
8574 for (int i=0; i<N; i++) {
8575 WindowState win = allAppWindows.get(i);
8576 if (win == startingWindow && clientHidden) {
8577 // Don't hide the starting window.
8578 continue;
8579 }
8580 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008581 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 "Setting visibility of " + win + ": " + (!clientHidden));
8583 win.mClient.dispatchAppVisibility(!clientHidden);
8584 } catch (RemoteException e) {
8585 }
8586 }
8587 }
Romain Guy06882f82009-06-10 13:36:04 -07008588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 void showAllWindowsLocked() {
8590 final int NW = allAppWindows.size();
8591 for (int i=0; i<NW; i++) {
8592 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008593 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 "performing show on: " + w);
8595 w.performShowLocked();
8596 }
8597 }
Romain Guy06882f82009-06-10 13:36:04 -07008598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 // This must be called while inside a transaction.
8600 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008601 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 if (animation == sDummyAnimation) {
8605 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008606 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 // when it is really time to animate, this will be set to
8608 // a real animation and the next call will execute normally.
8609 return false;
8610 }
Romain Guy06882f82009-06-10 13:36:04 -07008611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8613 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008614 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008615 TAG, "Starting animation in " + this +
8616 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8617 + " scale=" + mTransitionAnimationScale
8618 + " allDrawn=" + allDrawn + " animating=" + animating);
8619 animation.initialize(dw, dh, dw, dh);
8620 animation.setStartTime(currentTime);
8621 animating = true;
8622 }
8623 transformation.clear();
8624 final boolean more = animation.getTransformation(
8625 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008626 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 TAG, "Stepped animation in " + this +
8628 ": more=" + more + ", xform=" + transformation);
8629 if (more) {
8630 // we're done!
8631 hasTransformation = true;
8632 return true;
8633 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008634 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 TAG, "Finished animation in " + this +
8636 " @ " + currentTime);
8637 animation = null;
8638 }
8639 } else if (animation != null) {
8640 // If the display is frozen, and there is a pending animation,
8641 // clear it and make sure we run the cleanup code.
8642 animating = true;
8643 animation = null;
8644 }
8645
8646 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 if (!animating) {
8649 return false;
8650 }
8651
8652 clearAnimation();
8653 animating = false;
8654 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8655 moveInputMethodWindowsIfNeededLocked(true);
8656 }
Romain Guy06882f82009-06-10 13:36:04 -07008657
Joe Onorato8a9b2202010-02-26 18:56:32 -08008658 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 TAG, "Animation done in " + this
8660 + ": reportedVisible=" + reportedVisible);
8661
8662 transformation.clear();
8663 if (animLayerAdjustment != 0) {
8664 animLayerAdjustment = 0;
8665 updateLayers();
8666 }
Romain Guy06882f82009-06-10 13:36:04 -07008667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 final int N = windows.size();
8669 for (int i=0; i<N; i++) {
8670 ((WindowState)windows.get(i)).finishExit();
8671 }
8672 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 return false;
8675 }
8676
8677 void updateReportedVisibilityLocked() {
8678 if (appToken == null) {
8679 return;
8680 }
Romain Guy06882f82009-06-10 13:36:04 -07008681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 int numInteresting = 0;
8683 int numVisible = 0;
8684 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008685
Joe Onorato8a9b2202010-02-26 18:56:32 -08008686 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687 final int N = allAppWindows.size();
8688 for (int i=0; i<N; i++) {
8689 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008690 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008691 || win.mViewVisibility != View.VISIBLE
8692 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693 continue;
8694 }
8695 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008696 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008697 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008699 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008700 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 + " pv=" + win.mPolicyVisibility
8702 + " dp=" + win.mDrawPending
8703 + " cdp=" + win.mCommitDrawPending
8704 + " ah=" + win.mAttachedHidden
8705 + " th="
8706 + (win.mAppToken != null
8707 ? win.mAppToken.hiddenRequested : false)
8708 + " a=" + win.mAnimating);
8709 }
8710 }
8711 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008712 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008713 if (!win.isAnimating()) {
8714 numVisible++;
8715 }
8716 nowGone = false;
8717 } else if (win.isAnimating()) {
8718 nowGone = false;
8719 }
8720 }
Romain Guy06882f82009-06-10 13:36:04 -07008721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008723 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 + numInteresting + " visible=" + numVisible);
8725 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008726 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008727 TAG, "Visibility changed in " + this
8728 + ": vis=" + nowVisible);
8729 reportedVisible = nowVisible;
8730 Message m = mH.obtainMessage(
8731 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8732 nowVisible ? 1 : 0,
8733 nowGone ? 1 : 0,
8734 this);
8735 mH.sendMessage(m);
8736 }
8737 }
Romain Guy06882f82009-06-10 13:36:04 -07008738
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008739 WindowState findMainWindow() {
8740 int j = windows.size();
8741 while (j > 0) {
8742 j--;
8743 WindowState win = windows.get(j);
8744 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8745 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8746 return win;
8747 }
8748 }
8749 return null;
8750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 void dump(PrintWriter pw, String prefix) {
8753 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008754 if (appToken != null) {
8755 pw.print(prefix); pw.println("app=true");
8756 }
8757 if (allAppWindows.size() > 0) {
8758 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8759 }
8760 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008761 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008762 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8763 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8764 pw.print(" clientHidden="); pw.print(clientHidden);
8765 pw.print(" willBeHidden="); pw.print(willBeHidden);
8766 pw.print(" reportedVisible="); pw.println(reportedVisible);
8767 if (paused || freezingScreen) {
8768 pw.print(prefix); pw.print("paused="); pw.print(paused);
8769 pw.print(" freezingScreen="); pw.println(freezingScreen);
8770 }
8771 if (numInterestingWindows != 0 || numDrawnWindows != 0
8772 || inPendingTransaction || allDrawn) {
8773 pw.print(prefix); pw.print("numInterestingWindows=");
8774 pw.print(numInterestingWindows);
8775 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8776 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8777 pw.print(" allDrawn="); pw.println(allDrawn);
8778 }
8779 if (animating || animation != null) {
8780 pw.print(prefix); pw.print("animating="); pw.print(animating);
8781 pw.print(" animation="); pw.println(animation);
8782 }
8783 if (animLayerAdjustment != 0) {
8784 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8785 }
8786 if (hasTransformation) {
8787 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8788 pw.print(" transformation="); transformation.printShortString(pw);
8789 pw.println();
8790 }
8791 if (startingData != null || removed || firstWindowDrawn) {
8792 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8793 pw.print(" removed="); pw.print(removed);
8794 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8795 }
8796 if (startingWindow != null || startingView != null
8797 || startingDisplayed || startingMoved) {
8798 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8799 pw.print(" startingView="); pw.print(startingView);
8800 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8801 pw.print(" startingMoved"); pw.println(startingMoved);
8802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 }
8804
8805 @Override
8806 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008807 if (stringName == null) {
8808 StringBuilder sb = new StringBuilder();
8809 sb.append("AppWindowToken{");
8810 sb.append(Integer.toHexString(System.identityHashCode(this)));
8811 sb.append(" token="); sb.append(token); sb.append('}');
8812 stringName = sb.toString();
8813 }
8814 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008815 }
8816 }
Romain Guy06882f82009-06-10 13:36:04 -07008817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 // -------------------------------------------------------------
8819 // DummyAnimation
8820 // -------------------------------------------------------------
8821
8822 // This is an animation that does nothing: it just immediately finishes
8823 // itself every time it is called. It is used as a stub animation in cases
8824 // where we want to synchronize multiple things that may be animating.
8825 static final class DummyAnimation extends Animation {
8826 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8827 return false;
8828 }
8829 }
8830 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 // -------------------------------------------------------------
8833 // Async Handler
8834 // -------------------------------------------------------------
8835
8836 static final class StartingData {
8837 final String pkg;
8838 final int theme;
8839 final CharSequence nonLocalizedLabel;
8840 final int labelRes;
8841 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8844 int _labelRes, int _icon) {
8845 pkg = _pkg;
8846 theme = _theme;
8847 nonLocalizedLabel = _nonLocalizedLabel;
8848 labelRes = _labelRes;
8849 icon = _icon;
8850 }
8851 }
8852
8853 private final class H extends Handler {
8854 public static final int REPORT_FOCUS_CHANGE = 2;
8855 public static final int REPORT_LOSING_FOCUS = 3;
8856 public static final int ANIMATE = 4;
8857 public static final int ADD_STARTING = 5;
8858 public static final int REMOVE_STARTING = 6;
8859 public static final int FINISHED_STARTING = 7;
8860 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8862 public static final int HOLD_SCREEN_CHANGED = 12;
8863 public static final int APP_TRANSITION_TIMEOUT = 13;
8864 public static final int PERSIST_ANIMATION_SCALE = 14;
8865 public static final int FORCE_GC = 15;
8866 public static final int ENABLE_SCREEN = 16;
8867 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008868 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008870 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 public H() {
8873 }
Romain Guy06882f82009-06-10 13:36:04 -07008874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 @Override
8876 public void handleMessage(Message msg) {
8877 switch (msg.what) {
8878 case REPORT_FOCUS_CHANGE: {
8879 WindowState lastFocus;
8880 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882 synchronized(mWindowMap) {
8883 lastFocus = mLastFocus;
8884 newFocus = mCurrentFocus;
8885 if (lastFocus == newFocus) {
8886 // Focus is not changing, so nothing to do.
8887 return;
8888 }
8889 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008890 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891 // + " to " + newFocus);
8892 if (newFocus != null && lastFocus != null
8893 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008894 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 mLosingFocus.add(lastFocus);
8896 lastFocus = null;
8897 }
8898 }
8899
8900 if (lastFocus != newFocus) {
8901 //System.out.println("Changing focus from " + lastFocus
8902 // + " to " + newFocus);
8903 if (newFocus != null) {
8904 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008905 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8907 } catch (RemoteException e) {
8908 // Ignore if process has died.
8909 }
8910 }
8911
8912 if (lastFocus != null) {
8913 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008914 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8916 } catch (RemoteException e) {
8917 // Ignore if process has died.
8918 }
8919 }
8920 }
8921 } break;
8922
8923 case REPORT_LOSING_FOCUS: {
8924 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 synchronized(mWindowMap) {
8927 losers = mLosingFocus;
8928 mLosingFocus = new ArrayList<WindowState>();
8929 }
8930
8931 final int N = losers.size();
8932 for (int i=0; i<N; i++) {
8933 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008934 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008935 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8936 } catch (RemoteException e) {
8937 // Ignore if process has died.
8938 }
8939 }
8940 } break;
8941
8942 case ANIMATE: {
8943 synchronized(mWindowMap) {
8944 mAnimationPending = false;
8945 performLayoutAndPlaceSurfacesLocked();
8946 }
8947 } break;
8948
8949 case ADD_STARTING: {
8950 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8951 final StartingData sd = wtoken.startingData;
8952
8953 if (sd == null) {
8954 // Animation has been canceled... do nothing.
8955 return;
8956 }
Romain Guy06882f82009-06-10 13:36:04 -07008957
Joe Onorato8a9b2202010-02-26 18:56:32 -08008958 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 View view = null;
8962 try {
8963 view = mPolicy.addStartingWindow(
8964 wtoken.token, sd.pkg,
8965 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8966 sd.icon);
8967 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008968 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 }
8970
8971 if (view != null) {
8972 boolean abort = false;
8973
8974 synchronized(mWindowMap) {
8975 if (wtoken.removed || wtoken.startingData == null) {
8976 // If the window was successfully added, then
8977 // we need to remove it.
8978 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008979 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 "Aborted starting " + wtoken
8981 + ": removed=" + wtoken.removed
8982 + " startingData=" + wtoken.startingData);
8983 wtoken.startingWindow = null;
8984 wtoken.startingData = null;
8985 abort = true;
8986 }
8987 } else {
8988 wtoken.startingView = view;
8989 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008990 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 "Added starting " + wtoken
8992 + ": startingWindow="
8993 + wtoken.startingWindow + " startingView="
8994 + wtoken.startingView);
8995 }
8996
8997 if (abort) {
8998 try {
8999 mPolicy.removeStartingWindow(wtoken.token, view);
9000 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009001 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 }
9003 }
9004 }
9005 } break;
9006
9007 case REMOVE_STARTING: {
9008 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9009 IBinder token = null;
9010 View view = null;
9011 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009012 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009013 + wtoken + ": startingWindow="
9014 + wtoken.startingWindow + " startingView="
9015 + wtoken.startingView);
9016 if (wtoken.startingWindow != null) {
9017 view = wtoken.startingView;
9018 token = wtoken.token;
9019 wtoken.startingData = null;
9020 wtoken.startingView = null;
9021 wtoken.startingWindow = null;
9022 }
9023 }
9024 if (view != null) {
9025 try {
9026 mPolicy.removeStartingWindow(token, view);
9027 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009028 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 }
9030 }
9031 } break;
9032
9033 case FINISHED_STARTING: {
9034 IBinder token = null;
9035 View view = null;
9036 while (true) {
9037 synchronized (mWindowMap) {
9038 final int N = mFinishedStarting.size();
9039 if (N <= 0) {
9040 break;
9041 }
9042 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9043
Joe Onorato8a9b2202010-02-26 18:56:32 -08009044 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 "Finished starting " + wtoken
9046 + ": startingWindow=" + wtoken.startingWindow
9047 + " startingView=" + wtoken.startingView);
9048
9049 if (wtoken.startingWindow == null) {
9050 continue;
9051 }
9052
9053 view = wtoken.startingView;
9054 token = wtoken.token;
9055 wtoken.startingData = null;
9056 wtoken.startingView = null;
9057 wtoken.startingWindow = null;
9058 }
9059
9060 try {
9061 mPolicy.removeStartingWindow(token, view);
9062 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009063 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 }
9065 }
9066 } break;
9067
9068 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9069 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9070
9071 boolean nowVisible = msg.arg1 != 0;
9072 boolean nowGone = msg.arg2 != 0;
9073
9074 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009075 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009076 TAG, "Reporting visible in " + wtoken
9077 + " visible=" + nowVisible
9078 + " gone=" + nowGone);
9079 if (nowVisible) {
9080 wtoken.appToken.windowsVisible();
9081 } else {
9082 wtoken.appToken.windowsGone();
9083 }
9084 } catch (RemoteException ex) {
9085 }
9086 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 case WINDOW_FREEZE_TIMEOUT: {
9089 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009090 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 int i = mWindows.size();
9092 while (i > 0) {
9093 i--;
9094 WindowState w = (WindowState)mWindows.get(i);
9095 if (w.mOrientationChanging) {
9096 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009097 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 }
9099 }
9100 performLayoutAndPlaceSurfacesLocked();
9101 }
9102 break;
9103 }
Romain Guy06882f82009-06-10 13:36:04 -07009104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 case HOLD_SCREEN_CHANGED: {
9106 Session oldHold;
9107 Session newHold;
9108 synchronized (mWindowMap) {
9109 oldHold = mLastReportedHold;
9110 newHold = (Session)msg.obj;
9111 mLastReportedHold = newHold;
9112 }
Romain Guy06882f82009-06-10 13:36:04 -07009113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009114 if (oldHold != newHold) {
9115 try {
9116 if (oldHold != null) {
9117 mBatteryStats.noteStopWakelock(oldHold.mUid,
9118 "window",
9119 BatteryStats.WAKE_TYPE_WINDOW);
9120 }
9121 if (newHold != null) {
9122 mBatteryStats.noteStartWakelock(newHold.mUid,
9123 "window",
9124 BatteryStats.WAKE_TYPE_WINDOW);
9125 }
9126 } catch (RemoteException e) {
9127 }
9128 }
9129 break;
9130 }
Romain Guy06882f82009-06-10 13:36:04 -07009131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009132 case APP_TRANSITION_TIMEOUT: {
9133 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009134 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009135 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 "*** APP TRANSITION TIMEOUT");
9137 mAppTransitionReady = true;
9138 mAppTransitionTimeout = true;
9139 performLayoutAndPlaceSurfacesLocked();
9140 }
9141 }
9142 break;
9143 }
Romain Guy06882f82009-06-10 13:36:04 -07009144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 case PERSIST_ANIMATION_SCALE: {
9146 Settings.System.putFloat(mContext.getContentResolver(),
9147 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9148 Settings.System.putFloat(mContext.getContentResolver(),
9149 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9150 break;
9151 }
Romain Guy06882f82009-06-10 13:36:04 -07009152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 case FORCE_GC: {
9154 synchronized(mWindowMap) {
9155 if (mAnimationPending) {
9156 // If we are animating, don't do the gc now but
9157 // delay a bit so we don't interrupt the animation.
9158 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9159 2000);
9160 return;
9161 }
9162 // If we are currently rotating the display, it will
9163 // schedule a new message when done.
9164 if (mDisplayFrozen) {
9165 return;
9166 }
9167 mFreezeGcPending = 0;
9168 }
9169 Runtime.getRuntime().gc();
9170 break;
9171 }
Romain Guy06882f82009-06-10 13:36:04 -07009172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 case ENABLE_SCREEN: {
9174 performEnableScreen();
9175 break;
9176 }
Romain Guy06882f82009-06-10 13:36:04 -07009177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009178 case APP_FREEZE_TIMEOUT: {
9179 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009180 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 int i = mAppTokens.size();
9182 while (i > 0) {
9183 i--;
9184 AppWindowToken tok = mAppTokens.get(i);
9185 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009186 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 unsetAppFreezingScreenLocked(tok, true, true);
9188 }
9189 }
9190 }
9191 break;
9192 }
Romain Guy06882f82009-06-10 13:36:04 -07009193
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009194 case SEND_NEW_CONFIGURATION: {
9195 removeMessages(SEND_NEW_CONFIGURATION);
9196 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009197 break;
9198 }
Romain Guy06882f82009-06-10 13:36:04 -07009199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 }
9201 }
9202 }
9203
9204 // -------------------------------------------------------------
9205 // IWindowManager API
9206 // -------------------------------------------------------------
9207
9208 public IWindowSession openSession(IInputMethodClient client,
9209 IInputContext inputContext) {
9210 if (client == null) throw new IllegalArgumentException("null client");
9211 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9212 return new Session(client, inputContext);
9213 }
9214
9215 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9216 synchronized (mWindowMap) {
9217 // The focus for the client is the window immediately below
9218 // where we would place the input method window.
9219 int idx = findDesiredInputMethodWindowIndexLocked(false);
9220 WindowState imFocus;
9221 if (idx > 0) {
9222 imFocus = (WindowState)mWindows.get(idx-1);
9223 if (imFocus != null) {
9224 if (imFocus.mSession.mClient != null &&
9225 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9226 return true;
9227 }
9228 }
9229 }
9230 }
9231 return false;
9232 }
Romain Guy06882f82009-06-10 13:36:04 -07009233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 // -------------------------------------------------------------
9235 // Internals
9236 // -------------------------------------------------------------
9237
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009238 final WindowState windowForClientLocked(Session session, IWindow client,
9239 boolean throwOnError) {
9240 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 }
Romain Guy06882f82009-06-10 13:36:04 -07009242
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009243 final WindowState windowForClientLocked(Session session, IBinder client,
9244 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009246 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247 TAG, "Looking up client " + client + ": " + win);
9248 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009249 RuntimeException ex = new IllegalArgumentException(
9250 "Requested window " + client + " does not exist");
9251 if (throwOnError) {
9252 throw ex;
9253 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009254 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 return null;
9256 }
9257 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009258 RuntimeException ex = new IllegalArgumentException(
9259 "Requested window " + client + " is in session " +
9260 win.mSession + ", not " + session);
9261 if (throwOnError) {
9262 throw ex;
9263 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009264 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 return null;
9266 }
9267
9268 return win;
9269 }
9270
Dianne Hackborna8f60182009-09-01 19:01:50 -07009271 final void rebuildAppWindowListLocked() {
9272 int NW = mWindows.size();
9273 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009274 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009275 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009276
Dianne Hackborna8f60182009-09-01 19:01:50 -07009277 // First remove all existing app windows.
9278 i=0;
9279 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009280 WindowState w = (WindowState)mWindows.get(i);
9281 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009282 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009283 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009284 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009285 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009286 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009287 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009288 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9289 && lastWallpaper == i-1) {
9290 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009291 }
9292 i++;
9293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009294
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009295 // The wallpaper window(s) typically live at the bottom of the stack,
9296 // so skip them before adding app tokens.
9297 lastWallpaper++;
9298 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009299
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009300 // First add all of the exiting app tokens... these are no longer
9301 // in the main app list, but still have windows shown. We put them
9302 // in the back because now that the animation is over we no longer
9303 // will care about them.
9304 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009305 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009306 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009308
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009309 // And add in the still active app tokens in Z order.
9310 NT = mAppTokens.size();
9311 for (int j=0; j<NT; j++) {
9312 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009314
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009315 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009316 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009317 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009318 + " windows but added " + i);
9319 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009322 private final void assignLayersLocked() {
9323 int N = mWindows.size();
9324 int curBaseLayer = 0;
9325 int curLayer = 0;
9326 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 for (i=0; i<N; i++) {
9329 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009330 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9331 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 curLayer += WINDOW_LAYER_MULTIPLIER;
9333 w.mLayer = curLayer;
9334 } else {
9335 curBaseLayer = curLayer = w.mBaseLayer;
9336 w.mLayer = curLayer;
9337 }
9338 if (w.mTargetAppToken != null) {
9339 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9340 } else if (w.mAppToken != null) {
9341 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9342 } else {
9343 w.mAnimLayer = w.mLayer;
9344 }
9345 if (w.mIsImWindow) {
9346 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009347 } else if (w.mIsWallpaper) {
9348 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009350 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009351 + w.mAnimLayer);
9352 //System.out.println(
9353 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9354 }
9355 }
9356
9357 private boolean mInLayout = false;
9358 private final void performLayoutAndPlaceSurfacesLocked() {
9359 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009360 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 throw new RuntimeException("Recursive call!");
9362 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009363 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 return;
9365 }
9366
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009367 if (mWaitingForConfig) {
9368 // Our configuration has changed (most likely rotation), but we
9369 // don't yet have the complete configuration to report to
9370 // applications. Don't do any window layout until we have it.
9371 return;
9372 }
9373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 boolean recoveringMemory = false;
9375 if (mForceRemoves != null) {
9376 recoveringMemory = true;
9377 // Wait a little it for things to settle down, and off we go.
9378 for (int i=0; i<mForceRemoves.size(); i++) {
9379 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009380 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 removeWindowInnerLocked(ws.mSession, ws);
9382 }
9383 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009384 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 Object tmp = new Object();
9386 synchronized (tmp) {
9387 try {
9388 tmp.wait(250);
9389 } catch (InterruptedException e) {
9390 }
9391 }
9392 }
Romain Guy06882f82009-06-10 13:36:04 -07009393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394 mInLayout = true;
9395 try {
9396 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 int i = mPendingRemove.size()-1;
9399 if (i >= 0) {
9400 while (i >= 0) {
9401 WindowState w = mPendingRemove.get(i);
9402 removeWindowInnerLocked(w.mSession, w);
9403 i--;
9404 }
9405 mPendingRemove.clear();
9406
9407 mInLayout = false;
9408 assignLayersLocked();
9409 mLayoutNeeded = true;
9410 performLayoutAndPlaceSurfacesLocked();
9411
9412 } else {
9413 mInLayout = false;
9414 if (mLayoutNeeded) {
9415 requestAnimationLocked(0);
9416 }
9417 }
9418 } catch (RuntimeException e) {
9419 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009420 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009421 }
9422 }
9423
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009424 private final int performLayoutLockedInner() {
9425 if (!mLayoutNeeded) {
9426 return 0;
9427 }
9428
9429 mLayoutNeeded = false;
9430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 final int dw = mDisplay.getWidth();
9432 final int dh = mDisplay.getHeight();
9433
9434 final int N = mWindows.size();
9435 int i;
9436
Joe Onorato8a9b2202010-02-26 18:56:32 -08009437 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009438 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9439
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009440 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009441
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009442 int seq = mLayoutSeq+1;
9443 if (seq < 0) seq = 0;
9444 mLayoutSeq = seq;
9445
9446 // First perform layout of any root windows (not attached
9447 // to another window).
9448 int topAttached = -1;
9449 for (i = N-1; i >= 0; i--) {
9450 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009452 // Don't do layout of a window if it is not visible, or
9453 // soon won't be visible, to avoid wasting time and funky
9454 // changes while a window is animating away.
9455 final AppWindowToken atoken = win.mAppToken;
9456 final boolean gone = win.mViewVisibility == View.GONE
9457 || !win.mRelayoutCalled
9458 || win.mRootToken.hidden
9459 || (atoken != null && atoken.hiddenRequested)
9460 || win.mAttachedHidden
9461 || win.mExiting || win.mDestroying;
9462
9463 if (!win.mLayoutAttached) {
9464 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9465 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9466 + " mLayoutAttached=" + win.mLayoutAttached);
9467 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9468 + win.mViewVisibility + " mRelayoutCalled="
9469 + win.mRelayoutCalled + " hidden="
9470 + win.mRootToken.hidden + " hiddenRequested="
9471 + (atoken != null && atoken.hiddenRequested)
9472 + " mAttachedHidden=" + win.mAttachedHidden);
9473 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009474
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009475 // If this view is GONE, then skip it -- keep the current
9476 // frame, and let the caller know so they can ignore it
9477 // if they want. (We do the normal layout for INVISIBLE
9478 // windows, since that means "perform layout as normal,
9479 // just don't display").
9480 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009481 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009482 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9483 win.mLayoutSeq = seq;
9484 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9485 + win.mFrame + " mContainingFrame="
9486 + win.mContainingFrame + " mDisplayFrame="
9487 + win.mDisplayFrame);
9488 } else {
9489 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009490 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009493
9494 // Now perform layout of attached windows, which usually
9495 // depend on the position of the window they are attached to.
9496 // XXX does not deal with windows that are attached to windows
9497 // that are themselves attached.
9498 for (i = topAttached; i >= 0; i--) {
9499 WindowState win = (WindowState) mWindows.get(i);
9500
9501 // If this view is GONE, then skip it -- keep the current
9502 // frame, and let the caller know so they can ignore it
9503 // if they want. (We do the normal layout for INVISIBLE
9504 // windows, since that means "perform layout as normal,
9505 // just don't display").
9506 if (win.mLayoutAttached) {
9507 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9508 + " mHaveFrame=" + win.mHaveFrame
9509 + " mViewVisibility=" + win.mViewVisibility
9510 + " mRelayoutCalled=" + win.mRelayoutCalled);
9511 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9512 || !win.mHaveFrame) {
9513 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9514 win.mLayoutSeq = seq;
9515 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9516 + win.mFrame + " mContainingFrame="
9517 + win.mContainingFrame + " mDisplayFrame="
9518 + win.mDisplayFrame);
9519 }
9520 }
9521 }
9522
9523 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009524 }
Romain Guy06882f82009-06-10 13:36:04 -07009525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009526 private final void performLayoutAndPlaceSurfacesLockedInner(
9527 boolean recoveringMemory) {
9528 final long currentTime = SystemClock.uptimeMillis();
9529 final int dw = mDisplay.getWidth();
9530 final int dh = mDisplay.getHeight();
9531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009532 int i;
9533
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009534 if (mFocusMayChange) {
9535 mFocusMayChange = false;
9536 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9537 }
9538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 if (mFxSession == null) {
9540 mFxSession = new SurfaceSession();
9541 }
Romain Guy06882f82009-06-10 13:36:04 -07009542
Joe Onorato8a9b2202010-02-26 18:56:32 -08009543 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009544
9545 // Initialize state of exiting tokens.
9546 for (i=mExitingTokens.size()-1; i>=0; i--) {
9547 mExitingTokens.get(i).hasVisible = false;
9548 }
9549
9550 // Initialize state of exiting applications.
9551 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9552 mExitingAppTokens.get(i).hasVisible = false;
9553 }
9554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555 boolean orientationChangeComplete = true;
9556 Session holdScreen = null;
9557 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009558 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 boolean focusDisplayed = false;
9560 boolean animating = false;
9561
9562 Surface.openTransaction();
9563 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009564 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009565 int repeats = 0;
9566 int changes = 0;
9567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009569 repeats++;
9570 if (repeats > 6) {
9571 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9572 mLayoutNeeded = false;
9573 break;
9574 }
9575
9576 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9577 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9578 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9579 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9580 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9581 assignLayersLocked();
9582 mLayoutNeeded = true;
9583 }
9584 }
9585 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9586 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9587 if (updateOrientationFromAppTokensLocked()) {
9588 mLayoutNeeded = true;
9589 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9590 }
9591 }
9592 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9593 mLayoutNeeded = true;
9594 }
9595 }
9596
9597 // FIRST LOOP: Perform a layout, if needed.
9598 if (repeats < 4) {
9599 changes = performLayoutLockedInner();
9600 if (changes != 0) {
9601 continue;
9602 }
9603 } else {
9604 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9605 changes = 0;
9606 }
9607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 final int transactionSequence = ++mTransactionSequence;
9609
9610 // Update animations of all applications, including those
9611 // associated with exiting/removed apps
9612 boolean tokensAnimating = false;
9613 final int NAT = mAppTokens.size();
9614 for (i=0; i<NAT; i++) {
9615 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9616 tokensAnimating = true;
9617 }
9618 }
9619 final int NEAT = mExitingAppTokens.size();
9620 for (i=0; i<NEAT; i++) {
9621 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9622 tokensAnimating = true;
9623 }
9624 }
9625
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009626 // SECOND LOOP: Execute animations and update visibility of windows.
9627
Joe Onorato8a9b2202010-02-26 18:56:32 -08009628 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009629 + transactionSequence + " tokensAnimating="
9630 + tokensAnimating);
9631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633
9634 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009635 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009636 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637
9638 mPolicy.beginAnimationLw(dw, dh);
9639
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009640 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 for (i=N-1; i>=0; i--) {
9643 WindowState w = (WindowState)mWindows.get(i);
9644
9645 final WindowManager.LayoutParams attrs = w.mAttrs;
9646
9647 if (w.mSurface != null) {
9648 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009649 if (w.commitFinishDrawingLocked(currentTime)) {
9650 if ((w.mAttrs.flags
9651 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009652 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009653 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009654 wallpaperMayChange = true;
9655 }
9656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009657
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009658 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9660 animating = true;
9661 //w.dump(" ");
9662 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009663 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9664 wallpaperMayChange = true;
9665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009666
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009667 if (mPolicy.doesForceHide(w, attrs)) {
9668 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009669 if (DEBUG_VISIBILITY) Slog.v(TAG,
9670 "Animation done that could impact force hide: "
9671 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009672 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009673 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009674 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9675 forceHiding = true;
9676 }
9677 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9678 boolean changed;
9679 if (forceHiding) {
9680 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009681 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9682 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009683 } else {
9684 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009685 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9686 "Now policy shown: " + w);
9687 if (changed) {
9688 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009689 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009690 // Assume we will need to animate. If
9691 // we don't (because the wallpaper will
9692 // stay with the lock screen), then we will
9693 // clean up later.
9694 Animation a = mPolicy.createForceHideEnterAnimation();
9695 if (a != null) {
9696 w.setAnimation(a);
9697 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009698 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009699 if (mCurrentFocus == null ||
9700 mCurrentFocus.mLayer < w.mLayer) {
9701 // We are showing on to of the current
9702 // focus, so re-evaluate focus to make
9703 // sure it is correct.
9704 mFocusMayChange = true;
9705 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009706 }
9707 }
9708 if (changed && (attrs.flags
9709 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9710 wallpaperMayChange = true;
9711 }
9712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 mPolicy.animatingWindowLw(w, attrs);
9715 }
9716
9717 final AppWindowToken atoken = w.mAppToken;
9718 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9719 if (atoken.lastTransactionSequence != transactionSequence) {
9720 atoken.lastTransactionSequence = transactionSequence;
9721 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9722 atoken.startingDisplayed = false;
9723 }
9724 if ((w.isOnScreen() || w.mAttrs.type
9725 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9726 && !w.mExiting && !w.mDestroying) {
9727 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009728 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009729 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009730 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009731 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009732 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009733 + " pv=" + w.mPolicyVisibility
9734 + " dp=" + w.mDrawPending
9735 + " cdp=" + w.mCommitDrawPending
9736 + " ah=" + w.mAttachedHidden
9737 + " th=" + atoken.hiddenRequested
9738 + " a=" + w.mAnimating);
9739 }
9740 }
9741 if (w != atoken.startingWindow) {
9742 if (!atoken.freezingScreen || !w.mAppFreezing) {
9743 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009744 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009746 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009747 "tokenMayBeDrawn: " + atoken
9748 + " freezingScreen=" + atoken.freezingScreen
9749 + " mAppFreezing=" + w.mAppFreezing);
9750 tokenMayBeDrawn = true;
9751 }
9752 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009753 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009754 atoken.startingDisplayed = true;
9755 }
9756 }
9757 } else if (w.mReadyToShow) {
9758 w.performShowLocked();
9759 }
9760 }
9761
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009762 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763
9764 if (tokenMayBeDrawn) {
9765 // See if any windows have been drawn, so they (and others
9766 // associated with them) can now be shown.
9767 final int NT = mTokenList.size();
9768 for (i=0; i<NT; i++) {
9769 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9770 if (wtoken == null) {
9771 continue;
9772 }
9773 if (wtoken.freezingScreen) {
9774 int numInteresting = wtoken.numInterestingWindows;
9775 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009776 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 "allDrawn: " + wtoken
9778 + " interesting=" + numInteresting
9779 + " drawn=" + wtoken.numDrawnWindows);
9780 wtoken.showAllWindowsLocked();
9781 unsetAppFreezingScreenLocked(wtoken, false, true);
9782 orientationChangeComplete = true;
9783 }
9784 } else if (!wtoken.allDrawn) {
9785 int numInteresting = wtoken.numInterestingWindows;
9786 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009787 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 "allDrawn: " + wtoken
9789 + " interesting=" + numInteresting
9790 + " drawn=" + wtoken.numDrawnWindows);
9791 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009792 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793
9794 // We can now show all of the drawn windows!
9795 if (!mOpeningApps.contains(wtoken)) {
9796 wtoken.showAllWindowsLocked();
9797 }
9798 }
9799 }
9800 }
9801 }
9802
9803 // If we are ready to perform an app transition, check through
9804 // all of the app tokens to be shown and see if they are ready
9805 // to go.
9806 if (mAppTransitionReady) {
9807 int NN = mOpeningApps.size();
9808 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009809 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 "Checking " + NN + " opening apps (frozen="
9811 + mDisplayFrozen + " timeout="
9812 + mAppTransitionTimeout + ")...");
9813 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9814 // If the display isn't frozen, wait to do anything until
9815 // all of the apps are ready. Otherwise just go because
9816 // we'll unfreeze the display when everyone is ready.
9817 for (i=0; i<NN && goodToGo; i++) {
9818 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009819 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 "Check opening app" + wtoken + ": allDrawn="
9821 + wtoken.allDrawn + " startingDisplayed="
9822 + wtoken.startingDisplayed);
9823 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9824 && !wtoken.startingMoved) {
9825 goodToGo = false;
9826 }
9827 }
9828 }
9829 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009830 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 int transit = mNextAppTransition;
9832 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009833 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009835 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009837 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 mAppTransitionTimeout = false;
9839 mStartingIconInTransition = false;
9840 mSkipAppTransitionAnimation = false;
9841
9842 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9843
Dianne Hackborna8f60182009-09-01 19:01:50 -07009844 // If there are applications waiting to come to the
9845 // top of the stack, now is the time to move their windows.
9846 // (Note that we don't do apps going to the bottom
9847 // here -- we want to keep their windows in the old
9848 // Z-order until the animation completes.)
9849 if (mToTopApps.size() > 0) {
9850 NN = mAppTokens.size();
9851 for (i=0; i<NN; i++) {
9852 AppWindowToken wtoken = mAppTokens.get(i);
9853 if (wtoken.sendingToTop) {
9854 wtoken.sendingToTop = false;
9855 moveAppWindowsLocked(wtoken, NN, false);
9856 }
9857 }
9858 mToTopApps.clear();
9859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009860
Dianne Hackborn25994b42009-09-04 14:21:19 -07009861 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009862
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009863 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009864 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009865
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009866 // The top-most window will supply the layout params,
9867 // and we will determine it below.
9868 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009869 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009870 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009871
Joe Onorato8a9b2202010-02-26 18:56:32 -08009872 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009873 "New wallpaper target=" + mWallpaperTarget
9874 + ", lower target=" + mLowerWallpaperTarget
9875 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009876 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009877 // Do a first pass through the tokens for two
9878 // things:
9879 // (1) Determine if both the closing and opening
9880 // app token sets are wallpaper targets, in which
9881 // case special animations are needed
9882 // (since the wallpaper needs to stay static
9883 // behind them).
9884 // (2) Find the layout params of the top-most
9885 // application window in the tokens, which is
9886 // what will control the animation theme.
9887 final int NC = mClosingApps.size();
9888 NN = NC + mOpeningApps.size();
9889 for (i=0; i<NN; i++) {
9890 AppWindowToken wtoken;
9891 int mode;
9892 if (i < NC) {
9893 wtoken = mClosingApps.get(i);
9894 mode = 1;
9895 } else {
9896 wtoken = mOpeningApps.get(i-NC);
9897 mode = 2;
9898 }
9899 if (mLowerWallpaperTarget != null) {
9900 if (mLowerWallpaperTarget.mAppToken == wtoken
9901 || mUpperWallpaperTarget.mAppToken == wtoken) {
9902 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009903 }
9904 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009905 if (wtoken.appFullscreen) {
9906 WindowState ws = wtoken.findMainWindow();
9907 if (ws != null) {
9908 // If this is a compatibility mode
9909 // window, we will always use its anim.
9910 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9911 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009912 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009913 bestAnimLayer = Integer.MAX_VALUE;
9914 } else if (ws.mLayer > bestAnimLayer) {
9915 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009916 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009917 bestAnimLayer = ws.mLayer;
9918 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009919 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009920 }
9921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009922
Dianne Hackborn25994b42009-09-04 14:21:19 -07009923 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009924 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009925 "Wallpaper animation!");
9926 switch (transit) {
9927 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9928 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9929 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9930 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9931 break;
9932 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9933 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9934 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9935 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9936 break;
9937 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009938 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009939 "New transit: " + transit);
9940 } else if (oldWallpaper != null) {
9941 // We are transitioning from an activity with
9942 // a wallpaper to one without.
9943 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009944 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009945 "New transit away from wallpaper: " + transit);
9946 } else if (mWallpaperTarget != null) {
9947 // We are transitioning from an activity without
9948 // a wallpaper to now showing the wallpaper
9949 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009950 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009951 "New transit into wallpaper: " + transit);
9952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009953
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009954 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9955 mLastEnterAnimToken = animToken;
9956 mLastEnterAnimParams = animLp;
9957 } else if (mLastEnterAnimParams != null) {
9958 animLp = mLastEnterAnimParams;
9959 mLastEnterAnimToken = null;
9960 mLastEnterAnimParams = null;
9961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009962
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009963 // If all closing windows are obscured, then there is
9964 // no need to do an animation. This is the case, for
9965 // example, when this transition is being done behind
9966 // the lock screen.
9967 if (!mPolicy.allowAppAnimationsLw()) {
9968 animLp = null;
9969 }
9970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 NN = mOpeningApps.size();
9972 for (i=0; i<NN; i++) {
9973 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009974 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 "Now opening app" + wtoken);
9976 wtoken.reportedVisible = false;
9977 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009978 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009979 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009981 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 wtoken.showAllWindowsLocked();
9983 }
9984 NN = mClosingApps.size();
9985 for (i=0; i<NN; i++) {
9986 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009987 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 "Now closing app" + wtoken);
9989 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009990 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009991 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009993 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994 // Force the allDrawn flag, because we want to start
9995 // this guy's animations regardless of whether it's
9996 // gotten drawn.
9997 wtoken.allDrawn = true;
9998 }
9999
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010000 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 mOpeningApps.clear();
10003 mClosingApps.clear();
10004
10005 // This has changed the visibility of windows, so perform
10006 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010007 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010009 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10010 assignLayersLocked();
10011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010012 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010013 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 }
10015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010016
Dianne Hackborn16064f92010-03-25 00:47:24 -070010017 int adjResult = 0;
10018
Dianne Hackborna8f60182009-09-01 19:01:50 -070010019 if (!animating && mAppTransitionRunning) {
10020 // We have finished the animation of an app transition. To do
10021 // this, we have delayed a lot of operations like showing and
10022 // hiding apps, moving apps in Z-order, etc. The app token list
10023 // reflects the correct Z-order, but the window list may now
10024 // be out of sync with it. So here we will just rebuild the
10025 // entire app window list. Fun!
10026 mAppTransitionRunning = false;
10027 // Clear information about apps that were moving.
10028 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010029
Dianne Hackborna8f60182009-09-01 19:01:50 -070010030 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010031 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010032 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010033 moveInputMethodWindowsIfNeededLocked(false);
10034 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010035 // Since the window list has been rebuilt, focus might
10036 // have to be recomputed since the actual order of windows
10037 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010038 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010040
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010041 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010042 // At this point, there was a window with a wallpaper that
10043 // was force hiding other windows behind it, but now it
10044 // is going away. This may be simple -- just animate
10045 // away the wallpaper and its window -- or it may be
10046 // hard -- the wallpaper now needs to be shown behind
10047 // something that was hidden.
10048 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010049 if (mLowerWallpaperTarget != null
10050 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010051 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010052 "wallpaperForceHiding changed with lower="
10053 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010054 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010055 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10056 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10057 if (mLowerWallpaperTarget.mAppToken.hidden) {
10058 // The lower target has become hidden before we
10059 // actually started the animation... let's completely
10060 // re-evaluate everything.
10061 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010062 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010063 }
10064 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010065 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010066 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010067 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010068 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010069 + " NEW: " + mWallpaperTarget
10070 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010071 if (mLowerWallpaperTarget == null) {
10072 // Whoops, we don't need a special wallpaper animation.
10073 // Clear them out.
10074 forceHiding = false;
10075 for (i=N-1; i>=0; i--) {
10076 WindowState w = (WindowState)mWindows.get(i);
10077 if (w.mSurface != null) {
10078 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010079 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010080 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010081 forceHiding = true;
10082 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10083 if (!w.mAnimating) {
10084 // We set the animation above so it
10085 // is not yet running.
10086 w.clearAnimation();
10087 }
10088 }
10089 }
10090 }
10091 }
10092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010093
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010094 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010095 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010096 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010097 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010099
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010100 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010101 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010102 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010103 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010104 assignLayersLocked();
10105 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010106 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010107 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010108 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010110
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010111 if (mFocusMayChange) {
10112 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010113 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010114 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010115 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010116 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010117 }
10118
10119 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010120 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010122
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010123 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10124 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010125
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010126 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010127
10128 // THIRD LOOP: Update the surfaces of all windows.
10129
10130 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10131
10132 boolean obscured = false;
10133 boolean blurring = false;
10134 boolean dimming = false;
10135 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010136 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010137 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010139 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 for (i=N-1; i>=0; i--) {
10142 WindowState w = (WindowState)mWindows.get(i);
10143
10144 boolean displayed = false;
10145 final WindowManager.LayoutParams attrs = w.mAttrs;
10146 final int attrFlags = attrs.flags;
10147
10148 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010149 // XXX NOTE: The logic here could be improved. We have
10150 // the decision about whether to resize a window separated
10151 // from whether to hide the surface. This can cause us to
10152 // resize a surface even if we are going to hide it. You
10153 // can see this by (1) holding device in landscape mode on
10154 // home screen; (2) tapping browser icon (device will rotate
10155 // to landscape; (3) tap home. The wallpaper will be resized
10156 // in step 2 but then immediately hidden, causing us to
10157 // have to resize and then redraw it again in step 3. It
10158 // would be nice to figure out how to avoid this, but it is
10159 // difficult because we do need to resize surfaces in some
10160 // cases while they are hidden such as when first showing a
10161 // window.
10162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010164 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 TAG, "Placing surface #" + i + " " + w.mSurface
10166 + ": new=" + w.mShownFrame + ", old="
10167 + w.mLastShownFrame);
10168
10169 boolean resize;
10170 int width, height;
10171 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10172 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10173 w.mLastRequestedHeight != w.mRequestedHeight;
10174 // for a scaled surface, we just want to use
10175 // the requested size.
10176 width = w.mRequestedWidth;
10177 height = w.mRequestedHeight;
10178 w.mLastRequestedWidth = width;
10179 w.mLastRequestedHeight = height;
10180 w.mLastShownFrame.set(w.mShownFrame);
10181 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010182 if (SHOW_TRANSACTIONS) logSurface(w,
10183 "POS " + w.mShownFrame.left
10184 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010185 w.mSurfaceX = w.mShownFrame.left;
10186 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10188 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010189 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 if (!recoveringMemory) {
10191 reclaimSomeSurfaceMemoryLocked(w, "position");
10192 }
10193 }
10194 } else {
10195 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10196 width = w.mShownFrame.width();
10197 height = w.mShownFrame.height();
10198 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 }
10200
10201 if (resize) {
10202 if (width < 1) width = 1;
10203 if (height < 1) height = 1;
10204 if (w.mSurface != null) {
10205 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010206 if (SHOW_TRANSACTIONS) logSurface(w,
10207 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010208 + w.mShownFrame.top + " SIZE "
10209 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010210 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010211 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010212 w.mSurfaceW = width;
10213 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010215 w.mSurfaceX = w.mShownFrame.left;
10216 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 w.mSurface.setPosition(w.mShownFrame.left,
10218 w.mShownFrame.top);
10219 } catch (RuntimeException e) {
10220 // If something goes wrong with the surface (such
10221 // as running out of memory), don't take down the
10222 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010223 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 + "size=(" + width + "x" + height
10225 + "), pos=(" + w.mShownFrame.left
10226 + "," + w.mShownFrame.top + ")", e);
10227 if (!recoveringMemory) {
10228 reclaimSomeSurfaceMemoryLocked(w, "size");
10229 }
10230 }
10231 }
10232 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010233 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 w.mContentInsetsChanged =
10235 !w.mLastContentInsets.equals(w.mContentInsets);
10236 w.mVisibleInsetsChanged =
10237 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010238 boolean configChanged =
10239 w.mConfiguration != mCurConfiguration
10240 && (w.mConfiguration == null
10241 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010242 if (DEBUG_CONFIGURATION && configChanged) {
10243 Slog.v(TAG, "Win " + w + " config changed: "
10244 + mCurConfiguration);
10245 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010246 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010247 + ": configChanged=" + configChanged
10248 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010249 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010251 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010252 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010253 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010254 w.mLastFrame.set(w.mFrame);
10255 w.mLastContentInsets.set(w.mContentInsets);
10256 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010257 // If the screen is currently frozen, then keep
10258 // it frozen until this window draws at its new
10259 // orientation.
10260 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010261 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010262 "Resizing while display frozen: " + w);
10263 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010264 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010265 mWindowsFreezingScreen = true;
10266 // XXX should probably keep timeout from
10267 // when we first froze the display.
10268 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10269 mH.sendMessageDelayed(mH.obtainMessage(
10270 H.WINDOW_FREEZE_TIMEOUT), 2000);
10271 }
10272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 // If the orientation is changing, then we need to
10274 // hold off on unfreezing the display until this
10275 // window has been redrawn; to do that, we need
10276 // to go through the process of getting informed
10277 // by the application when it has finished drawing.
10278 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010279 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 "Orientation start waiting for draw in "
10281 + w + ", surface " + w.mSurface);
10282 w.mDrawPending = true;
10283 w.mCommitDrawPending = false;
10284 w.mReadyToShow = false;
10285 if (w.mAppToken != null) {
10286 w.mAppToken.allDrawn = false;
10287 }
10288 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010289 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 "Resizing window " + w + " to " + w.mFrame);
10291 mResizingWindows.add(w);
10292 } else if (w.mOrientationChanging) {
10293 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010294 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 "Orientation not waiting for draw in "
10296 + w + ", surface " + w.mSurface);
10297 w.mOrientationChanging = false;
10298 }
10299 }
10300 }
10301
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010302 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 if (!w.mLastHidden) {
10304 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010305 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10306 + w.mRootToken.waitingToShow + " polvis="
10307 + w.mPolicyVisibility + " atthid="
10308 + w.mAttachedHidden + " tokhid="
10309 + w.mRootToken.hidden + " vis="
10310 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010312 if (SHOW_TRANSACTIONS) logSurface(w,
10313 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010315 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 try {
10317 w.mSurface.hide();
10318 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010319 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 }
10321 }
10322 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10323 }
10324 // If we are waiting for this window to handle an
10325 // orientation change, well, it is hidden, so
10326 // doesn't really matter. Note that this does
10327 // introduce a potential glitch if the window
10328 // becomes unhidden before it has drawn for the
10329 // new orientation.
10330 if (w.mOrientationChanging) {
10331 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010332 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 "Orientation change skips hidden " + w);
10334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 } else if (w.mLastLayer != w.mAnimLayer
10336 || w.mLastAlpha != w.mShownAlpha
10337 || w.mLastDsDx != w.mDsDx
10338 || w.mLastDtDx != w.mDtDx
10339 || w.mLastDsDy != w.mDsDy
10340 || w.mLastDtDy != w.mDtDy
10341 || w.mLastHScale != w.mHScale
10342 || w.mLastVScale != w.mVScale
10343 || w.mLastHidden) {
10344 displayed = true;
10345 w.mLastAlpha = w.mShownAlpha;
10346 w.mLastLayer = w.mAnimLayer;
10347 w.mLastDsDx = w.mDsDx;
10348 w.mLastDtDx = w.mDtDx;
10349 w.mLastDsDy = w.mDsDy;
10350 w.mLastDtDy = w.mDtDy;
10351 w.mLastHScale = w.mHScale;
10352 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010353 if (SHOW_TRANSACTIONS) logSurface(w,
10354 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010355 + " matrix=[" + (w.mDsDx*w.mHScale)
10356 + "," + (w.mDtDx*w.mVScale)
10357 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010358 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 if (w.mSurface != null) {
10360 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010361 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010363 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364 w.mSurface.setLayer(w.mAnimLayer);
10365 w.mSurface.setMatrix(
10366 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10367 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10368 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010369 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010370 if (!recoveringMemory) {
10371 reclaimSomeSurfaceMemoryLocked(w, "update");
10372 }
10373 }
10374 }
10375
10376 if (w.mLastHidden && !w.mDrawPending
10377 && !w.mCommitDrawPending
10378 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010379 if (SHOW_TRANSACTIONS) logSurface(w,
10380 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010381 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 + " during relayout");
10383 if (showSurfaceRobustlyLocked(w)) {
10384 w.mHasDrawn = true;
10385 w.mLastHidden = false;
10386 } else {
10387 w.mOrientationChanging = false;
10388 }
10389 }
10390 if (w.mSurface != null) {
10391 w.mToken.hasVisible = true;
10392 }
10393 } else {
10394 displayed = true;
10395 }
10396
10397 if (displayed) {
10398 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010399 if (attrs.width == LayoutParams.MATCH_PARENT
10400 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 covered = true;
10402 }
10403 }
10404 if (w.mOrientationChanging) {
10405 if (w.mDrawPending || w.mCommitDrawPending) {
10406 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010407 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 "Orientation continue waiting for draw in " + w);
10409 } else {
10410 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010411 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010412 "Orientation change complete in " + w);
10413 }
10414 }
10415 w.mToken.hasVisible = true;
10416 }
10417 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010418 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 "Orientation change skips hidden " + w);
10420 w.mOrientationChanging = false;
10421 }
10422
10423 final boolean canBeSeen = w.isDisplayedLw();
10424
10425 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10426 focusDisplayed = true;
10427 }
10428
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010429 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010432 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 if (w.mSurface != null) {
10434 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10435 holdScreen = w.mSession;
10436 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010437 if (!syswin && w.mAttrs.screenBrightness >= 0
10438 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 screenBrightness = w.mAttrs.screenBrightness;
10440 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010441 if (!syswin && w.mAttrs.buttonBrightness >= 0
10442 && buttonBrightness < 0) {
10443 buttonBrightness = w.mAttrs.buttonBrightness;
10444 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010445 if (canBeSeen
10446 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10447 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10448 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010449 syswin = true;
10450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010452
Dianne Hackborn25994b42009-09-04 14:21:19 -070010453 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10454 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 // This window completely covers everything behind it,
10456 // so we want to leave all of them as unblurred (for
10457 // performance reasons).
10458 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010459 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010460 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010461 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010462 obscured = true;
10463 if (mBackgroundFillerSurface == null) {
10464 try {
10465 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010466 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010467 0, dw, dh,
10468 PixelFormat.OPAQUE,
10469 Surface.FX_SURFACE_NORMAL);
10470 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010471 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010472 }
10473 }
10474 try {
10475 mBackgroundFillerSurface.setPosition(0, 0);
10476 mBackgroundFillerSurface.setSize(dw, dh);
10477 // Using the same layer as Dim because they will never be shown at the
10478 // same time.
10479 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10480 mBackgroundFillerSurface.show();
10481 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010482 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010483 }
10484 backgroundFillerShown = true;
10485 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010486 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010487 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010488 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 + ": blurring=" + blurring
10490 + " obscured=" + obscured
10491 + " displayed=" + displayed);
10492 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10493 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010494 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010496 if (mDimAnimator == null) {
10497 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010499 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010500 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 }
10503 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10504 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010505 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010508 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 + mBlurSurface + ": CREATE");
10510 try {
Romain Guy06882f82009-06-10 13:36:04 -070010511 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010512 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 -1, 16, 16,
10514 PixelFormat.OPAQUE,
10515 Surface.FX_SURFACE_BLUR);
10516 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010517 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 }
10519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010521 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10522 + mBlurSurface + ": pos=(0,0) (" +
10523 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 mBlurSurface.setPosition(0, 0);
10525 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010526 mBlurSurface.setLayer(w.mAnimLayer-2);
10527 if (!mBlurShown) {
10528 try {
10529 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10530 + mBlurSurface + ": SHOW");
10531 mBlurSurface.show();
10532 } catch (RuntimeException e) {
10533 Slog.w(TAG, "Failure showing blur surface", e);
10534 }
10535 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537 }
10538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 }
10540 }
10541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010542
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010543 if (obscuredChanged && mWallpaperTarget == w) {
10544 // This is the wallpaper target and its obscured state
10545 // changed... make sure the current wallaper's visibility
10546 // has been updated accordingly.
10547 updateWallpaperVisibilityLocked();
10548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010550
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010551 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10552 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010553 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010554 try {
10555 mBackgroundFillerSurface.hide();
10556 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010557 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010558 }
10559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010561 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010562 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10563 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 }
Romain Guy06882f82009-06-10 13:36:04 -070010565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010567 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 + ": HIDE");
10569 try {
10570 mBlurSurface.hide();
10571 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010572 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 }
10574 mBlurShown = false;
10575 }
10576
Joe Onorato8a9b2202010-02-26 18:56:32 -080010577 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010579 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580 }
10581
10582 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010583
Joe Onorato8a9b2202010-02-26 18:56:32 -080010584 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 "With display frozen, orientationChangeComplete="
10586 + orientationChangeComplete);
10587 if (orientationChangeComplete) {
10588 if (mWindowsFreezingScreen) {
10589 mWindowsFreezingScreen = false;
10590 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10591 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010592 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 }
Romain Guy06882f82009-06-10 13:36:04 -070010594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 i = mResizingWindows.size();
10596 if (i > 0) {
10597 do {
10598 i--;
10599 WindowState win = mResizingWindows.get(i);
10600 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010601 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10602 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010603 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010604 boolean configChanged =
10605 win.mConfiguration != mCurConfiguration
10606 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010607 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10608 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10609 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010610 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010611 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010612 + " / " + mCurConfiguration + " / 0x"
10613 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010614 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010615 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 win.mClient.resized(win.mFrame.width(),
10617 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010618 win.mLastVisibleInsets, win.mDrawPending,
10619 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 win.mContentInsetsChanged = false;
10621 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010622 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 } catch (RemoteException e) {
10624 win.mOrientationChanging = false;
10625 }
10626 } while (i > 0);
10627 mResizingWindows.clear();
10628 }
Romain Guy06882f82009-06-10 13:36:04 -070010629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010631 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 i = mDestroySurface.size();
10633 if (i > 0) {
10634 do {
10635 i--;
10636 WindowState win = mDestroySurface.get(i);
10637 win.mDestroying = false;
10638 if (mInputMethodWindow == win) {
10639 mInputMethodWindow = null;
10640 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010641 if (win == mWallpaperTarget) {
10642 wallpaperDestroyed = true;
10643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 win.destroySurfaceLocked();
10645 } while (i > 0);
10646 mDestroySurface.clear();
10647 }
10648
10649 // Time to remove any exiting tokens?
10650 for (i=mExitingTokens.size()-1; i>=0; i--) {
10651 WindowToken token = mExitingTokens.get(i);
10652 if (!token.hasVisible) {
10653 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010654 if (token.windowType == TYPE_WALLPAPER) {
10655 mWallpaperTokens.remove(token);
10656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 }
10658 }
10659
10660 // Time to remove any exiting applications?
10661 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10662 AppWindowToken token = mExitingAppTokens.get(i);
10663 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010664 // Make sure there is no animation running on this token,
10665 // so any windows associated with it will be removed as
10666 // soon as their animations are complete
10667 token.animation = null;
10668 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010669 mAppTokens.remove(token);
10670 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010671 if (mLastEnterAnimToken == token) {
10672 mLastEnterAnimToken = null;
10673 mLastEnterAnimParams = null;
10674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 }
10676 }
10677
Dianne Hackborna8f60182009-09-01 19:01:50 -070010678 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010679
Dianne Hackborna8f60182009-09-01 19:01:50 -070010680 if (!animating && mAppTransitionRunning) {
10681 // We have finished the animation of an app transition. To do
10682 // this, we have delayed a lot of operations like showing and
10683 // hiding apps, moving apps in Z-order, etc. The app token list
10684 // reflects the correct Z-order, but the window list may now
10685 // be out of sync with it. So here we will just rebuild the
10686 // entire app window list. Fun!
10687 mAppTransitionRunning = false;
10688 needRelayout = true;
10689 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010690 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010691 // Clear information about apps that were moving.
10692 mToBottomApps.clear();
10693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 if (focusDisplayed) {
10696 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10697 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010698 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010699 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010700 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010701 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010702 requestAnimationLocked(0);
10703 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10705 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010706
10707 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10708 + " holdScreen=" + holdScreen);
10709 if (!mDisplayFrozen) {
10710 mQueue.setHoldScreenLocked(holdScreen != null);
10711 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10712 mPowerManager.setScreenBrightnessOverride(-1);
10713 } else {
10714 mPowerManager.setScreenBrightnessOverride((int)
10715 (screenBrightness * Power.BRIGHTNESS_ON));
10716 }
10717 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10718 mPowerManager.setButtonBrightnessOverride(-1);
10719 } else {
10720 mPowerManager.setButtonBrightnessOverride((int)
10721 (buttonBrightness * Power.BRIGHTNESS_ON));
10722 }
10723 if (holdScreen != mHoldingScreenOn) {
10724 mHoldingScreenOn = holdScreen;
10725 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10726 mH.sendMessage(m);
10727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010729
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010730 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010731 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010732 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10733 LocalPowerManager.BUTTON_EVENT, true);
10734 mTurnOnScreen = false;
10735 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010736
10737 // Check to see if we are now in a state where the screen should
10738 // be enabled, because the window obscured flags have changed.
10739 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 }
10741
10742 void requestAnimationLocked(long delay) {
10743 if (!mAnimationPending) {
10744 mAnimationPending = true;
10745 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10746 }
10747 }
Romain Guy06882f82009-06-10 13:36:04 -070010748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 /**
10750 * Have the surface flinger show a surface, robustly dealing with
10751 * error conditions. In particular, if there is not enough memory
10752 * to show the surface, then we will try to get rid of other surfaces
10753 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010754 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 * @return Returns true if the surface was successfully shown.
10756 */
10757 boolean showSurfaceRobustlyLocked(WindowState win) {
10758 try {
10759 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010760 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010762 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010763 if (DEBUG_VISIBILITY) Slog.v(TAG,
10764 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010765 win.mTurnOnScreen = false;
10766 mTurnOnScreen = true;
10767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 }
10769 return true;
10770 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010771 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 }
Romain Guy06882f82009-06-10 13:36:04 -070010773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010776 return false;
10777 }
Romain Guy06882f82009-06-10 13:36:04 -070010778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10780 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010781
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010782 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 if (mForceRemoves == null) {
10786 mForceRemoves = new ArrayList<WindowState>();
10787 }
Romain Guy06882f82009-06-10 13:36:04 -070010788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 long callingIdentity = Binder.clearCallingIdentity();
10790 try {
10791 // There was some problem... first, do a sanity check of the
10792 // window list to make sure we haven't left any dangling surfaces
10793 // around.
10794 int N = mWindows.size();
10795 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010796 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010797 for (int i=0; i<N; i++) {
10798 WindowState ws = (WindowState)mWindows.get(i);
10799 if (ws.mSurface != null) {
10800 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010801 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 + ws + " surface=" + ws.mSurface
10803 + " token=" + win.mToken
10804 + " pid=" + ws.mSession.mPid
10805 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010806 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010807 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010808 ws.mSurface = null;
10809 mForceRemoves.add(ws);
10810 i--;
10811 N--;
10812 leakedSurface = true;
10813 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010814 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 + ws + " surface=" + ws.mSurface
10816 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010817 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010818 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 ws.mSurface = null;
10820 leakedSurface = true;
10821 }
10822 }
10823 }
Romain Guy06882f82009-06-10 13:36:04 -070010824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 boolean killedApps = false;
10826 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010827 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828 SparseIntArray pidCandidates = new SparseIntArray();
10829 for (int i=0; i<N; i++) {
10830 WindowState ws = (WindowState)mWindows.get(i);
10831 if (ws.mSurface != null) {
10832 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10833 }
10834 }
10835 if (pidCandidates.size() > 0) {
10836 int[] pids = new int[pidCandidates.size()];
10837 for (int i=0; i<pids.length; i++) {
10838 pids[i] = pidCandidates.keyAt(i);
10839 }
10840 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010841 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 killedApps = true;
10843 }
10844 } catch (RemoteException e) {
10845 }
10846 }
10847 }
Romain Guy06882f82009-06-10 13:36:04 -070010848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 if (leakedSurface || killedApps) {
10850 // We managed to reclaim some memory, so get rid of the trouble
10851 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010852 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010854 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010855 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 win.mSurface = null;
10857 }
Romain Guy06882f82009-06-10 13:36:04 -070010858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 try {
10860 win.mClient.dispatchGetNewSurface();
10861 } catch (RemoteException e) {
10862 }
10863 }
10864 } finally {
10865 Binder.restoreCallingIdentity(callingIdentity);
10866 }
10867 }
Romain Guy06882f82009-06-10 13:36:04 -070010868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 private boolean updateFocusedWindowLocked(int mode) {
10870 WindowState newFocus = computeFocusedWindowLocked();
10871 if (mCurrentFocus != newFocus) {
10872 // This check makes sure that we don't already have the focus
10873 // change message pending.
10874 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10875 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010876 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010877 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10878 final WindowState oldFocus = mCurrentFocus;
10879 mCurrentFocus = newFocus;
10880 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 final WindowState imWindow = mInputMethodWindow;
10883 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010884 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010886 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10887 mLayoutNeeded = true;
10888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10890 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010891 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10892 // Client will do the layout, but we need to assign layers
10893 // for handleNewWindowLocked() below.
10894 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895 }
10896 }
Romain Guy06882f82009-06-10 13:36:04 -070010897
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010898 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10899 mKeyWaiter.handleNewWindowLocked(newFocus);
10900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 return true;
10902 }
10903 return false;
10904 }
10905
10906 private WindowState computeFocusedWindowLocked() {
10907 WindowState result = null;
10908 WindowState win;
10909
10910 int i = mWindows.size() - 1;
10911 int nextAppIndex = mAppTokens.size()-1;
10912 WindowToken nextApp = nextAppIndex >= 0
10913 ? mAppTokens.get(nextAppIndex) : null;
10914
10915 while (i >= 0) {
10916 win = (WindowState)mWindows.get(i);
10917
Joe Onorato8a9b2202010-02-26 18:56:32 -080010918 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 TAG, "Looking for focus: " + i
10920 + " = " + win
10921 + ", flags=" + win.mAttrs.flags
10922 + ", canReceive=" + win.canReceiveKeys());
10923
10924 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 // If this window's application has been removed, just skip it.
10927 if (thisApp != null && thisApp.removed) {
10928 i--;
10929 continue;
10930 }
Romain Guy06882f82009-06-10 13:36:04 -070010931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 // If there is a focused app, don't allow focus to go to any
10933 // windows below it. If this is an application window, step
10934 // through the app tokens until we find its app.
10935 if (thisApp != null && nextApp != null && thisApp != nextApp
10936 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10937 int origAppIndex = nextAppIndex;
10938 while (nextAppIndex > 0) {
10939 if (nextApp == mFocusedApp) {
10940 // Whoops, we are below the focused app... no focus
10941 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080010942 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 TAG, "Reached focused app: " + mFocusedApp);
10944 return null;
10945 }
10946 nextAppIndex--;
10947 nextApp = mAppTokens.get(nextAppIndex);
10948 if (nextApp == thisApp) {
10949 break;
10950 }
10951 }
10952 if (thisApp != nextApp) {
10953 // Uh oh, the app token doesn't exist! This shouldn't
10954 // happen, but if it does we can get totally hosed...
10955 // so restart at the original app.
10956 nextAppIndex = origAppIndex;
10957 nextApp = mAppTokens.get(nextAppIndex);
10958 }
10959 }
10960
10961 // Dispatch to this window if it is wants key events.
10962 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010963 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010964 TAG, "Found focus @ " + i + " = " + win);
10965 result = win;
10966 break;
10967 }
10968
10969 i--;
10970 }
10971
10972 return result;
10973 }
10974
10975 private void startFreezingDisplayLocked() {
10976 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010977 // Freezing the display also suspends key event delivery, to
10978 // keep events from going astray while the display is reconfigured.
10979 // If someone has changed orientation again while the screen is
10980 // still frozen, the events will continue to be blocked while the
10981 // successive orientation change is processed. To prevent spurious
10982 // ANRs, we reset the event dispatch timeout in this case.
10983 synchronized (mKeyWaiter) {
10984 mKeyWaiter.mWasFrozen = true;
10985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010986 return;
10987 }
Romain Guy06882f82009-06-10 13:36:04 -070010988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070010990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010992 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 if (mFreezeGcPending != 0) {
10994 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010995 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 mH.removeMessages(H.FORCE_GC);
10997 Runtime.getRuntime().gc();
10998 mFreezeGcPending = now;
10999 }
11000 } else {
11001 mFreezeGcPending = now;
11002 }
Romain Guy06882f82009-06-10 13:36:04 -070011003
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011004 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011007 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11008 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011009 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 mAppTransitionReady = true;
11011 }
Romain Guy06882f82009-06-10 13:36:04 -070011012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 if (PROFILE_ORIENTATION) {
11014 File file = new File("/data/system/frozen");
11015 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11016 }
11017 Surface.freezeDisplay(0);
11018 }
Romain Guy06882f82009-06-10 13:36:04 -070011019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 private void stopFreezingDisplayLocked() {
11021 if (!mDisplayFrozen) {
11022 return;
11023 }
Romain Guy06882f82009-06-10 13:36:04 -070011024
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011025 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11026 return;
11027 }
11028
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011029 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011031 mDisplayFrozen = false;
11032 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11033 if (PROFILE_ORIENTATION) {
11034 Debug.stopMethodTracing();
11035 }
11036 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011037
Chris Tate2ad63a92009-03-25 17:36:48 -070011038 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11039 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 synchronized (mKeyWaiter) {
11041 mKeyWaiter.mWasFrozen = true;
11042 mKeyWaiter.notifyAll();
11043 }
11044
Christopher Tateb696aee2010-04-02 19:08:30 -070011045 // While the display is frozen we don't re-compute the orientation
11046 // to avoid inconsistent states. However, something interesting
11047 // could have actually changed during that time so re-evaluate it
11048 // now to catch that.
11049 if (updateOrientationFromAppTokensLocked()) {
11050 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11051 }
11052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011053 // A little kludge: a lot could have happened while the
11054 // display was frozen, so now that we are coming back we
11055 // do a gc so that any remote references the system
11056 // processes holds on others can be released if they are
11057 // no longer needed.
11058 mH.removeMessages(H.FORCE_GC);
11059 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11060 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 mScreenFrozenLock.release();
11063 }
Romain Guy06882f82009-06-10 13:36:04 -070011064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011065 @Override
11066 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11067 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11068 != PackageManager.PERMISSION_GRANTED) {
11069 pw.println("Permission Denial: can't dump WindowManager from from pid="
11070 + Binder.getCallingPid()
11071 + ", uid=" + Binder.getCallingUid());
11072 return;
11073 }
Romain Guy06882f82009-06-10 13:36:04 -070011074
Dianne Hackborna2e92262010-03-02 17:19:29 -080011075 pw.println("Input State:");
11076 mQueue.dump(pw, " ");
11077 pw.println(" ");
11078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 synchronized(mWindowMap) {
11080 pw.println("Current Window Manager state:");
11081 for (int i=mWindows.size()-1; i>=0; i--) {
11082 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011083 pw.print(" Window #"); pw.print(i); pw.print(' ');
11084 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011085 w.dump(pw, " ");
11086 }
11087 if (mInputMethodDialogs.size() > 0) {
11088 pw.println(" ");
11089 pw.println(" Input method dialogs:");
11090 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11091 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011092 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 }
11094 }
11095 if (mPendingRemove.size() > 0) {
11096 pw.println(" ");
11097 pw.println(" Remove pending for:");
11098 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11099 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011100 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11101 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011102 w.dump(pw, " ");
11103 }
11104 }
11105 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11106 pw.println(" ");
11107 pw.println(" Windows force removing:");
11108 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11109 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011110 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11111 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 w.dump(pw, " ");
11113 }
11114 }
11115 if (mDestroySurface.size() > 0) {
11116 pw.println(" ");
11117 pw.println(" Windows waiting to destroy their surface:");
11118 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11119 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011120 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11121 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 w.dump(pw, " ");
11123 }
11124 }
11125 if (mLosingFocus.size() > 0) {
11126 pw.println(" ");
11127 pw.println(" Windows losing focus:");
11128 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11129 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011130 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11131 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 w.dump(pw, " ");
11133 }
11134 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011135 if (mResizingWindows.size() > 0) {
11136 pw.println(" ");
11137 pw.println(" Windows waiting to resize:");
11138 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11139 WindowState w = mResizingWindows.get(i);
11140 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11141 pw.print(w); pw.println(":");
11142 w.dump(pw, " ");
11143 }
11144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 if (mSessions.size() > 0) {
11146 pw.println(" ");
11147 pw.println(" All active sessions:");
11148 Iterator<Session> it = mSessions.iterator();
11149 while (it.hasNext()) {
11150 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011151 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 s.dump(pw, " ");
11153 }
11154 }
11155 if (mTokenMap.size() > 0) {
11156 pw.println(" ");
11157 pw.println(" All tokens:");
11158 Iterator<WindowToken> it = mTokenMap.values().iterator();
11159 while (it.hasNext()) {
11160 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011161 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 token.dump(pw, " ");
11163 }
11164 }
11165 if (mTokenList.size() > 0) {
11166 pw.println(" ");
11167 pw.println(" Window token list:");
11168 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011169 pw.print(" #"); pw.print(i); pw.print(": ");
11170 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 }
11172 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011173 if (mWallpaperTokens.size() > 0) {
11174 pw.println(" ");
11175 pw.println(" Wallpaper tokens:");
11176 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11177 WindowToken token = mWallpaperTokens.get(i);
11178 pw.print(" Wallpaper #"); pw.print(i);
11179 pw.print(' '); pw.print(token); pw.println(':');
11180 token.dump(pw, " ");
11181 }
11182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 if (mAppTokens.size() > 0) {
11184 pw.println(" ");
11185 pw.println(" Application tokens in Z order:");
11186 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011187 pw.print(" App #"); pw.print(i); pw.print(": ");
11188 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 }
11190 }
11191 if (mFinishedStarting.size() > 0) {
11192 pw.println(" ");
11193 pw.println(" Finishing start of application tokens:");
11194 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11195 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011196 pw.print(" Finished Starting #"); pw.print(i);
11197 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011198 token.dump(pw, " ");
11199 }
11200 }
11201 if (mExitingTokens.size() > 0) {
11202 pw.println(" ");
11203 pw.println(" Exiting tokens:");
11204 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11205 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011206 pw.print(" Exiting #"); pw.print(i);
11207 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208 token.dump(pw, " ");
11209 }
11210 }
11211 if (mExitingAppTokens.size() > 0) {
11212 pw.println(" ");
11213 pw.println(" Exiting application tokens:");
11214 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11215 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011216 pw.print(" Exiting App #"); pw.print(i);
11217 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 token.dump(pw, " ");
11219 }
11220 }
11221 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011222 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11223 pw.print(" mLastFocus="); pw.println(mLastFocus);
11224 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11225 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11226 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011227 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011228 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11229 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11230 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11231 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011232 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11233 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11234 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011235 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11236 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11237 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11238 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011239 if (mDimAnimator != null) {
11240 mDimAnimator.printTo(pw);
11241 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011242 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011243 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011244 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011245 pw.print(mInputMethodAnimLayerAdjustment);
11246 pw.print(" mWallpaperAnimLayerAdjustment=");
11247 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011248 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11249 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011250 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11251 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011252 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11253 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011254 pw.print(" mRotation="); pw.print(mRotation);
11255 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11256 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11257 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11258 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11259 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11260 pw.print(" mNextAppTransition=0x");
11261 pw.print(Integer.toHexString(mNextAppTransition));
11262 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011263 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011264 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011265 if (mNextAppTransitionPackage != null) {
11266 pw.print(" mNextAppTransitionPackage=");
11267 pw.print(mNextAppTransitionPackage);
11268 pw.print(", mNextAppTransitionEnter=0x");
11269 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11270 pw.print(", mNextAppTransitionExit=0x");
11271 pw.print(Integer.toHexString(mNextAppTransitionExit));
11272 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011273 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11274 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011275 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11276 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11277 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11278 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011279 if (mOpeningApps.size() > 0) {
11280 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11281 }
11282 if (mClosingApps.size() > 0) {
11283 pw.print(" mClosingApps="); pw.println(mClosingApps);
11284 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011285 if (mToTopApps.size() > 0) {
11286 pw.print(" mToTopApps="); pw.println(mToTopApps);
11287 }
11288 if (mToBottomApps.size() > 0) {
11289 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11290 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011291 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11292 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011293 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011294 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11295 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11296 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11297 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11298 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11299 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011300 }
11301 }
11302
11303 public void monitor() {
11304 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011305 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011306 synchronized (mKeyWaiter) { }
11307 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011308
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011309 public void virtualKeyFeedback(KeyEvent event) {
11310 mPolicy.keyFeedbackFromInput(event);
11311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011312
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011313 /**
11314 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011315 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011316 */
11317 private static class DimAnimator {
11318 Surface mDimSurface;
11319 boolean mDimShown = false;
11320 float mDimCurrentAlpha;
11321 float mDimTargetAlpha;
11322 float mDimDeltaPerMs;
11323 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011324
11325 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011326
11327 DimAnimator (SurfaceSession session) {
11328 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011329 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011330 + mDimSurface + ": CREATE");
11331 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011332 mDimSurface = new Surface(session, 0,
11333 "DimSurface",
11334 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011335 Surface.FX_SURFACE_DIM);
11336 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011337 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011338 }
11339 }
11340 }
11341
11342 /**
11343 * Show the dim surface.
11344 */
11345 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011346 if (!mDimShown) {
11347 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11348 dw + "x" + dh + ")");
11349 mDimShown = true;
11350 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011351 mLastDimWidth = dw;
11352 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011353 mDimSurface.setPosition(0, 0);
11354 mDimSurface.setSize(dw, dh);
11355 mDimSurface.show();
11356 } catch (RuntimeException e) {
11357 Slog.w(TAG, "Failure showing dim surface", e);
11358 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011359 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11360 mLastDimWidth = dw;
11361 mLastDimHeight = dh;
11362 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011363 }
11364 }
11365
11366 /**
11367 * Set's the dim surface's layer and update dim parameters that will be used in
11368 * {@link updateSurface} after all windows are examined.
11369 */
11370 void updateParameters(WindowState w, long currentTime) {
11371 mDimSurface.setLayer(w.mAnimLayer-1);
11372
11373 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011374 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011375 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011376 if (mDimTargetAlpha != target) {
11377 // If the desired dim level has changed, then
11378 // start an animation to it.
11379 mLastDimAnimTime = currentTime;
11380 long duration = (w.mAnimating && w.mAnimation != null)
11381 ? w.mAnimation.computeDurationHint()
11382 : DEFAULT_DIM_DURATION;
11383 if (target > mDimTargetAlpha) {
11384 // This is happening behind the activity UI,
11385 // so we can make it run a little longer to
11386 // give a stronger impression without disrupting
11387 // the user.
11388 duration *= DIM_DURATION_MULTIPLIER;
11389 }
11390 if (duration < 1) {
11391 // Don't divide by zero
11392 duration = 1;
11393 }
11394 mDimTargetAlpha = target;
11395 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11396 }
11397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011398
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011399 /**
11400 * Updating the surface's alpha. Returns true if the animation continues, or returns
11401 * false when the animation is finished and the dim surface is hidden.
11402 */
11403 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11404 if (!dimming) {
11405 if (mDimTargetAlpha != 0) {
11406 mLastDimAnimTime = currentTime;
11407 mDimTargetAlpha = 0;
11408 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11409 }
11410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011411
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011412 boolean animating = false;
11413 if (mLastDimAnimTime != 0) {
11414 mDimCurrentAlpha += mDimDeltaPerMs
11415 * (currentTime-mLastDimAnimTime);
11416 boolean more = true;
11417 if (displayFrozen) {
11418 // If the display is frozen, there is no reason to animate.
11419 more = false;
11420 } else if (mDimDeltaPerMs > 0) {
11421 if (mDimCurrentAlpha > mDimTargetAlpha) {
11422 more = false;
11423 }
11424 } else if (mDimDeltaPerMs < 0) {
11425 if (mDimCurrentAlpha < mDimTargetAlpha) {
11426 more = false;
11427 }
11428 } else {
11429 more = false;
11430 }
11431
11432 // Do we need to continue animating?
11433 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011434 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011435 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11436 mLastDimAnimTime = currentTime;
11437 mDimSurface.setAlpha(mDimCurrentAlpha);
11438 animating = true;
11439 } else {
11440 mDimCurrentAlpha = mDimTargetAlpha;
11441 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011442 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011443 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11444 mDimSurface.setAlpha(mDimCurrentAlpha);
11445 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011446 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011447 + ": HIDE");
11448 try {
11449 mDimSurface.hide();
11450 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011451 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011452 }
11453 mDimShown = false;
11454 }
11455 }
11456 }
11457 return animating;
11458 }
11459
11460 public void printTo(PrintWriter pw) {
11461 pw.print(" mDimShown="); pw.print(mDimShown);
11462 pw.print(" current="); pw.print(mDimCurrentAlpha);
11463 pw.print(" target="); pw.print(mDimTargetAlpha);
11464 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11465 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11466 }
11467 }
11468
11469 /**
11470 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11471 * This is used for opening/closing transition for apps in compatible mode.
11472 */
11473 private static class FadeInOutAnimation extends Animation {
11474 int mWidth;
11475 boolean mFadeIn;
11476
11477 public FadeInOutAnimation(boolean fadeIn) {
11478 setInterpolator(new AccelerateInterpolator());
11479 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11480 mFadeIn = fadeIn;
11481 }
11482
11483 @Override
11484 protected void applyTransformation(float interpolatedTime, Transformation t) {
11485 float x = interpolatedTime;
11486 if (!mFadeIn) {
11487 x = 1.0f - x; // reverse the interpolation for fade out
11488 }
11489 if (x < 0.5) {
11490 // move the window out of the screen.
11491 t.getMatrix().setTranslate(mWidth, 0);
11492 } else {
11493 t.getMatrix().setTranslate(0, 0);// show
11494 t.setAlpha((x - 0.5f) * 2);
11495 }
11496 }
11497
11498 @Override
11499 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11500 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11501 mWidth = width;
11502 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011503
11504 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011505 public int getZAdjustment() {
11506 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011507 }
11508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509}