blob: a1c08fd1d63c477294146526135836fe11a3cda9 [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()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 lastKeyTime = curTime;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006534 if (lastKey != null &&
6535 ke.getKeyCode() == lastKey.getKeyCode()) {
6536 keyRepeatCount++;
6537 // Arbitrary long timeout to block
6538 // repeating here since we know that
6539 // the device driver takes care of it.
6540 nextKeyTime = lastKeyTime + LONG_WAIT;
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006541 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006542 TAG, "Received repeated key down");
6543 } else {
6544 downTime = curTime;
6545 keyRepeatCount = 0;
6546 nextKeyTime = lastKeyTime
6547 + ViewConfiguration.getLongPressTimeout();
The Android Open Source Project2a9ae012010-05-12 12:33:35 -07006548 if (DEBUG_INPUT) Slog.v(
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006549 TAG, "Received key down: first repeat @ "
6550 + nextKeyTime);
6551 }
6552 lastKey = ke;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 } else {
6554 lastKey = null;
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006555 downTime = 0;
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006556 keyRepeatCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 // Arbitrary long timeout.
6558 lastKeyTime = curTime;
6559 nextKeyTime = curTime + LONG_WAIT;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006560 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 TAG, "Received key up: ignore repeat @ "
6562 + nextKeyTime);
6563 }
Kristian Dreher133bfdf2010-02-23 08:50:58 +01006564 if (keyRepeatCount > 0) {
6565 dispatchKey(KeyEvent.changeTimeRepeat(ke,
6566 ke.getEventTime(), keyRepeatCount), 0, 0);
6567 } else {
6568 dispatchKey(ke, 0, 0);
6569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 mQueue.recycleEvent(ev);
6571 break;
6572 case RawInputEvent.CLASS_TOUCHSCREEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08006573 //Slog.i(TAG, "Read next event " + ev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 dispatchPointer(ev, (MotionEvent)ev.event, 0, 0);
6575 break;
6576 case RawInputEvent.CLASS_TRACKBALL:
6577 dispatchTrackball(ev, (MotionEvent)ev.event, 0, 0);
6578 break;
6579 case RawInputEvent.CLASS_CONFIGURATION_CHANGED:
6580 configChanged = true;
6581 break;
6582 default:
6583 mQueue.recycleEvent(ev);
6584 break;
6585 }
Romain Guy06882f82009-06-10 13:36:04 -07006586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006587 } else if (configChanged) {
6588 configChanged = false;
6589 sendNewConfiguration();
Romain Guy06882f82009-06-10 13:36:04 -07006590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 } else if (lastKey != null) {
6592 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 // Timeout occurred while key was down. If it is at or
6595 // past the key repeat time, dispatch the repeat.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006596 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 TAG, "Key timeout: repeat=" + nextKeyTime
6598 + ", now=" + curTime);
6599 if (curTime < nextKeyTime) {
6600 continue;
6601 }
Romain Guy06882f82009-06-10 13:36:04 -07006602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 lastKeyTime = nextKeyTime;
6604 nextKeyTime = nextKeyTime + KEY_REPEAT_DELAY;
6605 keyRepeatCount++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006606 if (DEBUG_INPUT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 TAG, "Key repeat: count=" + keyRepeatCount
6608 + ", next @ " + nextKeyTime);
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006609 KeyEvent newEvent;
6610 if (downTime != 0 && (downTime
6611 + ViewConfiguration.getLongPressTimeout())
6612 <= curTime) {
6613 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6614 curTime, keyRepeatCount,
6615 lastKey.getFlags() | KeyEvent.FLAG_LONG_PRESS);
6616 downTime = 0;
6617 } else {
6618 newEvent = KeyEvent.changeTimeRepeat(lastKey,
6619 curTime, keyRepeatCount);
6620 }
6621 dispatchKey(newEvent, 0, 0);
Romain Guy06882f82009-06-10 13:36:04 -07006622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 } else {
6624 curTime = SystemClock.uptimeMillis();
Romain Guy06882f82009-06-10 13:36:04 -07006625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 lastKeyTime = curTime;
6627 nextKeyTime = curTime + LONG_WAIT;
6628 }
Romain Guy06882f82009-06-10 13:36:04 -07006629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006631 Slog.e(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 "Input thread received uncaught exception: " + e, e);
6633 }
6634 }
6635 }
6636 }
6637
6638 // -------------------------------------------------------------
6639 // Client Session State
6640 // -------------------------------------------------------------
6641
6642 private final class Session extends IWindowSession.Stub
6643 implements IBinder.DeathRecipient {
6644 final IInputMethodClient mClient;
6645 final IInputContext mInputContext;
6646 final int mUid;
6647 final int mPid;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006648 final String mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 SurfaceSession mSurfaceSession;
6650 int mNumWindow = 0;
6651 boolean mClientDead = false;
Romain Guy06882f82009-06-10 13:36:04 -07006652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 /**
6654 * Current pointer move event being dispatched to client window... must
6655 * hold key lock to access.
6656 */
6657 QueuedEvent mPendingPointerMove;
6658 WindowState mPendingPointerWindow;
Romain Guy06882f82009-06-10 13:36:04 -07006659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 /**
6661 * Current trackball move event being dispatched to client window... must
6662 * hold key lock to access.
6663 */
6664 QueuedEvent mPendingTrackballMove;
6665 WindowState mPendingTrackballWindow;
6666
6667 public Session(IInputMethodClient client, IInputContext inputContext) {
6668 mClient = client;
6669 mInputContext = inputContext;
6670 mUid = Binder.getCallingUid();
6671 mPid = Binder.getCallingPid();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006672 StringBuilder sb = new StringBuilder();
6673 sb.append("Session{");
6674 sb.append(Integer.toHexString(System.identityHashCode(this)));
6675 sb.append(" uid ");
6676 sb.append(mUid);
6677 sb.append("}");
6678 mStringName = sb.toString();
Romain Guy06882f82009-06-10 13:36:04 -07006679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 synchronized (mWindowMap) {
6681 if (mInputMethodManager == null && mHaveInputMethods) {
6682 IBinder b = ServiceManager.getService(
6683 Context.INPUT_METHOD_SERVICE);
6684 mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
6685 }
6686 }
6687 long ident = Binder.clearCallingIdentity();
6688 try {
6689 // Note: it is safe to call in to the input method manager
6690 // here because we are not holding our lock.
6691 if (mInputMethodManager != null) {
6692 mInputMethodManager.addClient(client, inputContext,
6693 mUid, mPid);
6694 } else {
6695 client.setUsingInputMethod(false);
6696 }
6697 client.asBinder().linkToDeath(this, 0);
6698 } catch (RemoteException e) {
6699 // The caller has died, so we can just forget about this.
6700 try {
6701 if (mInputMethodManager != null) {
6702 mInputMethodManager.removeClient(client);
6703 }
6704 } catch (RemoteException ee) {
6705 }
6706 } finally {
6707 Binder.restoreCallingIdentity(ident);
6708 }
6709 }
Romain Guy06882f82009-06-10 13:36:04 -07006710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 @Override
6712 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
6713 throws RemoteException {
6714 try {
6715 return super.onTransact(code, data, reply, flags);
6716 } catch (RuntimeException e) {
6717 // Log all 'real' exceptions thrown to the caller
6718 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006719 Slog.e(TAG, "Window Session Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006720 }
6721 throw e;
6722 }
6723 }
6724
6725 public void binderDied() {
6726 // Note: it is safe to call in to the input method manager
6727 // here because we are not holding our lock.
6728 try {
6729 if (mInputMethodManager != null) {
6730 mInputMethodManager.removeClient(mClient);
6731 }
6732 } catch (RemoteException e) {
6733 }
6734 synchronized(mWindowMap) {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07006735 mClient.asBinder().unlinkToDeath(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 mClientDead = true;
6737 killSessionLocked();
6738 }
6739 }
6740
6741 public int add(IWindow window, WindowManager.LayoutParams attrs,
6742 int viewVisibility, Rect outContentInsets) {
6743 return addWindow(this, window, attrs, viewVisibility, outContentInsets);
6744 }
Romain Guy06882f82009-06-10 13:36:04 -07006745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 public void remove(IWindow window) {
6747 removeWindow(this, window);
6748 }
Romain Guy06882f82009-06-10 13:36:04 -07006749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 public int relayout(IWindow window, WindowManager.LayoutParams attrs,
6751 int requestedWidth, int requestedHeight, int viewFlags,
6752 boolean insetsPending, Rect outFrame, Rect outContentInsets,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006753 Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 return relayoutWindow(this, window, attrs,
6755 requestedWidth, requestedHeight, viewFlags, insetsPending,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07006756 outFrame, outContentInsets, outVisibleInsets, outConfig, outSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 }
Romain Guy06882f82009-06-10 13:36:04 -07006758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 public void setTransparentRegion(IWindow window, Region region) {
6760 setTransparentRegionWindow(this, window, region);
6761 }
Romain Guy06882f82009-06-10 13:36:04 -07006762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006763 public void setInsets(IWindow window, int touchableInsets,
6764 Rect contentInsets, Rect visibleInsets) {
6765 setInsetsWindow(this, window, touchableInsets, contentInsets,
6766 visibleInsets);
6767 }
Romain Guy06882f82009-06-10 13:36:04 -07006768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 public void getDisplayFrame(IWindow window, Rect outDisplayFrame) {
6770 getWindowDisplayFrame(this, window, outDisplayFrame);
6771 }
Romain Guy06882f82009-06-10 13:36:04 -07006772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 public void finishDrawing(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006774 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 TAG, "IWindow finishDrawing called for " + window);
6776 finishDrawingWindow(this, window);
6777 }
6778
6779 public void finishKey(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006780 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 TAG, "IWindow finishKey called for " + window);
6782 mKeyWaiter.finishedKey(this, window, false,
6783 KeyWaiter.RETURN_NOTHING);
6784 }
6785
6786 public MotionEvent getPendingPointerMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006787 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 TAG, "IWindow getPendingMotionEvent called for " + window);
6789 return mKeyWaiter.finishedKey(this, window, false,
6790 KeyWaiter.RETURN_PENDING_POINTER);
6791 }
Romain Guy06882f82009-06-10 13:36:04 -07006792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006793 public MotionEvent getPendingTrackballMove(IWindow window) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006794 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 TAG, "IWindow getPendingMotionEvent called for " + window);
6796 return mKeyWaiter.finishedKey(this, window, false,
6797 KeyWaiter.RETURN_PENDING_TRACKBALL);
6798 }
6799
6800 public void setInTouchMode(boolean mode) {
6801 synchronized(mWindowMap) {
6802 mInTouchMode = mode;
6803 }
6804 }
6805
6806 public boolean getInTouchMode() {
6807 synchronized(mWindowMap) {
6808 return mInTouchMode;
6809 }
6810 }
6811
6812 public boolean performHapticFeedback(IWindow window, int effectId,
6813 boolean always) {
6814 synchronized(mWindowMap) {
6815 long ident = Binder.clearCallingIdentity();
6816 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07006817 return mPolicy.performHapticFeedbackLw(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006818 windowForClientLocked(this, window, true),
6819 effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 } finally {
6821 Binder.restoreCallingIdentity(ident);
6822 }
6823 }
6824 }
Romain Guy06882f82009-06-10 13:36:04 -07006825
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006826 public void setWallpaperPosition(IBinder window, float x, float y, float xStep, float yStep) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006827 synchronized(mWindowMap) {
6828 long ident = Binder.clearCallingIdentity();
6829 try {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006830 setWindowWallpaperPositionLocked(
6831 windowForClientLocked(this, window, true),
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006832 x, y, xStep, yStep);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07006833 } finally {
6834 Binder.restoreCallingIdentity(ident);
6835 }
6836 }
6837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006838
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006839 public void wallpaperOffsetsComplete(IBinder window) {
6840 WindowManagerService.this.wallpaperOffsetsComplete(window);
6841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006842
Dianne Hackborn75804932009-10-20 20:15:20 -07006843 public Bundle sendWallpaperCommand(IBinder window, String action, int x, int y,
6844 int z, Bundle extras, boolean sync) {
6845 synchronized(mWindowMap) {
6846 long ident = Binder.clearCallingIdentity();
6847 try {
6848 return sendWindowWallpaperCommandLocked(
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006849 windowForClientLocked(this, window, true),
Dianne Hackborn75804932009-10-20 20:15:20 -07006850 action, x, y, z, extras, sync);
6851 } finally {
6852 Binder.restoreCallingIdentity(ident);
6853 }
6854 }
6855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006856
Dianne Hackborn75804932009-10-20 20:15:20 -07006857 public void wallpaperCommandComplete(IBinder window, Bundle result) {
6858 WindowManagerService.this.wallpaperCommandComplete(window, result);
6859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 void windowAddedLocked() {
6862 if (mSurfaceSession == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006863 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864 TAG, "First window added to " + this + ", creating SurfaceSession");
6865 mSurfaceSession = new SurfaceSession();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006866 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006867 TAG, " NEW SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 mSessions.add(this);
6869 }
6870 mNumWindow++;
6871 }
6872
6873 void windowRemovedLocked() {
6874 mNumWindow--;
6875 killSessionLocked();
6876 }
Romain Guy06882f82009-06-10 13:36:04 -07006877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 void killSessionLocked() {
6879 if (mNumWindow <= 0 && mClientDead) {
6880 mSessions.remove(this);
6881 if (mSurfaceSession != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006882 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 TAG, "Last window removed from " + this
6884 + ", destroying " + mSurfaceSession);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006885 if (SHOW_TRANSACTIONS) Slog.i(
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07006886 TAG, " KILL SURFACE SESSION " + mSurfaceSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 try {
6888 mSurfaceSession.kill();
6889 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006890 Slog.w(TAG, "Exception thrown when killing surface session "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 + mSurfaceSession + " in session " + this
6892 + ": " + e.toString());
6893 }
6894 mSurfaceSession = null;
6895 }
6896 }
6897 }
Romain Guy06882f82009-06-10 13:36:04 -07006898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006900 pw.print(prefix); pw.print("mNumWindow="); pw.print(mNumWindow);
6901 pw.print(" mClientDead="); pw.print(mClientDead);
6902 pw.print(" mSurfaceSession="); pw.println(mSurfaceSession);
6903 if (mPendingPointerWindow != null || mPendingPointerMove != null) {
6904 pw.print(prefix);
6905 pw.print("mPendingPointerWindow="); pw.print(mPendingPointerWindow);
6906 pw.print(" mPendingPointerMove="); pw.println(mPendingPointerMove);
6907 }
6908 if (mPendingTrackballWindow != null || mPendingTrackballMove != null) {
6909 pw.print(prefix);
6910 pw.print("mPendingTrackballWindow="); pw.print(mPendingTrackballWindow);
6911 pw.print(" mPendingTrackballMove="); pw.println(mPendingTrackballMove);
6912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 }
6914
6915 @Override
6916 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006917 return mStringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 }
6919 }
6920
6921 // -------------------------------------------------------------
6922 // Client Window State
6923 // -------------------------------------------------------------
6924
6925 private final class WindowState implements WindowManagerPolicy.WindowState {
6926 final Session mSession;
6927 final IWindow mClient;
6928 WindowToken mToken;
The Android Open Source Project10592532009-03-18 17:39:46 -07006929 WindowToken mRootToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 AppWindowToken mAppToken;
6931 AppWindowToken mTargetAppToken;
6932 final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
6933 final DeathRecipient mDeathRecipient;
6934 final WindowState mAttachedWindow;
6935 final ArrayList mChildWindows = new ArrayList();
6936 final int mBaseLayer;
6937 final int mSubLayer;
6938 final boolean mLayoutAttached;
6939 final boolean mIsImWindow;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07006940 final boolean mIsWallpaper;
6941 final boolean mIsFloatingLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 int mViewVisibility;
6943 boolean mPolicyVisibility = true;
6944 boolean mPolicyVisibilityAfterAnim = true;
6945 boolean mAppFreezing;
6946 Surface mSurface;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07006947 boolean mReportDestroySurface;
6948 boolean mSurfacePendingDestroy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 boolean mAttachedHidden; // is our parent window hidden?
6950 boolean mLastHidden; // was this window last hidden?
Dianne Hackborn759a39e2009-08-09 17:20:27 -07006951 boolean mWallpaperVisible; // for wallpaper, what was last vis report?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 int mRequestedWidth;
6953 int mRequestedHeight;
6954 int mLastRequestedWidth;
6955 int mLastRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 int mLayer;
6957 int mAnimLayer;
6958 int mLastLayer;
6959 boolean mHaveFrame;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07006960 boolean mObscured;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07006961 boolean mTurnOnScreen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962
6963 WindowState mNextOutsideTouch;
Romain Guy06882f82009-06-10 13:36:04 -07006964
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006965 int mLayoutSeq = -1;
6966
6967 Configuration mConfiguration = null;
6968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 // Actual frame shown on-screen (may be modified by animation)
6970 final Rect mShownFrame = new Rect();
6971 final Rect mLastShownFrame = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07006972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 /**
Dianne Hackbornac3587d2010-03-11 11:12:11 -08006974 * Set when we have changed the size of the surface, to know that
6975 * we must tell them application to resize (and thus redraw itself).
6976 */
6977 boolean mSurfaceResized;
6978
6979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 * Insets that determine the actually visible area
6981 */
6982 final Rect mVisibleInsets = new Rect();
6983 final Rect mLastVisibleInsets = new Rect();
6984 boolean mVisibleInsetsChanged;
6985
6986 /**
6987 * Insets that are covered by system windows
6988 */
6989 final Rect mContentInsets = new Rect();
6990 final Rect mLastContentInsets = new Rect();
6991 boolean mContentInsetsChanged;
6992
6993 /**
6994 * Set to true if we are waiting for this window to receive its
6995 * given internal insets before laying out other windows based on it.
6996 */
6997 boolean mGivenInsetsPending;
Romain Guy06882f82009-06-10 13:36:04 -07006998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 /**
7000 * These are the content insets that were given during layout for
7001 * this window, to be applied to windows behind it.
7002 */
7003 final Rect mGivenContentInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 /**
7006 * These are the visible insets that were given during layout for
7007 * this window, to be applied to windows behind it.
7008 */
7009 final Rect mGivenVisibleInsets = new Rect();
Romain Guy06882f82009-06-10 13:36:04 -07007010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 /**
7012 * Flag indicating whether the touchable region should be adjusted by
7013 * the visible insets; if false the area outside the visible insets is
7014 * NOT touchable, so we must use those to adjust the frame during hit
7015 * tests.
7016 */
7017 int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
Romain Guy06882f82009-06-10 13:36:04 -07007018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 // Current transformation being applied.
7020 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
7021 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
7022 float mHScale=1, mVScale=1;
7023 float mLastHScale=1, mLastVScale=1;
7024 final Matrix mTmpMatrix = new Matrix();
7025
7026 // "Real" frame that the application sees.
7027 final Rect mFrame = new Rect();
7028 final Rect mLastFrame = new Rect();
7029
7030 final Rect mContainingFrame = new Rect();
7031 final Rect mDisplayFrame = new Rect();
7032 final Rect mContentFrame = new Rect();
7033 final Rect mVisibleFrame = new Rect();
7034
7035 float mShownAlpha = 1;
7036 float mAlpha = 1;
7037 float mLastAlpha = 1;
7038
7039 // Set to true if, when the window gets displayed, it should perform
7040 // an enter animation.
7041 boolean mEnterAnimationPending;
7042
7043 // Currently running animation.
7044 boolean mAnimating;
7045 boolean mLocalAnimating;
7046 Animation mAnimation;
7047 boolean mAnimationIsEntrance;
7048 boolean mHasTransformation;
7049 boolean mHasLocalTransformation;
7050 final Transformation mTransformation = new Transformation();
7051
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007052 // If a window showing a wallpaper: the requested offset for the
7053 // wallpaper; if a wallpaper window: the currently applied offset.
7054 float mWallpaperX = -1;
7055 float mWallpaperY = -1;
Marco Nelissenbf6956b2009-11-09 15:21:13 -08007056
7057 // If a window showing a wallpaper: what fraction of the offset
7058 // range corresponds to a full virtual screen.
7059 float mWallpaperXStep = -1;
7060 float mWallpaperYStep = -1;
7061
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007062 // Wallpaper windows: pixels offset based on above variables.
7063 int mXOffset;
7064 int mYOffset;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 // This is set after IWindowSession.relayout() has been called at
7067 // least once for the window. It allows us to detect the situation
7068 // where we don't yet have a surface, but should have one soon, so
7069 // we can give the window focus before waiting for the relayout.
7070 boolean mRelayoutCalled;
Romain Guy06882f82009-06-10 13:36:04 -07007071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 // This is set after the Surface has been created but before the
7073 // window has been drawn. During this time the surface is hidden.
7074 boolean mDrawPending;
7075
7076 // This is set after the window has finished drawing for the first
7077 // time but before its surface is shown. The surface will be
7078 // displayed when the next layout is run.
7079 boolean mCommitDrawPending;
7080
7081 // This is set during the time after the window's drawing has been
7082 // committed, and before its surface is actually shown. It is used
7083 // to delay showing the surface until all windows in a token are ready
7084 // to be shown.
7085 boolean mReadyToShow;
Romain Guy06882f82009-06-10 13:36:04 -07007086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 // Set when the window has been shown in the screen the first time.
7088 boolean mHasDrawn;
7089
7090 // Currently running an exit animation?
7091 boolean mExiting;
7092
7093 // Currently on the mDestroySurface list?
7094 boolean mDestroying;
Romain Guy06882f82009-06-10 13:36:04 -07007095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 // Completely remove from window manager after exit animation?
7097 boolean mRemoveOnExit;
7098
7099 // Set when the orientation is changing and this window has not yet
7100 // been updated for the new orientation.
7101 boolean mOrientationChanging;
Romain Guy06882f82009-06-10 13:36:04 -07007102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 // Is this window now (or just being) removed?
7104 boolean mRemoved;
Romain Guy06882f82009-06-10 13:36:04 -07007105
Dianne Hackborn16064f92010-03-25 00:47:24 -07007106 // For debugging, this is the last information given to the surface flinger.
7107 boolean mSurfaceShown;
7108 int mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
7109 int mSurfaceLayer;
7110 float mSurfaceAlpha;
7111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 WindowState(Session s, IWindow c, WindowToken token,
7113 WindowState attachedWindow, WindowManager.LayoutParams a,
7114 int viewVisibility) {
7115 mSession = s;
7116 mClient = c;
7117 mToken = token;
7118 mAttrs.copyFrom(a);
7119 mViewVisibility = viewVisibility;
7120 DeathRecipient deathRecipient = new DeathRecipient();
7121 mAlpha = a.alpha;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007122 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 TAG, "Window " + this + " client=" + c.asBinder()
7124 + " token=" + token + " (" + mAttrs.token + ")");
7125 try {
7126 c.asBinder().linkToDeath(deathRecipient, 0);
7127 } catch (RemoteException e) {
7128 mDeathRecipient = null;
7129 mAttachedWindow = null;
7130 mLayoutAttached = false;
7131 mIsImWindow = false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007132 mIsWallpaper = false;
7133 mIsFloatingLayer = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 mBaseLayer = 0;
7135 mSubLayer = 0;
7136 return;
7137 }
7138 mDeathRecipient = deathRecipient;
Romain Guy06882f82009-06-10 13:36:04 -07007139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 if ((mAttrs.type >= FIRST_SUB_WINDOW &&
7141 mAttrs.type <= LAST_SUB_WINDOW)) {
7142 // The multiplier here is to reserve space for multiple
7143 // windows in the same type layer.
7144 mBaseLayer = mPolicy.windowTypeToLayerLw(
7145 attachedWindow.mAttrs.type) * TYPE_LAYER_MULTIPLIER
7146 + TYPE_LAYER_OFFSET;
7147 mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
7148 mAttachedWindow = attachedWindow;
7149 mAttachedWindow.mChildWindows.add(this);
7150 mLayoutAttached = mAttrs.type !=
7151 WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
7152 mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
7153 || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007154 mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
7155 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 } else {
7157 // The multiplier here is to reserve space for multiple
7158 // windows in the same type layer.
7159 mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
7160 * TYPE_LAYER_MULTIPLIER
7161 + TYPE_LAYER_OFFSET;
7162 mSubLayer = 0;
7163 mAttachedWindow = null;
7164 mLayoutAttached = false;
7165 mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
7166 || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07007167 mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
7168 mIsFloatingLayer = mIsImWindow || mIsWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 }
7170
7171 WindowState appWin = this;
7172 while (appWin.mAttachedWindow != null) {
7173 appWin = mAttachedWindow;
7174 }
7175 WindowToken appToken = appWin.mToken;
7176 while (appToken.appWindowToken == null) {
7177 WindowToken parent = mTokenMap.get(appToken.token);
7178 if (parent == null || appToken == parent) {
7179 break;
7180 }
7181 appToken = parent;
7182 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007183 mRootToken = appToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 mAppToken = appToken.appWindowToken;
7185
7186 mSurface = null;
7187 mRequestedWidth = 0;
7188 mRequestedHeight = 0;
7189 mLastRequestedWidth = 0;
7190 mLastRequestedHeight = 0;
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007191 mXOffset = 0;
7192 mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193 mLayer = 0;
7194 mAnimLayer = 0;
7195 mLastLayer = 0;
7196 }
7197
7198 void attach() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007199 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 TAG, "Attaching " + this + " token=" + mToken
7201 + ", list=" + mToken.windows);
7202 mSession.windowAddedLocked();
7203 }
7204
7205 public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
7206 mHaveFrame = true;
7207
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007208 final Rect container = mContainingFrame;
7209 container.set(pf);
7210
7211 final Rect display = mDisplayFrame;
7212 display.set(df);
7213
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07007214 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007215 container.intersect(mCompatibleScreenFrame);
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07007216 if ((mAttrs.flags & FLAG_LAYOUT_NO_LIMITS) == 0) {
7217 display.intersect(mCompatibleScreenFrame);
7218 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07007219 }
7220
7221 final int pw = container.right - container.left;
7222 final int ph = container.bottom - container.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007223
7224 int w,h;
7225 if ((mAttrs.flags & mAttrs.FLAG_SCALED) != 0) {
7226 w = mAttrs.width < 0 ? pw : mAttrs.width;
7227 h = mAttrs.height< 0 ? ph : mAttrs.height;
7228 } else {
Romain Guy980a9382010-01-08 15:06:28 -08007229 w = mAttrs.width == mAttrs.MATCH_PARENT ? pw : mRequestedWidth;
7230 h = mAttrs.height== mAttrs.MATCH_PARENT ? ph : mRequestedHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 }
Romain Guy06882f82009-06-10 13:36:04 -07007232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 final Rect content = mContentFrame;
7234 content.set(cf);
Romain Guy06882f82009-06-10 13:36:04 -07007235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007236 final Rect visible = mVisibleFrame;
7237 visible.set(vf);
Romain Guy06882f82009-06-10 13:36:04 -07007238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 final Rect frame = mFrame;
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007240 final int fw = frame.width();
7241 final int fh = frame.height();
Romain Guy06882f82009-06-10 13:36:04 -07007242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007243 //System.out.println("In: w=" + w + " h=" + h + " container=" +
7244 // container + " x=" + mAttrs.x + " y=" + mAttrs.y);
7245
7246 Gravity.apply(mAttrs.gravity, w, h, container,
7247 (int) (mAttrs.x + mAttrs.horizontalMargin * pw),
7248 (int) (mAttrs.y + mAttrs.verticalMargin * ph), frame);
7249
7250 //System.out.println("Out: " + mFrame);
7251
7252 // Now make sure the window fits in the overall display.
7253 Gravity.applyDisplay(mAttrs.gravity, df, frame);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 // Make sure the content and visible frames are inside of the
7256 // final window frame.
7257 if (content.left < frame.left) content.left = frame.left;
7258 if (content.top < frame.top) content.top = frame.top;
7259 if (content.right > frame.right) content.right = frame.right;
7260 if (content.bottom > frame.bottom) content.bottom = frame.bottom;
7261 if (visible.left < frame.left) visible.left = frame.left;
7262 if (visible.top < frame.top) visible.top = frame.top;
7263 if (visible.right > frame.right) visible.right = frame.right;
7264 if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007266 final Rect contentInsets = mContentInsets;
7267 contentInsets.left = content.left-frame.left;
7268 contentInsets.top = content.top-frame.top;
7269 contentInsets.right = frame.right-content.right;
7270 contentInsets.bottom = frame.bottom-content.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 final Rect visibleInsets = mVisibleInsets;
7273 visibleInsets.left = visible.left-frame.left;
7274 visibleInsets.top = visible.top-frame.top;
7275 visibleInsets.right = frame.right-visible.right;
7276 visibleInsets.bottom = frame.bottom-visible.bottom;
Romain Guy06882f82009-06-10 13:36:04 -07007277
Dianne Hackborn284ac932009-08-28 10:34:25 -07007278 if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
7279 updateWallpaperOffsetLocked(this, mDisplay.getWidth(),
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007280 mDisplay.getHeight(), false);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -07007281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 if (localLOGV) {
7284 //if ("com.google.android.youtube".equals(mAttrs.packageName)
7285 // && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007286 Slog.v(TAG, "Resolving (mRequestedWidth="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 + mRequestedWidth + ", mRequestedheight="
7288 + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
7289 + "): frame=" + mFrame.toShortString()
7290 + " ci=" + contentInsets.toShortString()
7291 + " vi=" + visibleInsets.toShortString());
7292 //}
7293 }
7294 }
Romain Guy06882f82009-06-10 13:36:04 -07007295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 public Rect getFrameLw() {
7297 return mFrame;
7298 }
7299
7300 public Rect getShownFrameLw() {
7301 return mShownFrame;
7302 }
7303
7304 public Rect getDisplayFrameLw() {
7305 return mDisplayFrame;
7306 }
7307
7308 public Rect getContentFrameLw() {
7309 return mContentFrame;
7310 }
7311
7312 public Rect getVisibleFrameLw() {
7313 return mVisibleFrame;
7314 }
7315
7316 public boolean getGivenInsetsPendingLw() {
7317 return mGivenInsetsPending;
7318 }
7319
7320 public Rect getGivenContentInsetsLw() {
7321 return mGivenContentInsets;
7322 }
Romain Guy06882f82009-06-10 13:36:04 -07007323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 public Rect getGivenVisibleInsetsLw() {
7325 return mGivenVisibleInsets;
7326 }
Romain Guy06882f82009-06-10 13:36:04 -07007327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328 public WindowManager.LayoutParams getAttrs() {
7329 return mAttrs;
7330 }
7331
7332 public int getSurfaceLayer() {
7333 return mLayer;
7334 }
Romain Guy06882f82009-06-10 13:36:04 -07007335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 public IApplicationToken getAppToken() {
7337 return mAppToken != null ? mAppToken.appToken : null;
7338 }
7339
7340 public boolean hasAppShownWindows() {
7341 return mAppToken != null ? mAppToken.firstWindowDrawn : false;
7342 }
7343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007344 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007345 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007346 TAG, "Setting animation in " + this + ": " + anim);
7347 mAnimating = false;
7348 mLocalAnimating = false;
7349 mAnimation = anim;
7350 mAnimation.restrictDuration(MAX_ANIMATION_DURATION);
7351 mAnimation.scaleCurrentDuration(mWindowAnimationScale);
7352 }
7353
7354 public void clearAnimation() {
7355 if (mAnimation != null) {
7356 mAnimating = true;
7357 mLocalAnimating = false;
7358 mAnimation = null;
7359 }
7360 }
Romain Guy06882f82009-06-10 13:36:04 -07007361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 Surface createSurfaceLocked() {
7363 if (mSurface == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007364 mReportDestroySurface = false;
7365 mSurfacePendingDestroy = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 mDrawPending = true;
7367 mCommitDrawPending = false;
7368 mReadyToShow = false;
7369 if (mAppToken != null) {
7370 mAppToken.allDrawn = false;
7371 }
7372
7373 int flags = 0;
Mathias Agopian317a6282009-08-13 17:29:02 -07007374 if (mAttrs.memoryType == MEMORY_TYPE_PUSH_BUFFERS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 flags |= Surface.PUSH_BUFFERS;
7376 }
7377
7378 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
7379 flags |= Surface.SECURE;
7380 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007381 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 TAG, "Creating surface in session "
7383 + mSession.mSurfaceSession + " window " + this
7384 + " w=" + mFrame.width()
7385 + " h=" + mFrame.height() + " format="
7386 + mAttrs.format + " flags=" + flags);
7387
7388 int w = mFrame.width();
7389 int h = mFrame.height();
7390 if ((mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
7391 // for a scaled surface, we always want the requested
7392 // size.
7393 w = mRequestedWidth;
7394 h = mRequestedHeight;
7395 }
7396
Romain Guy9825ec62009-10-01 00:58:09 -07007397 // Something is wrong and SurfaceFlinger will not like this,
7398 // try to revert to sane values
7399 if (w <= 0) w = 1;
7400 if (h <= 0) h = 1;
7401
Dianne Hackborn16064f92010-03-25 00:47:24 -07007402 mSurfaceShown = false;
7403 mSurfaceLayer = 0;
7404 mSurfaceAlpha = 1;
7405 mSurfaceX = 0;
7406 mSurfaceY = 0;
7407 mSurfaceW = w;
7408 mSurfaceH = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 try {
7410 mSurface = new Surface(
Romain Guy06882f82009-06-10 13:36:04 -07007411 mSession.mSurfaceSession, mSession.mPid,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -08007412 mAttrs.getTitle().toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 0, w, h, mAttrs.format, flags);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007414 if (SHOW_TRANSACTIONS) Slog.i(TAG, " CREATE SURFACE "
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007415 + mSurface + " IN SESSION "
7416 + mSession.mSurfaceSession
7417 + ": pid=" + mSession.mPid + " format="
7418 + mAttrs.format + " flags=0x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007419 + Integer.toHexString(flags)
7420 + " / " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 } catch (Surface.OutOfResourcesException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007422 Slog.w(TAG, "OutOfResourcesException creating surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 reclaimSomeSurfaceMemoryLocked(this, "create");
7424 return null;
7425 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007426 Slog.e(TAG, "Exception creating surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 return null;
7428 }
Romain Guy06882f82009-06-10 13:36:04 -07007429
Joe Onorato8a9b2202010-02-26 18:56:32 -08007430 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 TAG, "Got surface: " + mSurface
7432 + ", set left=" + mFrame.left + " top=" + mFrame.top
7433 + ", animLayer=" + mAnimLayer);
7434 if (SHOW_TRANSACTIONS) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007435 Slog.i(TAG, ">>> OPEN TRANSACTION");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007436 if (SHOW_TRANSACTIONS) logSurface(this,
7437 "CREATE pos=(" + mFrame.left + "," + mFrame.top + ") (" +
7438 mFrame.width() + "x" + mFrame.height() + "), layer=" +
7439 mAnimLayer + " HIDE", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 }
7441 Surface.openTransaction();
7442 try {
7443 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -07007444 mSurfaceX = mFrame.left + mXOffset;
Dianne Hackborn529bef62010-03-25 11:48:43 -07007445 mSurfaceY = mFrame.top + mYOffset;
Dianne Hackborn16064f92010-03-25 00:47:24 -07007446 mSurface.setPosition(mSurfaceX, mSurfaceY);
7447 mSurfaceLayer = mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 mSurface.setLayer(mAnimLayer);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007449 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 mSurface.hide();
7451 if ((mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007452 if (SHOW_TRANSACTIONS) logSurface(this, "DITHER", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 mSurface.setFlags(Surface.SURFACE_DITHER,
7454 Surface.SURFACE_DITHER);
7455 }
7456 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007457 Slog.w(TAG, "Error creating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007458 reclaimSomeSurfaceMemoryLocked(this, "create-init");
7459 }
7460 mLastHidden = true;
7461 } finally {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007462 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 Surface.closeTransaction();
7464 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007465 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466 TAG, "Created surface " + this);
7467 }
7468 return mSurface;
7469 }
Romain Guy06882f82009-06-10 13:36:04 -07007470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 void destroySurfaceLocked() {
7472 // Window is no longer on-screen, so can no longer receive
7473 // key events... if we were waiting for it to finish
7474 // handling a key event, the wait is over!
7475 mKeyWaiter.finishedKey(mSession, mClient, true,
7476 KeyWaiter.RETURN_NOTHING);
7477 mKeyWaiter.releasePendingPointerLocked(mSession);
7478 mKeyWaiter.releasePendingTrackballLocked(mSession);
7479
7480 if (mAppToken != null && this == mAppToken.startingWindow) {
7481 mAppToken.startingDisplayed = false;
7482 }
Romain Guy06882f82009-06-10 13:36:04 -07007483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 if (mSurface != null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007485 mDrawPending = false;
7486 mCommitDrawPending = false;
7487 mReadyToShow = false;
7488
7489 int i = mChildWindows.size();
7490 while (i > 0) {
7491 i--;
7492 WindowState c = (WindowState)mChildWindows.get(i);
7493 c.mAttachedHidden = true;
7494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007495
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007496 if (mReportDestroySurface) {
7497 mReportDestroySurface = false;
7498 mSurfacePendingDestroy = true;
7499 try {
7500 mClient.dispatchGetNewSurface();
7501 // We'll really destroy on the next time around.
7502 return;
7503 } catch (RemoteException e) {
7504 }
7505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007508 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007509 RuntimeException e = null;
7510 if (!HIDE_STACK_CRAWLS) {
7511 e = new RuntimeException();
7512 e.fillInStackTrace();
7513 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007514 Slog.w(TAG, "Window " + this + " destroying surface "
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007515 + mSurface + ", session " + mSession, e);
7516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007517 if (SHOW_TRANSACTIONS) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007518 RuntimeException e = null;
7519 if (!HIDE_STACK_CRAWLS) {
7520 e = new RuntimeException();
7521 e.fillInStackTrace();
7522 }
7523 if (SHOW_TRANSACTIONS) logSurface(this, "DESTROY", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07007525 mSurface.destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007527 Slog.w(TAG, "Exception thrown when destroying Window " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 + " surface " + mSurface + " session " + mSession
7529 + ": " + e.toString());
7530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007531
Dianne Hackborn16064f92010-03-25 00:47:24 -07007532 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 }
7535 }
7536
7537 boolean finishDrawingLocked() {
7538 if (mDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007539 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 TAG, "finishDrawingLocked: " + mSurface);
7541 mCommitDrawPending = true;
7542 mDrawPending = false;
7543 return true;
7544 }
7545 return false;
7546 }
7547
7548 // This must be called while inside a transaction.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007549 boolean commitFinishDrawingLocked(long currentTime) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007550 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007551 if (!mCommitDrawPending) {
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007552 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 }
7554 mCommitDrawPending = false;
7555 mReadyToShow = true;
7556 final boolean starting = mAttrs.type == TYPE_APPLICATION_STARTING;
7557 final AppWindowToken atoken = mAppToken;
7558 if (atoken == null || atoken.allDrawn || starting) {
7559 performShowLocked();
7560 }
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07007561 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
7563
7564 // This must be called while inside a transaction.
7565 boolean performShowLocked() {
7566 if (DEBUG_VISIBILITY) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007567 RuntimeException e = null;
7568 if (!HIDE_STACK_CRAWLS) {
7569 e = new RuntimeException();
7570 e.fillInStackTrace();
7571 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007572 Slog.v(TAG, "performShow on " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 + ": readyToShow=" + mReadyToShow + " readyForDisplay=" + isReadyForDisplay()
7574 + " starting=" + (mAttrs.type == TYPE_APPLICATION_STARTING), e);
7575 }
7576 if (mReadyToShow && isReadyForDisplay()) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007577 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) logSurface(this,
7578 "SHOW (performShowLocked)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007579 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 + " during animation: policyVis=" + mPolicyVisibility
7581 + " attHidden=" + mAttachedHidden
7582 + " tok.hiddenRequested="
7583 + (mAppToken != null ? mAppToken.hiddenRequested : false)
Dianne Hackborn248b1882009-09-16 16:46:44 -07007584 + " tok.hidden="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 + (mAppToken != null ? mAppToken.hidden : false)
7586 + " animating=" + mAnimating
7587 + " tok animating="
7588 + (mAppToken != null ? mAppToken.animating : false));
7589 if (!showSurfaceRobustlyLocked(this)) {
7590 return false;
7591 }
7592 mLastAlpha = -1;
7593 mHasDrawn = true;
7594 mLastHidden = false;
7595 mReadyToShow = false;
7596 enableScreenIfNeededLocked();
7597
7598 applyEnterAnimationLocked(this);
Romain Guy06882f82009-06-10 13:36:04 -07007599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 int i = mChildWindows.size();
7601 while (i > 0) {
7602 i--;
7603 WindowState c = (WindowState)mChildWindows.get(i);
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007604 if (c.mAttachedHidden) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 c.mAttachedHidden = false;
Dianne Hackbornf09c1a22010-04-22 15:59:21 -07007606 if (c.mSurface != null) {
7607 c.performShowLocked();
7608 // It hadn't been shown, which means layout not
7609 // performed on it, so now we want to make sure to
7610 // do a layout. If called from within the transaction
7611 // loop, this will cause it to restart with a new
7612 // layout.
7613 mLayoutNeeded = true;
7614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 }
7616 }
Romain Guy06882f82009-06-10 13:36:04 -07007617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 if (mAttrs.type != TYPE_APPLICATION_STARTING
7619 && mAppToken != null) {
7620 mAppToken.firstWindowDrawn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007621
Dianne Hackborn248b1882009-09-16 16:46:44 -07007622 if (mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007623 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG,
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007624 "Finish starting " + mToken
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 + ": first real window is shown, no animation");
Dianne Hackborn248b1882009-09-16 16:46:44 -07007626 // If this initial window is animating, stop it -- we
7627 // will do an animation to reveal it from behind the
7628 // starting window, so there is no need for it to also
7629 // be doing its own stuff.
7630 if (mAnimation != null) {
7631 mAnimation = null;
7632 // Make sure we clean up the animation.
7633 mAnimating = true;
7634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 mFinishedStarting.add(mAppToken);
7636 mH.sendEmptyMessage(H.FINISHED_STARTING);
7637 }
7638 mAppToken.updateReportedVisibilityLocked();
7639 }
7640 }
7641 return true;
7642 }
Romain Guy06882f82009-06-10 13:36:04 -07007643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 // This must be called while inside a transaction. Returns true if
7645 // there is more animation to run.
7646 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08007647 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07007649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 if (!mDrawPending && !mCommitDrawPending && mAnimation != null) {
7651 mHasTransformation = true;
7652 mHasLocalTransformation = true;
7653 if (!mLocalAnimating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007654 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007655 TAG, "Starting animation in " + this +
7656 " @ " + currentTime + ": ww=" + mFrame.width() + " wh=" + mFrame.height() +
7657 " dw=" + dw + " dh=" + dh + " scale=" + mWindowAnimationScale);
7658 mAnimation.initialize(mFrame.width(), mFrame.height(), dw, dh);
7659 mAnimation.setStartTime(currentTime);
7660 mLocalAnimating = true;
7661 mAnimating = true;
7662 }
7663 mTransformation.clear();
7664 final boolean more = mAnimation.getTransformation(
7665 currentTime, mTransformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007666 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 TAG, "Stepped animation in " + this +
7668 ": more=" + more + ", xform=" + mTransformation);
7669 if (more) {
7670 // we're not done!
7671 return true;
7672 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007673 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 TAG, "Finished animation in " + this +
7675 " @ " + currentTime);
7676 mAnimation = null;
7677 //WindowManagerService.this.dump();
7678 }
7679 mHasLocalTransformation = false;
7680 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppToken != null
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007681 && mAppToken.animation != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 // When our app token is animating, we kind-of pretend like
7683 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
7684 // part of this check means that we will only do this if
7685 // our window is not currently exiting, or it is not
7686 // locally animating itself. The idea being that one that
7687 // is exiting and doing a local animation should be removed
7688 // once that animation is done.
7689 mAnimating = true;
7690 mHasTransformation = true;
7691 mTransformation.clear();
7692 return false;
7693 } else if (mHasTransformation) {
7694 // Little trick to get through the path below to act like
7695 // we have finished an animation.
7696 mAnimating = true;
7697 } else if (isAnimating()) {
7698 mAnimating = true;
7699 }
7700 } else if (mAnimation != null) {
7701 // If the display is frozen, and there is a pending animation,
7702 // clear it and make sure we run the cleanup code.
7703 mAnimating = true;
7704 mLocalAnimating = true;
7705 mAnimation = null;
7706 }
Romain Guy06882f82009-06-10 13:36:04 -07007707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007708 if (!mAnimating && !mLocalAnimating) {
7709 return false;
7710 }
7711
Joe Onorato8a9b2202010-02-26 18:56:32 -08007712 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 TAG, "Animation done in " + this + ": exiting=" + mExiting
7714 + ", reportedVisible="
7715 + (mAppToken != null ? mAppToken.reportedVisible : false));
Romain Guy06882f82009-06-10 13:36:04 -07007716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007717 mAnimating = false;
7718 mLocalAnimating = false;
7719 mAnimation = null;
7720 mAnimLayer = mLayer;
7721 if (mIsImWindow) {
7722 mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007723 } else if (mIsWallpaper) {
7724 mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007726 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727 + " anim layer: " + mAnimLayer);
7728 mHasTransformation = false;
7729 mHasLocalTransformation = false;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08007730 if (mPolicyVisibility != mPolicyVisibilityAfterAnim) {
7731 if (DEBUG_VISIBILITY) {
7732 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
7733 + mPolicyVisibilityAfterAnim);
7734 }
7735 mPolicyVisibility = mPolicyVisibilityAfterAnim;
7736 if (!mPolicyVisibility) {
7737 if (mCurrentFocus == this) {
7738 mFocusMayChange = true;
7739 }
7740 // Window is no longer visible -- make sure if we were waiting
7741 // for it to be displayed before enabling the display, that
7742 // we allow the display to be enabled now.
7743 enableScreenIfNeededLocked();
7744 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08007745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 mTransformation.clear();
7747 if (mHasDrawn
7748 && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
7749 && mAppToken != null
7750 && mAppToken.firstWindowDrawn
7751 && mAppToken.startingData != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007752 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 + mToken + ": first real window done animating");
7754 mFinishedStarting.add(mAppToken);
7755 mH.sendEmptyMessage(H.FINISHED_STARTING);
7756 }
Romain Guy06882f82009-06-10 13:36:04 -07007757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 finishExit();
7759
7760 if (mAppToken != null) {
7761 mAppToken.updateReportedVisibilityLocked();
7762 }
7763
7764 return false;
7765 }
7766
7767 void finishExit() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007768 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 TAG, "finishExit in " + this
7770 + ": exiting=" + mExiting
7771 + " remove=" + mRemoveOnExit
7772 + " windowAnimating=" + isWindowAnimating());
Romain Guy06882f82009-06-10 13:36:04 -07007773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 final int N = mChildWindows.size();
7775 for (int i=0; i<N; i++) {
7776 ((WindowState)mChildWindows.get(i)).finishExit();
7777 }
Romain Guy06882f82009-06-10 13:36:04 -07007778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 if (!mExiting) {
7780 return;
7781 }
Romain Guy06882f82009-06-10 13:36:04 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 if (isWindowAnimating()) {
7784 return;
7785 }
7786
Joe Onorato8a9b2202010-02-26 18:56:32 -08007787 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 TAG, "Exit animation finished in " + this
7789 + ": remove=" + mRemoveOnExit);
7790 if (mSurface != null) {
7791 mDestroySurface.add(this);
7792 mDestroying = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08007793 if (SHOW_TRANSACTIONS) logSurface(this, "HIDE (finishExit)", null);
Dianne Hackborn16064f92010-03-25 00:47:24 -07007794 mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 try {
7796 mSurface.hide();
7797 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007798 Slog.w(TAG, "Error hiding surface in " + this, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 }
7800 mLastHidden = true;
7801 mKeyWaiter.releasePendingPointerLocked(mSession);
7802 }
7803 mExiting = false;
7804 if (mRemoveOnExit) {
7805 mPendingRemove.add(this);
7806 mRemoveOnExit = false;
7807 }
7808 }
Romain Guy06882f82009-06-10 13:36:04 -07007809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
7811 if (dsdx < .99999f || dsdx > 1.00001f) return false;
7812 if (dtdy < .99999f || dtdy > 1.00001f) return false;
7813 if (dtdx < -.000001f || dtdx > .000001f) return false;
7814 if (dsdy < -.000001f || dsdy > .000001f) return false;
7815 return true;
7816 }
Romain Guy06882f82009-06-10 13:36:04 -07007817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 void computeShownFrameLocked() {
7819 final boolean selfTransformation = mHasLocalTransformation;
7820 Transformation attachedTransformation =
7821 (mAttachedWindow != null && mAttachedWindow.mHasLocalTransformation)
7822 ? mAttachedWindow.mTransformation : null;
7823 Transformation appTransformation =
7824 (mAppToken != null && mAppToken.hasTransformation)
7825 ? mAppToken.transformation : null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007826
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007827 // Wallpapers are animated based on the "real" window they
7828 // are currently targeting.
Dianne Hackborn3be63c02009-08-20 19:31:38 -07007829 if (mAttrs.type == TYPE_WALLPAPER && mLowerWallpaperTarget == null
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07007830 && mWallpaperTarget != null) {
Dianne Hackborn5baba162009-09-23 17:01:12 -07007831 if (mWallpaperTarget.mHasLocalTransformation &&
7832 mWallpaperTarget.mAnimation != null &&
7833 !mWallpaperTarget.mAnimation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007834 attachedTransformation = mWallpaperTarget.mTransformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007835 if (DEBUG_WALLPAPER && attachedTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007836 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007837 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007838 }
7839 if (mWallpaperTarget.mAppToken != null &&
Dianne Hackborn5baba162009-09-23 17:01:12 -07007840 mWallpaperTarget.mAppToken.hasTransformation &&
7841 mWallpaperTarget.mAppToken.animation != null &&
7842 !mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007843 appTransformation = mWallpaperTarget.mAppToken.transformation;
Dianne Hackborn5baba162009-09-23 17:01:12 -07007844 if (DEBUG_WALLPAPER && appTransformation != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007845 Slog.v(TAG, "WP target app xform: " + appTransformation);
Dianne Hackborn5baba162009-09-23 17:01:12 -07007846 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07007847 }
Dianne Hackborn759a39e2009-08-09 17:20:27 -07007848 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007850 if (selfTransformation || attachedTransformation != null
7851 || appTransformation != null) {
Romain Guy06882f82009-06-10 13:36:04 -07007852 // cache often used attributes locally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 final Rect frame = mFrame;
7854 final float tmpFloats[] = mTmpFloats;
7855 final Matrix tmpMatrix = mTmpMatrix;
7856
7857 // Compute the desired transformation.
Dianne Hackborn65c23872009-09-18 17:47:02 -07007858 tmpMatrix.setTranslate(0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007859 if (selfTransformation) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007860 tmpMatrix.postConcat(mTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 }
Dianne Hackborn65c23872009-09-18 17:47:02 -07007862 tmpMatrix.postTranslate(frame.left, frame.top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 if (attachedTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007864 tmpMatrix.postConcat(attachedTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 }
7866 if (appTransformation != null) {
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07007867 tmpMatrix.postConcat(appTransformation.getMatrix());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 }
7869
7870 // "convert" it into SurfaceFlinger's format
7871 // (a 2x2 matrix + an offset)
7872 // Here we must not transform the position of the surface
7873 // since it is already included in the transformation.
Joe Onorato8a9b2202010-02-26 18:56:32 -08007874 //Slog.i(TAG, "Transform: " + matrix);
Romain Guy06882f82009-06-10 13:36:04 -07007875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 tmpMatrix.getValues(tmpFloats);
7877 mDsDx = tmpFloats[Matrix.MSCALE_X];
7878 mDtDx = tmpFloats[Matrix.MSKEW_X];
7879 mDsDy = tmpFloats[Matrix.MSKEW_Y];
7880 mDtDy = tmpFloats[Matrix.MSCALE_Y];
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007881 int x = (int)tmpFloats[Matrix.MTRANS_X] + mXOffset;
7882 int y = (int)tmpFloats[Matrix.MTRANS_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 int w = frame.width();
7884 int h = frame.height();
7885 mShownFrame.set(x, y, x+w, y+h);
7886
7887 // Now set the alpha... but because our current hardware
7888 // can't do alpha transformation on a non-opaque surface,
7889 // turn it off if we are running an animation that is also
7890 // transforming since it is more important to have that
7891 // animation be smooth.
7892 mShownAlpha = mAlpha;
7893 if (!mLimitedAlphaCompositing
7894 || (!PixelFormat.formatHasAlpha(mAttrs.format)
7895 || (isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
7896 && x == frame.left && y == frame.top))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007897 //Slog.i(TAG, "Applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 if (selfTransformation) {
7899 mShownAlpha *= mTransformation.getAlpha();
7900 }
7901 if (attachedTransformation != null) {
7902 mShownAlpha *= attachedTransformation.getAlpha();
7903 }
7904 if (appTransformation != null) {
7905 mShownAlpha *= appTransformation.getAlpha();
7906 }
7907 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007908 //Slog.i(TAG, "Not applying alpha transform");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909 }
Romain Guy06882f82009-06-10 13:36:04 -07007910
Joe Onorato8a9b2202010-02-26 18:56:32 -08007911 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 TAG, "Continuing animation in " + this +
7913 ": " + mShownFrame +
7914 ", alpha=" + mTransformation.getAlpha());
7915 return;
7916 }
Romain Guy06882f82009-06-10 13:36:04 -07007917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 mShownFrame.set(mFrame);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07007919 if (mXOffset != 0 || mYOffset != 0) {
7920 mShownFrame.offset(mXOffset, mYOffset);
7921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 mShownAlpha = mAlpha;
7923 mDsDx = 1;
7924 mDtDx = 0;
7925 mDsDy = 0;
7926 mDtDy = 1;
7927 }
Romain Guy06882f82009-06-10 13:36:04 -07007928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 /**
7930 * Is this window visible? It is not visible if there is no
7931 * surface, or we are in the process of running an exit animation
7932 * that will remove the surface, or its app token has been hidden.
7933 */
7934 public boolean isVisibleLw() {
7935 final AppWindowToken atoken = mAppToken;
7936 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7937 && (atoken == null || !atoken.hiddenRequested)
7938 && !mExiting && !mDestroying;
7939 }
7940
7941 /**
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007942 * Like {@link #isVisibleLw}, but also counts a window that is currently
7943 * "hidden" behind the keyguard as visible. This allows us to apply
7944 * things like window flags that impact the keyguard.
7945 * XXX I am starting to think we need to have ANOTHER visibility flag
7946 * for this "hidden behind keyguard" state rather than overloading
7947 * mPolicyVisibility. Ungh.
7948 */
7949 public boolean isVisibleOrBehindKeyguardLw() {
7950 final AppWindowToken atoken = mAppToken;
7951 return mSurface != null && !mAttachedHidden
7952 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
Dianne Hackborn5943c202010-04-12 21:36:49 -07007953 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending))
Dianne Hackborn3d163f072009-10-07 21:26:57 -07007954 && !mExiting && !mDestroying;
7955 }
7956
7957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 * Is this window visible, ignoring its app token? It is not visible
7959 * if there is no surface, or we are in the process of running an exit animation
7960 * that will remove the surface.
7961 */
7962 public boolean isWinVisibleLw() {
7963 final AppWindowToken atoken = mAppToken;
7964 return mSurface != null && mPolicyVisibility && !mAttachedHidden
7965 && (atoken == null || !atoken.hiddenRequested || atoken.animating)
7966 && !mExiting && !mDestroying;
7967 }
7968
7969 /**
7970 * The same as isVisible(), but follows the current hidden state of
7971 * the associated app token, not the pending requested hidden state.
7972 */
7973 boolean isVisibleNow() {
7974 return mSurface != null && mPolicyVisibility && !mAttachedHidden
The Android Open Source Project10592532009-03-18 17:39:46 -07007975 && !mRootToken.hidden && !mExiting && !mDestroying;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 }
7977
7978 /**
7979 * Same as isVisible(), but we also count it as visible between the
7980 * call to IWindowSession.add() and the first relayout().
7981 */
7982 boolean isVisibleOrAdding() {
7983 final AppWindowToken atoken = mAppToken;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07007984 return ((mSurface != null && !mReportDestroySurface)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
7986 && mPolicyVisibility && !mAttachedHidden
7987 && (atoken == null || !atoken.hiddenRequested)
7988 && !mExiting && !mDestroying;
7989 }
7990
7991 /**
7992 * Is this window currently on-screen? It is on-screen either if it
7993 * is visible or it is currently running an animation before no longer
7994 * being visible.
7995 */
7996 boolean isOnScreen() {
7997 final AppWindowToken atoken = mAppToken;
7998 if (atoken != null) {
7999 return mSurface != null && mPolicyVisibility && !mDestroying
8000 && ((!mAttachedHidden && !atoken.hiddenRequested)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008001 || mAnimation != null || atoken.animation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 } else {
8003 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008004 && (!mAttachedHidden || mAnimation != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 }
8006 }
Romain Guy06882f82009-06-10 13:36:04 -07008007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 /**
8009 * Like isOnScreen(), but we don't return true if the window is part
8010 * of a transition that has not yet been started.
8011 */
8012 boolean isReadyForDisplay() {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008013 if (mRootToken.waitingToShow &&
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07008014 mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Dianne Hackborna8f60182009-09-01 19:01:50 -07008015 return false;
8016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 final AppWindowToken atoken = mAppToken;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008018 final boolean animating = atoken != null
8019 ? (atoken.animation != null) : false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 return mSurface != null && mPolicyVisibility && !mDestroying
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008021 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
8022 && !mRootToken.hidden)
Dianne Hackborn0cd48872009-08-13 18:51:59 -07008023 || mAnimation != null || animating);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 }
8025
8026 /** Is the window or its container currently animating? */
8027 boolean isAnimating() {
8028 final WindowState attached = mAttachedWindow;
8029 final AppWindowToken atoken = mAppToken;
8030 return mAnimation != null
8031 || (attached != null && attached.mAnimation != null)
Romain Guy06882f82009-06-10 13:36:04 -07008032 || (atoken != null &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 (atoken.animation != null
8034 || atoken.inPendingTransaction));
8035 }
8036
8037 /** Is this window currently animating? */
8038 boolean isWindowAnimating() {
8039 return mAnimation != null;
8040 }
8041
8042 /**
8043 * Like isOnScreen, but returns false if the surface hasn't yet
8044 * been drawn.
8045 */
8046 public boolean isDisplayedLw() {
8047 final AppWindowToken atoken = mAppToken;
8048 return mSurface != null && mPolicyVisibility && !mDestroying
8049 && !mDrawPending && !mCommitDrawPending
8050 && ((!mAttachedHidden &&
8051 (atoken == null || !atoken.hiddenRequested))
8052 || mAnimating);
8053 }
8054
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008055 /**
8056 * Returns true if the window has a surface that it has drawn a
Dianne Hackborn5943c202010-04-12 21:36:49 -07008057 * complete UI in to. Note that this returns true if the orientation
8058 * is changing even if the window hasn't redrawn because we don't want
8059 * to stop things from executing during that time.
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008060 */
8061 public boolean isDrawnLw() {
8062 final AppWindowToken atoken = mAppToken;
8063 return mSurface != null && !mDestroying
Dianne Hackborn5943c202010-04-12 21:36:49 -07008064 && (mOrientationChanging || (!mDrawPending && !mCommitDrawPending));
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008065 }
8066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008067 public boolean fillsScreenLw(int screenWidth, int screenHeight,
8068 boolean shownFrame, boolean onlyOpaque) {
8069 if (mSurface == null) {
8070 return false;
8071 }
8072 if (mAppToken != null && !mAppToken.appFullscreen) {
8073 return false;
8074 }
8075 if (onlyOpaque && mAttrs.format != PixelFormat.OPAQUE) {
8076 return false;
8077 }
8078 final Rect frame = shownFrame ? mShownFrame : mFrame;
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008079
8080 if ((mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0) {
8081 return frame.left <= mCompatibleScreenFrame.left &&
8082 frame.top <= mCompatibleScreenFrame.top &&
8083 frame.right >= mCompatibleScreenFrame.right &&
8084 frame.bottom >= mCompatibleScreenFrame.bottom;
8085 } else {
8086 return frame.left <= 0 && frame.top <= 0
8087 && frame.right >= screenWidth
8088 && frame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 }
Romain Guy06882f82009-06-10 13:36:04 -07008091
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008092 /**
Dianne Hackborn25994b42009-09-04 14:21:19 -07008093 * Return true if the window is opaque and fully drawn. This indicates
8094 * it may obscure windows behind it.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008095 */
8096 boolean isOpaqueDrawn() {
Dianne Hackborn25994b42009-09-04 14:21:19 -07008097 return (mAttrs.format == PixelFormat.OPAQUE
8098 || mAttrs.type == TYPE_WALLPAPER)
8099 && mSurface != null && mAnimation == null
8100 && (mAppToken == null || mAppToken.animation == null)
8101 && !mDrawPending && !mCommitDrawPending;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008102 }
8103
8104 boolean needsBackgroundFiller(int screenWidth, int screenHeight) {
8105 return
8106 // only if the application is requesting compatible window
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008107 (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0 &&
8108 // only if it's visible
8109 mHasDrawn && mViewVisibility == View.VISIBLE &&
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008110 // and only if the application fills the compatible screen
8111 mFrame.left <= mCompatibleScreenFrame.left &&
8112 mFrame.top <= mCompatibleScreenFrame.top &&
8113 mFrame.right >= mCompatibleScreenFrame.right &&
8114 mFrame.bottom >= mCompatibleScreenFrame.bottom &&
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -07008115 // and starting window do not need background filler
Mitsuru Oshimad2967e22009-07-20 14:01:43 -07008116 mAttrs.type != mAttrs.TYPE_APPLICATION_STARTING;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07008117 }
8118
8119 boolean isFullscreen(int screenWidth, int screenHeight) {
8120 return mFrame.left <= 0 && mFrame.top <= 0 &&
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008121 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 }
8123
8124 void removeLocked() {
8125 if (mAttachedWindow != null) {
8126 mAttachedWindow.mChildWindows.remove(this);
8127 }
8128 destroySurfaceLocked();
8129 mSession.windowRemovedLocked();
8130 try {
8131 mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
8132 } catch (RuntimeException e) {
8133 // Ignore if it has already been removed (usually because
8134 // we are doing this as part of processing a death note.)
8135 }
8136 }
8137
8138 private class DeathRecipient implements IBinder.DeathRecipient {
8139 public void binderDied() {
8140 try {
8141 synchronized(mWindowMap) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008142 WindowState win = windowForClientLocked(mSession, mClient, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008143 Slog.i(TAG, "WIN DEATH: " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 if (win != null) {
8145 removeWindowLocked(mSession, win);
8146 }
8147 }
8148 } catch (IllegalArgumentException ex) {
8149 // This will happen if the window has already been
8150 // removed.
8151 }
8152 }
8153 }
8154
8155 /** Returns true if this window desires key events. */
8156 public final boolean canReceiveKeys() {
8157 return isVisibleOrAdding()
8158 && (mViewVisibility == View.VISIBLE)
8159 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
8160 }
8161
8162 public boolean hasDrawnLw() {
8163 return mHasDrawn;
8164 }
8165
8166 public boolean showLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008167 return showLw(doAnimation, true);
8168 }
8169
8170 boolean showLw(boolean doAnimation, boolean requestAnim) {
8171 if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
8172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 }
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008174 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008175 if (doAnimation) {
8176 if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
8177 + mPolicyVisibility + " mAnimation=" + mAnimation);
8178 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8179 doAnimation = false;
8180 } else if (mPolicyVisibility && mAnimation == null) {
8181 // Check for the case where we are currently visible and
8182 // not animating; we do not want to do animation at such a
8183 // point to become visible when we already are.
8184 doAnimation = false;
8185 }
8186 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008187 mPolicyVisibility = true;
8188 mPolicyVisibilityAfterAnim = true;
8189 if (doAnimation) {
8190 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_ENTER, true);
8191 }
8192 if (requestAnim) {
8193 requestAnimationLocked(0);
8194 }
8195 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197
8198 public boolean hideLw(boolean doAnimation) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008199 return hideLw(doAnimation, true);
8200 }
8201
8202 boolean hideLw(boolean doAnimation, boolean requestAnim) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008203 if (doAnimation) {
8204 if (mDisplayFrozen || !mPolicy.isScreenOn()) {
8205 doAnimation = false;
8206 }
8207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008208 boolean current = doAnimation ? mPolicyVisibilityAfterAnim
8209 : mPolicyVisibility;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008210 if (!current) {
8211 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008213 if (doAnimation) {
8214 applyAnimationLocked(this, WindowManagerPolicy.TRANSIT_EXIT, false);
8215 if (mAnimation == null) {
8216 doAnimation = false;
8217 }
8218 }
8219 if (doAnimation) {
8220 mPolicyVisibilityAfterAnim = false;
8221 } else {
Dianne Hackbornb601ce12010-03-01 23:36:02 -08008222 if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008223 mPolicyVisibilityAfterAnim = false;
8224 mPolicyVisibility = false;
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -08008225 // Window is no longer visible -- make sure if we were waiting
8226 // for it to be displayed before enabling the display, that
8227 // we allow the display to be enabled now.
8228 enableScreenIfNeededLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08008229 if (mCurrentFocus == this) {
8230 mFocusMayChange = true;
8231 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07008232 }
8233 if (requestAnim) {
8234 requestAnimationLocked(0);
8235 }
8236 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 }
8238
8239 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008240 pw.print(prefix); pw.print("mSession="); pw.print(mSession);
8241 pw.print(" mClient="); pw.println(mClient.asBinder());
8242 pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
8243 if (mAttachedWindow != null || mLayoutAttached) {
8244 pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
8245 pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
8246 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07008247 if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
8248 pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
8249 pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008250 pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
8251 pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008252 }
8253 pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
8254 pw.print(" mSubLayer="); pw.print(mSubLayer);
8255 pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
8256 pw.print((mTargetAppToken != null ? mTargetAppToken.animLayerAdjustment
8257 : (mAppToken != null ? mAppToken.animLayerAdjustment : 0)));
8258 pw.print("="); pw.print(mAnimLayer);
8259 pw.print(" mLastLayer="); pw.println(mLastLayer);
8260 if (mSurface != null) {
8261 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Dianne Hackborn16064f92010-03-25 00:47:24 -07008262 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
8263 pw.print(" layer="); pw.print(mSurfaceLayer);
8264 pw.print(" alpha="); pw.print(mSurfaceAlpha);
8265 pw.print(" rect=("); pw.print(mSurfaceX);
8266 pw.print(","); pw.print(mSurfaceY);
8267 pw.print(") "); pw.print(mSurfaceW);
8268 pw.print(" x "); pw.println(mSurfaceH);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008269 }
8270 pw.print(prefix); pw.print("mToken="); pw.println(mToken);
8271 pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
8272 if (mAppToken != null) {
8273 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
8274 }
8275 if (mTargetAppToken != null) {
8276 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
8277 }
8278 pw.print(prefix); pw.print("mViewVisibility=0x");
8279 pw.print(Integer.toHexString(mViewVisibility));
8280 pw.print(" mLastHidden="); pw.print(mLastHidden);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07008281 pw.print(" mHaveFrame="); pw.print(mHaveFrame);
8282 pw.print(" mObscured="); pw.println(mObscured);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008283 if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || mAttachedHidden) {
8284 pw.print(prefix); pw.print("mPolicyVisibility=");
8285 pw.print(mPolicyVisibility);
8286 pw.print(" mPolicyVisibilityAfterAnim=");
8287 pw.print(mPolicyVisibilityAfterAnim);
8288 pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
8289 }
Dianne Hackborn9b52a212009-12-11 14:51:35 -08008290 if (!mRelayoutCalled) {
8291 pw.print(prefix); pw.print("mRelayoutCalled="); pw.println(mRelayoutCalled);
8292 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008293 pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008294 pw.print(" h="); pw.print(mRequestedHeight);
8295 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008296 if (mXOffset != 0 || mYOffset != 0) {
8297 pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
8298 pw.print(" y="); pw.println(mYOffset);
8299 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008300 pw.print(prefix); pw.print("mGivenContentInsets=");
8301 mGivenContentInsets.printShortString(pw);
8302 pw.print(" mGivenVisibleInsets=");
8303 mGivenVisibleInsets.printShortString(pw);
8304 pw.println();
8305 if (mTouchableInsets != 0 || mGivenInsetsPending) {
8306 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
8307 pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
8308 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008309 pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008310 pw.print(prefix); pw.print("mShownFrame=");
8311 mShownFrame.printShortString(pw);
8312 pw.print(" last="); mLastShownFrame.printShortString(pw);
8313 pw.println();
8314 pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
8315 pw.print(" last="); mLastFrame.printShortString(pw);
8316 pw.println();
8317 pw.print(prefix); pw.print("mContainingFrame=");
8318 mContainingFrame.printShortString(pw);
8319 pw.print(" mDisplayFrame=");
8320 mDisplayFrame.printShortString(pw);
8321 pw.println();
8322 pw.print(prefix); pw.print("mContentFrame="); mContentFrame.printShortString(pw);
8323 pw.print(" mVisibleFrame="); mVisibleFrame.printShortString(pw);
8324 pw.println();
8325 pw.print(prefix); pw.print("mContentInsets="); mContentInsets.printShortString(pw);
8326 pw.print(" last="); mLastContentInsets.printShortString(pw);
8327 pw.print(" mVisibleInsets="); mVisibleInsets.printShortString(pw);
8328 pw.print(" last="); mLastVisibleInsets.printShortString(pw);
8329 pw.println();
8330 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
8331 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
8332 pw.print(" mAlpha="); pw.print(mAlpha);
8333 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
8334 }
8335 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
8336 || mAnimation != null) {
8337 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
8338 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
8339 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
8340 pw.print(" mAnimation="); pw.println(mAnimation);
8341 }
8342 if (mHasTransformation || mHasLocalTransformation) {
8343 pw.print(prefix); pw.print("XForm: has=");
8344 pw.print(mHasTransformation);
8345 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
8346 pw.print(" "); mTransformation.printShortString(pw);
8347 pw.println();
8348 }
8349 pw.print(prefix); pw.print("mDrawPending="); pw.print(mDrawPending);
8350 pw.print(" mCommitDrawPending="); pw.print(mCommitDrawPending);
8351 pw.print(" mReadyToShow="); pw.print(mReadyToShow);
8352 pw.print(" mHasDrawn="); pw.println(mHasDrawn);
8353 if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
8354 pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
8355 pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
8356 pw.print(" mDestroying="); pw.print(mDestroying);
8357 pw.print(" mRemoved="); pw.println(mRemoved);
8358 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008359 if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008360 pw.print(prefix); pw.print("mOrientationChanging=");
8361 pw.print(mOrientationChanging);
Dianne Hackborn93e462b2009-09-15 22:50:40 -07008362 pw.print(" mAppFreezing="); pw.print(mAppFreezing);
8363 pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008364 }
Mitsuru Oshima589cebe2009-07-22 20:38:58 -07008365 if (mHScale != 1 || mVScale != 1) {
8366 pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
8367 pw.print(" mVScale="); pw.println(mVScale);
8368 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07008369 if (mWallpaperX != -1 || mWallpaperY != -1) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008370 pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
8371 pw.print(" mWallpaperY="); pw.println(mWallpaperY);
8372 }
Marco Nelissenbf6956b2009-11-09 15:21:13 -08008373 if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
8374 pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
8375 pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
8376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 }
8378
8379 @Override
8380 public String toString() {
8381 return "Window{"
8382 + Integer.toHexString(System.identityHashCode(this))
8383 + " " + mAttrs.getTitle() + " paused=" + mToken.paused + "}";
8384 }
8385 }
Romain Guy06882f82009-06-10 13:36:04 -07008386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 // -------------------------------------------------------------
8388 // Window Token State
8389 // -------------------------------------------------------------
8390
8391 class WindowToken {
8392 // The actual token.
8393 final IBinder token;
8394
8395 // The type of window this token is for, as per WindowManager.LayoutParams.
8396 final int windowType;
Romain Guy06882f82009-06-10 13:36:04 -07008397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 // Set if this token was explicitly added by a client, so should
8399 // not be removed when all windows are removed.
8400 final boolean explicit;
Romain Guy06882f82009-06-10 13:36:04 -07008401
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008402 // For printing.
8403 String stringName;
Romain Guy06882f82009-06-10 13:36:04 -07008404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 // If this is an AppWindowToken, this is non-null.
8406 AppWindowToken appWindowToken;
Romain Guy06882f82009-06-10 13:36:04 -07008407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 // All of the windows associated with this token.
8409 final ArrayList<WindowState> windows = new ArrayList<WindowState>();
8410
8411 // Is key dispatching paused for this token?
8412 boolean paused = false;
8413
8414 // Should this token's windows be hidden?
8415 boolean hidden;
8416
8417 // Temporary for finding which tokens no longer have visible windows.
8418 boolean hasVisible;
8419
Dianne Hackborna8f60182009-09-01 19:01:50 -07008420 // Set to true when this token is in a pending transaction where it
8421 // will be shown.
8422 boolean waitingToShow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008423
Dianne Hackborna8f60182009-09-01 19:01:50 -07008424 // Set to true when this token is in a pending transaction where it
8425 // will be hidden.
8426 boolean waitingToHide;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008427
Dianne Hackborna8f60182009-09-01 19:01:50 -07008428 // Set to true when this token is in a pending transaction where its
8429 // windows will be put to the bottom of the list.
8430 boolean sendingToBottom;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008431
Dianne Hackborna8f60182009-09-01 19:01:50 -07008432 // Set to true when this token is in a pending transaction where its
8433 // windows will be put to the top of the list.
8434 boolean sendingToTop;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 WindowToken(IBinder _token, int type, boolean _explicit) {
8437 token = _token;
8438 windowType = type;
8439 explicit = _explicit;
8440 }
8441
8442 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008443 pw.print(prefix); pw.print("token="); pw.println(token);
8444 pw.print(prefix); pw.print("windows="); pw.println(windows);
8445 pw.print(prefix); pw.print("windowType="); pw.print(windowType);
8446 pw.print(" hidden="); pw.print(hidden);
8447 pw.print(" hasVisible="); pw.println(hasVisible);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008448 if (waitingToShow || waitingToHide || sendingToBottom || sendingToTop) {
8449 pw.print(prefix); pw.print("waitingToShow="); pw.print(waitingToShow);
8450 pw.print(" waitingToHide="); pw.print(waitingToHide);
8451 pw.print(" sendingToBottom="); pw.print(sendingToBottom);
8452 pw.print(" sendingToTop="); pw.println(sendingToTop);
8453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 }
8455
8456 @Override
8457 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008458 if (stringName == null) {
8459 StringBuilder sb = new StringBuilder();
8460 sb.append("WindowToken{");
8461 sb.append(Integer.toHexString(System.identityHashCode(this)));
8462 sb.append(" token="); sb.append(token); sb.append('}');
8463 stringName = sb.toString();
8464 }
8465 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 }
8467 };
8468
8469 class AppWindowToken extends WindowToken {
8470 // Non-null only for application tokens.
8471 final IApplicationToken appToken;
8472
8473 // All of the windows and child windows that are included in this
8474 // application token. Note this list is NOT sorted!
8475 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
8476
8477 int groupId = -1;
8478 boolean appFullscreen;
8479 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Romain Guy06882f82009-06-10 13:36:04 -07008480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008481 // These are used for determining when all windows associated with
8482 // an activity have been drawn, so they can be made visible together
8483 // at the same time.
8484 int lastTransactionSequence = mTransactionSequence-1;
8485 int numInterestingWindows;
8486 int numDrawnWindows;
8487 boolean inPendingTransaction;
8488 boolean allDrawn;
Romain Guy06882f82009-06-10 13:36:04 -07008489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 // Is this token going to be hidden in a little while? If so, it
8491 // won't be taken into account for setting the screen orientation.
8492 boolean willBeHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 // Is this window's surface needed? This is almost like hidden, except
8495 // it will sometimes be true a little earlier: when the token has
8496 // been shown, but is still waiting for its app transition to execute
8497 // before making its windows shown.
8498 boolean hiddenRequested;
Romain Guy06882f82009-06-10 13:36:04 -07008499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 // Have we told the window clients to hide themselves?
8501 boolean clientHidden;
Romain Guy06882f82009-06-10 13:36:04 -07008502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 // Last visibility state we reported to the app token.
8504 boolean reportedVisible;
8505
8506 // Set to true when the token has been removed from the window mgr.
8507 boolean removed;
8508
8509 // Have we been asked to have this token keep the screen frozen?
8510 boolean freezingScreen;
Romain Guy06882f82009-06-10 13:36:04 -07008511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 boolean animating;
8513 Animation animation;
8514 boolean hasTransformation;
8515 final Transformation transformation = new Transformation();
Romain Guy06882f82009-06-10 13:36:04 -07008516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 // Offset to the window of all layers in the token, for use by
8518 // AppWindowToken animations.
8519 int animLayerAdjustment;
Romain Guy06882f82009-06-10 13:36:04 -07008520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 // Information about an application starting window if displayed.
8522 StartingData startingData;
8523 WindowState startingWindow;
8524 View startingView;
8525 boolean startingDisplayed;
8526 boolean startingMoved;
8527 boolean firstWindowDrawn;
8528
8529 AppWindowToken(IApplicationToken _token) {
8530 super(_token.asBinder(),
8531 WindowManager.LayoutParams.TYPE_APPLICATION, true);
8532 appWindowToken = this;
8533 appToken = _token;
8534 }
Romain Guy06882f82009-06-10 13:36:04 -07008535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 public void setAnimation(Animation anim) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008537 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538 TAG, "Setting animation in " + this + ": " + anim);
8539 animation = anim;
8540 animating = false;
8541 anim.restrictDuration(MAX_ANIMATION_DURATION);
8542 anim.scaleCurrentDuration(mTransitionAnimationScale);
8543 int zorder = anim.getZAdjustment();
8544 int adj = 0;
8545 if (zorder == Animation.ZORDER_TOP) {
8546 adj = TYPE_LAYER_OFFSET;
8547 } else if (zorder == Animation.ZORDER_BOTTOM) {
8548 adj = -TYPE_LAYER_OFFSET;
8549 }
Romain Guy06882f82009-06-10 13:36:04 -07008550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 if (animLayerAdjustment != adj) {
8552 animLayerAdjustment = adj;
8553 updateLayers();
8554 }
8555 }
Romain Guy06882f82009-06-10 13:36:04 -07008556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 public void setDummyAnimation() {
8558 if (animation == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008559 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008560 TAG, "Setting dummy animation in " + this);
8561 animation = sDummyAnimation;
8562 }
8563 }
8564
8565 public void clearAnimation() {
8566 if (animation != null) {
8567 animation = null;
8568 animating = true;
8569 }
8570 }
Romain Guy06882f82009-06-10 13:36:04 -07008571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 void updateLayers() {
8573 final int N = allAppWindows.size();
8574 final int adj = animLayerAdjustment;
8575 for (int i=0; i<N; i++) {
8576 WindowState w = allAppWindows.get(i);
8577 w.mAnimLayer = w.mLayer + adj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008578 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 + w.mAnimLayer);
8580 if (w == mInputMethodTarget) {
8581 setInputMethodAnimLayerAdjustment(adj);
8582 }
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008583 if (w == mWallpaperTarget && mLowerWallpaperTarget == null) {
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07008584 setWallpaperAnimLayerAdjustmentLocked(adj);
Dianne Hackborn759a39e2009-08-09 17:20:27 -07008585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 }
8587 }
Romain Guy06882f82009-06-10 13:36:04 -07008588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 void sendAppVisibilityToClients() {
8590 final int N = allAppWindows.size();
8591 for (int i=0; i<N; i++) {
8592 WindowState win = allAppWindows.get(i);
8593 if (win == startingWindow && clientHidden) {
8594 // Don't hide the starting window.
8595 continue;
8596 }
8597 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008598 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 "Setting visibility of " + win + ": " + (!clientHidden));
8600 win.mClient.dispatchAppVisibility(!clientHidden);
8601 } catch (RemoteException e) {
8602 }
8603 }
8604 }
Romain Guy06882f82009-06-10 13:36:04 -07008605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 void showAllWindowsLocked() {
8607 final int NW = allAppWindows.size();
8608 for (int i=0; i<NW; i++) {
8609 WindowState w = allAppWindows.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008610 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 "performing show on: " + w);
8612 w.performShowLocked();
8613 }
8614 }
Romain Guy06882f82009-06-10 13:36:04 -07008615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 // This must be called while inside a transaction.
8617 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08008618 if (!mDisplayFrozen && mPolicy.isScreenOn()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 // We will run animations as long as the display isn't frozen.
Romain Guy06882f82009-06-10 13:36:04 -07008620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 if (animation == sDummyAnimation) {
8622 // This guy is going to animate, but not yet. For now count
Dianne Hackborn3be63c02009-08-20 19:31:38 -07008623 // it as not animating for purposes of scheduling transactions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 // when it is really time to animate, this will be set to
8625 // a real animation and the next call will execute normally.
8626 return false;
8627 }
Romain Guy06882f82009-06-10 13:36:04 -07008628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 if ((allDrawn || animating || startingDisplayed) && animation != null) {
8630 if (!animating) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008631 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 TAG, "Starting animation in " + this +
8633 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
8634 + " scale=" + mTransitionAnimationScale
8635 + " allDrawn=" + allDrawn + " animating=" + animating);
8636 animation.initialize(dw, dh, dw, dh);
8637 animation.setStartTime(currentTime);
8638 animating = true;
8639 }
8640 transformation.clear();
8641 final boolean more = animation.getTransformation(
8642 currentTime, transformation);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008643 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008644 TAG, "Stepped animation in " + this +
8645 ": more=" + more + ", xform=" + transformation);
8646 if (more) {
8647 // we're done!
8648 hasTransformation = true;
8649 return true;
8650 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008651 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 TAG, "Finished animation in " + this +
8653 " @ " + currentTime);
8654 animation = null;
8655 }
8656 } else if (animation != null) {
8657 // If the display is frozen, and there is a pending animation,
8658 // clear it and make sure we run the cleanup code.
8659 animating = true;
8660 animation = null;
8661 }
8662
8663 hasTransformation = false;
Romain Guy06882f82009-06-10 13:36:04 -07008664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 if (!animating) {
8666 return false;
8667 }
8668
8669 clearAnimation();
8670 animating = false;
8671 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == this) {
8672 moveInputMethodWindowsIfNeededLocked(true);
8673 }
Romain Guy06882f82009-06-10 13:36:04 -07008674
Joe Onorato8a9b2202010-02-26 18:56:32 -08008675 if (DEBUG_ANIM) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 TAG, "Animation done in " + this
8677 + ": reportedVisible=" + reportedVisible);
8678
8679 transformation.clear();
8680 if (animLayerAdjustment != 0) {
8681 animLayerAdjustment = 0;
8682 updateLayers();
8683 }
Romain Guy06882f82009-06-10 13:36:04 -07008684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 final int N = windows.size();
8686 for (int i=0; i<N; i++) {
8687 ((WindowState)windows.get(i)).finishExit();
8688 }
8689 updateReportedVisibilityLocked();
Romain Guy06882f82009-06-10 13:36:04 -07008690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 return false;
8692 }
8693
8694 void updateReportedVisibilityLocked() {
8695 if (appToken == null) {
8696 return;
8697 }
Romain Guy06882f82009-06-10 13:36:04 -07008698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 int numInteresting = 0;
8700 int numVisible = 0;
8701 boolean nowGone = true;
Romain Guy06882f82009-06-10 13:36:04 -07008702
Joe Onorato8a9b2202010-02-26 18:56:32 -08008703 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008704 final int N = allAppWindows.size();
8705 for (int i=0; i<N; i++) {
8706 WindowState win = allAppWindows.get(i);
Dianne Hackborn6cf67fa2009-12-21 16:46:34 -08008707 if (win == startingWindow || win.mAppFreezing
The Android Open Source Project727cec02010-04-08 11:35:37 -07008708 || win.mViewVisibility != View.VISIBLE
8709 || win.mAttrs.type == TYPE_APPLICATION_STARTING) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 continue;
8711 }
8712 if (DEBUG_VISIBILITY) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008713 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008714 + win.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 + ", isAnimating=" + win.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008716 if (!win.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008717 Slog.v(TAG, "Not displayed: s=" + win.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 + " pv=" + win.mPolicyVisibility
8719 + " dp=" + win.mDrawPending
8720 + " cdp=" + win.mCommitDrawPending
8721 + " ah=" + win.mAttachedHidden
8722 + " th="
8723 + (win.mAppToken != null
8724 ? win.mAppToken.hiddenRequested : false)
8725 + " a=" + win.mAnimating);
8726 }
8727 }
8728 numInteresting++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07008729 if (win.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 if (!win.isAnimating()) {
8731 numVisible++;
8732 }
8733 nowGone = false;
8734 } else if (win.isAnimating()) {
8735 nowGone = false;
8736 }
8737 }
Romain Guy06882f82009-06-10 13:36:04 -07008738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008740 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 + numInteresting + " visible=" + numVisible);
8742 if (nowVisible != reportedVisible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008743 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 TAG, "Visibility changed in " + this
8745 + ": vis=" + nowVisible);
8746 reportedVisible = nowVisible;
8747 Message m = mH.obtainMessage(
8748 H.REPORT_APPLICATION_TOKEN_WINDOWS,
8749 nowVisible ? 1 : 0,
8750 nowGone ? 1 : 0,
8751 this);
8752 mH.sendMessage(m);
8753 }
8754 }
Romain Guy06882f82009-06-10 13:36:04 -07008755
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07008756 WindowState findMainWindow() {
8757 int j = windows.size();
8758 while (j > 0) {
8759 j--;
8760 WindowState win = windows.get(j);
8761 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
8762 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
8763 return win;
8764 }
8765 }
8766 return null;
8767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 void dump(PrintWriter pw, String prefix) {
8770 super.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008771 if (appToken != null) {
8772 pw.print(prefix); pw.println("app=true");
8773 }
8774 if (allAppWindows.size() > 0) {
8775 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
8776 }
8777 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
Dianne Hackborna8f60182009-09-01 19:01:50 -07008778 pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008779 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
8780 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
8781 pw.print(" clientHidden="); pw.print(clientHidden);
8782 pw.print(" willBeHidden="); pw.print(willBeHidden);
8783 pw.print(" reportedVisible="); pw.println(reportedVisible);
8784 if (paused || freezingScreen) {
8785 pw.print(prefix); pw.print("paused="); pw.print(paused);
8786 pw.print(" freezingScreen="); pw.println(freezingScreen);
8787 }
8788 if (numInterestingWindows != 0 || numDrawnWindows != 0
8789 || inPendingTransaction || allDrawn) {
8790 pw.print(prefix); pw.print("numInterestingWindows=");
8791 pw.print(numInterestingWindows);
8792 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
8793 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
8794 pw.print(" allDrawn="); pw.println(allDrawn);
8795 }
8796 if (animating || animation != null) {
8797 pw.print(prefix); pw.print("animating="); pw.print(animating);
8798 pw.print(" animation="); pw.println(animation);
8799 }
8800 if (animLayerAdjustment != 0) {
8801 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
8802 }
8803 if (hasTransformation) {
8804 pw.print(prefix); pw.print("hasTransformation="); pw.print(hasTransformation);
8805 pw.print(" transformation="); transformation.printShortString(pw);
8806 pw.println();
8807 }
8808 if (startingData != null || removed || firstWindowDrawn) {
8809 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
8810 pw.print(" removed="); pw.print(removed);
8811 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
8812 }
8813 if (startingWindow != null || startingView != null
8814 || startingDisplayed || startingMoved) {
8815 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
8816 pw.print(" startingView="); pw.print(startingView);
8817 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
8818 pw.print(" startingMoved"); pw.println(startingMoved);
8819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 }
8821
8822 @Override
8823 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008824 if (stringName == null) {
8825 StringBuilder sb = new StringBuilder();
8826 sb.append("AppWindowToken{");
8827 sb.append(Integer.toHexString(System.identityHashCode(this)));
8828 sb.append(" token="); sb.append(token); sb.append('}');
8829 stringName = sb.toString();
8830 }
8831 return stringName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 }
8833 }
Romain Guy06882f82009-06-10 13:36:04 -07008834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 // -------------------------------------------------------------
8836 // DummyAnimation
8837 // -------------------------------------------------------------
8838
8839 // This is an animation that does nothing: it just immediately finishes
8840 // itself every time it is called. It is used as a stub animation in cases
8841 // where we want to synchronize multiple things that may be animating.
8842 static final class DummyAnimation extends Animation {
8843 public boolean getTransformation(long currentTime, Transformation outTransformation) {
8844 return false;
8845 }
8846 }
8847 static final Animation sDummyAnimation = new DummyAnimation();
Romain Guy06882f82009-06-10 13:36:04 -07008848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008849 // -------------------------------------------------------------
8850 // Async Handler
8851 // -------------------------------------------------------------
8852
8853 static final class StartingData {
8854 final String pkg;
8855 final int theme;
8856 final CharSequence nonLocalizedLabel;
8857 final int labelRes;
8858 final int icon;
Romain Guy06882f82009-06-10 13:36:04 -07008859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 StartingData(String _pkg, int _theme, CharSequence _nonLocalizedLabel,
8861 int _labelRes, int _icon) {
8862 pkg = _pkg;
8863 theme = _theme;
8864 nonLocalizedLabel = _nonLocalizedLabel;
8865 labelRes = _labelRes;
8866 icon = _icon;
8867 }
8868 }
8869
8870 private final class H extends Handler {
8871 public static final int REPORT_FOCUS_CHANGE = 2;
8872 public static final int REPORT_LOSING_FOCUS = 3;
8873 public static final int ANIMATE = 4;
8874 public static final int ADD_STARTING = 5;
8875 public static final int REMOVE_STARTING = 6;
8876 public static final int FINISHED_STARTING = 7;
8877 public static final int REPORT_APPLICATION_TOKEN_WINDOWS = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008878 public static final int WINDOW_FREEZE_TIMEOUT = 11;
8879 public static final int HOLD_SCREEN_CHANGED = 12;
8880 public static final int APP_TRANSITION_TIMEOUT = 13;
8881 public static final int PERSIST_ANIMATION_SCALE = 14;
8882 public static final int FORCE_GC = 15;
8883 public static final int ENABLE_SCREEN = 16;
8884 public static final int APP_FREEZE_TIMEOUT = 17;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008885 public static final int SEND_NEW_CONFIGURATION = 18;
Romain Guy06882f82009-06-10 13:36:04 -07008886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008887 private Session mLastReportedHold;
Romain Guy06882f82009-06-10 13:36:04 -07008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 public H() {
8890 }
Romain Guy06882f82009-06-10 13:36:04 -07008891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008892 @Override
8893 public void handleMessage(Message msg) {
8894 switch (msg.what) {
8895 case REPORT_FOCUS_CHANGE: {
8896 WindowState lastFocus;
8897 WindowState newFocus;
Romain Guy06882f82009-06-10 13:36:04 -07008898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008899 synchronized(mWindowMap) {
8900 lastFocus = mLastFocus;
8901 newFocus = mCurrentFocus;
8902 if (lastFocus == newFocus) {
8903 // Focus is not changing, so nothing to do.
8904 return;
8905 }
8906 mLastFocus = newFocus;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008907 //Slog.i(TAG, "Focus moving from " + lastFocus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008908 // + " to " + newFocus);
8909 if (newFocus != null && lastFocus != null
8910 && !newFocus.isDisplayedLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008911 //Slog.i(TAG, "Delaying loss of focus...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008912 mLosingFocus.add(lastFocus);
8913 lastFocus = null;
8914 }
8915 }
8916
8917 if (lastFocus != newFocus) {
8918 //System.out.println("Changing focus from " + lastFocus
8919 // + " to " + newFocus);
8920 if (newFocus != null) {
8921 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008922 //Slog.i(TAG, "Gaining focus: " + newFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 newFocus.mClient.windowFocusChanged(true, mInTouchMode);
8924 } catch (RemoteException e) {
8925 // Ignore if process has died.
8926 }
8927 }
8928
8929 if (lastFocus != null) {
8930 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008931 //Slog.i(TAG, "Losing focus: " + lastFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 lastFocus.mClient.windowFocusChanged(false, mInTouchMode);
8933 } catch (RemoteException e) {
8934 // Ignore if process has died.
8935 }
8936 }
8937 }
8938 } break;
8939
8940 case REPORT_LOSING_FOCUS: {
8941 ArrayList<WindowState> losers;
Romain Guy06882f82009-06-10 13:36:04 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 synchronized(mWindowMap) {
8944 losers = mLosingFocus;
8945 mLosingFocus = new ArrayList<WindowState>();
8946 }
8947
8948 final int N = losers.size();
8949 for (int i=0; i<N; i++) {
8950 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008951 //Slog.i(TAG, "Losing delayed focus: " + losers.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 losers.get(i).mClient.windowFocusChanged(false, mInTouchMode);
8953 } catch (RemoteException e) {
8954 // Ignore if process has died.
8955 }
8956 }
8957 } break;
8958
8959 case ANIMATE: {
8960 synchronized(mWindowMap) {
8961 mAnimationPending = false;
8962 performLayoutAndPlaceSurfacesLocked();
8963 }
8964 } break;
8965
8966 case ADD_STARTING: {
8967 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
8968 final StartingData sd = wtoken.startingData;
8969
8970 if (sd == null) {
8971 // Animation has been canceled... do nothing.
8972 return;
8973 }
Romain Guy06882f82009-06-10 13:36:04 -07008974
Joe Onorato8a9b2202010-02-26 18:56:32 -08008975 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Add starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008976 + wtoken + ": pkg=" + sd.pkg);
Romain Guy06882f82009-06-10 13:36:04 -07008977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008978 View view = null;
8979 try {
8980 view = mPolicy.addStartingWindow(
8981 wtoken.token, sd.pkg,
8982 sd.theme, sd.nonLocalizedLabel, sd.labelRes,
8983 sd.icon);
8984 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008985 Slog.w(TAG, "Exception when adding starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 }
8987
8988 if (view != null) {
8989 boolean abort = false;
8990
8991 synchronized(mWindowMap) {
8992 if (wtoken.removed || wtoken.startingData == null) {
8993 // If the window was successfully added, then
8994 // we need to remove it.
8995 if (wtoken.startingWindow != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008996 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 "Aborted starting " + wtoken
8998 + ": removed=" + wtoken.removed
8999 + " startingData=" + wtoken.startingData);
9000 wtoken.startingWindow = null;
9001 wtoken.startingData = null;
9002 abort = true;
9003 }
9004 } else {
9005 wtoken.startingView = view;
9006 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009007 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 "Added starting " + wtoken
9009 + ": startingWindow="
9010 + wtoken.startingWindow + " startingView="
9011 + wtoken.startingView);
9012 }
9013
9014 if (abort) {
9015 try {
9016 mPolicy.removeStartingWindow(wtoken.token, view);
9017 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009018 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 }
9020 }
9021 }
9022 } break;
9023
9024 case REMOVE_STARTING: {
9025 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9026 IBinder token = null;
9027 View view = null;
9028 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009029 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Remove starting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 + wtoken + ": startingWindow="
9031 + wtoken.startingWindow + " startingView="
9032 + wtoken.startingView);
9033 if (wtoken.startingWindow != null) {
9034 view = wtoken.startingView;
9035 token = wtoken.token;
9036 wtoken.startingData = null;
9037 wtoken.startingView = null;
9038 wtoken.startingWindow = null;
9039 }
9040 }
9041 if (view != null) {
9042 try {
9043 mPolicy.removeStartingWindow(token, view);
9044 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009045 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 }
9047 }
9048 } break;
9049
9050 case FINISHED_STARTING: {
9051 IBinder token = null;
9052 View view = null;
9053 while (true) {
9054 synchronized (mWindowMap) {
9055 final int N = mFinishedStarting.size();
9056 if (N <= 0) {
9057 break;
9058 }
9059 AppWindowToken wtoken = mFinishedStarting.remove(N-1);
9060
Joe Onorato8a9b2202010-02-26 18:56:32 -08009061 if (DEBUG_STARTING_WINDOW) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009062 "Finished starting " + wtoken
9063 + ": startingWindow=" + wtoken.startingWindow
9064 + " startingView=" + wtoken.startingView);
9065
9066 if (wtoken.startingWindow == null) {
9067 continue;
9068 }
9069
9070 view = wtoken.startingView;
9071 token = wtoken.token;
9072 wtoken.startingData = null;
9073 wtoken.startingView = null;
9074 wtoken.startingWindow = null;
9075 }
9076
9077 try {
9078 mPolicy.removeStartingWindow(token, view);
9079 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009080 Slog.w(TAG, "Exception when removing starting window", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 }
9082 }
9083 } break;
9084
9085 case REPORT_APPLICATION_TOKEN_WINDOWS: {
9086 final AppWindowToken wtoken = (AppWindowToken)msg.obj;
9087
9088 boolean nowVisible = msg.arg1 != 0;
9089 boolean nowGone = msg.arg2 != 0;
9090
9091 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009092 if (DEBUG_VISIBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009093 TAG, "Reporting visible in " + wtoken
9094 + " visible=" + nowVisible
9095 + " gone=" + nowGone);
9096 if (nowVisible) {
9097 wtoken.appToken.windowsVisible();
9098 } else {
9099 wtoken.appToken.windowsGone();
9100 }
9101 } catch (RemoteException ex) {
9102 }
9103 } break;
Romain Guy06882f82009-06-10 13:36:04 -07009104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 case WINDOW_FREEZE_TIMEOUT: {
9106 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009107 Slog.w(TAG, "Window freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 int i = mWindows.size();
9109 while (i > 0) {
9110 i--;
9111 WindowState w = (WindowState)mWindows.get(i);
9112 if (w.mOrientationChanging) {
9113 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009114 Slog.w(TAG, "Force clearing orientation change: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009115 }
9116 }
9117 performLayoutAndPlaceSurfacesLocked();
9118 }
9119 break;
9120 }
Romain Guy06882f82009-06-10 13:36:04 -07009121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 case HOLD_SCREEN_CHANGED: {
9123 Session oldHold;
9124 Session newHold;
9125 synchronized (mWindowMap) {
9126 oldHold = mLastReportedHold;
9127 newHold = (Session)msg.obj;
9128 mLastReportedHold = newHold;
9129 }
Romain Guy06882f82009-06-10 13:36:04 -07009130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009131 if (oldHold != newHold) {
9132 try {
9133 if (oldHold != null) {
9134 mBatteryStats.noteStopWakelock(oldHold.mUid,
9135 "window",
9136 BatteryStats.WAKE_TYPE_WINDOW);
9137 }
9138 if (newHold != null) {
9139 mBatteryStats.noteStartWakelock(newHold.mUid,
9140 "window",
9141 BatteryStats.WAKE_TYPE_WINDOW);
9142 }
9143 } catch (RemoteException e) {
9144 }
9145 }
9146 break;
9147 }
Romain Guy06882f82009-06-10 13:36:04 -07009148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 case APP_TRANSITION_TIMEOUT: {
9150 synchronized (mWindowMap) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009151 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009152 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 "*** APP TRANSITION TIMEOUT");
9154 mAppTransitionReady = true;
9155 mAppTransitionTimeout = true;
9156 performLayoutAndPlaceSurfacesLocked();
9157 }
9158 }
9159 break;
9160 }
Romain Guy06882f82009-06-10 13:36:04 -07009161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 case PERSIST_ANIMATION_SCALE: {
9163 Settings.System.putFloat(mContext.getContentResolver(),
9164 Settings.System.WINDOW_ANIMATION_SCALE, mWindowAnimationScale);
9165 Settings.System.putFloat(mContext.getContentResolver(),
9166 Settings.System.TRANSITION_ANIMATION_SCALE, mTransitionAnimationScale);
9167 break;
9168 }
Romain Guy06882f82009-06-10 13:36:04 -07009169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009170 case FORCE_GC: {
9171 synchronized(mWindowMap) {
9172 if (mAnimationPending) {
9173 // If we are animating, don't do the gc now but
9174 // delay a bit so we don't interrupt the animation.
9175 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
9176 2000);
9177 return;
9178 }
9179 // If we are currently rotating the display, it will
9180 // schedule a new message when done.
9181 if (mDisplayFrozen) {
9182 return;
9183 }
9184 mFreezeGcPending = 0;
9185 }
9186 Runtime.getRuntime().gc();
9187 break;
9188 }
Romain Guy06882f82009-06-10 13:36:04 -07009189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 case ENABLE_SCREEN: {
9191 performEnableScreen();
9192 break;
9193 }
Romain Guy06882f82009-06-10 13:36:04 -07009194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009195 case APP_FREEZE_TIMEOUT: {
9196 synchronized (mWindowMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009197 Slog.w(TAG, "App freeze timeout expired.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009198 int i = mAppTokens.size();
9199 while (i > 0) {
9200 i--;
9201 AppWindowToken tok = mAppTokens.get(i);
9202 if (tok.freezingScreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009203 Slog.w(TAG, "Force clearing freeze: " + tok);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 unsetAppFreezingScreenLocked(tok, true, true);
9205 }
9206 }
9207 }
9208 break;
9209 }
Romain Guy06882f82009-06-10 13:36:04 -07009210
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009211 case SEND_NEW_CONFIGURATION: {
9212 removeMessages(SEND_NEW_CONFIGURATION);
9213 sendNewConfiguration();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07009214 break;
9215 }
Romain Guy06882f82009-06-10 13:36:04 -07009216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 }
9218 }
9219 }
9220
9221 // -------------------------------------------------------------
9222 // IWindowManager API
9223 // -------------------------------------------------------------
9224
9225 public IWindowSession openSession(IInputMethodClient client,
9226 IInputContext inputContext) {
9227 if (client == null) throw new IllegalArgumentException("null client");
9228 if (inputContext == null) throw new IllegalArgumentException("null inputContext");
9229 return new Session(client, inputContext);
9230 }
9231
9232 public boolean inputMethodClientHasFocus(IInputMethodClient client) {
9233 synchronized (mWindowMap) {
9234 // The focus for the client is the window immediately below
9235 // where we would place the input method window.
9236 int idx = findDesiredInputMethodWindowIndexLocked(false);
9237 WindowState imFocus;
9238 if (idx > 0) {
9239 imFocus = (WindowState)mWindows.get(idx-1);
9240 if (imFocus != null) {
9241 if (imFocus.mSession.mClient != null &&
9242 imFocus.mSession.mClient.asBinder() == client.asBinder()) {
9243 return true;
9244 }
9245 }
9246 }
9247 }
9248 return false;
9249 }
Romain Guy06882f82009-06-10 13:36:04 -07009250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 // -------------------------------------------------------------
9252 // Internals
9253 // -------------------------------------------------------------
9254
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009255 final WindowState windowForClientLocked(Session session, IWindow client,
9256 boolean throwOnError) {
9257 return windowForClientLocked(session, client.asBinder(), throwOnError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 }
Romain Guy06882f82009-06-10 13:36:04 -07009259
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009260 final WindowState windowForClientLocked(Session session, IBinder client,
9261 boolean throwOnError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 WindowState win = mWindowMap.get(client);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009263 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 TAG, "Looking up client " + client + ": " + win);
9265 if (win == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009266 RuntimeException ex = new IllegalArgumentException(
9267 "Requested window " + client + " does not exist");
9268 if (throwOnError) {
9269 throw ex;
9270 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009271 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009272 return null;
9273 }
9274 if (session != null && win.mSession != session) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009275 RuntimeException ex = new IllegalArgumentException(
9276 "Requested window " + client + " is in session " +
9277 win.mSession + ", not " + session);
9278 if (throwOnError) {
9279 throw ex;
9280 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009281 Slog.w(TAG, "Failed looking up window", ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 return null;
9283 }
9284
9285 return win;
9286 }
9287
Dianne Hackborna8f60182009-09-01 19:01:50 -07009288 final void rebuildAppWindowListLocked() {
9289 int NW = mWindows.size();
9290 int i;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009291 int lastWallpaper = -1;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009292 int numRemoved = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009293
Dianne Hackborna8f60182009-09-01 19:01:50 -07009294 // First remove all existing app windows.
9295 i=0;
9296 while (i < NW) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009297 WindowState w = (WindowState)mWindows.get(i);
9298 if (w.mAppToken != null) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009299 WindowState win = (WindowState)mWindows.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009300 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG,
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009301 "Rebuild removing window: " + win);
Dianne Hackborna8f60182009-09-01 19:01:50 -07009302 NW--;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009303 numRemoved++;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009304 continue;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009305 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER
9306 && lastWallpaper == i-1) {
9307 lastWallpaper = i;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009308 }
9309 i++;
9310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009311
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009312 // The wallpaper window(s) typically live at the bottom of the stack,
9313 // so skip them before adding app tokens.
9314 lastWallpaper++;
9315 i = lastWallpaper;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009316
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009317 // First add all of the exiting app tokens... these are no longer
9318 // in the main app list, but still have windows shown. We put them
9319 // in the back because now that the animation is over we no longer
9320 // will care about them.
9321 int NT = mExitingAppTokens.size();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009322 for (int j=0; j<NT; j++) {
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009323 i = reAddAppWindowsLocked(i, mExitingAppTokens.get(j));
9324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009325
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009326 // And add in the still active app tokens in Z order.
9327 NT = mAppTokens.size();
9328 for (int j=0; j<NT; j++) {
9329 i = reAddAppWindowsLocked(i, mAppTokens.get(j));
Dianne Hackborna8f60182009-09-01 19:01:50 -07009330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009331
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009332 i -= lastWallpaper;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009333 if (i != numRemoved) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009334 Slog.w(TAG, "Rebuild removed " + numRemoved
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009335 + " windows but added " + i);
9336 }
Dianne Hackborna8f60182009-09-01 19:01:50 -07009337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 private final void assignLayersLocked() {
9340 int N = mWindows.size();
9341 int curBaseLayer = 0;
9342 int curLayer = 0;
9343 int i;
Romain Guy06882f82009-06-10 13:36:04 -07009344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 for (i=0; i<N; i++) {
9346 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn7341d7a2009-08-14 11:37:52 -07009347 if (w.mBaseLayer == curBaseLayer || w.mIsImWindow
9348 || (i > 0 && w.mIsWallpaper)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 curLayer += WINDOW_LAYER_MULTIPLIER;
9350 w.mLayer = curLayer;
9351 } else {
9352 curBaseLayer = curLayer = w.mBaseLayer;
9353 w.mLayer = curLayer;
9354 }
9355 if (w.mTargetAppToken != null) {
9356 w.mAnimLayer = w.mLayer + w.mTargetAppToken.animLayerAdjustment;
9357 } else if (w.mAppToken != null) {
9358 w.mAnimLayer = w.mLayer + w.mAppToken.animLayerAdjustment;
9359 } else {
9360 w.mAnimLayer = w.mLayer;
9361 }
9362 if (w.mIsImWindow) {
9363 w.mAnimLayer += mInputMethodAnimLayerAdjustment;
Dianne Hackborn759a39e2009-08-09 17:20:27 -07009364 } else if (w.mIsWallpaper) {
9365 w.mAnimLayer += mWallpaperAnimLayerAdjustment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009367 if (DEBUG_LAYERS) Slog.v(TAG, "Assign layer " + w + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 + w.mAnimLayer);
9369 //System.out.println(
9370 // "Assigned layer " + curLayer + " to " + w.mClient.asBinder());
9371 }
9372 }
9373
9374 private boolean mInLayout = false;
9375 private final void performLayoutAndPlaceSurfacesLocked() {
9376 if (mInLayout) {
Dave Bortcfe65242009-04-09 14:51:04 -07009377 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009378 throw new RuntimeException("Recursive call!");
9379 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009380 Slog.w(TAG, "performLayoutAndPlaceSurfacesLocked called while in layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 return;
9382 }
9383
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009384 if (mWaitingForConfig) {
9385 // Our configuration has changed (most likely rotation), but we
9386 // don't yet have the complete configuration to report to
9387 // applications. Don't do any window layout until we have it.
9388 return;
9389 }
9390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 boolean recoveringMemory = false;
9392 if (mForceRemoves != null) {
9393 recoveringMemory = true;
9394 // Wait a little it for things to settle down, and off we go.
9395 for (int i=0; i<mForceRemoves.size(); i++) {
9396 WindowState ws = mForceRemoves.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009397 Slog.i(TAG, "Force removing: " + ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 removeWindowInnerLocked(ws.mSession, ws);
9399 }
9400 mForceRemoves = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009401 Slog.w(TAG, "Due to memory failure, waiting a bit for next layout");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 Object tmp = new Object();
9403 synchronized (tmp) {
9404 try {
9405 tmp.wait(250);
9406 } catch (InterruptedException e) {
9407 }
9408 }
9409 }
Romain Guy06882f82009-06-10 13:36:04 -07009410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009411 mInLayout = true;
9412 try {
9413 performLayoutAndPlaceSurfacesLockedInner(recoveringMemory);
Romain Guy06882f82009-06-10 13:36:04 -07009414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 int i = mPendingRemove.size()-1;
9416 if (i >= 0) {
9417 while (i >= 0) {
9418 WindowState w = mPendingRemove.get(i);
9419 removeWindowInnerLocked(w.mSession, w);
9420 i--;
9421 }
9422 mPendingRemove.clear();
9423
9424 mInLayout = false;
9425 assignLayersLocked();
9426 mLayoutNeeded = true;
9427 performLayoutAndPlaceSurfacesLocked();
9428
9429 } else {
9430 mInLayout = false;
9431 if (mLayoutNeeded) {
9432 requestAnimationLocked(0);
9433 }
9434 }
9435 } catch (RuntimeException e) {
9436 mInLayout = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009437 Slog.e(TAG, "Unhandled exception while layout out windows", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009438 }
9439 }
9440
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009441 private final int performLayoutLockedInner() {
9442 if (!mLayoutNeeded) {
9443 return 0;
9444 }
9445
9446 mLayoutNeeded = false;
9447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 final int dw = mDisplay.getWidth();
9449 final int dh = mDisplay.getHeight();
9450
9451 final int N = mWindows.size();
9452 int i;
9453
Joe Onorato8a9b2202010-02-26 18:56:32 -08009454 if (DEBUG_LAYOUT) Slog.v(TAG, "performLayout: needed="
Dianne Hackborn9b52a212009-12-11 14:51:35 -08009455 + mLayoutNeeded + " dw=" + dw + " dh=" + dh);
9456
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009457 mPolicy.beginLayoutLw(dw, dh);
Romain Guy06882f82009-06-10 13:36:04 -07009458
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009459 int seq = mLayoutSeq+1;
9460 if (seq < 0) seq = 0;
9461 mLayoutSeq = seq;
9462
9463 // First perform layout of any root windows (not attached
9464 // to another window).
9465 int topAttached = -1;
9466 for (i = N-1; i >= 0; i--) {
9467 WindowState win = (WindowState) mWindows.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009469 // Don't do layout of a window if it is not visible, or
9470 // soon won't be visible, to avoid wasting time and funky
9471 // changes while a window is animating away.
9472 final AppWindowToken atoken = win.mAppToken;
9473 final boolean gone = win.mViewVisibility == View.GONE
9474 || !win.mRelayoutCalled
9475 || win.mRootToken.hidden
9476 || (atoken != null && atoken.hiddenRequested)
9477 || win.mAttachedHidden
9478 || win.mExiting || win.mDestroying;
9479
9480 if (!win.mLayoutAttached) {
9481 if (DEBUG_LAYOUT) Slog.v(TAG, "First pass " + win
9482 + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
9483 + " mLayoutAttached=" + win.mLayoutAttached);
9484 if (DEBUG_LAYOUT && gone) Slog.v(TAG, " (mViewVisibility="
9485 + win.mViewVisibility + " mRelayoutCalled="
9486 + win.mRelayoutCalled + " hidden="
9487 + win.mRootToken.hidden + " hiddenRequested="
9488 + (atoken != null && atoken.hiddenRequested)
9489 + " mAttachedHidden=" + win.mAttachedHidden);
9490 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009491
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009492 // If this view is GONE, then skip it -- keep the current
9493 // frame, and let the caller know so they can ignore it
9494 // if they want. (We do the normal layout for INVISIBLE
9495 // windows, since that means "perform layout as normal,
9496 // just don't display").
9497 if (!gone || !win.mHaveFrame) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08009498 if (!win.mLayoutAttached) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009499 mPolicy.layoutWindowLw(win, win.mAttrs, null);
9500 win.mLayoutSeq = seq;
9501 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9502 + win.mFrame + " mContainingFrame="
9503 + win.mContainingFrame + " mDisplayFrame="
9504 + win.mDisplayFrame);
9505 } else {
9506 if (topAttached < 0) topAttached = i;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07009507 }
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 }
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009510
9511 // Now perform layout of attached windows, which usually
9512 // depend on the position of the window they are attached to.
9513 // XXX does not deal with windows that are attached to windows
9514 // that are themselves attached.
9515 for (i = topAttached; i >= 0; i--) {
9516 WindowState win = (WindowState) mWindows.get(i);
9517
9518 // If this view is GONE, then skip it -- keep the current
9519 // frame, and let the caller know so they can ignore it
9520 // if they want. (We do the normal layout for INVISIBLE
9521 // windows, since that means "perform layout as normal,
9522 // just don't display").
9523 if (win.mLayoutAttached) {
9524 if (DEBUG_LAYOUT) Slog.v(TAG, "Second pass " + win
9525 + " mHaveFrame=" + win.mHaveFrame
9526 + " mViewVisibility=" + win.mViewVisibility
9527 + " mRelayoutCalled=" + win.mRelayoutCalled);
9528 if ((win.mViewVisibility != View.GONE && win.mRelayoutCalled)
9529 || !win.mHaveFrame) {
9530 mPolicy.layoutWindowLw(win, win.mAttrs, win.mAttachedWindow);
9531 win.mLayoutSeq = seq;
9532 if (DEBUG_LAYOUT) Slog.v(TAG, "-> mFrame="
9533 + win.mFrame + " mContainingFrame="
9534 + win.mContainingFrame + " mDisplayFrame="
9535 + win.mDisplayFrame);
9536 }
9537 }
9538 }
9539
9540 return mPolicy.finishLayoutLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 }
Romain Guy06882f82009-06-10 13:36:04 -07009542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 private final void performLayoutAndPlaceSurfacesLockedInner(
9544 boolean recoveringMemory) {
9545 final long currentTime = SystemClock.uptimeMillis();
9546 final int dw = mDisplay.getWidth();
9547 final int dh = mDisplay.getHeight();
9548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 int i;
9550
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009551 if (mFocusMayChange) {
9552 mFocusMayChange = false;
9553 updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES);
9554 }
9555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 if (mFxSession == null) {
9557 mFxSession = new SurfaceSession();
9558 }
Romain Guy06882f82009-06-10 13:36:04 -07009559
Joe Onorato8a9b2202010-02-26 18:56:32 -08009560 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009561
9562 // Initialize state of exiting tokens.
9563 for (i=mExitingTokens.size()-1; i>=0; i--) {
9564 mExitingTokens.get(i).hasVisible = false;
9565 }
9566
9567 // Initialize state of exiting applications.
9568 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
9569 mExitingAppTokens.get(i).hasVisible = false;
9570 }
9571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572 boolean orientationChangeComplete = true;
9573 Session holdScreen = null;
9574 float screenBrightness = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05009575 float buttonBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 boolean focusDisplayed = false;
9577 boolean animating = false;
9578
9579 Surface.openTransaction();
9580 try {
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009581 boolean wallpaperForceHidingChanged = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009582 int repeats = 0;
9583 int changes = 0;
9584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 do {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009586 repeats++;
9587 if (repeats > 6) {
9588 Slog.w(TAG, "Animation repeat aborted after too many iterations");
9589 mLayoutNeeded = false;
9590 break;
9591 }
9592
9593 if ((changes&(WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER
9594 | WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG
9595 | WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT)) != 0) {
9596 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
9597 if ((adjustWallpaperWindowsLocked()&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
9598 assignLayersLocked();
9599 mLayoutNeeded = true;
9600 }
9601 }
9602 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG) != 0) {
9603 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
9604 if (updateOrientationFromAppTokensLocked()) {
9605 mLayoutNeeded = true;
9606 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
9607 }
9608 }
9609 if ((changes&WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT) != 0) {
9610 mLayoutNeeded = true;
9611 }
9612 }
9613
9614 // FIRST LOOP: Perform a layout, if needed.
9615 if (repeats < 4) {
9616 changes = performLayoutLockedInner();
9617 if (changes != 0) {
9618 continue;
9619 }
9620 } else {
9621 Slog.w(TAG, "Layout repeat skipped after too many iterations");
9622 changes = 0;
9623 }
9624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 final int transactionSequence = ++mTransactionSequence;
9626
9627 // Update animations of all applications, including those
9628 // associated with exiting/removed apps
9629 boolean tokensAnimating = false;
9630 final int NAT = mAppTokens.size();
9631 for (i=0; i<NAT; i++) {
9632 if (mAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9633 tokensAnimating = true;
9634 }
9635 }
9636 final int NEAT = mExitingAppTokens.size();
9637 for (i=0; i<NEAT; i++) {
9638 if (mExitingAppTokens.get(i).stepAnimationLocked(currentTime, dw, dh)) {
9639 tokensAnimating = true;
9640 }
9641 }
9642
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009643 // SECOND LOOP: Execute animations and update visibility of windows.
9644
Joe Onorato8a9b2202010-02-26 18:56:32 -08009645 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: seq="
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009646 + transactionSequence + " tokensAnimating="
9647 + tokensAnimating);
9648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009649 animating = tokensAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650
9651 boolean tokenMayBeDrawn = false;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009652 boolean wallpaperMayChange = false;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009653 boolean forceHiding = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654
9655 mPolicy.beginAnimationLw(dw, dh);
9656
Dianne Hackbornbdd52b22009-09-02 21:46:19 -07009657 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659 for (i=N-1; i>=0; i--) {
9660 WindowState w = (WindowState)mWindows.get(i);
9661
9662 final WindowManager.LayoutParams attrs = w.mAttrs;
9663
9664 if (w.mSurface != null) {
9665 // Execute animation.
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009666 if (w.commitFinishDrawingLocked(currentTime)) {
9667 if ((w.mAttrs.flags
9668 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009669 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07009670 "First draw done in potential wallpaper target " + w);
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009671 wallpaperMayChange = true;
9672 }
9673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009674
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009675 boolean wasAnimating = w.mAnimating;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 if (w.stepAnimationLocked(currentTime, dw, dh)) {
9677 animating = true;
9678 //w.dump(" ");
9679 }
Dianne Hackborn6136b7e2009-09-18 01:53:49 -07009680 if (wasAnimating && !w.mAnimating && mWallpaperTarget == w) {
9681 wallpaperMayChange = true;
9682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009683
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009684 if (mPolicy.doesForceHide(w, attrs)) {
9685 if (!wasAnimating && animating) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009686 if (DEBUG_VISIBILITY) Slog.v(TAG,
9687 "Animation done that could impact force hide: "
9688 + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009689 wallpaperForceHidingChanged = true;
Dianne Hackbornb601ce12010-03-01 23:36:02 -08009690 mFocusMayChange = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009691 } else if (w.isReadyForDisplay() && w.mAnimation == null) {
9692 forceHiding = true;
9693 }
9694 } else if (mPolicy.canBeForceHidden(w, attrs)) {
9695 boolean changed;
9696 if (forceHiding) {
9697 changed = w.hideLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009698 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9699 "Now policy hidden: " + w);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009700 } else {
9701 changed = w.showLw(false, false);
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009702 if (DEBUG_VISIBILITY && changed) Slog.v(TAG,
9703 "Now policy shown: " + w);
9704 if (changed) {
9705 if (wallpaperForceHidingChanged
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009706 && w.isVisibleNow() /*w.isReadyForDisplay()*/) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009707 // Assume we will need to animate. If
9708 // we don't (because the wallpaper will
9709 // stay with the lock screen), then we will
9710 // clean up later.
9711 Animation a = mPolicy.createForceHideEnterAnimation();
9712 if (a != null) {
9713 w.setAnimation(a);
9714 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009715 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08009716 if (mCurrentFocus == null ||
9717 mCurrentFocus.mLayer < w.mLayer) {
9718 // We are showing on to of the current
9719 // focus, so re-evaluate focus to make
9720 // sure it is correct.
9721 mFocusMayChange = true;
9722 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009723 }
9724 }
9725 if (changed && (attrs.flags
9726 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
9727 wallpaperMayChange = true;
9728 }
9729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009731 mPolicy.animatingWindowLw(w, attrs);
9732 }
9733
9734 final AppWindowToken atoken = w.mAppToken;
9735 if (atoken != null && (!atoken.allDrawn || atoken.freezingScreen)) {
9736 if (atoken.lastTransactionSequence != transactionSequence) {
9737 atoken.lastTransactionSequence = transactionSequence;
9738 atoken.numInterestingWindows = atoken.numDrawnWindows = 0;
9739 atoken.startingDisplayed = false;
9740 }
9741 if ((w.isOnScreen() || w.mAttrs.type
9742 == WindowManager.LayoutParams.TYPE_BASE_APPLICATION)
9743 && !w.mExiting && !w.mDestroying) {
9744 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009745 Slog.v(TAG, "Eval win " + w + ": isDrawn="
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009746 + w.isDrawnLw()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009747 + ", isAnimating=" + w.isAnimating());
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009748 if (!w.isDrawnLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009749 Slog.v(TAG, "Not displayed: s=" + w.mSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 + " pv=" + w.mPolicyVisibility
9751 + " dp=" + w.mDrawPending
9752 + " cdp=" + w.mCommitDrawPending
9753 + " ah=" + w.mAttachedHidden
9754 + " th=" + atoken.hiddenRequested
9755 + " a=" + w.mAnimating);
9756 }
9757 }
9758 if (w != atoken.startingWindow) {
9759 if (!atoken.freezingScreen || !w.mAppFreezing) {
9760 atoken.numInterestingWindows++;
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009761 if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009762 atoken.numDrawnWindows++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009763 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 "tokenMayBeDrawn: " + atoken
9765 + " freezingScreen=" + atoken.freezingScreen
9766 + " mAppFreezing=" + w.mAppFreezing);
9767 tokenMayBeDrawn = true;
9768 }
9769 }
Dianne Hackborn7433e8a2009-09-27 13:21:20 -07009770 } else if (w.isDrawnLw()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009771 atoken.startingDisplayed = true;
9772 }
9773 }
9774 } else if (w.mReadyToShow) {
9775 w.performShowLocked();
9776 }
9777 }
9778
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009779 changes |= mPolicy.finishAnimationLw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780
9781 if (tokenMayBeDrawn) {
9782 // See if any windows have been drawn, so they (and others
9783 // associated with them) can now be shown.
9784 final int NT = mTokenList.size();
9785 for (i=0; i<NT; i++) {
9786 AppWindowToken wtoken = mTokenList.get(i).appWindowToken;
9787 if (wtoken == null) {
9788 continue;
9789 }
9790 if (wtoken.freezingScreen) {
9791 int numInteresting = wtoken.numInterestingWindows;
9792 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009793 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 "allDrawn: " + wtoken
9795 + " interesting=" + numInteresting
9796 + " drawn=" + wtoken.numDrawnWindows);
9797 wtoken.showAllWindowsLocked();
9798 unsetAppFreezingScreenLocked(wtoken, false, true);
9799 orientationChangeComplete = true;
9800 }
9801 } else if (!wtoken.allDrawn) {
9802 int numInteresting = wtoken.numInterestingWindows;
9803 if (numInteresting > 0 && wtoken.numDrawnWindows >= numInteresting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009804 if (DEBUG_VISIBILITY) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 "allDrawn: " + wtoken
9806 + " interesting=" + numInteresting
9807 + " drawn=" + wtoken.numDrawnWindows);
9808 wtoken.allDrawn = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -08009809 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810
9811 // We can now show all of the drawn windows!
9812 if (!mOpeningApps.contains(wtoken)) {
9813 wtoken.showAllWindowsLocked();
9814 }
9815 }
9816 }
9817 }
9818 }
9819
9820 // If we are ready to perform an app transition, check through
9821 // all of the app tokens to be shown and see if they are ready
9822 // to go.
9823 if (mAppTransitionReady) {
9824 int NN = mOpeningApps.size();
9825 boolean goodToGo = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009826 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 "Checking " + NN + " opening apps (frozen="
9828 + mDisplayFrozen + " timeout="
9829 + mAppTransitionTimeout + ")...");
9830 if (!mDisplayFrozen && !mAppTransitionTimeout) {
9831 // If the display isn't frozen, wait to do anything until
9832 // all of the apps are ready. Otherwise just go because
9833 // we'll unfreeze the display when everyone is ready.
9834 for (i=0; i<NN && goodToGo; i++) {
9835 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009836 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 "Check opening app" + wtoken + ": allDrawn="
9838 + wtoken.allDrawn + " startingDisplayed="
9839 + wtoken.startingDisplayed);
9840 if (!wtoken.allDrawn && !wtoken.startingDisplayed
9841 && !wtoken.startingMoved) {
9842 goodToGo = false;
9843 }
9844 }
9845 }
9846 if (goodToGo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009847 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 int transit = mNextAppTransition;
9849 if (mSkipAppTransitionAnimation) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009850 transit = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 }
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07009852 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 mAppTransitionReady = false;
Dianne Hackborna8f60182009-09-01 19:01:50 -07009854 mAppTransitionRunning = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 mAppTransitionTimeout = false;
9856 mStartingIconInTransition = false;
9857 mSkipAppTransitionAnimation = false;
9858
9859 mH.removeMessages(H.APP_TRANSITION_TIMEOUT);
9860
Dianne Hackborna8f60182009-09-01 19:01:50 -07009861 // If there are applications waiting to come to the
9862 // top of the stack, now is the time to move their windows.
9863 // (Note that we don't do apps going to the bottom
9864 // here -- we want to keep their windows in the old
9865 // Z-order until the animation completes.)
9866 if (mToTopApps.size() > 0) {
9867 NN = mAppTokens.size();
9868 for (i=0; i<NN; i++) {
9869 AppWindowToken wtoken = mAppTokens.get(i);
9870 if (wtoken.sendingToTop) {
9871 wtoken.sendingToTop = false;
9872 moveAppWindowsLocked(wtoken, NN, false);
9873 }
9874 }
9875 mToTopApps.clear();
9876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009877
Dianne Hackborn25994b42009-09-04 14:21:19 -07009878 WindowState oldWallpaper = mWallpaperTarget;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009879
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009880 adjustWallpaperWindowsLocked();
Dianne Hackborn6c3f5712009-08-25 18:42:59 -07009881 wallpaperMayChange = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009882
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009883 // The top-most window will supply the layout params,
9884 // and we will determine it below.
9885 LayoutParams animLp = null;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009886 AppWindowToken animToken = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009887 int bestAnimLayer = -1;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009888
Joe Onorato8a9b2202010-02-26 18:56:32 -08009889 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn3be63c02009-08-20 19:31:38 -07009890 "New wallpaper target=" + mWallpaperTarget
9891 + ", lower target=" + mLowerWallpaperTarget
9892 + ", upper target=" + mUpperWallpaperTarget);
Dianne Hackborn25994b42009-09-04 14:21:19 -07009893 int foundWallpapers = 0;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009894 // Do a first pass through the tokens for two
9895 // things:
9896 // (1) Determine if both the closing and opening
9897 // app token sets are wallpaper targets, in which
9898 // case special animations are needed
9899 // (since the wallpaper needs to stay static
9900 // behind them).
9901 // (2) Find the layout params of the top-most
9902 // application window in the tokens, which is
9903 // what will control the animation theme.
9904 final int NC = mClosingApps.size();
9905 NN = NC + mOpeningApps.size();
9906 for (i=0; i<NN; i++) {
9907 AppWindowToken wtoken;
9908 int mode;
9909 if (i < NC) {
9910 wtoken = mClosingApps.get(i);
9911 mode = 1;
9912 } else {
9913 wtoken = mOpeningApps.get(i-NC);
9914 mode = 2;
9915 }
9916 if (mLowerWallpaperTarget != null) {
9917 if (mLowerWallpaperTarget.mAppToken == wtoken
9918 || mUpperWallpaperTarget.mAppToken == wtoken) {
9919 foundWallpapers |= mode;
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009920 }
9921 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009922 if (wtoken.appFullscreen) {
9923 WindowState ws = wtoken.findMainWindow();
9924 if (ws != null) {
9925 // If this is a compatibility mode
9926 // window, we will always use its anim.
9927 if ((ws.mAttrs.flags&FLAG_COMPATIBLE_WINDOW) != 0) {
9928 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009929 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009930 bestAnimLayer = Integer.MAX_VALUE;
9931 } else if (ws.mLayer > bestAnimLayer) {
9932 animLp = ws.mAttrs;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009933 animToken = ws.mAppToken;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009934 bestAnimLayer = ws.mLayer;
9935 }
Dianne Hackborn25994b42009-09-04 14:21:19 -07009936 }
Dianne Hackbornf8fbdb62009-08-19 12:39:43 -07009937 }
9938 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009939
Dianne Hackborn25994b42009-09-04 14:21:19 -07009940 if (foundWallpapers == 3) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009941 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009942 "Wallpaper animation!");
9943 switch (transit) {
9944 case WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN:
9945 case WindowManagerPolicy.TRANSIT_TASK_OPEN:
9946 case WindowManagerPolicy.TRANSIT_TASK_TO_FRONT:
9947 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_OPEN;
9948 break;
9949 case WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE:
9950 case WindowManagerPolicy.TRANSIT_TASK_CLOSE:
9951 case WindowManagerPolicy.TRANSIT_TASK_TO_BACK:
9952 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_INTRA_CLOSE;
9953 break;
9954 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009955 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009956 "New transit: " + transit);
9957 } else if (oldWallpaper != null) {
9958 // We are transitioning from an activity with
9959 // a wallpaper to one without.
9960 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_CLOSE;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009961 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009962 "New transit away from wallpaper: " + transit);
9963 } else if (mWallpaperTarget != null) {
9964 // We are transitioning from an activity without
9965 // a wallpaper to now showing the wallpaper
9966 transit = WindowManagerPolicy.TRANSIT_WALLPAPER_OPEN;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009967 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
Dianne Hackborn25994b42009-09-04 14:21:19 -07009968 "New transit into wallpaper: " + transit);
9969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009970
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07009971 if ((transit&WindowManagerPolicy.TRANSIT_ENTER_MASK) != 0) {
9972 mLastEnterAnimToken = animToken;
9973 mLastEnterAnimParams = animLp;
9974 } else if (mLastEnterAnimParams != null) {
9975 animLp = mLastEnterAnimParams;
9976 mLastEnterAnimToken = null;
9977 mLastEnterAnimParams = null;
9978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009979
Dianne Hackbornde2606d2009-12-18 16:53:55 -08009980 // If all closing windows are obscured, then there is
9981 // no need to do an animation. This is the case, for
9982 // example, when this transition is being done behind
9983 // the lock screen.
9984 if (!mPolicy.allowAppAnimationsLw()) {
9985 animLp = null;
9986 }
9987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 NN = mOpeningApps.size();
9989 for (i=0; i<NN; i++) {
9990 AppWindowToken wtoken = mOpeningApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009991 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 "Now opening app" + wtoken);
9993 wtoken.reportedVisible = false;
9994 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -07009995 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07009996 setTokenVisibilityLocked(wtoken, animLp, true, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009997 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -07009998 wtoken.waitingToShow = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 wtoken.showAllWindowsLocked();
10000 }
10001 NN = mClosingApps.size();
10002 for (i=0; i<NN; i++) {
10003 AppWindowToken wtoken = mClosingApps.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010004 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 "Now closing app" + wtoken);
10006 wtoken.inPendingTransaction = false;
Dianne Hackborn83360b32009-08-24 18:43:32 -070010007 wtoken.animation = null;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070010008 setTokenVisibilityLocked(wtoken, animLp, false, transit, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010009 wtoken.updateReportedVisibilityLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010010 wtoken.waitingToHide = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 // Force the allDrawn flag, because we want to start
10012 // this guy's animations regardless of whether it's
10013 // gotten drawn.
10014 wtoken.allDrawn = true;
10015 }
10016
Dianne Hackborn8b571a82009-09-25 16:09:43 -070010017 mNextAppTransitionPackage = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 mOpeningApps.clear();
10020 mClosingApps.clear();
10021
10022 // This has changed the visibility of windows, so perform
10023 // a new layout to get them all up-to-date.
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010024 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 mLayoutNeeded = true;
Dianne Hackborn20583ff2009-07-27 21:51:05 -070010026 if (!moveInputMethodWindowsIfNeededLocked(true)) {
10027 assignLayersLocked();
10028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES);
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010030 mFocusMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010031 }
10032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010033
Dianne Hackborn16064f92010-03-25 00:47:24 -070010034 int adjResult = 0;
10035
Dianne Hackborna8f60182009-09-01 19:01:50 -070010036 if (!animating && mAppTransitionRunning) {
10037 // We have finished the animation of an app transition. To do
10038 // this, we have delayed a lot of operations like showing and
10039 // hiding apps, moving apps in Z-order, etc. The app token list
10040 // reflects the correct Z-order, but the window list may now
10041 // be out of sync with it. So here we will just rebuild the
10042 // entire app window list. Fun!
10043 mAppTransitionRunning = false;
10044 // Clear information about apps that were moving.
10045 mToBottomApps.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010046
Dianne Hackborna8f60182009-09-01 19:01:50 -070010047 rebuildAppWindowListLocked();
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010048 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010049 adjResult |= ADJUST_WALLPAPER_LAYERS_CHANGED;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010050 moveInputMethodWindowsIfNeededLocked(false);
10051 wallpaperMayChange = true;
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -080010052 // Since the window list has been rebuilt, focus might
10053 // have to be recomputed since the actual order of windows
10054 // might have changed again.
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010055 mFocusMayChange = true;
Dianne Hackborna8f60182009-09-01 19:01:50 -070010056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010057
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010058 if (wallpaperForceHidingChanged && changes == 0 && !mAppTransitionReady) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010059 // At this point, there was a window with a wallpaper that
10060 // was force hiding other windows behind it, but now it
10061 // is going away. This may be simple -- just animate
10062 // away the wallpaper and its window -- or it may be
10063 // hard -- the wallpaper now needs to be shown behind
10064 // something that was hidden.
10065 WindowState oldWallpaper = mWallpaperTarget;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010066 if (mLowerWallpaperTarget != null
10067 && mLowerWallpaperTarget.mAppToken != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010068 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010069 "wallpaperForceHiding changed with lower="
10070 + mLowerWallpaperTarget);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010071 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010072 "hidden=" + mLowerWallpaperTarget.mAppToken.hidden +
10073 " hiddenRequested=" + mLowerWallpaperTarget.mAppToken.hiddenRequested);
10074 if (mLowerWallpaperTarget.mAppToken.hidden) {
10075 // The lower target has become hidden before we
10076 // actually started the animation... let's completely
10077 // re-evaluate everything.
10078 mLowerWallpaperTarget = mUpperWallpaperTarget = null;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010079 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010080 }
10081 }
Dianne Hackborn16064f92010-03-25 00:47:24 -070010082 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010083 wallpaperMayChange = false;
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010084 wallpaperForceHidingChanged = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010085 if (DEBUG_WALLPAPER) Slog.v(TAG, "****** OLD: " + oldWallpaper
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010086 + " NEW: " + mWallpaperTarget
10087 + " LOWER: " + mLowerWallpaperTarget);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010088 if (mLowerWallpaperTarget == null) {
10089 // Whoops, we don't need a special wallpaper animation.
10090 // Clear them out.
10091 forceHiding = false;
10092 for (i=N-1; i>=0; i--) {
10093 WindowState w = (WindowState)mWindows.get(i);
10094 if (w.mSurface != null) {
10095 final WindowManager.LayoutParams attrs = w.mAttrs;
Suchi Amalapurapuc03d28b2009-10-28 14:32:05 -070010096 if (mPolicy.doesForceHide(w, attrs) && w.isVisibleLw()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010097 if (DEBUG_FOCUS) Slog.i(TAG, "win=" + w + " force hides other windows");
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010098 forceHiding = true;
10099 } else if (mPolicy.canBeForceHidden(w, attrs)) {
10100 if (!w.mAnimating) {
10101 // We set the animation above so it
10102 // is not yet running.
10103 w.clearAnimation();
10104 }
10105 }
10106 }
10107 }
10108 }
10109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010110
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010111 if (wallpaperMayChange) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010112 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010113 "Wallpaper may change! Adjusting");
Dianne Hackborn16064f92010-03-25 00:47:24 -070010114 adjResult |= adjustWallpaperWindowsLocked();
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010116
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010117 if ((adjResult&ADJUST_WALLPAPER_LAYERS_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010118 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010119 "Wallpaper layer changed: assigning layers + relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010120 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010121 assignLayersLocked();
10122 } else if ((adjResult&ADJUST_WALLPAPER_VISIBILITY_CHANGED) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010123 if (DEBUG_WALLPAPER) Slog.v(TAG,
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010124 "Wallpaper visibility changed: relayout");
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010125 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010127
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010128 if (mFocusMayChange) {
10129 mFocusMayChange = false;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010130 if (updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010131 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010132 adjResult = 0;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010133 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010134 }
10135
10136 if (mLayoutNeeded) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010137 changes |= PhoneWindowManager.FINISH_LAYOUT_REDO_LAYOUT;
Dianne Hackborn6c3f5712009-08-25 18:42:59 -070010138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010139
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010140 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "*** ANIM STEP: changes=0x"
10141 + Integer.toHexString(changes));
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010142
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010143 } while (changes != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144
10145 // THIRD LOOP: Update the surfaces of all windows.
10146
10147 final boolean someoneLosingFocus = mLosingFocus.size() != 0;
10148
10149 boolean obscured = false;
10150 boolean blurring = false;
10151 boolean dimming = false;
10152 boolean covered = false;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010153 boolean syswin = false;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010154 boolean backgroundFillerShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155
Dianne Hackbornbdd52b22009-09-02 21:46:19 -070010156 final int N = mWindows.size();
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010158 for (i=N-1; i>=0; i--) {
10159 WindowState w = (WindowState)mWindows.get(i);
10160
10161 boolean displayed = false;
10162 final WindowManager.LayoutParams attrs = w.mAttrs;
10163 final int attrFlags = attrs.flags;
10164
10165 if (w.mSurface != null) {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010166 // XXX NOTE: The logic here could be improved. We have
10167 // the decision about whether to resize a window separated
10168 // from whether to hide the surface. This can cause us to
10169 // resize a surface even if we are going to hide it. You
10170 // can see this by (1) holding device in landscape mode on
10171 // home screen; (2) tapping browser icon (device will rotate
10172 // to landscape; (3) tap home. The wallpaper will be resized
10173 // in step 2 but then immediately hidden, causing us to
10174 // have to resize and then redraw it again in step 3. It
10175 // would be nice to figure out how to avoid this, but it is
10176 // difficult because we do need to resize surfaces in some
10177 // cases while they are hidden such as when first showing a
10178 // window.
10179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 w.computeShownFrameLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010181 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 TAG, "Placing surface #" + i + " " + w.mSurface
10183 + ": new=" + w.mShownFrame + ", old="
10184 + w.mLastShownFrame);
10185
10186 boolean resize;
10187 int width, height;
10188 if ((w.mAttrs.flags & w.mAttrs.FLAG_SCALED) != 0) {
10189 resize = w.mLastRequestedWidth != w.mRequestedWidth ||
10190 w.mLastRequestedHeight != w.mRequestedHeight;
10191 // for a scaled surface, we just want to use
10192 // the requested size.
10193 width = w.mRequestedWidth;
10194 height = w.mRequestedHeight;
10195 w.mLastRequestedWidth = width;
10196 w.mLastRequestedHeight = height;
10197 w.mLastShownFrame.set(w.mShownFrame);
10198 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010199 if (SHOW_TRANSACTIONS) logSurface(w,
10200 "POS " + w.mShownFrame.left
10201 + ", " + w.mShownFrame.top, null);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010202 w.mSurfaceX = w.mShownFrame.left;
10203 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204 w.mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
10205 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010206 Slog.w(TAG, "Error positioning surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 if (!recoveringMemory) {
10208 reclaimSomeSurfaceMemoryLocked(w, "position");
10209 }
10210 }
10211 } else {
10212 resize = !w.mLastShownFrame.equals(w.mShownFrame);
10213 width = w.mShownFrame.width();
10214 height = w.mShownFrame.height();
10215 w.mLastShownFrame.set(w.mShownFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 }
10217
10218 if (resize) {
10219 if (width < 1) width = 1;
10220 if (height < 1) height = 1;
10221 if (w.mSurface != null) {
10222 try {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010223 if (SHOW_TRANSACTIONS) logSurface(w,
10224 "POS " + w.mShownFrame.left + ","
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010225 + w.mShownFrame.top + " SIZE "
10226 + w.mShownFrame.width() + "x"
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010227 + w.mShownFrame.height(), null);
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010228 w.mSurfaceResized = true;
Dianne Hackborn16064f92010-03-25 00:47:24 -070010229 w.mSurfaceW = width;
10230 w.mSurfaceH = height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 w.mSurface.setSize(width, height);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010232 w.mSurfaceX = w.mShownFrame.left;
10233 w.mSurfaceY = w.mShownFrame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 w.mSurface.setPosition(w.mShownFrame.left,
10235 w.mShownFrame.top);
10236 } catch (RuntimeException e) {
10237 // If something goes wrong with the surface (such
10238 // as running out of memory), don't take down the
10239 // entire system.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010240 Slog.e(TAG, "Failure updating surface of " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 + "size=(" + width + "x" + height
10242 + "), pos=(" + w.mShownFrame.left
10243 + "," + w.mShownFrame.top + ")", e);
10244 if (!recoveringMemory) {
10245 reclaimSomeSurfaceMemoryLocked(w, "size");
10246 }
10247 }
10248 }
10249 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010250 if (!w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 w.mContentInsetsChanged =
10252 !w.mLastContentInsets.equals(w.mContentInsets);
10253 w.mVisibleInsetsChanged =
10254 !w.mLastVisibleInsets.equals(w.mVisibleInsets);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010255 boolean configChanged =
10256 w.mConfiguration != mCurConfiguration
10257 && (w.mConfiguration == null
10258 || mCurConfiguration.diff(w.mConfiguration) != 0);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010259 if (DEBUG_CONFIGURATION && configChanged) {
10260 Slog.v(TAG, "Win " + w + " config changed: "
10261 + mCurConfiguration);
10262 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080010263 if (localLOGV) Slog.v(TAG, "Resizing " + w
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010264 + ": configChanged=" + configChanged
10265 + " last=" + w.mLastFrame + " frame=" + w.mFrame);
Romain Guy06882f82009-06-10 13:36:04 -070010266 if (!w.mLastFrame.equals(w.mFrame)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 || w.mContentInsetsChanged
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010268 || w.mVisibleInsetsChanged
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010269 || w.mSurfaceResized
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010270 || configChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 w.mLastFrame.set(w.mFrame);
10272 w.mLastContentInsets.set(w.mContentInsets);
10273 w.mLastVisibleInsets.set(w.mVisibleInsets);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010274 // If the screen is currently frozen, then keep
10275 // it frozen until this window draws at its new
10276 // orientation.
10277 if (mDisplayFrozen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010278 if (DEBUG_ORIENTATION) Slog.v(TAG,
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010279 "Resizing while display frozen: " + w);
10280 w.mOrientationChanging = true;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010281 if (!mWindowsFreezingScreen) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010282 mWindowsFreezingScreen = true;
10283 // XXX should probably keep timeout from
10284 // when we first froze the display.
10285 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10286 mH.sendMessageDelayed(mH.obtainMessage(
10287 H.WINDOW_FREEZE_TIMEOUT), 2000);
10288 }
10289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 // If the orientation is changing, then we need to
10291 // hold off on unfreezing the display until this
10292 // window has been redrawn; to do that, we need
10293 // to go through the process of getting informed
10294 // by the application when it has finished drawing.
10295 if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010296 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 "Orientation start waiting for draw in "
10298 + w + ", surface " + w.mSurface);
10299 w.mDrawPending = true;
10300 w.mCommitDrawPending = false;
10301 w.mReadyToShow = false;
10302 if (w.mAppToken != null) {
10303 w.mAppToken.allDrawn = false;
10304 }
10305 }
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010306 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 "Resizing window " + w + " to " + w.mFrame);
10308 mResizingWindows.add(w);
10309 } else if (w.mOrientationChanging) {
10310 if (!w.mDrawPending && !w.mCommitDrawPending) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010311 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010312 "Orientation not waiting for draw in "
10313 + w + ", surface " + w.mSurface);
10314 w.mOrientationChanging = false;
10315 }
10316 }
10317 }
10318
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010319 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 if (!w.mLastHidden) {
10321 //dump();
Dianne Hackborn5943c202010-04-12 21:36:49 -070010322 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Window hiding: waitingToShow="
10323 + w.mRootToken.waitingToShow + " polvis="
10324 + w.mPolicyVisibility + " atthid="
10325 + w.mAttachedHidden + " tokhid="
10326 + w.mRootToken.hidden + " vis="
10327 + w.mViewVisibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 w.mLastHidden = true;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010329 if (SHOW_TRANSACTIONS) logSurface(w,
10330 "HIDE (performLayout)", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 if (w.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010332 w.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 try {
10334 w.mSurface.hide();
10335 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010336 Slog.w(TAG, "Exception hiding surface in " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 }
10338 }
10339 mKeyWaiter.releasePendingPointerLocked(w.mSession);
10340 }
10341 // If we are waiting for this window to handle an
10342 // orientation change, well, it is hidden, so
10343 // doesn't really matter. Note that this does
10344 // introduce a potential glitch if the window
10345 // becomes unhidden before it has drawn for the
10346 // new orientation.
10347 if (w.mOrientationChanging) {
10348 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010349 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 "Orientation change skips hidden " + w);
10351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 } else if (w.mLastLayer != w.mAnimLayer
10353 || w.mLastAlpha != w.mShownAlpha
10354 || w.mLastDsDx != w.mDsDx
10355 || w.mLastDtDx != w.mDtDx
10356 || w.mLastDsDy != w.mDsDy
10357 || w.mLastDtDy != w.mDtDy
10358 || w.mLastHScale != w.mHScale
10359 || w.mLastVScale != w.mVScale
10360 || w.mLastHidden) {
10361 displayed = true;
10362 w.mLastAlpha = w.mShownAlpha;
10363 w.mLastLayer = w.mAnimLayer;
10364 w.mLastDsDx = w.mDsDx;
10365 w.mLastDtDx = w.mDtDx;
10366 w.mLastDsDy = w.mDsDy;
10367 w.mLastDtDy = w.mDtDy;
10368 w.mLastHScale = w.mHScale;
10369 w.mLastVScale = w.mVScale;
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010370 if (SHOW_TRANSACTIONS) logSurface(w,
10371 "alpha=" + w.mShownAlpha + " layer=" + w.mAnimLayer
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010372 + " matrix=[" + (w.mDsDx*w.mHScale)
10373 + "," + (w.mDtDx*w.mVScale)
10374 + "][" + (w.mDsDy*w.mHScale)
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010375 + "," + (w.mDtDy*w.mVScale) + "]", null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 if (w.mSurface != null) {
10377 try {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010378 w.mSurfaceAlpha = w.mShownAlpha;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 w.mSurface.setAlpha(w.mShownAlpha);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010380 w.mSurfaceLayer = w.mAnimLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 w.mSurface.setLayer(w.mAnimLayer);
10382 w.mSurface.setMatrix(
10383 w.mDsDx*w.mHScale, w.mDtDx*w.mVScale,
10384 w.mDsDy*w.mHScale, w.mDtDy*w.mVScale);
10385 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010386 Slog.w(TAG, "Error updating surface in " + w, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 if (!recoveringMemory) {
10388 reclaimSomeSurfaceMemoryLocked(w, "update");
10389 }
10390 }
10391 }
10392
10393 if (w.mLastHidden && !w.mDrawPending
10394 && !w.mCommitDrawPending
10395 && !w.mReadyToShow) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010396 if (SHOW_TRANSACTIONS) logSurface(w,
10397 "SHOW (performLayout)", null);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010398 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399 + " during relayout");
10400 if (showSurfaceRobustlyLocked(w)) {
10401 w.mHasDrawn = true;
10402 w.mLastHidden = false;
10403 } else {
10404 w.mOrientationChanging = false;
10405 }
10406 }
10407 if (w.mSurface != null) {
10408 w.mToken.hasVisible = true;
10409 }
10410 } else {
10411 displayed = true;
10412 }
10413
10414 if (displayed) {
10415 if (!covered) {
Romain Guy980a9382010-01-08 15:06:28 -080010416 if (attrs.width == LayoutParams.MATCH_PARENT
10417 && attrs.height == LayoutParams.MATCH_PARENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418 covered = true;
10419 }
10420 }
10421 if (w.mOrientationChanging) {
10422 if (w.mDrawPending || w.mCommitDrawPending) {
10423 orientationChangeComplete = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010424 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 "Orientation continue waiting for draw in " + w);
10426 } else {
10427 w.mOrientationChanging = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010428 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 "Orientation change complete in " + w);
10430 }
10431 }
10432 w.mToken.hasVisible = true;
10433 }
10434 } else if (w.mOrientationChanging) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010435 if (DEBUG_ORIENTATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 "Orientation change skips hidden " + w);
10437 w.mOrientationChanging = false;
10438 }
10439
10440 final boolean canBeSeen = w.isDisplayedLw();
10441
10442 if (someoneLosingFocus && w == mCurrentFocus && canBeSeen) {
10443 focusDisplayed = true;
10444 }
10445
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010446 final boolean obscuredChanged = w.mObscured != obscured;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 // Update effect.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010449 if (!(w.mObscured=obscured)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 if (w.mSurface != null) {
10451 if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
10452 holdScreen = w.mSession;
10453 }
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010454 if (!syswin && w.mAttrs.screenBrightness >= 0
10455 && screenBrightness < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 screenBrightness = w.mAttrs.screenBrightness;
10457 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -050010458 if (!syswin && w.mAttrs.buttonBrightness >= 0
10459 && buttonBrightness < 0) {
10460 buttonBrightness = w.mAttrs.buttonBrightness;
10461 }
Mike Lockwood46af6a82010-03-09 08:28:22 -050010462 if (canBeSeen
10463 && (attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
10464 || attrs.type == WindowManager.LayoutParams.TYPE_KEYGUARD
10465 || attrs.type == WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -070010466 syswin = true;
10467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010469
Dianne Hackborn25994b42009-09-04 14:21:19 -070010470 boolean opaqueDrawn = canBeSeen && w.isOpaqueDrawn();
10471 if (opaqueDrawn && w.isFullscreen(dw, dh)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 // This window completely covers everything behind it,
10473 // so we want to leave all of them as unblurred (for
10474 // performance reasons).
10475 obscured = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010476 } else if (opaqueDrawn && w.needsBackgroundFiller(dw, dh)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010477 if (SHOW_TRANSACTIONS) Slog.d(TAG, "showing background filler");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010478 // This window is in compatibility mode, and needs background filler.
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010479 obscured = true;
10480 if (mBackgroundFillerSurface == null) {
10481 try {
10482 mBackgroundFillerSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010483 "BackGroundFiller",
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010484 0, dw, dh,
10485 PixelFormat.OPAQUE,
10486 Surface.FX_SURFACE_NORMAL);
10487 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010488 Slog.e(TAG, "Exception creating filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010489 }
10490 }
10491 try {
10492 mBackgroundFillerSurface.setPosition(0, 0);
10493 mBackgroundFillerSurface.setSize(dw, dh);
10494 // Using the same layer as Dim because they will never be shown at the
10495 // same time.
10496 mBackgroundFillerSurface.setLayer(w.mAnimLayer - 1);
10497 mBackgroundFillerSurface.show();
10498 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010499 Slog.e(TAG, "Exception showing filler surface");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010500 }
10501 backgroundFillerShown = true;
10502 mBackgroundFillerShown = true;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010503 } else if (canBeSeen && !obscured &&
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 (attrFlags&FLAG_BLUR_BEHIND|FLAG_DIM_BEHIND) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010505 if (localLOGV) Slog.v(TAG, "Win " + w
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 + ": blurring=" + blurring
10507 + " obscured=" + obscured
10508 + " displayed=" + displayed);
10509 if ((attrFlags&FLAG_DIM_BEHIND) != 0) {
10510 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010511 //Slog.i(TAG, "DIM BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 dimming = true;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010513 if (mDimAnimator == null) {
10514 mDimAnimator = new DimAnimator(mFxSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010516 mDimAnimator.show(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010517 mDimAnimator.updateParameters(w, currentTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 }
10520 if ((attrFlags&FLAG_BLUR_BEHIND) != 0) {
10521 if (!blurring) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010522 //Slog.i(TAG, "BLUR BEHIND: " + w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 blurring = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 if (mBlurSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010525 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 + mBlurSurface + ": CREATE");
10527 try {
Romain Guy06882f82009-06-10 13:36:04 -070010528 mBlurSurface = new Surface(mFxSession, 0,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080010529 "BlurSurface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 -1, 16, 16,
10531 PixelFormat.OPAQUE,
10532 Surface.FX_SURFACE_BLUR);
10533 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010534 Slog.e(TAG, "Exception creating Blur surface", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 }
10536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 if (mBlurSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010538 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10539 + mBlurSurface + ": pos=(0,0) (" +
10540 dw + "x" + dh + "), layer=" + (w.mAnimLayer-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 mBlurSurface.setPosition(0, 0);
10542 mBlurSurface.setSize(dw, dh);
Dianne Hackborn16064f92010-03-25 00:47:24 -070010543 mBlurSurface.setLayer(w.mAnimLayer-2);
10544 if (!mBlurShown) {
10545 try {
10546 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR "
10547 + mBlurSurface + ": SHOW");
10548 mBlurSurface.show();
10549 } catch (RuntimeException e) {
10550 Slog.w(TAG, "Failure showing blur surface", e);
10551 }
10552 mBlurShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 }
10554 }
10555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 }
10557 }
10558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010559
Dianne Hackborne9e9bca2009-08-18 15:08:22 -070010560 if (obscuredChanged && mWallpaperTarget == w) {
10561 // This is the wallpaper target and its obscured state
10562 // changed... make sure the current wallaper's visibility
10563 // has been updated accordingly.
10564 updateWallpaperVisibilityLocked();
10565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010567
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010568 if (backgroundFillerShown == false && mBackgroundFillerShown) {
10569 mBackgroundFillerShown = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010570 if (SHOW_TRANSACTIONS) Slog.d(TAG, "hiding background filler");
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010571 try {
10572 mBackgroundFillerSurface.hide();
10573 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010574 Slog.e(TAG, "Exception hiding filler surface", e);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -070010575 }
10576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070010578 if (mDimAnimator != null && mDimAnimator.mDimShown) {
Dianne Hackbornde2606d2009-12-18 16:53:55 -080010579 animating |= mDimAnimator.updateSurface(dimming, currentTime,
10580 mDisplayFrozen || !mPolicy.isScreenOn());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 }
Romain Guy06882f82009-06-10 13:36:04 -070010582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 if (!blurring && mBlurShown) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010584 if (SHOW_TRANSACTIONS) Slog.i(TAG, " BLUR " + mBlurSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 + ": HIDE");
10586 try {
10587 mBlurSurface.hide();
10588 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010589 Slog.w(TAG, "Illegal argument exception hiding blur surface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591 mBlurShown = false;
10592 }
10593
Joe Onorato8a9b2202010-02-26 18:56:32 -080010594 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010596 Slog.e(TAG, "Unhandled exception in Window Manager", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 }
10598
10599 Surface.closeTransaction();
Romain Guy06882f82009-06-10 13:36:04 -070010600
Joe Onorato8a9b2202010-02-26 18:56:32 -080010601 if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 "With display frozen, orientationChangeComplete="
10603 + orientationChangeComplete);
10604 if (orientationChangeComplete) {
10605 if (mWindowsFreezingScreen) {
10606 mWindowsFreezingScreen = false;
10607 mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
10608 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010609 stopFreezingDisplayLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 }
Romain Guy06882f82009-06-10 13:36:04 -070010611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 i = mResizingWindows.size();
10613 if (i > 0) {
10614 do {
10615 i--;
10616 WindowState win = mResizingWindows.get(i);
10617 try {
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010618 if (DEBUG_RESIZE || DEBUG_ORIENTATION) Slog.v(TAG,
10619 "Reporting new frame to " + win + ": " + win.mFrame);
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010620 int diff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010621 boolean configChanged =
10622 win.mConfiguration != mCurConfiguration
10623 && (win.mConfiguration == null
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010624 || (diff=mCurConfiguration.diff(win.mConfiguration)) != 0);
10625 if ((DEBUG_RESIZE || DEBUG_ORIENTATION || DEBUG_CONFIGURATION)
10626 && configChanged) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010627 Slog.i(TAG, "Sending new config to window " + win + ": "
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010628 + win.mFrame.width() + "x" + win.mFrame.height()
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010629 + " / " + mCurConfiguration + " / 0x"
10630 + Integer.toHexString(diff));
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010631 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -070010632 win.mConfiguration = mCurConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 win.mClient.resized(win.mFrame.width(),
10634 win.mFrame.height(), win.mLastContentInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010635 win.mLastVisibleInsets, win.mDrawPending,
10636 configChanged ? win.mConfiguration : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 win.mContentInsetsChanged = false;
10638 win.mVisibleInsetsChanged = false;
Dianne Hackbornac3587d2010-03-11 11:12:11 -080010639 win.mSurfaceResized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 } catch (RemoteException e) {
10641 win.mOrientationChanging = false;
10642 }
10643 } while (i > 0);
10644 mResizingWindows.clear();
10645 }
Romain Guy06882f82009-06-10 13:36:04 -070010646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 // Destroy the surface of any windows that are no longer visible.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010648 boolean wallpaperDestroyed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 i = mDestroySurface.size();
10650 if (i > 0) {
10651 do {
10652 i--;
10653 WindowState win = mDestroySurface.get(i);
10654 win.mDestroying = false;
10655 if (mInputMethodWindow == win) {
10656 mInputMethodWindow = null;
10657 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010658 if (win == mWallpaperTarget) {
10659 wallpaperDestroyed = true;
10660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 win.destroySurfaceLocked();
10662 } while (i > 0);
10663 mDestroySurface.clear();
10664 }
10665
10666 // Time to remove any exiting tokens?
10667 for (i=mExitingTokens.size()-1; i>=0; i--) {
10668 WindowToken token = mExitingTokens.get(i);
10669 if (!token.hasVisible) {
10670 mExitingTokens.remove(i);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070010671 if (token.windowType == TYPE_WALLPAPER) {
10672 mWallpaperTokens.remove(token);
10673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 }
10675 }
10676
10677 // Time to remove any exiting applications?
10678 for (i=mExitingAppTokens.size()-1; i>=0; i--) {
10679 AppWindowToken token = mExitingAppTokens.get(i);
10680 if (!token.hasVisible && !mClosingApps.contains(token)) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070010681 // Make sure there is no animation running on this token,
10682 // so any windows associated with it will be removed as
10683 // soon as their animations are complete
10684 token.animation = null;
10685 token.animating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 mAppTokens.remove(token);
10687 mExitingAppTokens.remove(i);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070010688 if (mLastEnterAnimToken == token) {
10689 mLastEnterAnimToken = null;
10690 mLastEnterAnimParams = null;
10691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010692 }
10693 }
10694
Dianne Hackborna8f60182009-09-01 19:01:50 -070010695 boolean needRelayout = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010696
Dianne Hackborna8f60182009-09-01 19:01:50 -070010697 if (!animating && mAppTransitionRunning) {
10698 // We have finished the animation of an app transition. To do
10699 // this, we have delayed a lot of operations like showing and
10700 // hiding apps, moving apps in Z-order, etc. The app token list
10701 // reflects the correct Z-order, but the window list may now
10702 // be out of sync with it. So here we will just rebuild the
10703 // entire app window list. Fun!
10704 mAppTransitionRunning = false;
10705 needRelayout = true;
10706 rebuildAppWindowListLocked();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010707 assignLayersLocked();
Dianne Hackborna8f60182009-09-01 19:01:50 -070010708 // Clear information about apps that were moving.
10709 mToBottomApps.clear();
10710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 if (focusDisplayed) {
10713 mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
10714 }
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010715 if (wallpaperDestroyed) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010716 needRelayout = adjustWallpaperWindowsLocked() != 0;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010717 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070010718 if (needRelayout) {
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070010719 requestAnimationLocked(0);
10720 } else if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 requestAnimationLocked(currentTime+(1000/60)-SystemClock.uptimeMillis());
10722 }
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070010723
10724 if (DEBUG_FREEZE) Slog.v(TAG, "Layout: mDisplayFrozen=" + mDisplayFrozen
10725 + " holdScreen=" + holdScreen);
10726 if (!mDisplayFrozen) {
10727 mQueue.setHoldScreenLocked(holdScreen != null);
10728 if (screenBrightness < 0 || screenBrightness > 1.0f) {
10729 mPowerManager.setScreenBrightnessOverride(-1);
10730 } else {
10731 mPowerManager.setScreenBrightnessOverride((int)
10732 (screenBrightness * Power.BRIGHTNESS_ON));
10733 }
10734 if (buttonBrightness < 0 || buttonBrightness > 1.0f) {
10735 mPowerManager.setButtonBrightnessOverride(-1);
10736 } else {
10737 mPowerManager.setButtonBrightnessOverride((int)
10738 (buttonBrightness * Power.BRIGHTNESS_ON));
10739 }
10740 if (holdScreen != mHoldingScreenOn) {
10741 mHoldingScreenOn = holdScreen;
10742 Message m = mH.obtainMessage(H.HOLD_SCREEN_CHANGED, holdScreen);
10743 mH.sendMessage(m);
10744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010746
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010747 if (mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010748 if (DEBUG_VISIBILITY) Slog.v(TAG, "Turning screen on after layout!");
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010749 mPowerManager.userActivity(SystemClock.uptimeMillis(), false,
10750 LocalPowerManager.BUTTON_EVENT, true);
10751 mTurnOnScreen = false;
10752 }
Dianne Hackbornf3bea9c2009-12-09 18:26:21 -080010753
10754 // Check to see if we are now in a state where the screen should
10755 // be enabled, because the window obscured flags have changed.
10756 enableScreenIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 }
10758
10759 void requestAnimationLocked(long delay) {
10760 if (!mAnimationPending) {
10761 mAnimationPending = true;
10762 mH.sendMessageDelayed(mH.obtainMessage(H.ANIMATE), delay);
10763 }
10764 }
Romain Guy06882f82009-06-10 13:36:04 -070010765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 /**
10767 * Have the surface flinger show a surface, robustly dealing with
10768 * error conditions. In particular, if there is not enough memory
10769 * to show the surface, then we will try to get rid of other surfaces
10770 * in order to succeed.
Romain Guy06882f82009-06-10 13:36:04 -070010771 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010772 * @return Returns true if the surface was successfully shown.
10773 */
10774 boolean showSurfaceRobustlyLocked(WindowState win) {
10775 try {
10776 if (win.mSurface != null) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070010777 win.mSurfaceShown = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 win.mSurface.show();
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010779 if (win.mTurnOnScreen) {
Dianne Hackbornb601ce12010-03-01 23:36:02 -080010780 if (DEBUG_VISIBILITY) Slog.v(TAG,
10781 "Show surface turning screen on: " + win);
Dianne Hackborn93e462b2009-09-15 22:50:40 -070010782 win.mTurnOnScreen = false;
10783 mTurnOnScreen = true;
10784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 }
10786 return true;
10787 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010788 Slog.w(TAG, "Failure showing surface " + win.mSurface + " in " + win);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 }
Romain Guy06882f82009-06-10 13:36:04 -070010790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010791 reclaimSomeSurfaceMemoryLocked(win, "show");
Romain Guy06882f82009-06-10 13:36:04 -070010792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 return false;
10794 }
Romain Guy06882f82009-06-10 13:36:04 -070010795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010796 void reclaimSomeSurfaceMemoryLocked(WindowState win, String operation) {
10797 final Surface surface = win.mSurface;
Romain Guy06882f82009-06-10 13:36:04 -070010798
Doug Zongkerab5c49c2009-12-04 10:31:43 -080010799 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, win.toString(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 win.mSession.mPid, operation);
Romain Guy06882f82009-06-10 13:36:04 -070010801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 if (mForceRemoves == null) {
10803 mForceRemoves = new ArrayList<WindowState>();
10804 }
Romain Guy06882f82009-06-10 13:36:04 -070010805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 long callingIdentity = Binder.clearCallingIdentity();
10807 try {
10808 // There was some problem... first, do a sanity check of the
10809 // window list to make sure we haven't left any dangling surfaces
10810 // around.
10811 int N = mWindows.size();
10812 boolean leakedSurface = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010813 Slog.i(TAG, "Out of memory for surface! Looking for leaks...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 for (int i=0; i<N; i++) {
10815 WindowState ws = (WindowState)mWindows.get(i);
10816 if (ws.mSurface != null) {
10817 if (!mSessions.contains(ws.mSession)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010818 Slog.w(TAG, "LEAKED SURFACE (session doesn't exist): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 + ws + " surface=" + ws.mSurface
10820 + " token=" + win.mToken
10821 + " pid=" + ws.mSession.mPid
10822 + " uid=" + ws.mSession.mUid);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010823 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010824 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 ws.mSurface = null;
10826 mForceRemoves.add(ws);
10827 i--;
10828 N--;
10829 leakedSurface = true;
10830 } else if (win.mAppToken != null && win.mAppToken.clientHidden) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010831 Slog.w(TAG, "LEAKED SURFACE (app token hidden): "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010832 + ws + " surface=" + ws.mSurface
10833 + " token=" + win.mAppToken);
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010834 ws.mSurface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010835 ws.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 ws.mSurface = null;
10837 leakedSurface = true;
10838 }
10839 }
10840 }
Romain Guy06882f82009-06-10 13:36:04 -070010841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 boolean killedApps = false;
10843 if (!leakedSurface) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010844 Slog.w(TAG, "No leaked surfaces; killing applicatons!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 SparseIntArray pidCandidates = new SparseIntArray();
10846 for (int i=0; i<N; i++) {
10847 WindowState ws = (WindowState)mWindows.get(i);
10848 if (ws.mSurface != null) {
10849 pidCandidates.append(ws.mSession.mPid, ws.mSession.mPid);
10850 }
10851 }
10852 if (pidCandidates.size() > 0) {
10853 int[] pids = new int[pidCandidates.size()];
10854 for (int i=0; i<pids.length; i++) {
10855 pids[i] = pidCandidates.keyAt(i);
10856 }
10857 try {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -070010858 if (mActivityManager.killPids(pids, "Free memory")) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 killedApps = true;
10860 }
10861 } catch (RemoteException e) {
10862 }
10863 }
10864 }
Romain Guy06882f82009-06-10 13:36:04 -070010865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866 if (leakedSurface || killedApps) {
10867 // We managed to reclaim some memory, so get rid of the trouble
10868 // surface and ask the app to request another one.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010869 Slog.w(TAG, "Looks like we have reclaimed some memory, clearing surface for retry.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 if (surface != null) {
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070010871 surface.destroy();
Dianne Hackborn16064f92010-03-25 00:47:24 -070010872 win.mSurfaceShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 win.mSurface = null;
10874 }
Romain Guy06882f82009-06-10 13:36:04 -070010875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876 try {
10877 win.mClient.dispatchGetNewSurface();
10878 } catch (RemoteException e) {
10879 }
10880 }
10881 } finally {
10882 Binder.restoreCallingIdentity(callingIdentity);
10883 }
10884 }
Romain Guy06882f82009-06-10 13:36:04 -070010885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 private boolean updateFocusedWindowLocked(int mode) {
10887 WindowState newFocus = computeFocusedWindowLocked();
10888 if (mCurrentFocus != newFocus) {
10889 // This check makes sure that we don't already have the focus
10890 // change message pending.
10891 mH.removeMessages(H.REPORT_FOCUS_CHANGE);
10892 mH.sendEmptyMessage(H.REPORT_FOCUS_CHANGE);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010893 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 TAG, "Changing focus from " + mCurrentFocus + " to " + newFocus);
10895 final WindowState oldFocus = mCurrentFocus;
10896 mCurrentFocus = newFocus;
10897 mLosingFocus.remove(newFocus);
Romain Guy06882f82009-06-10 13:36:04 -070010898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 final WindowState imWindow = mInputMethodWindow;
10900 if (newFocus != imWindow && oldFocus != imWindow) {
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010901 if (moveInputMethodWindowsIfNeededLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS &&
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010903 mode != UPDATE_FOCUS_WILL_PLACE_SURFACES)) {
10904 mLayoutNeeded = true;
10905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010906 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
10907 performLayoutLockedInner();
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010908 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
10909 // Client will do the layout, but we need to assign layers
10910 // for handleNewWindowLocked() below.
10911 assignLayersLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 }
10913 }
Romain Guy06882f82009-06-10 13:36:04 -070010914
The Android Open Source Projectc474dec2009-03-04 09:49:09 -080010915 if (newFocus != null && mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
10916 mKeyWaiter.handleNewWindowLocked(newFocus);
10917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010918 return true;
10919 }
10920 return false;
10921 }
10922
10923 private WindowState computeFocusedWindowLocked() {
10924 WindowState result = null;
10925 WindowState win;
10926
10927 int i = mWindows.size() - 1;
10928 int nextAppIndex = mAppTokens.size()-1;
10929 WindowToken nextApp = nextAppIndex >= 0
10930 ? mAppTokens.get(nextAppIndex) : null;
10931
10932 while (i >= 0) {
10933 win = (WindowState)mWindows.get(i);
10934
Joe Onorato8a9b2202010-02-26 18:56:32 -080010935 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010936 TAG, "Looking for focus: " + i
10937 + " = " + win
10938 + ", flags=" + win.mAttrs.flags
10939 + ", canReceive=" + win.canReceiveKeys());
10940
10941 AppWindowToken thisApp = win.mAppToken;
Romain Guy06882f82009-06-10 13:36:04 -070010942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 // If this window's application has been removed, just skip it.
10944 if (thisApp != null && thisApp.removed) {
10945 i--;
10946 continue;
10947 }
Romain Guy06882f82009-06-10 13:36:04 -070010948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949 // If there is a focused app, don't allow focus to go to any
10950 // windows below it. If this is an application window, step
10951 // through the app tokens until we find its app.
10952 if (thisApp != null && nextApp != null && thisApp != nextApp
10953 && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
10954 int origAppIndex = nextAppIndex;
10955 while (nextAppIndex > 0) {
10956 if (nextApp == mFocusedApp) {
10957 // Whoops, we are below the focused app... no focus
10958 // for you!
Joe Onorato8a9b2202010-02-26 18:56:32 -080010959 if (localLOGV || DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 TAG, "Reached focused app: " + mFocusedApp);
10961 return null;
10962 }
10963 nextAppIndex--;
10964 nextApp = mAppTokens.get(nextAppIndex);
10965 if (nextApp == thisApp) {
10966 break;
10967 }
10968 }
10969 if (thisApp != nextApp) {
10970 // Uh oh, the app token doesn't exist! This shouldn't
10971 // happen, but if it does we can get totally hosed...
10972 // so restart at the original app.
10973 nextAppIndex = origAppIndex;
10974 nextApp = mAppTokens.get(nextAppIndex);
10975 }
10976 }
10977
10978 // Dispatch to this window if it is wants key events.
10979 if (win.canReceiveKeys()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010980 if (DEBUG_FOCUS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 TAG, "Found focus @ " + i + " = " + win);
10982 result = win;
10983 break;
10984 }
10985
10986 i--;
10987 }
10988
10989 return result;
10990 }
10991
10992 private void startFreezingDisplayLocked() {
10993 if (mDisplayFrozen) {
Chris Tate2ad63a92009-03-25 17:36:48 -070010994 // Freezing the display also suspends key event delivery, to
10995 // keep events from going astray while the display is reconfigured.
10996 // If someone has changed orientation again while the screen is
10997 // still frozen, the events will continue to be blocked while the
10998 // successive orientation change is processed. To prevent spurious
10999 // ANRs, we reset the event dispatch timeout in this case.
11000 synchronized (mKeyWaiter) {
11001 mKeyWaiter.mWasFrozen = true;
11002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 return;
11004 }
Romain Guy06882f82009-06-10 13:36:04 -070011005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 mScreenFrozenLock.acquire();
Romain Guy06882f82009-06-10 13:36:04 -070011007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011008 long now = SystemClock.uptimeMillis();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011009 //Slog.i(TAG, "Freezing, gc pending: " + mFreezeGcPending + ", now " + now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 if (mFreezeGcPending != 0) {
11011 if (now > (mFreezeGcPending+1000)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011012 //Slog.i(TAG, "Gc! " + now + " > " + (mFreezeGcPending+1000));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 mH.removeMessages(H.FORCE_GC);
11014 Runtime.getRuntime().gc();
11015 mFreezeGcPending = now;
11016 }
11017 } else {
11018 mFreezeGcPending = now;
11019 }
Romain Guy06882f82009-06-10 13:36:04 -070011020
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011021 if (DEBUG_FREEZE) Slog.v(TAG, "*** FREEZING DISPLAY", new RuntimeException());
11022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 mDisplayFrozen = true;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -070011024 if (mNextAppTransition != WindowManagerPolicy.TRANSIT_UNSET) {
11025 mNextAppTransition = WindowManagerPolicy.TRANSIT_UNSET;
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011026 mNextAppTransitionPackage = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011027 mAppTransitionReady = true;
11028 }
Romain Guy06882f82009-06-10 13:36:04 -070011029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 if (PROFILE_ORIENTATION) {
11031 File file = new File("/data/system/frozen");
11032 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
11033 }
11034 Surface.freezeDisplay(0);
11035 }
Romain Guy06882f82009-06-10 13:36:04 -070011036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 private void stopFreezingDisplayLocked() {
11038 if (!mDisplayFrozen) {
11039 return;
11040 }
Romain Guy06882f82009-06-10 13:36:04 -070011041
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011042 if (mWaitingForConfig || mAppsFreezingScreen > 0 || mWindowsFreezingScreen) {
11043 return;
11044 }
11045
Dianne Hackbornce73c1e2010-04-12 23:11:38 -070011046 if (DEBUG_FREEZE) Slog.v(TAG, "*** UNFREEZING DISPLAY", new RuntimeException());
11047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 mDisplayFrozen = false;
11049 mH.removeMessages(H.APP_FREEZE_TIMEOUT);
11050 if (PROFILE_ORIENTATION) {
11051 Debug.stopMethodTracing();
11052 }
11053 Surface.unfreezeDisplay(0);
Romain Guy06882f82009-06-10 13:36:04 -070011054
Chris Tate2ad63a92009-03-25 17:36:48 -070011055 // Reset the key delivery timeout on unfreeze, too. We force a wakeup here
11056 // too because regular key delivery processing should resume immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 synchronized (mKeyWaiter) {
11058 mKeyWaiter.mWasFrozen = true;
11059 mKeyWaiter.notifyAll();
11060 }
11061
Christopher Tateb696aee2010-04-02 19:08:30 -070011062 // While the display is frozen we don't re-compute the orientation
11063 // to avoid inconsistent states. However, something interesting
11064 // could have actually changed during that time so re-evaluate it
11065 // now to catch that.
11066 if (updateOrientationFromAppTokensLocked()) {
11067 mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
11068 }
11069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 // A little kludge: a lot could have happened while the
11071 // display was frozen, so now that we are coming back we
11072 // do a gc so that any remote references the system
11073 // processes holds on others can be released if they are
11074 // no longer needed.
11075 mH.removeMessages(H.FORCE_GC);
11076 mH.sendMessageDelayed(mH.obtainMessage(H.FORCE_GC),
11077 2000);
Romain Guy06882f82009-06-10 13:36:04 -070011078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 mScreenFrozenLock.release();
11080 }
Romain Guy06882f82009-06-10 13:36:04 -070011081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082 @Override
11083 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11084 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
11085 != PackageManager.PERMISSION_GRANTED) {
11086 pw.println("Permission Denial: can't dump WindowManager from from pid="
11087 + Binder.getCallingPid()
11088 + ", uid=" + Binder.getCallingUid());
11089 return;
11090 }
Romain Guy06882f82009-06-10 13:36:04 -070011091
Dianne Hackborna2e92262010-03-02 17:19:29 -080011092 pw.println("Input State:");
11093 mQueue.dump(pw, " ");
11094 pw.println(" ");
11095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 synchronized(mWindowMap) {
11097 pw.println("Current Window Manager state:");
11098 for (int i=mWindows.size()-1; i>=0; i--) {
11099 WindowState w = (WindowState)mWindows.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011100 pw.print(" Window #"); 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 if (mInputMethodDialogs.size() > 0) {
11105 pw.println(" ");
11106 pw.println(" Input method dialogs:");
11107 for (int i=mInputMethodDialogs.size()-1; i>=0; i--) {
11108 WindowState w = mInputMethodDialogs.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011109 pw.print(" IM Dialog #"); pw.print(i); pw.print(": "); pw.println(w);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 }
11111 }
11112 if (mPendingRemove.size() > 0) {
11113 pw.println(" ");
11114 pw.println(" Remove pending for:");
11115 for (int i=mPendingRemove.size()-1; i>=0; i--) {
11116 WindowState w = mPendingRemove.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011117 pw.print(" Remove #"); pw.print(i); pw.print(' ');
11118 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 w.dump(pw, " ");
11120 }
11121 }
11122 if (mForceRemoves != null && mForceRemoves.size() > 0) {
11123 pw.println(" ");
11124 pw.println(" Windows force removing:");
11125 for (int i=mForceRemoves.size()-1; i>=0; i--) {
11126 WindowState w = mForceRemoves.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011127 pw.print(" Removing #"); pw.print(i); pw.print(' ');
11128 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 w.dump(pw, " ");
11130 }
11131 }
11132 if (mDestroySurface.size() > 0) {
11133 pw.println(" ");
11134 pw.println(" Windows waiting to destroy their surface:");
11135 for (int i=mDestroySurface.size()-1; i>=0; i--) {
11136 WindowState w = mDestroySurface.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011137 pw.print(" Destroy #"); pw.print(i); pw.print(' ');
11138 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 w.dump(pw, " ");
11140 }
11141 }
11142 if (mLosingFocus.size() > 0) {
11143 pw.println(" ");
11144 pw.println(" Windows losing focus:");
11145 for (int i=mLosingFocus.size()-1; i>=0; i--) {
11146 WindowState w = mLosingFocus.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011147 pw.print(" Losing #"); pw.print(i); pw.print(' ');
11148 pw.print(w); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 w.dump(pw, " ");
11150 }
11151 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011152 if (mResizingWindows.size() > 0) {
11153 pw.println(" ");
11154 pw.println(" Windows waiting to resize:");
11155 for (int i=mResizingWindows.size()-1; i>=0; i--) {
11156 WindowState w = mResizingWindows.get(i);
11157 pw.print(" Resizing #"); pw.print(i); pw.print(' ');
11158 pw.print(w); pw.println(":");
11159 w.dump(pw, " ");
11160 }
11161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 if (mSessions.size() > 0) {
11163 pw.println(" ");
11164 pw.println(" All active sessions:");
11165 Iterator<Session> it = mSessions.iterator();
11166 while (it.hasNext()) {
11167 Session s = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011168 pw.print(" Session "); pw.print(s); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 s.dump(pw, " ");
11170 }
11171 }
11172 if (mTokenMap.size() > 0) {
11173 pw.println(" ");
11174 pw.println(" All tokens:");
11175 Iterator<WindowToken> it = mTokenMap.values().iterator();
11176 while (it.hasNext()) {
11177 WindowToken token = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011178 pw.print(" Token "); pw.print(token.token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011179 token.dump(pw, " ");
11180 }
11181 }
11182 if (mTokenList.size() > 0) {
11183 pw.println(" ");
11184 pw.println(" Window token list:");
11185 for (int i=0; i<mTokenList.size(); i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011186 pw.print(" #"); pw.print(i); pw.print(": ");
11187 pw.println(mTokenList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 }
11189 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070011190 if (mWallpaperTokens.size() > 0) {
11191 pw.println(" ");
11192 pw.println(" Wallpaper tokens:");
11193 for (int i=mWallpaperTokens.size()-1; i>=0; i--) {
11194 WindowToken token = mWallpaperTokens.get(i);
11195 pw.print(" Wallpaper #"); pw.print(i);
11196 pw.print(' '); pw.print(token); pw.println(':');
11197 token.dump(pw, " ");
11198 }
11199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 if (mAppTokens.size() > 0) {
11201 pw.println(" ");
11202 pw.println(" Application tokens in Z order:");
11203 for (int i=mAppTokens.size()-1; i>=0; i--) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011204 pw.print(" App #"); pw.print(i); pw.print(": ");
11205 pw.println(mAppTokens.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 }
11207 }
11208 if (mFinishedStarting.size() > 0) {
11209 pw.println(" ");
11210 pw.println(" Finishing start of application tokens:");
11211 for (int i=mFinishedStarting.size()-1; i>=0; i--) {
11212 WindowToken token = mFinishedStarting.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011213 pw.print(" Finished Starting #"); pw.print(i);
11214 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 token.dump(pw, " ");
11216 }
11217 }
11218 if (mExitingTokens.size() > 0) {
11219 pw.println(" ");
11220 pw.println(" Exiting tokens:");
11221 for (int i=mExitingTokens.size()-1; i>=0; i--) {
11222 WindowToken token = mExitingTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011223 pw.print(" Exiting #"); pw.print(i);
11224 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 token.dump(pw, " ");
11226 }
11227 }
11228 if (mExitingAppTokens.size() > 0) {
11229 pw.println(" ");
11230 pw.println(" Exiting application tokens:");
11231 for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
11232 WindowToken token = mExitingAppTokens.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011233 pw.print(" Exiting App #"); pw.print(i);
11234 pw.print(' '); pw.print(token); pw.println(':');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011235 token.dump(pw, " ");
11236 }
11237 }
11238 pw.println(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011239 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
11240 pw.print(" mLastFocus="); pw.println(mLastFocus);
11241 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
11242 pw.print(" mInputMethodTarget="); pw.println(mInputMethodTarget);
11243 pw.print(" mInputMethodWindow="); pw.println(mInputMethodWindow);
Dianne Hackbornf21adf62009-08-13 10:20:21 -070011244 pw.print(" mWallpaperTarget="); pw.println(mWallpaperTarget);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011245 if (mLowerWallpaperTarget != null && mUpperWallpaperTarget != null) {
11246 pw.print(" mLowerWallpaperTarget="); pw.println(mLowerWallpaperTarget);
11247 pw.print(" mUpperWallpaperTarget="); pw.println(mUpperWallpaperTarget);
11248 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011249 pw.print(" mCurConfiguration="); pw.println(this.mCurConfiguration);
11250 pw.print(" mInTouchMode="); pw.print(mInTouchMode);
11251 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011252 pw.print(" mSystemBooted="); pw.print(mSystemBooted);
11253 pw.print(" mDisplayEnabled="); pw.println(mDisplayEnabled);
11254 pw.print(" mLayoutNeeded="); pw.print(mLayoutNeeded);
11255 pw.print(" mBlurShown="); pw.println(mBlurShown);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011256 if (mDimAnimator != null) {
11257 mDimAnimator.printTo(pw);
11258 } else {
Dianne Hackborna2e92262010-03-02 17:19:29 -080011259 pw.println( " no DimAnimator ");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011260 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011261 pw.print(" mInputMethodAnimLayerAdjustment=");
Dianne Hackborn759a39e2009-08-09 17:20:27 -070011262 pw.print(mInputMethodAnimLayerAdjustment);
11263 pw.print(" mWallpaperAnimLayerAdjustment=");
11264 pw.println(mWallpaperAnimLayerAdjustment);
Dianne Hackborn284ac932009-08-28 10:34:25 -070011265 pw.print(" mLastWallpaperX="); pw.print(mLastWallpaperX);
11266 pw.print(" mLastWallpaperY="); pw.println(mLastWallpaperY);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011267 pw.print(" mDisplayFrozen="); pw.print(mDisplayFrozen);
11268 pw.print(" mWindowsFreezingScreen="); pw.print(mWindowsFreezingScreen);
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011269 pw.print(" mAppsFreezingScreen="); pw.print(mAppsFreezingScreen);
11270 pw.print(" mWaitingForConfig="); pw.println(mWaitingForConfig);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011271 pw.print(" mRotation="); pw.print(mRotation);
11272 pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
11273 pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
11274 pw.print(" mAnimationPending="); pw.print(mAnimationPending);
11275 pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
11276 pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
11277 pw.print(" mNextAppTransition=0x");
11278 pw.print(Integer.toHexString(mNextAppTransition));
11279 pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
Dianne Hackborna8f60182009-09-01 19:01:50 -070011280 pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011281 pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011282 if (mNextAppTransitionPackage != null) {
11283 pw.print(" mNextAppTransitionPackage=");
11284 pw.print(mNextAppTransitionPackage);
11285 pw.print(", mNextAppTransitionEnter=0x");
11286 pw.print(Integer.toHexString(mNextAppTransitionEnter));
11287 pw.print(", mNextAppTransitionExit=0x");
11288 pw.print(Integer.toHexString(mNextAppTransitionExit));
11289 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011290 pw.print(" mStartingIconInTransition="); pw.print(mStartingIconInTransition);
11291 pw.print(", mSkipAppTransitionAnimation="); pw.println(mSkipAppTransitionAnimation);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -070011292 if (mLastEnterAnimToken != null || mLastEnterAnimToken != null) {
11293 pw.print(" mLastEnterAnimToken="); pw.print(mLastEnterAnimToken);
11294 pw.print(", mLastEnterAnimParams="); pw.println(mLastEnterAnimParams);
11295 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011296 if (mOpeningApps.size() > 0) {
11297 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
11298 }
11299 if (mClosingApps.size() > 0) {
11300 pw.print(" mClosingApps="); pw.println(mClosingApps);
11301 }
Dianne Hackborna8f60182009-09-01 19:01:50 -070011302 if (mToTopApps.size() > 0) {
11303 pw.print(" mToTopApps="); pw.println(mToTopApps);
11304 }
11305 if (mToBottomApps.size() > 0) {
11306 pw.print(" mToBottomApps="); pw.println(mToBottomApps);
11307 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011308 pw.print(" DisplayWidth="); pw.print(mDisplay.getWidth());
11309 pw.print(" DisplayHeight="); pw.println(mDisplay.getHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 pw.println(" KeyWaiter state:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011311 pw.print(" mLastWin="); pw.print(mKeyWaiter.mLastWin);
11312 pw.print(" mLastBinder="); pw.println(mKeyWaiter.mLastBinder);
11313 pw.print(" mFinished="); pw.print(mKeyWaiter.mFinished);
11314 pw.print(" mGotFirstWindow="); pw.print(mKeyWaiter.mGotFirstWindow);
11315 pw.print(" mEventDispatching="); pw.print(mKeyWaiter.mEventDispatching);
11316 pw.print(" mTimeToSwitch="); pw.println(mKeyWaiter.mTimeToSwitch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011317 }
11318 }
11319
11320 public void monitor() {
11321 synchronized (mWindowMap) { }
Mike Lockwood983ee092009-11-22 01:42:24 -050011322 synchronized (mKeyguardTokenWatcher) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 synchronized (mKeyWaiter) { }
11324 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011325
Dianne Hackbornddca3ee2009-07-23 19:01:31 -070011326 public void virtualKeyFeedback(KeyEvent event) {
11327 mPolicy.keyFeedbackFromInput(event);
11328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011329
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011330 /**
11331 * DimAnimator class that controls the dim animation. This holds the surface and
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011332 * all state used for dim animation.
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011333 */
11334 private static class DimAnimator {
11335 Surface mDimSurface;
11336 boolean mDimShown = false;
11337 float mDimCurrentAlpha;
11338 float mDimTargetAlpha;
11339 float mDimDeltaPerMs;
11340 long mLastDimAnimTime;
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011341
11342 int mLastDimWidth, mLastDimHeight;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011343
11344 DimAnimator (SurfaceSession session) {
11345 if (mDimSurface == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011346 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011347 + mDimSurface + ": CREATE");
11348 try {
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080011349 mDimSurface = new Surface(session, 0,
11350 "DimSurface",
11351 -1, 16, 16, PixelFormat.OPAQUE,
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011352 Surface.FX_SURFACE_DIM);
Maciej Białka9ee5c222010-03-24 10:25:40 +010011353 mDimSurface.setAlpha(0.0f);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011354 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011355 Slog.e(TAG, "Exception creating Dim surface", e);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011356 }
11357 }
11358 }
11359
11360 /**
11361 * Show the dim surface.
11362 */
11363 void show(int dw, int dh) {
Dianne Hackborn16064f92010-03-25 00:47:24 -070011364 if (!mDimShown) {
11365 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
11366 dw + "x" + dh + ")");
11367 mDimShown = true;
11368 try {
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011369 mLastDimWidth = dw;
11370 mLastDimHeight = dh;
Dianne Hackborn16064f92010-03-25 00:47:24 -070011371 mDimSurface.setPosition(0, 0);
11372 mDimSurface.setSize(dw, dh);
11373 mDimSurface.show();
11374 } catch (RuntimeException e) {
11375 Slog.w(TAG, "Failure showing dim surface", e);
11376 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -070011377 } else if (mLastDimWidth != dw || mLastDimHeight != dh) {
11378 mLastDimWidth = dw;
11379 mLastDimHeight = dh;
11380 mDimSurface.setSize(dw, dh);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011381 }
11382 }
11383
11384 /**
11385 * Set's the dim surface's layer and update dim parameters that will be used in
11386 * {@link updateSurface} after all windows are examined.
11387 */
11388 void updateParameters(WindowState w, long currentTime) {
11389 mDimSurface.setLayer(w.mAnimLayer-1);
11390
11391 final float target = w.mExiting ? 0 : w.mAttrs.dimAmount;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011392 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Dianne Hackborn0586a1b2009-09-06 21:08:27 -070011393 + ": layer=" + (w.mAnimLayer-1) + " target=" + target);
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011394 if (mDimTargetAlpha != target) {
11395 // If the desired dim level has changed, then
11396 // start an animation to it.
11397 mLastDimAnimTime = currentTime;
11398 long duration = (w.mAnimating && w.mAnimation != null)
11399 ? w.mAnimation.computeDurationHint()
11400 : DEFAULT_DIM_DURATION;
11401 if (target > mDimTargetAlpha) {
11402 // This is happening behind the activity UI,
11403 // so we can make it run a little longer to
11404 // give a stronger impression without disrupting
11405 // the user.
11406 duration *= DIM_DURATION_MULTIPLIER;
11407 }
11408 if (duration < 1) {
11409 // Don't divide by zero
11410 duration = 1;
11411 }
11412 mDimTargetAlpha = target;
11413 mDimDeltaPerMs = (mDimTargetAlpha-mDimCurrentAlpha) / duration;
11414 }
11415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011416
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011417 /**
11418 * Updating the surface's alpha. Returns true if the animation continues, or returns
11419 * false when the animation is finished and the dim surface is hidden.
11420 */
11421 boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
11422 if (!dimming) {
11423 if (mDimTargetAlpha != 0) {
11424 mLastDimAnimTime = currentTime;
11425 mDimTargetAlpha = 0;
11426 mDimDeltaPerMs = (-mDimCurrentAlpha) / DEFAULT_DIM_DURATION;
11427 }
11428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -080011429
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011430 boolean animating = false;
11431 if (mLastDimAnimTime != 0) {
11432 mDimCurrentAlpha += mDimDeltaPerMs
11433 * (currentTime-mLastDimAnimTime);
11434 boolean more = true;
11435 if (displayFrozen) {
11436 // If the display is frozen, there is no reason to animate.
11437 more = false;
11438 } else if (mDimDeltaPerMs > 0) {
11439 if (mDimCurrentAlpha > mDimTargetAlpha) {
11440 more = false;
11441 }
11442 } else if (mDimDeltaPerMs < 0) {
11443 if (mDimCurrentAlpha < mDimTargetAlpha) {
11444 more = false;
11445 }
11446 } else {
11447 more = false;
11448 }
11449
11450 // Do we need to continue animating?
11451 if (more) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011452 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011453 + mDimSurface + ": alpha=" + mDimCurrentAlpha);
11454 mLastDimAnimTime = currentTime;
11455 mDimSurface.setAlpha(mDimCurrentAlpha);
11456 animating = true;
11457 } else {
11458 mDimCurrentAlpha = mDimTargetAlpha;
11459 mLastDimAnimTime = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011460 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM "
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011461 + mDimSurface + ": final alpha=" + mDimCurrentAlpha);
11462 mDimSurface.setAlpha(mDimCurrentAlpha);
11463 if (!dimming) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011464 if (SHOW_TRANSACTIONS) Slog.i(TAG, " DIM " + mDimSurface
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011465 + ": HIDE");
11466 try {
11467 mDimSurface.hide();
11468 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011469 Slog.w(TAG, "Illegal argument exception hiding dim surface");
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011470 }
11471 mDimShown = false;
11472 }
11473 }
11474 }
11475 return animating;
11476 }
11477
11478 public void printTo(PrintWriter pw) {
11479 pw.print(" mDimShown="); pw.print(mDimShown);
11480 pw.print(" current="); pw.print(mDimCurrentAlpha);
11481 pw.print(" target="); pw.print(mDimTargetAlpha);
11482 pw.print(" delta="); pw.print(mDimDeltaPerMs);
11483 pw.print(" lastAnimTime="); pw.println(mLastDimAnimTime);
11484 }
11485 }
11486
11487 /**
11488 * Animation that fade in after 0.5 interpolate time, or fade out in reverse order.
11489 * This is used for opening/closing transition for apps in compatible mode.
11490 */
11491 private static class FadeInOutAnimation extends Animation {
11492 int mWidth;
11493 boolean mFadeIn;
11494
11495 public FadeInOutAnimation(boolean fadeIn) {
11496 setInterpolator(new AccelerateInterpolator());
11497 setDuration(DEFAULT_FADE_IN_OUT_DURATION);
11498 mFadeIn = fadeIn;
11499 }
11500
11501 @Override
11502 protected void applyTransformation(float interpolatedTime, Transformation t) {
11503 float x = interpolatedTime;
11504 if (!mFadeIn) {
11505 x = 1.0f - x; // reverse the interpolation for fade out
11506 }
11507 if (x < 0.5) {
11508 // move the window out of the screen.
11509 t.getMatrix().setTranslate(mWidth, 0);
11510 } else {
11511 t.getMatrix().setTranslate(0, 0);// show
11512 t.setAlpha((x - 0.5f) * 2);
11513 }
11514 }
11515
11516 @Override
11517 public void initialize(int width, int height, int parentWidth, int parentHeight) {
11518 // width is the screen width {@see AppWindowToken#stepAnimatinoLocked}
11519 mWidth = width;
11520 }
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011521
11522 @Override
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -070011523 public int getZAdjustment() {
11524 return Animation.ZORDER_TOP;
Mitsuru Oshima0a5d2c42009-07-14 14:10:30 -070011525 }
11526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527}